PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.9.1
Secure Custom Fields v6.9.1
6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / fields / class-acf-field-clone.php
secure-custom-fields / includes / fields Last commit date
FlexibleContent 2 months ago class-acf-field-accordion.php 2 months ago class-acf-field-button-group.php 2 months ago class-acf-field-checkbox.php 3 days ago class-acf-field-clone.php 2 months ago class-acf-field-color_picker.php 2 months ago class-acf-field-date_picker.php 2 months ago class-acf-field-date_time_picker.php 2 months ago class-acf-field-email.php 2 months ago class-acf-field-file.php 2 months ago class-acf-field-flexible-content.php 1 week ago class-acf-field-gallery.php 3 weeks ago class-acf-field-google-map.php 2 months ago class-acf-field-group.php 2 months ago class-acf-field-icon_picker.php 7 months ago class-acf-field-image.php 2 months ago class-acf-field-link.php 2 months ago class-acf-field-message.php 1 year ago class-acf-field-nav-menu.php 1 week ago class-acf-field-number.php 2 months ago class-acf-field-oembed.php 3 weeks ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 3 weeks ago class-acf-field-password.php 2 months ago class-acf-field-post_object.php 3 weeks ago class-acf-field-radio.php 3 days ago class-acf-field-range.php 2 months ago class-acf-field-relationship.php 3 weeks ago class-acf-field-repeater.php 3 weeks ago class-acf-field-select.php 3 days ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 3 weeks ago class-acf-field-text.php 3 weeks ago class-acf-field-textarea.php 3 weeks ago class-acf-field-time_picker.php 2 months ago class-acf-field-true_false.php 2 months ago class-acf-field-url.php 3 weeks ago class-acf-field-user.php 3 weeks ago class-acf-field-wysiwyg.php 2 months ago class-acf-field.php 2 months ago class-acf-repeater-table.php 1 year ago index.php 1 year ago
class-acf-field-clone.php
1210 lines
1 <?php
2 /**
3 * Clone Field Class
4 *
5 * This class handles the clone field type, which allows users to select and display existing fields.
6 *
7 * @package wordpress/secure-custom-fields
8 * @since ACF 5.0.0
9 */
10
11 // phpcs:disable PEAR.NamingConventions.ValidClassName
12 if ( ! class_exists( 'acf_field_clone' ) ) :
13 /**
14 * Class acf_field_clone
15 *
16 * Handles the functionality for the clone field type.
17 *
18 * @since ACF 5.0.0
19 */
20 class acf_field_clone extends acf_field {
21
22 /**
23 * Array to keep track of fields being cloned.
24 *
25 * @var array $cloning
26 */
27 public $cloning = array();
28 /**
29 * The type of rows the field supports.
30 *
31 * @var array $have_rows
32 */
33 public $have_rows = 'single';
34 /**
35 * Initialize the field type.
36 *
37 * @type function
38 * @date 5/03/2014
39 * @since ACF 5.0
40 *
41 * @return void
42 */
43 public function initialize() {
44
45 // vars
46 $this->name = 'clone';
47 $this->label = _x( 'Clone', 'noun', 'secure-custom-fields' );
48 $this->category = 'layout';
49 $this->description = __( 'Allows you to select and display existing fields. It does not duplicate any fields in the database, but loads and displays the selected fields at run-time. The Clone field can either replace itself with the selected fields or display the selected fields as a group of subfields.', 'secure-custom-fields' );
50 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-clone.png';
51 $this->doc_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/clone/';
52 $this->tutorial_url = 'https://developer.wordpress.org/secure-custom-fields/features/fields/clone/clone-tutorial/';
53 $this->pro = true;
54 $this->supports = array( 'bindings' => false );
55 $this->defaults = array(
56 'clone' => '',
57 'prefix_label' => 0,
58 'prefix_name' => 0,
59 'display' => 'seamless',
60 'layout' => 'block',
61 );
62 $this->have_rows = 'single';
63
64 // register filter
65 acf_enable_filter( 'clone' );
66
67 // ajax
68 add_action( 'wp_ajax_acf/fields/clone/query', array( $this, 'ajax_query' ) );
69
70 // filters
71 add_filter( 'acf/get_fields', array( $this, 'acf_get_fields' ), 5, 2 );
72 add_filter( 'acf/prepare_field', array( $this, 'acf_prepare_field' ), 10, 1 );
73 add_filter( 'acf/clone_field', array( $this, 'acf_clone_field' ), 10, 2 );
74 }
75
76
77 /**
78 * Returns true if ACF local functionality is enabled.
79 *
80 * @type function
81 * @date 14/07/2016
82 * @since ACF 5.4.0
83 *
84 * @return bool True if clone filter is enabled.
85 */
86 public function is_enabled() {
87 return acf_is_filter_enabled( 'clone' );
88 }
89
90
91 /**
92 * Filter applied to the field after it is loaded from the database.
93 *
94 * @type filter
95 * @since ACF 3.6
96 * @date 23/01/13
97 *
98 * @param array $field The field array holding all the field options.
99 * @return array The modified field array.
100 */
101 public function load_field( $field ) {
102
103 // bail early if not enabled
104 if ( ! $this->is_enabled() ) {
105 return $field;
106 }
107
108 // load sub fields
109 // - sub field name's will be modified to include prefix_name settings
110 $field['sub_fields'] = $this->get_cloned_fields( $field );
111
112 // return
113 return $field;
114 }
115
116
117 /**
118 * Hooks into 'acf/get_fields' filter to inject/replace seamless clone fields.
119 *
120 * @param array $fields Field list.
121 * @param array $parent_field Parent field.
122 * @return array Modified field list.
123 */
124 public function acf_get_fields( $fields, $parent_field ) {
125 // bail early if empty.
126 if ( empty( $fields ) ) {
127 return $fields;
128 }
129
130 // bail early if not enabled.
131 if ( ! $this->is_enabled() ) {
132 return $fields;
133 }
134
135 // vars.
136 $i = 0;
137
138 // loop.
139 $count = count( $fields );
140 while ( $i < $count ) {
141
142 // Skip invalid/null field entries.
143 if ( ! isset( $fields[ $i ] ) || ! is_array( $fields[ $i ] ) ) {
144 ++$i;
145 continue;
146 }
147
148 // vars.
149 $field = $fields[ $i ];
150
151 // Increment $i.
152 ++$i;
153
154 // Bail early if not a clone field.
155 if ( 'clone' !== $field['type'] ) {
156 continue;
157 }
158
159 // Bail early if not seamless.
160 if ( 'seamless' !== $field['display'] ) {
161 continue;
162 }
163
164 // bail early if sub_fields isn't set or not an array
165 if ( ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
166 continue;
167 }
168
169 // replace this clone field with sub fields
170 --$i;
171 array_splice( $fields, $i, 1, $field['sub_fields'] );
172 }
173
174 // return
175 return $fields;
176 }
177
178
179 /**
180 * Returns an array of fields for a given clone field.
181 *
182 * @param array $field The clone field array.
183 * @return array Array of cloned fields.
184 */
185 public function get_cloned_fields( $field ) {
186 // vars.
187 $fields = array();
188
189 // bail early if no clone setting.
190 if ( empty( $field['clone'] ) ) {
191 return $fields;
192 }
193
194 // bail early if already cloning this field (avoid infinite looping).
195 if ( isset( $this->cloning[ $field['key'] ] ) ) {
196 return $fields;
197 }
198
199 // update local ref.
200 $this->cloning[ $field['key'] ] = 1;
201
202 // Loop over selectors and load fields.
203 foreach ( $field['clone'] as $selector ) {
204
205 // Field Group selector.
206 if ( acf_is_field_group_key( $selector ) ) {
207 $field_group = acf_get_field_group( $selector );
208 if ( ! $field_group ) {
209 continue;
210 }
211
212 $field_group_fields = acf_get_fields( $field_group );
213 if ( ! $field_group_fields ) {
214 continue;
215 }
216
217 $fields = array_merge( $fields, $field_group_fields );
218
219 // Field selector.
220 } elseif ( acf_is_field_key( $selector ) ) {
221 $fields[] = acf_get_field( $selector );
222 }
223 }
224
225 // field has ve been loaded for this $parent, time to remove cloning ref.
226 unset( $this->cloning[ $field['key'] ] );
227
228 // clear false values (fields that don't exist).
229 $fields = array_filter( $fields );
230
231 // bail early if no sub fields.
232 if ( empty( $fields ) ) {
233 return array();
234 }
235
236 // loop.
237 // run acf_clone_field() on each cloned field to modify name, key, etc.
238 foreach ( array_keys( $fields ) as $i ) {
239 $fields[ $i ] = acf_clone_field( $fields[ $i ], $field );
240 }
241
242 return $fields;
243 }
244
245 /**
246 * This function is run when cloning a clone field
247 * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
248 *
249 * @type function
250 * @date 28/06/2016
251 * @since 5.3.8
252 *
253 * @param array $field The field array.
254 * @param array $clone_field The clone field array.
255 * @return array $field
256 */
257 public function acf_clone_field( $field, $clone_field ) {
258
259 // bail early if this field is being cloned by some other kind of field (future proof)
260 if ( 'clone' !== $clone_field['type'] ) {
261 return $field;
262 }
263
264 // backup (used later)
265 // - backup only once (cloned clone fields can cause issues)
266 if ( ! isset( $field['__key'] ) ) {
267 $field['__key'] = $field['key'];
268 $field['__name'] = $field['_name'];
269 $field['__label'] = $field['label'];
270 }
271
272 // seamless
273 if ( 'seamless' === $clone_field['display'] ) {
274
275 // modify key
276 // - this will allow sub clone fields to correctly load values for the same cloned field
277 // - the original key will later be restored by acf/prepare_field allowing conditional logic JS to work
278 $field['key'] = $clone_field['key'] . '_' . $field['key'];
279
280 // modify prefix allowing clone field to save sub fields
281 // - only used for parent seamless fields. Block or sub field's prefix will be overridden which also works
282 $field['prefix'] = $clone_field['prefix'] . '[' . $clone_field['key'] . ']';
283
284 // modify parent
285 $field['parent'] = $clone_field['parent'];
286
287 // label_format
288 if ( $clone_field['prefix_label'] ) {
289 $field['label'] = $clone_field['label'] . ' ' . $field['label'];
290 }
291 }
292
293 // prefix_name
294 if ( $clone_field['prefix_name'] ) {
295
296 // modify the field name
297 // - this will allow field to load / save correctly
298 $field['name'] = $clone_field['name'] . '_' . $field['_name'];
299
300 // modify the field _name (orig name)
301 // - this will allow fields to correctly understand the modified field
302 if ( 'seamless' === $clone_field['display'] ) {
303 $field['_name'] = $clone_field['_name'] . '_' . $field['_name'];
304 }
305 }
306
307 // required
308 if ( $clone_field['required'] ) {
309 $field['required'] = 1;
310 }
311
312 // type specific
313 // note: seamless clone fields will not be triggered
314 if ( 'clone' === $field['type'] ) {
315 $field = $this->acf_clone_clone_field( $field, $clone_field );
316 }
317
318 // return
319 return $field;
320 }
321 /**
322 * This function is run when cloning a clone field
323 * Important to run the acf_clone_field function on sub fields to pass on settings such as 'parent_layout'
324 * Do not delete! Removing this logic causes major issues with cloned clone fields within a flexible content layout.
325 *
326 * @param array $field The field being cloned.
327 * @param array $clone_field The clone field.
328 * @return array The modified field.
329 */
330 public function acf_clone_clone_field( $field, $clone_field ) {
331
332 // Modify the $clone_field name.
333 // This seems odd, however, the $clone_field is later passed into the acf_clone_field() function.
334 // Do not delete!
335 // When cloning a clone field, it is important to also change the _name too.
336 // This allows sub clone fields to appear correctly in get_row() row array.
337 if ( $field['prefix_name'] ) {
338 $clone_field['name'] = $field['_name'];
339 $clone_field['_name'] = $field['_name'];
340 }
341
342 // bail early if no sub fields
343 if ( empty( $field['sub_fields'] ) ) {
344 return $field;
345 }
346
347 // loop
348 foreach ( $field['sub_fields'] as &$sub_field ) {
349
350 // clone
351 $sub_field = acf_clone_field( $sub_field, $clone_field );
352 }
353
354 // return
355 return $field;
356 }
357
358
359 /**
360 * Prepares the field for database storage.
361 *
362 * @param array $field The field array.
363 * @return array The prepared field array.
364 */
365 public function prepare_field_for_db( $field ) {
366
367 // bail early if no sub fields
368 if ( empty( $field['sub_fields'] ) ) {
369 return $field;
370 }
371
372 // Bail early if name == _name.
373 // This is a parent clone field and does not require any modification to sub field names.
374 if ( $field['name'] === $field['_name'] ) {
375 return $field;
376 }
377
378 // This is a sub field.
379 // _name = 'my_field' phpcs:ignore
380 // name = 'rep_0_my_field' phpcs:ignore
381 // Modify all sub fields to add 'rep_0_' name prefix (prefix_name setting has already been applied).
382 $length = strlen( $field['_name'] );
383 $prefix = substr( $field['name'], 0, -$length );
384
385 // bail early if _name is not found at the end of name (unknown potential error)
386 if ( $prefix . $field['_name'] !== $field['name'] ) {
387 return $field;
388 }
389
390 // Loop through each sub field and modify its name.
391 foreach ( $field['sub_fields'] as &$sub_field ) {
392 $sub_field['name'] = $prefix . $sub_field['name'];
393 }
394
395 return $field;
396 }
397
398
399 /**
400 * This filter is applied to the $value after it is loaded from the db.
401 *
402 * @param mixed $value The value found in the database.
403 * @param mixed $post_id The post_id from which the value was loaded.
404 * @param array $field The field array holding all the field options.
405 * @return mixed
406 */
407 public function load_value( $value, $post_id, $field ) {
408
409 // bail early if no sub fields
410 if ( empty( $field['sub_fields'] ) ) {
411 return $value;
412 }
413
414 // modify names
415 $field = $this->prepare_field_for_db( $field );
416
417 // load sub fields
418 $value = array();
419
420 // loop
421 foreach ( $field['sub_fields'] as $sub_field ) {
422
423 // add value
424 $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field );
425 }
426
427 // return
428 return $value;
429 }
430
431
432 /**
433 * This filter is applied to the $value after it is loaded from the db and before it is returned to the template
434 *
435 * @type filter
436 * @since ACF 3.6 3.6
437 *
438 * @param mixed $value The value which was loaded from the database.
439 * @param mixed $post_id The $post_id from which the value was loaded.
440 * @param array $field The field array holding all the field options.
441 * @param boolean $escape_html Should the field return a HTML safe formatted value.
442 * @return mixed $value The modified value.
443 */
444 public function format_value( $value, $post_id, $field, $escape_html = false ) {
445
446 // bail early if no value
447 if ( empty( $value ) ) {
448 return false;
449 }
450
451 // modify names
452 $field = $this->prepare_field_for_db( $field );
453
454 // loop
455 foreach ( $field['sub_fields'] as $sub_field ) {
456
457 // extract value
458 $sub_value = acf_extract_var( $value, $sub_field['key'] );
459
460 // format value
461 $sub_value = acf_format_value( $sub_value, $post_id, $sub_field, $escape_html );
462
463 // append to $row
464 $value[ $sub_field['__name'] ] = $sub_value;
465 }
466
467 // return
468 return $value;
469 }
470
471 /**
472 * Formats the value for REST API output.
473 *
474 * @param mixed $value The field value.
475 * @param string|integer $post_id The post ID.
476 * @param array $field The field array.
477 * @return mixed The formatted value.
478 */
479 public function format_value_for_rest( $value, $post_id, array $field ) {
480 if ( empty( $value ) || ! is_array( $value ) ) {
481 return $value;
482 }
483
484 if ( ! is_array( $field ) || ! isset( $field['sub_fields'] ) || ! is_array( $field['sub_fields'] ) ) {
485 return $value;
486 }
487
488 // Loop through each row and within that, each sub field to process sub fields individually.
489 foreach ( $field['sub_fields'] as $sub_field ) {
490
491 // Extract the sub field 'field_key'=>'value' pair from the $value and format it.
492 $sub_value = acf_extract_var( $value, $sub_field['key'] );
493 $sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
494
495 // Add the sub field value back to the $value but mapped to the field name instead
496 // of the key reference.
497 $value[ $sub_field['name'] ] = $sub_value;
498 }
499
500 return $value;
501 }
502
503 /**
504 * Updates the field value in the database.
505 *
506 * @param mixed $value The value to save.
507 * @param int $post_id The post ID where the value is saved.
508 * @param array $field The field array.
509 * @return string|null Empty string on success, null on failure.
510 */
511 public function update_value( $value, $post_id, $field ) {
512
513 // bail early if no value
514 if ( ! acf_is_array( $value ) ) {
515 return null;
516 }
517
518 // bail early if no sub fields
519 if ( empty( $field['sub_fields'] ) ) {
520 return null;
521 }
522
523 // modify names
524 $field = $this->prepare_field_for_db( $field );
525
526 // loop
527 foreach ( $field['sub_fields'] as $sub_field ) {
528
529 // vars
530 $v = false;
531
532 // key (backend)
533 if ( isset( $value[ $sub_field['key'] ] ) ) {
534 $v = $value[ $sub_field['key'] ];
535
536 // name (frontend)
537 } elseif ( isset( $value[ $sub_field['_name'] ] ) ) {
538 $v = $value[ $sub_field['_name'] ];
539
540 // empty
541 } else {
542
543 // input is not set (hidden by conditional logic)
544 continue;
545 }
546
547 // restore original field key
548 $sub_field = $this->acf_prepare_field( $sub_field );
549
550 // update value
551 acf_update_value( $v, $post_id, $sub_field );
552 }
553
554 // return
555 return '';
556 }
557
558
559 /**
560 * Renders the field input HTML.
561 *
562 * @param array $field The field array.
563 * @return void
564 */
565 public function render_field( $field ) {
566
567 // bail early if no sub fields
568 if ( empty( $field['sub_fields'] ) ) {
569 return;
570 }
571
572 // load values
573 foreach ( $field['sub_fields'] as &$sub_field ) {
574
575 // add value
576 if ( isset( $field['value'][ $sub_field['key'] ] ) ) {
577
578 // this is a normal value
579 $sub_field['value'] = $field['value'][ $sub_field['key'] ];
580 } elseif ( isset( $sub_field['default_value'] ) ) {
581
582 // no value, but this sub field has a default value
583 $sub_field['value'] = $sub_field['default_value'];
584 }
585
586 // update prefix to allow for nested values
587 $sub_field['prefix'] = $field['name'];
588
589 // restore label
590 $sub_field['label'] = $sub_field['__label'];
591
592 // restore required
593 if ( $field['required'] ) {
594 $sub_field['required'] = 0;
595 }
596 }
597
598 // Render the field based on the layout setting.
599 if ( 'table' === $field['layout'] ) {
600 $this->render_field_table( $field );
601 } else {
602 $this->render_field_block( $field );
603 }
604 }
605
606
607 /**
608 * Renders the clone fields in a block layout.
609 *
610 * @param array $field The field array.
611 * @return void
612 */
613 public function render_field_block( $field ) {
614
615 // vars
616 $label_placement = 'block' === $field['layout'] ? 'top' : 'left';
617
618 // html
619 echo '<div class="acf-clone-fields acf-fields -' . esc_attr( $label_placement ) . ' -border">';
620
621 foreach ( $field['sub_fields'] as $sub_field ) {
622 acf_render_field_wrap( $sub_field );
623 }
624
625 echo '</div>';
626 }
627
628
629 /**
630 * Renders the clone fields in a table layout.
631 *
632 * @param array $field The field array.
633 * @return void
634 */
635 public function render_field_table( $field ) {
636 ?>
637 <table class="acf-table">
638 <thead>
639 <tr>
640 <?php
641 foreach ( $field['sub_fields'] as $sub_field ) :
642
643 // Prepare field (allow sub fields to be removed).
644 $sub_field = acf_prepare_field( $sub_field );
645 if ( ! $sub_field ) {
646 continue;
647 }
648
649 // Define attrs.
650 $attrs = array();
651 $attrs['class'] = 'acf-th';
652 $attrs['data-name'] = $sub_field['_name'];
653 $attrs['data-type'] = $sub_field['type'];
654 $attrs['data-key'] = $sub_field['key'];
655
656 if ( $sub_field['wrapper']['width'] ) {
657 $attrs['data-width'] = $sub_field['wrapper']['width'];
658 $attrs['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
659 }
660
661 ?>
662 <th <?php echo acf_esc_attrs( $attrs ); ?>>
663 <?php acf_render_field_label( $sub_field ); ?>
664 <?php acf_render_field_instructions( $sub_field ); ?>
665 </th>
666 <?php endforeach; ?>
667 </tr>
668 </thead>
669 <tbody>
670 <tr class="acf-row">
671 <?php
672
673 foreach ( $field['sub_fields'] as $sub_field ) {
674 acf_render_field_wrap( $sub_field, 'td' );
675 }
676
677 ?>
678 </tr>
679 </tbody>
680 </table>
681 <?php
682 }
683
684
685 /**
686 * Renders the field settings HTML.
687 *
688 * @param array $field The field settings array.
689 * @return void
690 */
691 public function render_field_settings( $field ) {
692
693 // temp enable 'local' to allow .json fields to be displayed
694 acf_enable_filter( 'local' );
695
696 // default_value
697 acf_render_field_setting(
698 $field,
699 array(
700 'label' => __( 'Fields', 'secure-custom-fields' ),
701 'instructions' => __( 'Select one or more fields you wish to clone', 'secure-custom-fields' ),
702 'type' => 'select',
703 'name' => 'clone',
704 'multiple' => 1,
705 'allow_null' => 1,
706 'choices' => $this->get_clone_setting_choices( $field['clone'] ),
707 'ui' => 1,
708 'ajax' => 1,
709 'ajax_action' => 'acf/fields/clone/query',
710 'placeholder' => '',
711 'nonce' => wp_create_nonce( 'acf/fields/clone/query' ),
712 )
713 );
714
715 acf_disable_filter( 'local' );
716
717 // display
718 acf_render_field_setting(
719 $field,
720 array(
721 'label' => __( 'Display', 'secure-custom-fields' ),
722 'instructions' => __( 'Specify the style used to render the clone field', 'secure-custom-fields' ),
723 'type' => 'select',
724 'name' => 'display',
725 'class' => 'setting-display',
726 'choices' => array(
727 'group' => __( 'Group (displays selected fields in a group within this field)', 'secure-custom-fields' ),
728 'seamless' => __( 'Seamless (replaces this field with selected fields)', 'secure-custom-fields' ),
729 ),
730 )
731 );
732
733 // layout
734 acf_render_field_setting(
735 $field,
736 array(
737 'label' => __( 'Layout', 'secure-custom-fields' ),
738 'instructions' => __( 'Specify the style used to render the selected fields', 'secure-custom-fields' ),
739 'type' => 'radio',
740 'name' => 'layout',
741 'layout' => 'horizontal',
742 'choices' => array(
743 'block' => __( 'Block', 'secure-custom-fields' ),
744 'table' => __( 'Table', 'secure-custom-fields' ),
745 'row' => __( 'Row', 'secure-custom-fields' ),
746 ),
747 )
748 );
749
750 // prefix_label
751 /* translators: %s: field label */
752 $instructions = __( 'Labels will be displayed as %s', 'secure-custom-fields' );
753 $instructions = sprintf( $instructions, '<code class="prefix-label-code-1"></code>' );
754 acf_render_field_setting(
755 $field,
756 array(
757 'label' => __( 'Prefix Field Labels', 'secure-custom-fields' ),
758 'instructions' => $instructions,
759 'name' => 'prefix_label',
760 'class' => 'setting-prefix-label',
761 'type' => 'true_false',
762 'ui' => 1,
763 )
764 );
765
766 // prefix_name
767 /* translators: %s: field name */
768 $instructions = __( 'Values will be saved as %s', 'secure-custom-fields' );
769 $instructions = sprintf( $instructions, '<code class="prefix-name-code-1"></code>' );
770 acf_render_field_setting(
771 $field,
772 array(
773 'label' => __( 'Prefix Field Names', 'secure-custom-fields' ),
774 'instructions' => $instructions,
775 'name' => 'prefix_name',
776 'class' => 'setting-prefix-name',
777 'type' => 'true_false',
778 'ui' => 1,
779 )
780 );
781 }
782
783
784 /**
785 * Returns an array of field choices for Select2.
786 *
787 * @param mixed $value The field value.
788 * @return array Array of choices for Select2.
789 */
790 public function get_clone_setting_choices( $value ) {
791
792 // vars
793 $choices = array();
794
795 // bail early if no $value
796 if ( empty( $value ) ) {
797 return $choices;
798 }
799
800 // force value to array
801 $value = acf_get_array( $value );
802
803 // loop
804 foreach ( $value as $v ) {
805 $choices[ $v ] = $this->get_clone_setting_choice( $v );
806 }
807
808 // return
809 return $choices;
810 }
811
812
813 /**
814 * Returns the label for a given clone choice.
815 *
816 * @param mixed $selector The field selector.
817 * @return string The choice label.
818 */
819 public function get_clone_setting_choice( $selector = '' ) {
820
821 // bail early no selector
822 if ( ! $selector ) {
823 return '';
824 }
825
826 // phpcs:disable WordPress.Security.NonceVerification.Missing -- Verified elsewhere.
827 // ajax_fields
828 if ( isset( $_POST['fields'][ $selector ] ) ) {
829 return $this->get_clone_setting_field_choice( acf_sanitize_request_args( wp_unslash( $_POST['fields'][ $selector ] ) ) );
830 }
831 // phpcs:enable WordPress.Security.NonceVerification.Missing
832
833 // field
834 if ( acf_is_field_key( $selector ) ) {
835 return $this->get_clone_setting_field_choice( acf_get_field( $selector ) );
836 }
837
838 // group
839 if ( acf_is_field_group_key( $selector ) ) {
840 return $this->get_clone_setting_group_choice( acf_get_field_group( $selector ) );
841 }
842
843 // return
844 return $selector;
845 }
846
847
848 /**
849 * Returns the text label for a field choice.
850 *
851 * @param array|false $field The field array.
852 * @return string The formatted choice text.
853 */
854 public function get_clone_setting_field_choice( $field ) {
855
856 // bail early if no field
857 if ( ! $field ) {
858 return __( 'Unknown field', 'secure-custom-fields' );
859 }
860
861 // title
862 $title = $field['label'] ? $field['label'] : __( '(no title)', 'secure-custom-fields' );
863
864 // append type
865 $title .= ' (' . $field['type'] . ')';
866
867 // ancestors
868 // - allow for AJAX to send through ancestors count
869 $ancestors = isset( $field['ancestors'] ) ? $field['ancestors'] : count( acf_get_field_ancestors( $field ) );
870 $title = str_repeat( '- ', $ancestors ) . $title;
871
872 // return
873 return $title;
874 }
875
876
877 /**
878 * Returns the text label for a field group choice.
879 *
880 * @param array|false $field_group The field group array.
881 * @return string The formatted choice text.
882 */
883 public function get_clone_setting_group_choice( $field_group ) {
884
885 // bail early if no field group
886 if ( ! $field_group ) {
887 return __( 'Unknown field group', 'secure-custom-fields' );
888 }
889
890 // return
891 /* translators: %s: field group title */
892 return sprintf( __( 'All fields from %s field group', 'secure-custom-fields' ), $field_group['title'] );
893 }
894
895
896 /**
897 * AJAX handler for getting potential fields to clone.
898 *
899 * @since ACF 5.3.8.3.8
900 *
901 * @return void
902 */
903 public function ajax_query() {
904 $nonce = acf_request_arg( 'nonce', '' );
905
906 if ( ! acf_verify_ajax( $nonce, 'acf/fields/clone/query' ) ) {
907 die();
908 }
909
910 if ( ! acf_current_user_can_admin() ) {
911 die();
912 }
913
914 // disable field to allow clone fields to appear selectable
915 acf_disable_filter( 'clone' );
916
917 // options
918 $options = acf_parse_args(
919 $_POST,
920 array(
921 'post_id' => 0,
922 'paged' => 0,
923 's' => '',
924 'title' => '',
925 'fields' => array(),
926 )
927 );
928
929 // vars
930 $results = array();
931 $s = false;
932 $i = -1;
933 $limit = 20;
934 $range_start = $limit * ( $options['paged'] - 1 ); // 0, 20, 40
935 $range_end = $range_start + ( $limit - 1 ); // 19, 39, 59
936
937 // search
938 if ( '' !== $options['s'] ) {
939
940 // strip slashes (search may be integer)
941 $s = wp_unslash( strval( $options['s'] ) );
942 }
943
944 // load groups
945 $field_groups = acf_get_field_groups();
946 $field_group = false;
947
948 // bail early if no field groups
949 if ( empty( $field_groups ) ) {
950 die();
951 }
952
953 // move current field group to start
954 foreach ( array_keys( $field_groups ) as $j ) {
955
956 // check ID
957 if ( $field_groups[ $j ]['ID'] !== $options['post_id'] ) {
958 continue;
959 }
960
961 // extract field group and move to start
962 $field_group = acf_extract_var( $field_groups, $j );
963
964 // field group found, stop looking
965 break;
966 }
967
968 // if field group was not found, this is a new field group (not yet saved)
969 if ( ! $field_group ) {
970 $field_group = array(
971 'ID' => $options['post_id'],
972 'title' => $options['title'],
973 'key' => '',
974 );
975 }
976
977 // move current field group to start of list
978 array_unshift( $field_groups, $field_group );
979
980 // loop
981 foreach ( $field_groups as $field_group ) {
982
983 // vars
984 $fields = false;
985 $ignore_s = false;
986 $data = array(
987 'text' => $field_group['title'],
988 'children' => array(),
989 );
990
991 // get fields
992 if ( (int) $field_group['ID'] === (int) $options['post_id'] ) {
993 $fields = $options['fields'];
994 } else {
995 $fields = acf_get_fields( $field_group );
996 $fields = acf_prepare_fields_for_import( $fields );
997 }
998
999 // bail early if no fields
1000 if ( ! $fields ) {
1001 continue;
1002 }
1003
1004 // show all children for field group search match
1005 if ( false !== $s && stripos( $data['text'], $s ) !== false ) {
1006 $ignore_s = true;
1007 }
1008
1009 // populate children
1010 $children = array();
1011 $children[] = $field_group['key'];
1012 foreach ( $fields as $field ) {
1013 $children[] = $field['key'];
1014 }
1015
1016 // loop
1017 foreach ( $children as $child ) {
1018
1019 // bail early if no key (fake field group or corrupt field)
1020 if ( ! $child ) {
1021 continue;
1022 }
1023
1024 // vars
1025 $text = false;
1026
1027 // bail early if is search, and $text does not contain $s
1028 if ( false !== $s && ! $ignore_s ) {
1029
1030 // get early
1031 $text = $this->get_clone_setting_choice( $child );
1032
1033 // search
1034 if ( stripos( $text, $s ) === false ) {
1035 continue;
1036 }
1037 }
1038
1039 // $i
1040 ++$i;
1041
1042 // bail early if $i is out of bounds
1043 if ( $i < $range_start || $i > $range_end ) {
1044 continue;
1045 }
1046
1047 // load text
1048 if ( false === $text ) {
1049 $text = $this->get_clone_setting_choice( $child );
1050 }
1051
1052 // append
1053 $data['children'][] = array(
1054 'id' => $child,
1055 'text' => $text,
1056 );
1057 }
1058
1059 // bail early if no children
1060 // - this group contained fields, but none shown on this page
1061 if ( empty( $data['children'] ) ) {
1062 continue;
1063 }
1064
1065 // append
1066 $results[] = $data;
1067
1068 // end loop if $i is out of bounds
1069 // - no need to look further
1070 if ( $i > $range_end ) {
1071 break;
1072 }
1073 }
1074
1075 // return
1076 acf_send_ajax_results(
1077 array(
1078 'results' => $results,
1079 'limit' => $limit,
1080 )
1081 );
1082 }
1083
1084
1085 /**
1086 * Restores a field's key ready for input.
1087 *
1088 * @since ACF 5.4.0
1089 *
1090 * @param array $field The field array.
1091 * @return array The modified field array.
1092 */
1093 public function acf_prepare_field( $field ) {
1094
1095 // bail early if not cloned
1096 if ( empty( $field['_clone'] ) ) {
1097 return $field;
1098 }
1099
1100 // restore key
1101 if ( isset( $field['__key'] ) ) {
1102 $field['key'] = $field['__key'];
1103 }
1104
1105 // return
1106 return $field;
1107 }
1108
1109
1110 /**
1111 * Validates the value of a clone field.
1112 *
1113 * @since ACF 5.0.0
1114 *
1115 * @param bool $valid Whether the value is valid.
1116 * @param mixed $value The field value.
1117 * @param array $field The field array.
1118 * @param string $input The input element's name attribute.
1119 * @return bool Whether the value is valid.
1120 */
1121 public function validate_value( $valid, $value, $field, $input ) {
1122
1123 // bail early if no $value
1124 if ( empty( $value ) ) {
1125 return $valid;
1126 }
1127
1128 // bail early if no sub fields
1129 if ( empty( $field['sub_fields'] ) ) {
1130 return $valid;
1131 }
1132
1133 // loop
1134 foreach ( array_keys( $field['sub_fields'] ) as $i ) {
1135
1136 // get sub field
1137 $sub_field = $field['sub_fields'][ $i ];
1138 $k = $sub_field['key'];
1139
1140 // bail early if value not set (conditional logic?)
1141 if ( ! isset( $value[ $k ] ) ) {
1142 continue;
1143 }
1144
1145 // validate
1146 acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
1147 }
1148
1149 // return
1150 return $valid;
1151 }
1152
1153 /**
1154 * Returns the schema array for the REST API.
1155 *
1156 * @param array $field The field array.
1157 * @return array The schema array for the REST API.
1158 */
1159 public function get_rest_schema( array $field ) {
1160 $schema = array(
1161 'type' => array( 'object', 'null' ),
1162 'required' => ! empty( $field['required'] ) ? array() : false,
1163 'items' => array(
1164 'type' => 'object',
1165 'properties' => array(),
1166 ),
1167 );
1168
1169 foreach ( $field['sub_fields'] as $sub_field ) {
1170 /**
1171 * Field type instance.
1172 *
1173 * @var acf_field $type
1174 */
1175 $type = acf_get_field_type( $sub_field['type'] );
1176
1177 if ( ! $type ) {
1178 continue;
1179 }
1180
1181 $sub_field_schema = $type->get_rest_schema( $sub_field );
1182
1183 // Passing null to nested fields has no effect. Remove this as a possible type to prevent
1184 // confusion in the schema.
1185 $null_type_index = array_search( 'null', $sub_field_schema['type'], true );
1186 if ( false !== $null_type_index ) {
1187 unset( $sub_field_schema['type'][ $null_type_index ] );
1188 }
1189
1190 $schema['items']['properties'][ $sub_field['name'] ] = $sub_field_schema;
1191
1192 /**
1193 * If the clone field itself is marked as required, all subfields are required,
1194 * regardless of the status of the original fields.
1195 */
1196 if ( is_array( $schema['required'] ) ) {
1197 $schema['required'][] = $sub_field['name'];
1198 }
1199 }
1200
1201 return $schema;
1202 }
1203 }
1204
1205
1206 // initialize
1207 acf_register_field_type( 'acf_field_clone' );
1208 endif; // class_exists check
1209
1210 ?>