PluginProbe
Advanced Custom Fields (ACF®) / 5.9.4
Advanced Custom Fields (ACF®) v5.9.4
6.8.9 6.8.8 6.8.7 6.8.6 6.8.5 6.8.4 6.8.3 6.8.2 6.8.1 5.8.5 5.8.6 5.8.7 5.8.8 5.8.9 5.9.0 5.9.1 5.9.2 5.9.3 5.9.4 5.9.5 5.9.6 5.9.7 5.9.8 5.9.9 6.0.0 All 230 releases
advanced-custom-fields / includes / api / api-template.php
api-template.php
1,480 lines 27.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * get_field()
5 *
6 * This function will return a custom field value for a specific field name/key + post_id.
7 * There is a 3rd parameter to turn on/off formating. This means that an image field will not use
8 * its 'return option' to format the value but return only what was saved in the database
9 *
10 * @type function
11 * @since 3.6
12 * @date 29/01/13
13 *
14 * @param $selector (string) the field name or key
15 * @param $post_id (mixed) the post_id of which the value is saved against
16 * @param $format_value (boolean) whether or not to format the value as described above
17 * @return (mixed)
18 */
19
20 function get_field( $selector, $post_id = false, $format_value = true ) {
21
22 // filter post_id
23 $post_id = acf_get_valid_post_id( $post_id );
24
25
26 // get field
27 $field = acf_maybe_get_field( $selector, $post_id );
28
29
30 // create dummy field
31 if( !$field ) {
32
33 $field = acf_get_valid_field(array(
34 'name' => $selector,
35 'key' => '',
36 'type' => '',
37 ));
38
39
40 // prevent formatting
41 $format_value = false;
42
43 }
44
45
46 // get value for field
47 $value = acf_get_value( $post_id, $field );
48
49
50 // format value
51 if( $format_value ) {
52
53 // get value for field
54 $value = acf_format_value( $value, $post_id, $field );
55
56 }
57
58
59 // return
60 return $value;
61
62 }
63
64
65 /*
66 * the_field()
67 *
68 * This function is the same as echo get_field().
69 *
70 * @type function
71 * @since 1.0.3
72 * @date 29/01/13
73 *
74 * @param $selector (string) the field name or key
75 * @param $post_id (mixed) the post_id of which the value is saved against
76 * @return n/a
77 */
78
79 function the_field( $selector, $post_id = false, $format_value = true ) {
80
81 $value = get_field($selector, $post_id, $format_value);
82
83 if( is_array($value) ) {
84
85 $value = @implode( ', ', $value );
86
87 }
88
89 echo $value;
90
91 }
92
93
94 /*
95 * get_field_object()
96 *
97 * This function will return an array containing all the field data for a given field_name
98 *
99 * @type function
100 * @since 3.6
101 * @date 3/02/13
102 *
103 * @param $selector (string) the field name or key
104 * @param $post_id (mixed) the post_id of which the value is saved against
105 * @param $format_value (boolean) whether or not to format the field value
106 * @param $load_value (boolean) whether or not to load the field value
107 * @return $field (array)
108 */
109
110 function get_field_object( $selector, $post_id = false, $format_value = true, $load_value = true ) {
111
112 // compatibilty
113 if( is_array($format_value) ) extract( $format_value );
114
115
116 // get valid post_id
117 $post_id = acf_get_valid_post_id( $post_id );
118
119
120 // get field key
121 $field = acf_maybe_get_field( $selector, $post_id );
122
123
124 // bail early if no field found
125 if( !$field ) return false;
126
127
128 // load value
129 if( $load_value ) {
130
131 $field['value'] = acf_get_value( $post_id, $field );
132
133 }
134
135
136 // format value
137 if( $format_value ) {
138
139 // get value for field
140 $field['value'] = acf_format_value( $field['value'], $post_id, $field );
141
142 }
143
144
145 // return
146 return $field;
147
148 }
149
150 /*
151 * acf_get_object_field
152 *
153 * This function will return a field for the given selector.
154 * It will also review the field_reference to ensure the correct field is returned which makes it useful for the template API
155 *
156 * @type function
157 * @date 4/08/2015
158 * @since 5.2.3
159 *
160 * @param $selector (mixed) identifyer of field. Can be an ID, key, name or post object
161 * @param $post_id (mixed) the post_id of which the value is saved against
162 * @param $strict (boolean) if true, return a field only when a field key is found.
163 * @return $field (array)
164 */
165 function acf_maybe_get_field( $selector, $post_id = false, $strict = true ) {
166
167 // init
168 acf_init();
169
170 // Check if field key was given.
171 if( acf_is_field_key($selector) ) {
172 return acf_get_field( $selector );
173 }
174
175 // Lookup field via reference.
176 $post_id = acf_get_valid_post_id( $post_id );
177 $field = acf_get_meta_field( $selector, $post_id );
178 if( $field ) {
179 return $field;
180 }
181
182 // Lookup field loosely via name.
183 if( !$strict ) {
184 return acf_get_field( $selector );
185 }
186
187 // Return no result.
188 return false;
189 }
190
191 /*
192 * acf_maybe_get_sub_field
193 *
194 * This function will attempt to find a sub field
195 *
196 * @type function
197 * @date 3/10/2016
198 * @since 5.4.0
199 *
200 * @param $post_id (int)
201 * @return $post_id (int)
202 */
203
204 function acf_maybe_get_sub_field( $selectors, $post_id = false, $strict = true ) {
205
206 // bail ealry if not enough selectors
207 if( !is_array($selectors) || count($selectors) < 3 ) return false;
208
209
210 // vars
211 $offset = acf_get_setting('row_index_offset');
212 $selector = acf_extract_var( $selectors, 0 );
213 $selectors = array_values( $selectors ); // reset keys
214
215
216 // attempt get field
217 $field = acf_maybe_get_field( $selector, $post_id, $strict );
218
219
220 // bail early if no field
221 if( !$field ) return false;
222
223
224 // loop
225 for( $j = 0; $j < count($selectors); $j+=2 ) {
226
227 // vars
228 $sub_i = $selectors[ $j ];
229 $sub_s = $selectors[ $j+1 ];
230 $field_name = $field['name'];
231
232
233 // find sub field
234 $field = acf_get_sub_field( $sub_s, $field );
235
236
237 // bail early if no sub field
238 if( !$field ) return false;
239
240
241 // add to name
242 $field['name'] = $field_name . '_' . ($sub_i-$offset) . '_' . $field['name'];
243
244 }
245
246
247 // return
248 return $field;
249 }
250
251 /*
252 * get_fields()
253 *
254 * This function will return an array containing all the custom field values for a specific post_id.
255 * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the values.
256 *
257 * @type function
258 * @since 3.6
259 * @date 29/01/13
260 *
261 * @param $post_id (mixed) the post_id of which the value is saved against
262 * @param $format_value (boolean) whether or not to format the field value
263 * @return (array) associative array where field name => field value
264 */
265
266 function get_fields( $post_id = false, $format_value = true ) {
267
268 // vars
269 $fields = get_field_objects( $post_id, $format_value );
270 $meta = array();
271
272
273 // bail early
274 if( !$fields ) return false;
275
276
277 // populate
278 foreach( $fields as $k => $field ) {
279
280 $meta[ $k ] = $field['value'];
281
282 }
283
284
285 // return
286 return $meta;
287
288 }
289
290
291 /*
292 * get_field_objects()
293 *
294 * This function will return an array containing all the custom field objects for a specific post_id.
295 * The function is not very elegant and wastes a lot of PHP memory / SQL queries if you are not using all the fields / values.
296 *
297 * @type function
298 * @since 3.6
299 * @date 29/01/13
300 *
301 * @param $post_id (mixed) the post_id of which the value is saved against
302 * @param $format_value (boolean) whether or not to format the field value
303 * @param $load_value (boolean) whether or not to load the field value
304 * @return (array) associative array where field name => field
305 */
306
307 function get_field_objects( $post_id = false, $format_value = true, $load_value = true ) {
308
309 // init
310 acf_init();
311
312 // validate post_id
313 $post_id = acf_get_valid_post_id( $post_id );
314
315 // get meta
316 $meta = acf_get_meta( $post_id );
317
318 // bail early if no meta
319 if( empty($meta) ) return false;
320
321 // populate vars
322 $fields = array();
323 foreach( $meta as $key => $value ) {
324
325 // bail if reference key does not exist
326 if( !isset($meta["_$key"]) ) continue;
327
328 // get field
329 $field = acf_get_field($meta["_$key"]);
330
331 // bail early if no field, or if the field's name is different to $key
332 // - solves problem where sub fields (and clone fields) are incorrectly allowed
333 if( !$field || $field['name'] !== $key ) continue;
334
335 // load value
336 if( $load_value ) {
337 $field['value'] = acf_get_value( $post_id, $field );
338 }
339
340 // format value
341 if( $format_value ) {
342 $field['value'] = acf_format_value( $field['value'], $post_id, $field );
343 }
344
345 // append to $value
346 $fields[ $key ] = $field;
347 }
348
349 // no value
350 if( empty($fields) ) return false;
351
352 // return
353 return $fields;
354 }
355
356
357 /**
358 * have_rows
359 *
360 * Checks if a field (such as Repeater or Flexible Content) has any rows of data to loop over.
361 * This function is intended to be used in conjunction with the_row() to step through available values.
362 *
363 * @date 2/09/13
364 * @since 4.3.0
365 *
366 * @param string $selector The field name or field key.
367 * @param mixed $post_id The post ID where the value is saved. Defaults to the current post.
368 * @return bool
369 */
370 function have_rows( $selector, $post_id = false ) {
371
372 // Validate and backup $post_id.
373 $_post_id = $post_id;
374 $post_id = acf_get_valid_post_id( $post_id );
375
376 // Vars.
377 $key = "selector={$selector}/post_id={$post_id}";
378 $active_loop = acf_get_loop('active');
379 $prev_loop = acf_get_loop('previous');
380 $new_loop = false;
381 $sub_field = false;
382
383 // Check if no active loop.
384 if( !$active_loop ) {
385 $new_loop = 'parent';
386
387 // Detect "change" compared to the active loop.
388 } elseif( $key !== $active_loop['key'] ) {
389
390 // Find sub field and check if a sub value exists.
391 $sub_field_exists = false;
392 $sub_field = acf_get_sub_field($selector, $active_loop['field']);
393 if( $sub_field ) {
394 $sub_field_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] );
395 }
396
397 // Detect change in post_id.
398 if( $post_id != $active_loop['post_id'] ) {
399
400 // Case: Change in $post_id was due to this being a nested loop and not specifying the $post_id.
401 // Action: Move down one level into a new loop.
402 if( empty($_post_id) && $sub_field_exists ) {
403 $new_loop = 'child';
404
405 // Case: Change in $post_id was due to a nested loop ending.
406 // Action: move up one level through the loops.
407 } elseif( $prev_loop && $prev_loop['post_id'] == $post_id ) {
408 acf_remove_loop('active');
409 $active_loop = $prev_loop;
410
411 // Case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects.
412 // Action: leave this current loop alone and create a new parent loop.
413 } else {
414 $new_loop = 'parent';
415 }
416
417 // Detect change in selector.
418 } elseif( $selector != $active_loop['selector'] ) {
419
420 // Case: Change in $field_name was due to this being a nested loop.
421 // Action: move down one level into a new loop.
422 if( $sub_field_exists ) {
423 $new_loop = 'child';
424
425 // Case: Change in $field_name was due to a nested loop ending.
426 // Action: move up one level through the loops.
427 } elseif( $prev_loop && $prev_loop['selector'] == $selector && $prev_loop['post_id'] == $post_id ) {
428 acf_remove_loop('active');
429 $active_loop = $prev_loop;
430
431 // Case: Change in $field_name is the most obvious, this is a new loop for a different field within the $post.
432 // Action: leave this current loop alone and create a new parent loop.
433 } else {
434 $new_loop = 'parent';
435 }
436 }
437 }
438
439 // Add loop if required.
440 if( $new_loop ) {
441 $args = array(
442 'key' => $key,
443 'selector' => $selector,
444 'post_id' => $post_id,
445 'name' => null,
446 'value' => null,
447 'field' => null,
448 'i' => -1,
449 );
450
451 // Case: Parent loop.
452 if( $new_loop === 'parent' ) {
453 $field = get_field_object( $selector, $post_id, false );
454 if( $field ) {
455 $args['field'] = $field;
456 $args['value'] = $field['value'];
457 $args['name'] = $field['name'];
458 unset( $args['field']['value'] );
459 }
460
461 // Case: Child loop ($sub_field must exist).
462 } else {
463 $args['field'] = $sub_field;
464 $args['value'] = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ];
465 $args['name'] = "{$active_loop['name']}_{$active_loop['i']}_{$sub_field['name']}";
466 $args['post_id'] = $active_loop['post_id'];
467 }
468
469 // Bail early if value is either empty or a non array.
470 if( !$args['value'] || !is_array($args['value']) ) {
471 return false;
472 }
473
474 // Allow for non repeatable data for Group and Clone fields.
475 if( acf_get_field_type_prop($args['field']['type'], 'have_rows') === 'single' ) {
476 $args['value'] = array( $args['value'] );
477 }
478
479 // Add loop.
480 $active_loop = acf_add_loop($args);
481 }
482
483 // Return true if next row exists.
484 if( $active_loop && isset($active_loop['value'][ $active_loop['i']+1 ]) ) {
485 return true;
486 }
487
488 // Return false if no next row.
489 acf_remove_loop('active');
490 return false;
491 }
492
493
494 /*
495 * the_row
496 *
497 * This function will progress the global repeater or flexible content value 1 row
498 *
499 * @type function
500 * @date 2/09/13
501 * @since 4.3.0
502 *
503 * @param N/A
504 * @return (array) the current row data
505 */
506
507 function the_row( $format = false ) {
508
509 // vars
510 $i = acf_get_loop('active', 'i');
511
512
513 // increase
514 $i++;
515
516
517 // update
518 acf_update_loop('active', 'i', $i);
519
520
521 // return
522 return get_row( $format );
523
524 }
525
526 function get_row( $format = false ) {
527
528 // vars
529 $loop = acf_get_loop('active');
530
531
532 // bail early if no loop
533 if( !$loop ) return false;
534
535
536 // get value
537 $value = acf_maybe_get( $loop['value'], $loop['i'] );
538
539
540 // bail early if no current value
541 // possible if get_row_layout() is called before the_row()
542 if( !$value ) return false;
543
544
545 // format
546 if( $format ) {
547
548 // vars
549 $field = $loop['field'];
550
551
552 // single row
553 if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) {
554
555 // format value
556 $value = acf_format_value( $value, $loop['post_id'], $field );
557
558 // multiple rows
559 } else {
560
561 // format entire value
562 // - solves problem where cached value is incomplete
563 // - no performance issues here thanks to cache
564 $value = acf_format_value( $loop['value'], $loop['post_id'], $field );
565 $value = acf_maybe_get( $value, $loop['i'] );
566
567 }
568
569 }
570
571
572 // return
573 return $value;
574
575 }
576
577 function get_row_index() {
578
579 // vars
580 $i = acf_get_loop('active', 'i');
581 $offset = acf_get_setting('row_index_offset');
582
583
584 // return
585 return $offset + $i;
586
587 }
588
589 function the_row_index() {
590
591 echo get_row_index();
592
593 }
594
595
596 /*
597 * get_row_sub_field
598 *
599 * This function is used inside a 'has_sub_field' while loop to return a sub field object
600 *
601 * @type function
602 * @date 16/05/2016
603 * @since 5.3.8
604 *
605 * @param $selector (string)
606 * @return (array)
607 */
608
609 function get_row_sub_field( $selector ) {
610
611 // vars
612 $row = acf_get_loop('active');
613
614
615 // bail early if no row
616 if( !$row ) return false;
617
618
619 // attempt to find sub field
620 $sub_field = acf_get_sub_field($selector, $row['field']);
621
622
623 // bail early if no field
624 if( !$sub_field ) return false;
625
626
627 // update field's name based on row data
628 $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}";
629
630
631 // return
632 return $sub_field;
633
634 }
635
636
637 /*
638 * get_row_sub_value
639 *
640 * This function is used inside a 'has_sub_field' while loop to return a sub field value
641 *
642 * @type function
643 * @date 16/05/2016
644 * @since 5.3.8
645 *
646 * @param $selector (string)
647 * @return (mixed)
648 */
649
650 function get_row_sub_value( $selector ) {
651
652 // vars
653 $row = acf_get_loop('active');
654
655
656 // bail early if no row
657 if( !$row ) return null;
658
659
660 // return value
661 if( isset($row['value'][ $row['i'] ][ $selector ]) ) {
662
663 return $row['value'][ $row['i'] ][ $selector ];
664
665 }
666
667
668 // return
669 return null;
670
671 }
672
673
674 /*
675 * reset_rows
676 *
677 * This function will find the current loop and unset it from the global array.
678 * To bo used when loop finishes or a break is used
679 *
680 * @type function
681 * @date 26/10/13
682 * @since 5.0.0
683 *
684 * @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row
685 * @return (boolean)
686 */
687
688 function reset_rows() {
689
690 // remove last loop
691 acf_remove_loop('active');
692
693
694 // return
695 return true;
696
697 }
698
699
700 /*
701 * has_sub_field()
702 *
703 * This function is used inside a while loop to return either true or false (loop again or stop).
704 * When using a repeater or flexible content field, it will loop through the rows until
705 * there are none left or a break is detected
706 *
707 * @type function
708 * @since 1.0.3
709 * @date 29/01/13
710 *
711 * @param $field_name (string) the field name
712 * @param $post_id (mixed) the post_id of which the value is saved against
713 * @return (boolean)
714 */
715
716 function has_sub_field( $field_name, $post_id = false ) {
717
718 // vars
719 $r = have_rows( $field_name, $post_id );
720
721
722 // if has rows, progress through 1 row for the while loop to work
723 if( $r ) {
724
725 the_row();
726
727 }
728
729
730 // return
731 return $r;
732
733 }
734
735 function has_sub_fields( $field_name, $post_id = false ) {
736
737 return has_sub_field( $field_name, $post_id );
738
739 }
740
741
742 /*
743 * get_sub_field()
744 *
745 * This function is used inside a 'has_sub_field' while loop to return a sub field value
746 *
747 * @type function
748 * @since 1.0.3
749 * @date 29/01/13
750 *
751 * @param $field_name (string) the field name
752 * @return (mixed)
753 */
754
755 function get_sub_field( $selector = '', $format_value = true ) {
756
757 // get sub field
758 $sub_field = get_sub_field_object( $selector, $format_value );
759
760
761 // bail early if no sub field
762 if( !$sub_field ) return false;
763
764
765 // return
766 return $sub_field['value'];
767
768 }
769
770
771 /*
772 * the_sub_field()
773 *
774 * This function is the same as echo get_sub_field
775 *
776 * @type function
777 * @since 1.0.3
778 * @date 29/01/13
779 *
780 * @param $field_name (string) the field name
781 * @return n/a
782 */
783
784 function the_sub_field( $field_name, $format_value = true ) {
785
786 $value = get_sub_field( $field_name, $format_value );
787
788 if( is_array($value) ) {
789
790 $value = implode(', ',$value);
791
792 }
793
794 echo $value;
795 }
796
797
798 /*
799 * get_sub_field_object()
800 *
801 * This function is used inside a 'has_sub_field' while loop to return a sub field object
802 *
803 * @type function
804 * @since 3.5.8.1
805 * @date 29/01/13
806 *
807 * @param $child_name (string) the field name
808 * @return (array)
809 */
810
811 function get_sub_field_object( $selector, $format_value = true, $load_value = true ) {
812
813 // vars
814 $row = acf_get_loop('active');
815
816
817 // bail early if no row
818 if( !$row ) return false;
819
820
821 // attempt to find sub field
822 $sub_field = get_row_sub_field($selector);
823
824
825 // bail early if no sub field
826 if( !$sub_field ) return false;
827
828
829 // load value
830 if( $load_value ) {
831
832 $sub_field['value'] = get_row_sub_value( $sub_field['key'] );
833
834 }
835
836
837 // format value
838 if( $format_value ) {
839
840 // get value for field
841 $sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
842
843 }
844
845
846 // return
847 return $sub_field;
848
849 }
850
851
852 /*
853 * get_row_layout()
854 *
855 * This function will return a string representation of the current row layout within a 'have_rows' loop
856 *
857 * @type function
858 * @since 3.0.6
859 * @date 29/01/13
860 *
861 * @param n/a
862 * @return (string)
863 */
864
865 function get_row_layout() {
866
867 // vars
868 $row = get_row();
869
870
871 // return
872 if( isset($row['acf_fc_layout']) ) {
873
874 return $row['acf_fc_layout'];
875
876 }
877
878
879 // return
880 return false;
881
882 }
883
884
885 /*
886 * acf_shortcode()
887 *
888 * This function is used to add basic shortcode support for the ACF plugin
889 * eg. [acf field="heading" post_id="123" format_value="1"]
890 *
891 * @type function
892 * @since 1.1.1
893 * @date 29/01/13
894 *
895 * @param $field (string) the field name or key
896 * @param $post_id (mixed) the post_id of which the value is saved against
897 * @param $format_value (boolean) whether or not to format the field value
898 * @return (string)
899 */
900
901 function acf_shortcode( $atts ) {
902
903 // extract attributs
904 extract( shortcode_atts( array(
905 'field' => '',
906 'post_id' => false,
907 'format_value' => true
908 ), $atts ) );
909
910
911 // get value and return it
912 $value = get_field( $field, $post_id, $format_value );
913
914
915 // array
916 if( is_array($value) ) {
917
918 $value = @implode( ', ', $value );
919
920 }
921
922
923 // return
924 return $value;
925
926 }
927
928 add_shortcode('acf', 'acf_shortcode');
929
930
931 /*
932 * update_field()
933 *
934 * This function will update a value in the database
935 *
936 * @type function
937 * @since 3.1.9
938 * @date 29/01/13
939 *
940 * @param $selector (string) the field name or key
941 * @param $value (mixed) the value to save in the database
942 * @param $post_id (mixed) the post_id of which the value is saved against
943 * @return (boolean)
944 */
945
946 function update_field( $selector, $value, $post_id = false ) {
947
948 // filter post_id
949 $post_id = acf_get_valid_post_id( $post_id );
950
951
952 // get field
953 $field = acf_maybe_get_field( $selector, $post_id, false );
954
955
956 // create dummy field
957 if( !$field ) {
958
959 $field = acf_get_valid_field(array(
960 'name' => $selector,
961 'key' => '',
962 'type' => '',
963 ));
964
965 }
966
967
968 // save
969 return acf_update_value( $value, $post_id, $field );
970
971 }
972
973
974 /*
975 * update_sub_field
976 *
977 * This function will update a value of a sub field in the database
978 *
979 * @type function
980 * @date 2/04/2014
981 * @since 5.0.0
982 *
983 * @param $selector (mixed) the sub field name or key, or an array of ancestors
984 * @param $value (mixed) the value to save in the database
985 * @param $post_id (mixed) the post_id of which the value is saved against
986 * @return (boolean)
987 */
988
989 function update_sub_field( $selector, $value, $post_id = false ) {
990
991 // vars
992 $sub_field = false;
993
994
995 // get sub field
996 if( is_array($selector) ) {
997
998 $post_id = acf_get_valid_post_id( $post_id );
999 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1000
1001 } else {
1002
1003 $post_id = acf_get_loop('active', 'post_id');
1004 $sub_field = get_row_sub_field( $selector );
1005
1006 }
1007
1008
1009 // bail early if no sub field
1010 if( !$sub_field ) return false;
1011
1012
1013 // update
1014 return acf_update_value( $value, $post_id, $sub_field );
1015
1016 }
1017
1018
1019 /*
1020 * delete_field()
1021 *
1022 * This function will remove a value from the database
1023 *
1024 * @type function
1025 * @since 3.1.9
1026 * @date 29/01/13
1027 *
1028 * @param $selector (string) the field name or key
1029 * @param $post_id (mixed) the post_id of which the value is saved against
1030 * @return (boolean)
1031 */
1032
1033 function delete_field( $selector, $post_id = false ) {
1034
1035 // filter post_id
1036 $post_id = acf_get_valid_post_id( $post_id );
1037
1038
1039 // get field
1040 $field = acf_maybe_get_field( $selector, $post_id );
1041
1042
1043 // delete
1044 return acf_delete_value( $post_id, $field );
1045
1046 }
1047
1048
1049 /*
1050 * delete_sub_field
1051 *
1052 * This function will delete a value of a sub field in the database
1053 *
1054 * @type function
1055 * @date 2/04/2014
1056 * @since 5.0.0
1057 *
1058 * @param $selector (mixed) the sub field name or key, or an array of ancestors
1059 * @param $value (mixed) the value to save in the database
1060 * @param $post_id (mixed) the post_id of which the value is saved against
1061 * @return (boolean)
1062 */
1063
1064 function delete_sub_field( $selector, $post_id = false ) {
1065
1066 return update_sub_field( $selector, null, $post_id );
1067
1068 }
1069
1070
1071 /*
1072 * add_row
1073 *
1074 * This function will add a row of data to a field
1075 *
1076 * @type function
1077 * @date 16/10/2015
1078 * @since 5.2.3
1079 *
1080 * @param $selector (string)
1081 * @param $row (array)
1082 * @param $post_id (mixed)
1083 * @return (boolean)
1084 */
1085
1086 function add_row( $selector, $row = false, $post_id = false ) {
1087
1088 // filter post_id
1089 $post_id = acf_get_valid_post_id( $post_id );
1090
1091
1092 // get field
1093 $field = acf_maybe_get_field( $selector, $post_id, false );
1094
1095
1096 // bail early if no field
1097 if( !$field ) return false;
1098
1099
1100 // get raw value
1101 $value = acf_get_value( $post_id, $field );
1102
1103
1104 // ensure array
1105 $value = acf_get_array($value);
1106
1107
1108 // append
1109 $value[] = $row;
1110
1111
1112 // update value
1113 acf_update_value( $value, $post_id, $field );
1114
1115
1116 // return
1117 return count($value);
1118
1119 }
1120
1121
1122 /*
1123 * add_sub_row
1124 *
1125 * This function will add a row of data to a field
1126 *
1127 * @type function
1128 * @date 16/10/2015
1129 * @since 5.2.3
1130 *
1131 * @param $selector (string)
1132 * @param $row (array)
1133 * @param $post_id (mixed)
1134 * @return (boolean)
1135 */
1136
1137 function add_sub_row( $selector, $row = false, $post_id = false ) {
1138
1139 // vars
1140 $sub_field = false;
1141
1142
1143 // get sub field
1144 if( is_array($selector) ) {
1145
1146 $post_id = acf_get_valid_post_id( $post_id );
1147 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1148
1149 } else {
1150
1151 $post_id = acf_get_loop('active', 'post_id');
1152 $sub_field = get_row_sub_field( $selector );
1153
1154 }
1155
1156
1157 // bail early if no sub field
1158 if( !$sub_field ) return false;
1159
1160
1161 // get raw value
1162 $value = acf_get_value( $post_id, $sub_field );
1163
1164
1165 // ensure array
1166 $value = acf_get_array( $value );
1167
1168
1169 // append
1170 $value[] = $row;
1171
1172
1173 // update
1174 acf_update_value( $value, $post_id, $sub_field );
1175
1176
1177 // return
1178 return count($value);
1179
1180 }
1181
1182
1183 /*
1184 * update_row
1185 *
1186 * This function will update a row of data to a field
1187 *
1188 * @type function
1189 * @date 19/10/2015
1190 * @since 5.2.3
1191 *
1192 * @param $selector (string)
1193 * @param $i (int)
1194 * @param $row (array)
1195 * @param $post_id (mixed)
1196 * @return (boolean)
1197 */
1198
1199 function update_row( $selector, $i = 1, $row = false, $post_id = false ) {
1200
1201 // vars
1202 $offset = acf_get_setting('row_index_offset');
1203 $i = $i - $offset;
1204
1205
1206 // filter post_id
1207 $post_id = acf_get_valid_post_id( $post_id );
1208
1209
1210 // get field
1211 $field = acf_maybe_get_field( $selector, $post_id, false );
1212
1213
1214 // bail early if no field
1215 if( !$field ) return false;
1216
1217
1218 // get raw value
1219 $value = acf_get_value( $post_id, $field );
1220
1221
1222 // ensure array
1223 $value = acf_get_array($value);
1224
1225
1226 // update
1227 $value[ $i ] = $row;
1228
1229
1230 // update value
1231 acf_update_value( $value, $post_id, $field );
1232
1233
1234 // return
1235 return true;
1236
1237 }
1238
1239
1240 /*
1241 * update_sub_row
1242 *
1243 * This function will add a row of data to a field
1244 *
1245 * @type function
1246 * @date 16/10/2015
1247 * @since 5.2.3
1248 *
1249 * @param $selector (string)
1250 * @param $row (array)
1251 * @param $post_id (mixed)
1252 * @return (boolean)
1253 */
1254
1255 function update_sub_row( $selector, $i = 1, $row = false, $post_id = false ) {
1256
1257 // vars
1258 $sub_field = false;
1259 $offset = acf_get_setting('row_index_offset');
1260 $i = $i - $offset;
1261
1262
1263 // get sub field
1264 if( is_array($selector) ) {
1265
1266 $post_id = acf_get_valid_post_id( $post_id );
1267 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1268
1269 } else {
1270
1271 $post_id = acf_get_loop('active', 'post_id');
1272 $sub_field = get_row_sub_field( $selector );
1273
1274 }
1275
1276
1277 // bail early if no sub field
1278 if( !$sub_field ) return false;
1279
1280
1281 // get raw value
1282 $value = acf_get_value( $post_id, $sub_field );
1283
1284
1285 // ensure array
1286 $value = acf_get_array( $value );
1287
1288
1289 // append
1290 $value[ $i ] = $row;
1291
1292
1293 // update
1294 acf_update_value( $value, $post_id, $sub_field );
1295
1296
1297 // return
1298 return true;
1299
1300 }
1301
1302
1303 /*
1304 * delete_row
1305 *
1306 * This function will delete a row of data from a field
1307 *
1308 * @type function
1309 * @date 19/10/2015
1310 * @since 5.2.3
1311 *
1312 * @param $selector (string)
1313 * @param $i (int)
1314 * @param $post_id (mixed)
1315 * @return (boolean)
1316 */
1317
1318 function delete_row( $selector, $i = 1, $post_id = false ) {
1319
1320 // vars
1321 $offset = acf_get_setting('row_index_offset');
1322 $i = $i - $offset;
1323
1324
1325 // filter post_id
1326 $post_id = acf_get_valid_post_id( $post_id );
1327
1328
1329 // get field
1330 $field = acf_maybe_get_field( $selector, $post_id );
1331
1332
1333 // bail early if no field
1334 if( !$field ) return false;
1335
1336
1337 // get value
1338 $value = acf_get_value( $post_id, $field );
1339
1340
1341 // ensure array
1342 $value = acf_get_array($value);
1343
1344
1345 // bail early if index doesn't exist
1346 if( !isset($value[ $i ]) ) return false;
1347
1348
1349 // unset
1350 unset( $value[ $i ] );
1351
1352
1353 // update
1354 acf_update_value( $value, $post_id, $field );
1355
1356
1357 // return
1358 return true;
1359
1360 }
1361
1362
1363 /*
1364 * delete_sub_row
1365 *
1366 * This function will add a row of data to a field
1367 *
1368 * @type function
1369 * @date 16/10/2015
1370 * @since 5.2.3
1371 *
1372 * @param $selector (string)
1373 * @param $row (array)
1374 * @param $post_id (mixed)
1375 * @return (boolean)
1376 */
1377
1378 function delete_sub_row( $selector, $i = 1, $post_id = false ) {
1379
1380 // vars
1381 $sub_field = false;
1382 $offset = acf_get_setting('row_index_offset');
1383 $i = $i - $offset;
1384
1385
1386 // get sub field
1387 if( is_array($selector) ) {
1388
1389 $post_id = acf_get_valid_post_id( $post_id );
1390 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1391
1392 } else {
1393
1394 $post_id = acf_get_loop('active', 'post_id');
1395 $sub_field = get_row_sub_field( $selector );
1396
1397 }
1398
1399
1400 // bail early if no sub field
1401 if( !$sub_field ) return false;
1402
1403
1404 // get raw value
1405 $value = acf_get_value( $post_id, $sub_field );
1406
1407
1408 // ensure array
1409 $value = acf_get_array( $value );
1410
1411
1412 // bail early if index doesn't exist
1413 if( !isset($value[ $i ]) ) return false;
1414
1415
1416 // append
1417 unset( $value[ $i ] );
1418
1419
1420 // update
1421 acf_update_value( $value, $post_id, $sub_field );
1422
1423
1424 // return
1425 return true;
1426
1427 }
1428
1429
1430 /*
1431 * Depreceated Functions
1432 *
1433 * These functions are outdated
1434 *
1435 * @type function
1436 * @date 4/03/2014
1437 * @since 1.0.0
1438 *
1439 * @param n/a
1440 * @return n/a
1441 */
1442
1443 function create_field( $field ) {
1444
1445 acf_render_field( $field );
1446
1447 }
1448
1449 function render_field( $field ) {
1450
1451 acf_render_field( $field );
1452
1453 }
1454
1455 function reset_the_repeater_field() {
1456
1457 return reset_rows();
1458
1459 }
1460
1461 function the_repeater_field( $field_name, $post_id = false ) {
1462
1463 return has_sub_field( $field_name, $post_id );
1464
1465 }
1466
1467 function the_flexible_field( $field_name, $post_id = false ) {
1468
1469 return has_sub_field( $field_name, $post_id );
1470
1471 }
1472
1473 function acf_filter_post_id( $post_id ) {
1474
1475 return acf_get_valid_post_id( $post_id );
1476
1477 }
1478
1479 ?>
1480