12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382 |
- <?php
- if ( ! class_exists( 'acf_field_clone' ) ) :
- class acf_field_clone extends acf_field {
- /*
- * __construct
- *
- * This function will setup the field type data
- *
- * @type function
- * @date 5/03/2014
- * @since 5.0.0
- *
- * @param n/a
- * @return n/a
- */
- function initialize() {
- // vars
- $this->name = 'clone';
- $this->label = _x( 'Clone', 'noun', 'acf' );
- $this->category = 'layout';
- $this->defaults = array(
- 'clone' => '',
- 'prefix_label' => 0,
- 'prefix_name' => 0,
- 'display' => 'seamless',
- 'layout' => 'block',
- );
- $this->cloning = array();
- $this->have_rows = 'single';
- // register filter
- acf_enable_filter( 'clone' );
- // ajax
- add_action( 'wp_ajax_acf/fields/clone/query', array( $this, 'ajax_query' ) );
- // filters
- add_filter( 'acf/get_fields', array( $this, 'acf_get_fields' ), 5, 2 );
- add_filter( 'acf/prepare_field', array( $this, 'acf_prepare_field' ), 10, 1 );
- add_filter( 'acf/clone_field', array( $this, 'acf_clone_field' ), 10, 2 );
- }
- /*
- * is_enabled
- *
- * This function will return true if acf_local functionality is enabled
- *
- * @type function
- * @date 14/07/2016
- * @since 5.4.0
- *
- * @param n/a
- * @return n/a
- */
- function is_enabled() {
- return acf_is_filter_enabled( 'clone' );
- }
- /*
- * load_field()
- *
- * This filter is appied to the $field after it is loaded from the database
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $field - the field array holding all the field options
- *
- * @return $field - the field array holding all the field options
- */
- function load_field( $field ) {
- // bail early if not enabled
- if ( ! $this->is_enabled() ) {
- return $field;
- }
- // load sub fields
- // - sub field name's will be modified to include prefix_name settings
- $field['sub_fields'] = $this->get_cloned_fields( $field );
- // return
- return $field;
- }
- /*
- * acf_get_fields
- *
- * This function will hook into the 'acf/get_fields' filter and inject/replace seamless clones fields
- *
- * @type function
- * @date 17/06/2016
- * @since 5.3.8
- *
- * @param $fields (array)
- * @param $parent (array)
- * @return $fields
- */
- function acf_get_fields( $fields, $parent ) {
- // bail early if empty
- if ( empty( $fields ) ) {
- return $fields;
- }
- // bail early if not enabled
- if ( ! $this->is_enabled() ) {
- return $fields;
- }
- // vars
- $i = 0;
- // loop
- while ( $i < count( $fields ) ) {
- // vars
- $field = $fields[ $i ];
- // $i
- $i++;
- // bail early if not a clone field
- if ( $field['type'] != 'clone' ) {
- continue;
- }
- // bail early if not seamless
- if ( $field['display'] != 'seamless' ) {
- continue;
- }
- // bail early if sub_fields isn't set or not an array
- if ( ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
- continue;
- }
- // replace this clone field with sub fields
- $i--;
- array_splice( $fields, $i, 1, $field['sub_fields'] );
- }
- // return
- return $fields;
- }
- /*
- * get_cloned_fields
- *
- * This function will return an array of fields for a given clone field
- *
- * @type function
- * @date 28/06/2016
- * @since 5.3.8
- *
- * @param $field (array)
- * @param $parent (array)
- * @return (array)
- */
- function get_cloned_fields( $field ) {
- // vars
- $fields = array();
- // bail early if no clone setting
- if ( empty( $field['clone'] ) ) {
- return $fields;
- }
- // bail early if already cloning this field (avoid infinite looping)
- if ( isset( $this->cloning[ $field['key'] ] ) ) {
- return $fields;
- }
- // update local ref
- $this->cloning[ $field['key'] ] = 1;
- // Loop over selectors and load fields.
- foreach ( $field['clone'] as $selector ) {
- // Field Group selector.
- if ( acf_is_field_group_key( $selector ) ) {
- $field_group = acf_get_field_group( $selector );
- if ( ! $field_group ) {
- continue;
- }
- $field_group_fields = acf_get_fields( $field_group );
- if ( ! $field_group_fields ) {
- continue;
- }
- $fields = array_merge( $fields, $field_group_fields );
- // Field selector.
- } elseif ( acf_is_field_key( $selector ) ) {
- $fields[] = acf_get_field( $selector );
- }
- }
- // field has ve been loaded for this $parent, time to remove cloning ref
- unset( $this->cloning[ $field['key'] ] );
- // clear false values (fields that don't exist)
- $fields = array_filter( $fields );
- // bail early if no sub fields
- if ( empty( $fields ) ) {
- return array();
- }
- // loop
- // run acf_clone_field() on each cloned field to modify name, key, etc
- foreach ( array_keys( $fields ) as $i ) {
- $fields[ $i ] = acf_clone_field( $fields[ $i ], $field );
- }
- // return
- return $fields;
- }
- /*
- * acf_clone_field
- *
- * This function is run when cloning a clone field
- * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
- *
- * @type function
- * @date 28/06/2016
- * @since 5.3.8
- *
- * @param $field (array)
- * @param $clone_field (array)
- * @return $field
- */
- function acf_clone_field( $field, $clone_field ) {
- // bail early if this field is being cloned by some other kind of field (future proof)
- if ( $clone_field['type'] != 'clone' ) {
- return $field;
- }
- // backup (used later)
- // - backup only once (cloned clone fields can cause issues)
- if ( ! isset( $field['__key'] ) ) {
- $field['__key'] = $field['key'];
- $field['__name'] = $field['_name'];
- $field['__label'] = $field['label'];
- }
- // seamless
- if ( $clone_field['display'] == 'seamless' ) {
- // modify key
- // - this will allow sub clone fields to correctly load values for the same cloned field
- // - the original key will later be restored by acf/prepare_field allowing conditional logic JS to work
- $field['key'] = $clone_field['key'] . '_' . $field['key'];
- // modify prefix allowing clone field to save sub fields
- // - only used for parent seamless fields. Block or sub field's prefix will be overriden which also works
- $field['prefix'] = $clone_field['prefix'] . '[' . $clone_field['key'] . ']';
- // modify parent
- $field['parent'] = $clone_field['parent'];
- // label_format
- if ( $clone_field['prefix_label'] ) {
- $field['label'] = $clone_field['label'] . ' ' . $field['label'];
- }
- }
- // prefix_name
- if ( $clone_field['prefix_name'] ) {
- // modify the field name
- // - this will allow field to load / save correctly
- $field['name'] = $clone_field['name'] . '_' . $field['_name'];
- // modify the field _name (orig name)
- // - this will allow fields to correctly understand the modified field
- if ( $clone_field['display'] == 'seamless' ) {
- $field['_name'] = $clone_field['_name'] . '_' . $field['_name'];
- }
- }
- // required
- if ( $clone_field['required'] ) {
- $field['required'] = 1;
- }
- // type specific
- // note: seamless clone fields will not be triggered
- if ( $field['type'] == 'clone' ) {
- $field = $this->acf_clone_clone_field( $field, $clone_field );
- }
- // return
- return $field;
- }
- /*
- * acf_clone_clone_field
- *
- * This function is run when cloning a clone field
- * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
- * Do not delete! Removing this logic causes major issues with cloned clone fields within a flexible content layout.
- *
- * @type function
- * @date 28/06/2016
- * @since 5.3.8
- *
- * @param $field (array)
- * @param $clone_field (array)
- * @return $field
- */
- function acf_clone_clone_field( $field, $clone_field ) {
- // modify the $clone_field name
- // This seems odd, however, the $clone_field is later passed into the acf_clone_field() function
- // Do not delete!
- // when cloning a clone field, it is important to also change the _name too
- // this allows sub clone fields to appear correctly in get_row() row array
- if ( $field['prefix_name'] ) {
- $clone_field['name'] = $field['_name'];
- $clone_field['_name'] = $field['_name'];
- }
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $field;
- }
- // loop
- foreach ( $field['sub_fields'] as &$sub_field ) {
- // clone
- $sub_field = acf_clone_field( $sub_field, $clone_field );
- }
- // return
- return $field;
- }
- /*
- * prepare_field_for_db
- *
- * description
- *
- * @type function
- * @date 4/11/16
- * @since 5.5.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function prepare_field_for_db( $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $field;
- }
- // bail early if name == _name
- // this is a parent clone field and does not require any modification to sub field names
- if ( $field['name'] == $field['_name'] ) {
- return $field;
- }
- // this is a sub field
- // _name = 'my_field'
- // name = 'rep_0_my_field'
- // modify all sub fields to add 'rep_0_' name prefix (prefix_name setting has already been applied)
- $length = strlen( $field['_name'] );
- $prefix = substr( $field['name'], 0, -$length );
- // bail early if _name is not found at the end of name (unknown potential error)
- if ( $prefix . $field['_name'] !== $field['name'] ) {
- return $field;
- }
- // acf_log('== prepare_field_for_db ==');
- // acf_log('- clone name:', $field['name']);
- // acf_log('- clone _name:', $field['_name']);
- // loop
- foreach ( $field['sub_fields'] as &$sub_field ) {
- $sub_field['name'] = $prefix . $sub_field['name'];
- }
- // return
- return $field;
- }
- /*
- * load_value()
- *
- * This filter is applied to the $value after it is loaded from the db
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value (mixed) the value found in the database
- * @param $post_id (mixed) the $post_id from which the value was loaded
- * @param $field (array) the field array holding all the field options
- * @return $value
- */
- function load_value( $value, $post_id, $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $value;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // load sub fields
- $value = array();
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // add value
- $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field );
- }
- // return
- return $value;
- }
- /*
- * format_value()
- *
- * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value (mixed) the value which was loaded from the database
- * @param $post_id (mixed) the $post_id from which the value was loaded
- * @param $field (array) the field array holding all the field options
- *
- * @return $value (mixed) the modified value
- */
- function format_value( $value, $post_id, $field ) {
- // bail early if no value
- if ( empty( $value ) ) {
- return false;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // extract value
- $sub_value = acf_extract_var( $value, $sub_field['key'] );
- // format value
- $sub_value = acf_format_value( $sub_value, $post_id, $sub_field );
- // append to $row
- $value[ $sub_field['__name'] ] = $sub_value;
- }
- // return
- return $value;
- }
- /**
- * Apply basic formatting to prepare the value for default REST output.
- *
- * @param mixed $value
- * @param string|int $post_id
- * @param array $field
- * @return mixed
- */
- public function format_value_for_rest( $value, $post_id, array $field ) {
- if ( empty( $value ) || ! is_array( $value ) ) {
- return $value;
- }
- if ( ! is_array( $field ) || ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
- return $value;
- }
- // Loop through each row and within that, each sub field to process sub fields individually.
- foreach ( $field['sub_fields'] as $sub_field ) {
- // Extract the sub field 'field_key'=>'value' pair from the $value and format it.
- $sub_value = acf_extract_var( $value, $sub_field['key'] );
- $sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
- // Add the sub field value back to the $value but mapped to the field name instead
- // of the key reference.
- $value[ $sub_field['name'] ] = $sub_value;
- }
- return $value;
- }
- /*
- * update_value()
- *
- * This filter is appied to the $value before it is updated in the db
- *
- * @type filter
- * @since 3.6
- * @date 23/01/13
- *
- * @param $value - the value which will be saved in the database
- * @param $field - the field array holding all the field options
- * @param $post_id - the $post_id of which the value will be saved
- *
- * @return $value - the modified value
- */
- function update_value( $value, $post_id, $field ) {
- // bail early if no value
- if ( ! acf_is_array( $value ) ) {
- return null;
- }
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return null;
- }
- // modify names
- $field = $this->prepare_field_for_db( $field );
- // loop
- foreach ( $field['sub_fields'] as $sub_field ) {
- // vars
- $v = false;
- // key (backend)
- if ( isset( $value[ $sub_field['key'] ] ) ) {
- $v = $value[ $sub_field['key'] ];
- // name (frontend)
- } elseif ( isset( $value[ $sub_field['_name'] ] ) ) {
- $v = $value[ $sub_field['_name'] ];
- // empty
- } else {
- // input is not set (hidden by conditioanl logic)
- continue;
- }
- // restore original field key
- $sub_field = $this->acf_prepare_field( $sub_field );
- // update value
- acf_update_value( $v, $post_id, $sub_field );
- }
- // return
- return '';
- }
- /*
- * render_field()
- *
- * Create the HTML interface for your field
- *
- * @param $field - an array holding all the field's data
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- */
- function render_field( $field ) {
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return;
- }
- // load values
- foreach ( $field['sub_fields'] as &$sub_field ) {
- // add value
- if ( isset( $field['value'][ $sub_field['key'] ] ) ) {
- // this is a normal value
- $sub_field['value'] = $field['value'][ $sub_field['key'] ];
- } elseif ( isset( $sub_field['default_value'] ) ) {
- // no value, but this sub field has a default value
- $sub_field['value'] = $sub_field['default_value'];
- }
- // update prefix to allow for nested values
- $sub_field['prefix'] = $field['name'];
- // restore label
- $sub_field['label'] = $sub_field['__label'];
- // restore required
- if ( $field['required'] ) {
- $sub_field['required'] = 0;
- }
- }
- // render
- if ( $field['layout'] == 'table' ) {
- $this->render_field_table( $field );
- } else {
- $this->render_field_block( $field );
- }
- }
- /*
- * render_field_block
- *
- * description
- *
- * @type function
- * @date 12/07/2016
- * @since 5.4.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function render_field_block( $field ) {
- // vars
- $label_placement = $field['layout'] == 'block' ? 'top' : 'left';
- // html
- echo '<div class="acf-clone-fields acf-fields -' . $label_placement . ' -border">';
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_render_field_wrap( $sub_field );
- }
- echo '</div>';
- }
- /*
- * render_field_table
- *
- * description
- *
- * @type function
- * @date 12/07/2016
- * @since 5.4.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function render_field_table( $field ) {
- ?>
- <table class="acf-table">
- <thead>
- <tr>
- <?php
- foreach ( $field['sub_fields'] as $sub_field ) :
- // Prepare field (allow sub fields to be removed).
- $sub_field = acf_prepare_field( $sub_field );
- if ( ! $sub_field ) {
- continue;
- }
- // Define attrs.
- $attrs = array();
- $attrs['class'] = 'acf-th';
- $attrs['data-name'] = $sub_field['_name'];
- $attrs['data-type'] = $sub_field['type'];
- $attrs['data-key'] = $sub_field['key'];
- if ( $sub_field['wrapper']['width'] ) {
- $attrs['data-width'] = $sub_field['wrapper']['width'];
- $attrs['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
- }
- ?>
- <th <?php echo acf_esc_attrs( $attrs ); ?>>
- <?php acf_render_field_label( $sub_field ); ?>
- <?php acf_render_field_instructions( $sub_field ); ?>
- </th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <tr class="acf-row">
- <?php
- foreach ( $field['sub_fields'] as $sub_field ) {
- acf_render_field_wrap( $sub_field, 'td' );
- }
- ?>
- </tr>
- </tbody>
- </table>
- <?php
- }
- /*
- * render_field_settings()
- *
- * Create extra options for your field. This is rendered when editing a field.
- * The value of $field['name'] can be used (like bellow) to save extra data to the $field
- *
- * @param $field - an array holding all the field's data
- *
- * @type action
- * @since 3.6
- * @date 23/01/13
- */
- function render_field_settings( $field ) {
- // temp enable 'local' to allow .json fields to be displayed
- acf_enable_filter( 'local' );
- // default_value
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Fields', 'acf' ),
- 'instructions' => __( 'Select one or more fields you wish to clone', 'acf' ),
- 'type' => 'select',
- 'name' => 'clone',
- 'multiple' => 1,
- 'allow_null' => 1,
- 'choices' => $this->get_clone_setting_choices( $field['clone'] ),
- 'ui' => 1,
- 'ajax' => 1,
- 'ajax_action' => 'acf/fields/clone/query',
- 'placeholder' => '',
- )
- );
- acf_disable_filter( 'local' );
- // display
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Display', 'acf' ),
- 'instructions' => __( 'Specify the style used to render the clone field', 'acf' ),
- 'type' => 'select',
- 'name' => 'display',
- 'class' => 'setting-display',
- 'choices' => array(
- 'group' => __( 'Group (displays selected fields in a group within this field)', 'acf' ),
- 'seamless' => __( 'Seamless (replaces this field with selected fields)', 'acf' ),
- ),
- )
- );
- // layout
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Layout', 'acf' ),
- 'instructions' => __( 'Specify the style used to render the selected fields', 'acf' ),
- 'type' => 'radio',
- 'name' => 'layout',
- 'layout' => 'horizontal',
- 'choices' => array(
- 'block' => __( 'Block', 'acf' ),
- 'table' => __( 'Table', 'acf' ),
- 'row' => __( 'Row', 'acf' ),
- ),
- )
- );
- // prefix_label
- $instructions = __( 'Labels will be displayed as %s', 'acf' );
- $instructions = sprintf( $instructions, '<code class="prefix-label-code-1"></code>' );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Prefix Field Labels', 'acf' ),
- 'instructions' => $instructions,
- 'name' => 'prefix_label',
- 'class' => 'setting-prefix-label',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- // prefix_name
- $instructions = __( 'Values will be saved as %s', 'acf' );
- $instructions = sprintf( $instructions, '<code class="prefix-name-code-1"></code>' );
- acf_render_field_setting(
- $field,
- array(
- 'label' => __( 'Prefix Field Names', 'acf' ),
- 'instructions' => $instructions,
- 'name' => 'prefix_name',
- 'class' => 'setting-prefix-name',
- 'type' => 'true_false',
- 'ui' => 1,
- )
- );
- }
- /*
- * get_clone_setting_choices
- *
- * This function will return an array of choices data for Select2
- *
- * @type function
- * @date 17/06/2016
- * @since 5.3.8
- *
- * @param $value (mixed)
- * @return (array)
- */
- function get_clone_setting_choices( $value ) {
- // vars
- $choices = array();
- // bail early if no $value
- if ( empty( $value ) ) {
- return $choices;
- }
- // force value to array
- $value = acf_get_array( $value );
- // loop
- foreach ( $value as $v ) {
- $choices[ $v ] = $this->get_clone_setting_choice( $v );
- }
- // return
- return $choices;
- }
- /*
- * get_clone_setting_choice
- *
- * This function will return the label for a given clone choice
- *
- * @type function
- * @date 17/06/2016
- * @since 5.3.8
- *
- * @param $selector (mixed)
- * @return (string)
- */
- function get_clone_setting_choice( $selector = '' ) {
- // bail early no selector
- if ( ! $selector ) {
- return '';
- }
- // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
- // ajax_fields
- if ( isset( $_POST['fields'][ $selector ] ) ) {
- return $this->get_clone_setting_field_choice( acf_sanitize_request_args( $_POST['fields'][ $selector ] ) );
- }
- // phpcs:enable WordPress.Security.NonceVerification.Missing
- // field
- if ( acf_is_field_key( $selector ) ) {
- return $this->get_clone_setting_field_choice( acf_get_field( $selector ) );
- }
- // group
- if ( acf_is_field_group_key( $selector ) ) {
- return $this->get_clone_setting_group_choice( acf_get_field_group( $selector ) );
- }
- // return
- return $selector;
- }
- /*
- * get_clone_setting_field_choice
- *
- * This function will return the text for a field choice
- *
- * @type function
- * @date 20/07/2016
- * @since 5.4.0
- *
- * @param $field (array)
- * @return (string)
- */
- function get_clone_setting_field_choice( $field ) {
- // bail early if no field
- if ( ! $field ) {
- return __( 'Unknown field', 'acf' );
- }
- // title
- $title = $field['label'] ? $field['label'] : __( '(no title)', 'acf' );
- // append type
- $title .= ' (' . $field['type'] . ')';
- // ancestors
- // - allow for AJAX to send through ancestors count
- $ancestors = isset( $field['ancestors'] ) ? $field['ancestors'] : count( acf_get_field_ancestors( $field ) );
- $title = str_repeat( '- ', $ancestors ) . $title;
- // return
- return $title;
- }
- /*
- * get_clone_setting_group_choice
- *
- * This function will return the text for a group choice
- *
- * @type function
- * @date 20/07/2016
- * @since 5.4.0
- *
- * @param $field_group (array)
- * @return (string)
- */
- function get_clone_setting_group_choice( $field_group ) {
- // bail early if no field group
- if ( ! $field_group ) {
- return __( 'Unknown field group', 'acf' );
- }
- // return
- return sprintf( __( 'All fields from %s field group', 'acf' ), $field_group['title'] );
- }
- /*
- * ajax_query
- *
- * description
- *
- * @type function
- * @date 17/06/2016
- * @since 5.3.8
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function ajax_query() {
- // validate
- if ( ! acf_verify_ajax() ) {
- die();
- }
- // disable field to allow clone fields to appear selectable
- acf_disable_filter( 'clone' );
- // options
- $options = acf_parse_args(
- $_POST,
- array(
- 'post_id' => 0,
- 'paged' => 0,
- 's' => '',
- 'title' => '',
- 'fields' => array(),
- )
- );
- // vars
- $results = array();
- $s = false;
- $i = -1;
- $limit = 20;
- $range_start = $limit * ( $options['paged'] - 1 ); // 0, 20, 40
- $range_end = $range_start + ( $limit - 1 ); // 19, 39, 59
- // search
- if ( $options['s'] !== '' ) {
- // strip slashes (search may be integer)
- $s = wp_unslash( strval( $options['s'] ) );
- }
- // load groups
- $field_groups = acf_get_field_groups();
- $field_group = false;
- // bail early if no field groups
- if ( empty( $field_groups ) ) {
- die();
- }
- // move current field group to start
- foreach ( array_keys( $field_groups ) as $j ) {
- // check ID
- if ( $field_groups[ $j ]['ID'] !== $options['post_id'] ) {
- continue;
- }
- // extract field group and move to start
- $field_group = acf_extract_var( $field_groups, $j );
- // field group found, stop looking
- break;
- }
- // if field group was not found, this is a new field group (not yet saved)
- if ( ! $field_group ) {
- $field_group = array(
- 'ID' => $options['post_id'],
- 'title' => $options['title'],
- 'key' => '',
- );
- }
- // move current field group to start of list
- array_unshift( $field_groups, $field_group );
- // loop
- foreach ( $field_groups as $field_group ) {
- // vars
- $fields = false;
- $ignore_s = false;
- $data = array(
- 'text' => $field_group['title'],
- 'children' => array(),
- );
- // get fields
- if ( $field_group['ID'] == $options['post_id'] ) {
- $fields = $options['fields'];
- } else {
- $fields = acf_get_fields( $field_group );
- $fields = acf_prepare_fields_for_import( $fields );
- }
- // bail early if no fields
- if ( ! $fields ) {
- continue;
- }
- // show all children for field group search match
- if ( $s !== false && stripos( $data['text'], $s ) !== false ) {
- $ignore_s = true;
- }
- // populate children
- $children = array();
- $children[] = $field_group['key'];
- foreach ( $fields as $field ) {
- $children[] = $field['key']; }
- // loop
- foreach ( $children as $child ) {
- // bail early if no key (fake field group or corrupt field)
- if ( ! $child ) {
- continue;
- }
- // vars
- $text = false;
- // bail early if is search, and $text does not contain $s
- if ( $s !== false && ! $ignore_s ) {
- // get early
- $text = $this->get_clone_setting_choice( $child );
- // search
- if ( stripos( $text, $s ) === false ) {
- continue;
- }
- }
- // $i
- $i++;
- // bail early if $i is out of bounds
- if ( $i < $range_start || $i > $range_end ) {
- continue;
- }
- // load text
- if ( $text === false ) {
- $text = $this->get_clone_setting_choice( $child );
- }
- // append
- $data['children'][] = array(
- 'id' => $child,
- 'text' => $text,
- );
- }
- // bail early if no children
- // - this group contained fields, but none shown on this page
- if ( empty( $data['children'] ) ) {
- continue;
- }
- // append
- $results[] = $data;
- // end loop if $i is out of bounds
- // - no need to look further
- if ( $i > $range_end ) {
- break;
- }
- }
- // return
- acf_send_ajax_results(
- array(
- 'results' => $results,
- 'limit' => $limit,
- )
- );
- }
- /*
- * acf_prepare_field
- *
- * This function will restore a field's key ready for input
- *
- * @type function
- * @date 6/09/2016
- * @since 5.4.0
- *
- * @param $field (array)
- * @return $field
- */
- function acf_prepare_field( $field ) {
- // bail early if not cloned
- if ( empty( $field['_clone'] ) ) {
- return $field;
- }
- // restore key
- if ( isset( $field['__key'] ) ) {
- $field['key'] = $field['__key'];
- }
- // return
- return $field;
- }
- /*
- * validate_value
- *
- * description
- *
- * @type function
- * @date 11/02/2014
- * @since 5.0.0
- *
- * @param $post_id (int)
- * @return $post_id (int)
- */
- function validate_value( $valid, $value, $field, $input ) {
- // bail early if no $value
- if ( empty( $value ) ) {
- return $valid;
- }
- // bail early if no sub fields
- if ( empty( $field['sub_fields'] ) ) {
- return $valid;
- }
- // loop
- foreach ( array_keys( $field['sub_fields'] ) as $i ) {
- // get sub field
- $sub_field = $field['sub_fields'][ $i ];
- $k = $sub_field['key'];
- // bail early if valu enot set (conditional logic?)
- if ( ! isset( $value[ $k ] ) ) {
- continue;
- }
- // validate
- acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
- }
- // return
- return $valid;
- }
- /**
- * Return the schema array for the REST API.
- *
- * @param array $field
- * @return array
- */
- public function get_rest_schema( array $field ) {
- $schema = array(
- 'type' => array( 'object', 'null' ),
- 'required' => ! empty( $field['required'] ) ? array() : false,
- 'items' => array(
- 'type' => 'object',
- 'properties' => array(),
- ),
- );
- foreach ( $field['sub_fields'] as $sub_field ) {
- /** @var acf_field $type */
- $type = acf_get_field_type( $sub_field['type'] );
- if ( ! $type ) {
- continue;
- }
- $sub_field_schema = $type->get_rest_schema( $sub_field );
- // Passing null to nested fields has no effect. Remove this as a possible type to prevent
- // confusion in the schema.
- $null_type_index = array_search( 'null', $sub_field_schema['type'] );
- if ( $null_type_index !== false ) {
- unset( $sub_field_schema['type'][ $null_type_index ] );
- }
- $schema['items']['properties'][ $sub_field['name'] ] = $sub_field_schema;
- /**
- * If the clone field itself is marked as required, all subfields are required,
- * regardless of the status of the original fields.
- */
- if ( is_array( $schema['required'] ) ) {
- $schema['required'][] = $sub_field['name'];
- }
- }
- return $schema;
- }
- }
- // initialize
- acf_register_field_type( 'acf_field_clone' );
- endif; // class_exists check
- ?>
|