PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.0-beta2
Secure Custom Fields v6.4.0-beta2
6.9.5 6.9.4 6.9.3 6.9.2 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-group.php
secure-custom-fields / includes / fields Last commit date
class-acf-field-accordion.php 1 year ago class-acf-field-button-group.php 1 year ago class-acf-field-checkbox.php 1 year ago class-acf-field-color_picker.php 1 year ago class-acf-field-date_picker.php 1 year ago class-acf-field-date_time_picker.php 1 year ago class-acf-field-email.php 1 year ago class-acf-field-file.php 1 year ago class-acf-field-google-map.php 1 year ago class-acf-field-group.php 1 year ago class-acf-field-icon_picker.php 1 year ago class-acf-field-image.php 1 year ago class-acf-field-link.php 1 year ago class-acf-field-message.php 1 year ago class-acf-field-number.php 1 year ago class-acf-field-oembed.php 1 year ago class-acf-field-output.php 1 year ago class-acf-field-page_link.php 1 year ago class-acf-field-password.php 1 year ago class-acf-field-post_object.php 1 year ago class-acf-field-radio.php 1 year ago class-acf-field-range.php 1 year ago class-acf-field-relationship.php 1 year ago class-acf-field-select.php 1 year ago class-acf-field-separator.php 1 year ago class-acf-field-tab.php 1 year ago class-acf-field-taxonomy.php 1 year ago class-acf-field-text.php 1 year ago class-acf-field-textarea.php 1 year ago class-acf-field-time_picker.php 1 year ago class-acf-field-true_false.php 1 year ago class-acf-field-url.php 1 year ago class-acf-field-user.php 1 year ago class-acf-field-wysiwyg.php 1 year ago class-acf-field.php 1 year ago index.php 1 year ago
class-acf-field-group.php
650 lines
1 <?php
2
3 if ( ! class_exists( 'acf_field__group' ) ) :
4 #[AllowDynamicProperties]
5 class acf_field__group extends acf_field {
6
7
8 /**
9 * This function will setup the field type data
10 *
11 * @type function
12 * @date 5/03/2014
13 * @since 5.0.0
14 *
15 * @param n/a
16 * @return n/a
17 */
18 function initialize() {
19
20 // vars
21 $this->name = 'group';
22 $this->label = __( 'Group', 'acf' );
23 $this->category = 'layout';
24 $this->description = __( 'Provides a way to structure fields into groups to better organize the data and the edit screen.', 'acf' );
25 $this->preview_image = acf_get_url() . '/assets/images/field-type-previews/field-preview-group.png';
26 $this->doc_url = 'https://www.advancedcustomfields.com/resources/group/';
27 $this->supports = array(
28 'bindings' => false,
29 );
30 $this->defaults = array(
31 'sub_fields' => array(),
32 'layout' => 'block',
33 );
34 $this->have_rows = 'single';
35
36 // field filters
37 $this->add_field_filter( 'acf/prepare_field_for_export', array( $this, 'prepare_field_for_export' ) );
38 $this->add_field_filter( 'acf/prepare_field_for_import', array( $this, 'prepare_field_for_import' ) );
39 }
40
41
42 /**
43 * This filter is appied to the $field after it is loaded from the database
44 *
45 * @type filter
46 * @since 3.6
47 * @date 23/01/13
48 *
49 * @param $field - the field array holding all the field options
50 *
51 * @return $field - the field array holding all the field options
52 */
53 function load_field( $field ) {
54
55 // vars
56 $sub_fields = acf_get_fields( $field );
57
58 // append
59 if ( $sub_fields ) {
60 $field['sub_fields'] = $sub_fields;
61 }
62
63 // return
64 return $field;
65 }
66
67
68 /**
69 * This filter is applied to the $value after it is loaded from the db
70 *
71 * @type filter
72 * @since 3.6
73 * @date 23/01/13
74 *
75 * @param $value (mixed) the value found in the database
76 * @param $post_id (mixed) the post_id from which the value was loaded
77 * @param $field (array) the field array holding all the field options
78 * @return $value
79 */
80 function load_value( $value, $post_id, $field ) {
81
82 // bail early if no sub fields
83 if ( empty( $field['sub_fields'] ) ) {
84 return $value;
85 }
86
87 // modify names
88 $field = $this->prepare_field_for_db( $field );
89
90 // load sub fields
91 $value = array();
92
93 // loop
94 foreach ( $field['sub_fields'] as $sub_field ) {
95
96 // load
97 $value[ $sub_field['key'] ] = acf_get_value( $post_id, $sub_field );
98 }
99
100 // return
101 return $value;
102 }
103
104
105 /**
106 * This filter is appied to the $value after it is loaded from the db and before it is returned to the template
107 *
108 * @type filter
109 * @since 3.6
110 *
111 * @param mixed $value The value which was loaded from the database.
112 * @param mixed $post_id The $post_id from which the value was loaded.
113 * @param array $field The field array holding all the field options.
114 * @param boolean $escape_html Should the field return a HTML safe formatted value.
115 * @return mixed the modified value
116 */
117 public function format_value( $value, $post_id, $field, $escape_html = false ) {
118 // bail early if no value
119 if ( empty( $value ) ) {
120 return false;
121 }
122
123 // modify names
124 $field = $this->prepare_field_for_db( $field );
125
126 // loop
127 foreach ( $field['sub_fields'] as $sub_field ) {
128
129 // extract value
130 $sub_value = acf_extract_var( $value, $sub_field['key'] );
131
132 // format value
133 $sub_value = acf_format_value( $sub_value, $post_id, $sub_field, $escape_html );
134
135 // append to $row
136 $value[ $sub_field['_name'] ] = $sub_value;
137 }
138
139 // return
140 return $value;
141 }
142
143
144 /**
145 * This filter is appied to the $value before it is updated in the db
146 *
147 * @type filter
148 * @since 3.6
149 * @date 23/01/13
150 *
151 * @param $value - the value which will be saved in the database
152 * @param $field - the field array holding all the field options
153 * @param $post_id - the post_id of which the value will be saved
154 *
155 * @return $value - the modified value
156 */
157 function update_value( $value, $post_id, $field ) {
158
159 // bail early if no value
160 if ( ! acf_is_array( $value ) ) {
161 return null;
162 }
163
164 // bail early if no sub fields
165 if ( empty( $field['sub_fields'] ) ) {
166 return null;
167 }
168
169 // modify names
170 $field = $this->prepare_field_for_db( $field );
171
172 // loop
173 foreach ( $field['sub_fields'] as $sub_field ) {
174
175 // vars
176 $v = false;
177
178 // key (backend)
179 if ( isset( $value[ $sub_field['key'] ] ) ) {
180 $v = $value[ $sub_field['key'] ];
181
182 // name (frontend)
183 } elseif ( isset( $value[ $sub_field['_name'] ] ) ) {
184 $v = $value[ $sub_field['_name'] ];
185
186 // empty
187 } else {
188
189 // input is not set (hidden by conditioanl logic)
190 continue;
191 }
192
193 // update value
194 acf_update_value( $v, $post_id, $sub_field );
195 }
196
197 // return
198 return '';
199 }
200
201
202 /**
203 * This function will modify sub fields ready for update / load
204 *
205 * @type function
206 * @date 4/11/16
207 * @since 5.5.0
208 *
209 * @param $field (array)
210 * @return $field
211 */
212 function prepare_field_for_db( $field ) {
213
214 // bail early if no sub fields
215 if ( empty( $field['sub_fields'] ) ) {
216 return $field;
217 }
218
219 // loop
220 foreach ( $field['sub_fields'] as &$sub_field ) {
221
222 // prefix name
223 $sub_field['name'] = $field['name'] . '_' . $sub_field['_name'];
224 }
225
226 // return
227 return $field;
228 }
229
230
231 /**
232 * Create the HTML interface for your field
233 *
234 * @param $field - an array holding all the field's data
235 *
236 * @type action
237 * @since 3.6
238 * @date 23/01/13
239 */
240 function render_field( $field ) {
241
242 // bail early if no sub fields
243 if ( empty( $field['sub_fields'] ) ) {
244 return;
245 }
246
247 // load values
248 foreach ( $field['sub_fields'] as &$sub_field ) {
249
250 // add value
251 if ( isset( $field['value'][ $sub_field['key'] ] ) ) {
252
253 // this is a normal value
254 $sub_field['value'] = $field['value'][ $sub_field['key'] ];
255 } elseif ( isset( $sub_field['default_value'] ) ) {
256
257 // no value, but this sub field has a default value
258 $sub_field['value'] = $sub_field['default_value'];
259 }
260
261 // update prefix to allow for nested values
262 $sub_field['prefix'] = $field['name'];
263
264 // restore required
265 if ( $field['required'] ) {
266 $sub_field['required'] = 0;
267 }
268 }
269
270 // render
271 if ( $field['layout'] == 'table' ) {
272 $this->render_field_table( $field );
273 } else {
274 $this->render_field_block( $field );
275 }
276 }
277
278
279 /**
280 * description
281 *
282 * @type function
283 * @date 12/07/2016
284 * @since 5.4.0
285 *
286 * @param $post_id (int)
287 * @return $post_id (int)
288 */
289 function render_field_block( $field ) {
290
291 // vars
292 $label_placement = ( $field['layout'] == 'block' ) ? 'top' : 'left';
293
294 // html
295 echo '<div class="acf-fields -' . esc_attr( $label_placement ) . ' -border">';
296
297 foreach ( $field['sub_fields'] as $sub_field ) {
298 acf_render_field_wrap( $sub_field );
299 }
300
301 echo '</div>';
302 }
303
304
305 /**
306 * description
307 *
308 * @type function
309 * @date 12/07/2016
310 * @since 5.4.0
311 *
312 * @param $post_id (int)
313 * @return $post_id (int)
314 */
315 function render_field_table( $field ) {
316
317 ?>
318 <table class="acf-table">
319 <thead>
320 <tr>
321 <?php
322 foreach ( $field['sub_fields'] as $sub_field ) :
323
324 // prepare field (allow sub fields to be removed)
325 $sub_field = acf_prepare_field( $sub_field );
326
327 // bail early if no field
328 if ( ! $sub_field ) {
329 continue;
330 }
331
332 // vars
333 $atts = array();
334 $atts['class'] = 'acf-th';
335 $atts['data-name'] = $sub_field['_name'];
336 $atts['data-type'] = $sub_field['type'];
337 $atts['data-key'] = $sub_field['key'];
338
339 // Add custom width
340 if ( $sub_field['wrapper']['width'] ) {
341 $atts['data-width'] = $sub_field['wrapper']['width'];
342 $atts['style'] = 'width: ' . $sub_field['wrapper']['width'] . '%;';
343 }
344
345 ?>
346 <th <?php echo acf_esc_attrs( $atts ); ?>>
347 <?php acf_render_field_label( $sub_field ); ?>
348 <?php acf_render_field_instructions( $sub_field ); ?>
349 </th>
350 <?php endforeach; ?>
351 </tr>
352 </thead>
353 <tbody>
354 <tr class="acf-row">
355 <?php
356
357 foreach ( $field['sub_fields'] as $sub_field ) {
358 acf_render_field_wrap( $sub_field, 'td' );
359 }
360
361 ?>
362 </tr>
363 </tbody>
364 </table>
365 <?php
366 }
367
368
369 /**
370 * Create extra options for your field. This is rendered when editing a field.
371 * The value of $field['name'] can be used (like bellow) to save extra data to the $field
372 *
373 * @type action
374 * @since 3.6
375 * @date 23/01/13
376 *
377 * @param $field - an array holding all the field's data
378 */
379 function render_field_settings( $field ) {
380
381 // vars
382 $args = array(
383 'fields' => $field['sub_fields'],
384 'parent' => $field['ID'],
385 'is_subfield' => true,
386 );
387
388 ?>
389 <div class="acf-field acf-field-setting-sub_fields" data-setting="group" data-name="sub_fields">
390 <div class="acf-label">
391 <label><?php esc_html_e( 'Sub Fields', 'acf' ); ?></label>
392 </div>
393 <div class="acf-input acf-input-sub">
394 <?php
395
396 acf_get_view( 'acf-field-group/fields', $args );
397
398 ?>
399 </div>
400 </div>
401 <?php
402
403 // layout
404 acf_render_field_setting(
405 $field,
406 array(
407 'label' => __( 'Layout', 'acf' ),
408 'instructions' => __( 'Specify the style used to render the selected fields', 'acf' ),
409 'type' => 'radio',
410 'name' => 'layout',
411 'layout' => 'horizontal',
412 'choices' => array(
413 'block' => __( 'Block', 'acf' ),
414 'table' => __( 'Table', 'acf' ),
415 'row' => __( 'Row', 'acf' ),
416 ),
417 )
418 );
419 }
420
421
422 /**
423 * description
424 *
425 * @type function
426 * @date 11/02/2014
427 * @since 5.0.0
428 *
429 * @param $post_id (int)
430 * @return $post_id (int)
431 */
432 function validate_value( $valid, $value, $field, $input ) {
433
434 // bail early if no $value
435 if ( empty( $value ) ) {
436 return $valid;
437 }
438
439 // bail early if no sub fields
440 if ( empty( $field['sub_fields'] ) ) {
441 return $valid;
442 }
443
444 // loop
445 foreach ( $field['sub_fields'] as $sub_field ) {
446
447 // get sub field
448 $k = $sub_field['key'];
449
450 // bail early if value not set (conditional logic?)
451 if ( ! isset( $value[ $k ] ) ) {
452 continue;
453 }
454
455 // required
456 if ( $field['required'] ) {
457 $sub_field['required'] = 1;
458 }
459
460 // validate
461 acf_validate_value( $value[ $k ], $sub_field, "{$input}[{$k}]" );
462 }
463
464 // return
465 return $valid;
466 }
467
468
469 /**
470 * This filter is appied to the $field before it is duplicated and saved to the database
471 *
472 * @type filter
473 * @since 3.6
474 * @date 23/01/13
475 *
476 * @param $field - the field array holding all the field options
477 *
478 * @return $field - the modified field
479 */
480 function duplicate_field( $field ) {
481
482 // get sub fields
483 $sub_fields = acf_extract_var( $field, 'sub_fields' );
484
485 // save field to get ID
486 $field = acf_update_field( $field );
487
488 // duplicate sub fields
489 acf_duplicate_fields( $sub_fields, $field['ID'] );
490
491 // return
492 return $field;
493 }
494
495 /**
496 * prepare_field_for_export
497 *
498 * Prepares the field for export.
499 *
500 * @date 11/03/2014
501 * @since 5.0.0
502 *
503 * @param array $field The field settings.
504 * @return array
505 */
506 function prepare_field_for_export( $field ) {
507
508 // Check for sub fields.
509 if ( ! empty( $field['sub_fields'] ) ) {
510 $field['sub_fields'] = acf_prepare_fields_for_export( $field['sub_fields'] );
511 }
512 return $field;
513 }
514
515 /**
516 * prepare_field_for_import
517 *
518 * Returns a flat array of fields containing all sub fields ready for import.
519 *
520 * @date 11/03/2014
521 * @since 5.0.0
522 *
523 * @param array $field The field settings.
524 * @return array
525 */
526 function prepare_field_for_import( $field ) {
527
528 // Check for sub fields.
529 if ( ! empty( $field['sub_fields'] ) ) {
530 $sub_fields = acf_extract_var( $field, 'sub_fields' );
531
532 // Modify sub fields.
533 foreach ( $sub_fields as $i => $sub_field ) {
534 $sub_fields[ $i ]['parent'] = $field['key'];
535 $sub_fields[ $i ]['menu_order'] = $i;
536 }
537
538 // Return array of [field, sub_1, sub_2, ...].
539 return array_merge( array( $field ), $sub_fields );
540 }
541 return $field;
542 }
543
544
545 /**
546 * Called when deleting this field's value.
547 *
548 * @date 1/07/2015
549 * @since 5.2.3
550 *
551 * @param mixed $post_id The post ID being saved
552 * @param string $meta_key The field name as seen by the DB
553 * @param array $field The field settings
554 * @return void
555 */
556 function delete_value( $post_id, $meta_key, $field ) {
557
558 // bail early if no sub fields
559 if ( empty( $field['sub_fields'] ) ) {
560 return null;
561 }
562
563 // modify names
564 $field = $this->prepare_field_for_db( $field );
565
566 // loop
567 foreach ( $field['sub_fields'] as $sub_field ) {
568 acf_delete_value( $post_id, $sub_field );
569 }
570 }
571
572 /**
573 * delete_field
574 *
575 * Called when deleting a field of this type.
576 *
577 * @date 8/11/18
578 * @since 5.8.0
579 *
580 * @param arra $field The field settings.
581 * @return void
582 */
583 function delete_field( $field ) {
584
585 // loop over sub fields and delete them
586 if ( $field['sub_fields'] ) {
587 foreach ( $field['sub_fields'] as $sub_field ) {
588 acf_delete_field( $sub_field['ID'] );
589 }
590 }
591 }
592
593 /**
594 * Return the schema array for the REST API.
595 *
596 * @param array $field
597 * @return array
598 */
599 public function get_rest_schema( array $field ) {
600 $schema = array(
601 'type' => array( 'object', 'null' ),
602 'properties' => array(),
603 'required' => ! empty( $field['required'] ),
604 );
605
606 foreach ( $field['sub_fields'] as $sub_field ) {
607 if ( $sub_field_schema = acf_get_field_rest_schema( $sub_field ) ) {
608 $schema['properties'][ $sub_field['name'] ] = $sub_field_schema;
609 }
610 }
611
612 return $schema;
613 }
614
615 /**
616 * Apply basic formatting to prepare the value for default REST output.
617 *
618 * @param mixed $value
619 * @param integer|string $post_id
620 * @param array $field
621 * @return array|mixed
622 */
623 public function format_value_for_rest( $value, $post_id, array $field ) {
624 if ( empty( $value ) || ! is_array( $value ) || empty( $field['sub_fields'] ) ) {
625 return $value;
626 }
627
628 // Loop through each row and within that, each sub field to process sub fields individually.
629 foreach ( $field['sub_fields'] as $sub_field ) {
630
631 // Extract the sub field 'field_key'=>'value' pair from the $value and format it.
632 $sub_value = acf_extract_var( $value, $sub_field['key'] );
633 $sub_value = acf_format_value_for_rest( $sub_value, $post_id, $sub_field );
634
635 // Add the sub field value back to the $value but mapped to the field name instead
636 // of the key reference.
637 $value[ $sub_field['name'] ] = $sub_value;
638 }
639
640 return $value;
641 }
642 }
643
644
645 // initialize
646 acf_register_field_type( 'acf_field__group' );
647 endif; // class_exists check
648
649 ?>
650