PluginProbe
Advanced Custom Fields (ACF®) / 5.8.1
Advanced Custom Fields (ACF®) v5.8.1
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 in Advanced Custom Fields (ACF®) 5.8.1, at includes/api/api-template.php

1,540 lines 27.8 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 * This function will instantiate a global variable containing the rows of a repeater or flexible content field,
361 * after which, it will determine if another row exists to loop through
362 *
363 * @type function
364 * @date 2/09/13
365 * @since 4.3.0
366 *
367 * @param $field_name (string) the field name
368 * @param $post_id (mixed) the post_id of which the value is saved against
369 * @return (boolean)
370 */
371
372 function have_rows( $selector, $post_id = false ) {
373
374 // reference
375 $_post_id = $post_id;
376
377
378 // filter post_id
379 $post_id = acf_get_valid_post_id( $post_id );
380
381
382 // vars
383 $key = "selector={$selector}/post_id={$post_id}";
384 $active_loop = acf_get_loop('active');
385 $previous_loop = acf_get_loop('previous');
386 $new_parent_loop = false;
387 $new_child_loop = false;
388 $sub_field = false;
389 $sub_exists = false;
390 $change = false;
391
392
393 // no active loops
394 if( !$active_loop ) {
395
396 // create a new loop
397 $new_parent_loop = true;
398
399 // loop has changed
400 } elseif( $active_loop['key'] != $key ) {
401
402 // detect change
403 if( $post_id != $active_loop['post_id'] ) {
404
405 $change = 'post_id';
406
407 } elseif( $selector != $active_loop['selector'] ) {
408
409 $change = 'selector';
410
411 } else {
412
413 // key has changed due to a technicallity, however, the post_id and selector are the same
414
415 }
416
417
418 // attempt to find sub field
419 $sub_field = acf_get_sub_field($selector, $active_loop['field']);
420
421 if( $sub_field ) {
422
423 $sub_exists = isset( $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ] );
424
425 }
426
427
428 // If post_id has changed, this is most likely an archive loop
429 if( $change == 'post_id' ) {
430
431 if( empty($_post_id) && $sub_exists ) {
432
433 // case: Change in $post_id was due to this being a nested loop and not specifying the $post_id
434 // action: move down one level into a new loop
435 $new_child_loop = true;
436
437 } elseif( $previous_loop && $previous_loop['post_id'] == $post_id ) {
438
439 // case: Change in $post_id was due to a nested loop ending
440 // action: move up one level through the loops
441 acf_remove_loop('active');
442 $active_loop = $previous_loop;
443
444 } else {
445
446 // case: Chang in $post_id is the most obvious, used in an WP_Query loop with multiple $post objects
447 // action: leave this current loop alone and create a new parent loop
448 $new_parent_loop = true;
449
450 }
451
452 } elseif( $change == 'selector' ) {
453
454 if( $sub_exists ) {
455
456 // case: Change in $field_name was due to this being a nested loop
457 // action: move down one level into a new loop
458 $new_child_loop = true;
459
460 } elseif( $previous_loop && $previous_loop['selector'] == $selector && $previous_loop['post_id'] == $post_id ) {
461
462 // case: Change in $field_name was due to a nested loop ending
463 // action: move up one level through the loops
464 acf_remove_loop('active');
465 $active_loop = $previous_loop;
466
467 } else {
468
469 // case: Chang in $field_name is the most obvious, this is a new loop for a different field within the $post
470 // action: leave this current loop alone and create a new parent loop
471 $new_parent_loop = true;
472
473 }
474
475 }
476
477 // loop is the same
478 } else {
479
480 // do nothing
481
482 }
483
484
485 // add loop
486 if( $new_parent_loop || $new_child_loop ) {
487
488 // vars
489 $field = null;
490 $value = null;
491 $name = '';
492
493
494 // parent loop
495 if( $new_parent_loop ) {
496
497 $field = get_field_object( $selector, $post_id, false );
498 $value = acf_extract_var( $field, 'value' );
499 $name = $field['name'];
500
501 // child loop
502 } else {
503
504 $field = $sub_field;
505 $value = $active_loop['value'][ $active_loop['i'] ][ $sub_field['key'] ];
506 $name = $active_loop['name'] . '_' . $active_loop['i'] . '_' . $sub_field['name'];
507 $post_id = $active_loop['post_id'];
508
509 }
510
511
512 // bail early if value is either empty or a non array
513 if( !acf_is_array($value) ) return false;
514
515
516 // allow for non repeatable data (group)
517 if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) {
518 $value = array( $value );
519 }
520
521
522 // add loop
523 $active_loop = acf_add_loop(array(
524 'selector' => $selector,
525 'name' => $name, // used by update_sub_field
526 'value' => $value,
527 'field' => $field,
528 'i' => -1,
529 'post_id' => $post_id,
530 'key' => $key
531 ));
532
533 }
534
535
536 // return true if next row exists
537 if( $active_loop && isset($active_loop['value'][ $active_loop['i']+1 ]) ) {
538
539 return true;
540
541 }
542
543
544 // no next row!
545 acf_remove_loop('active');
546
547
548 // return
549 return false;
550
551 }
552
553
554 /*
555 * the_row
556 *
557 * This function will progress the global repeater or flexible content value 1 row
558 *
559 * @type function
560 * @date 2/09/13
561 * @since 4.3.0
562 *
563 * @param N/A
564 * @return (array) the current row data
565 */
566
567 function the_row( $format = false ) {
568
569 // vars
570 $i = acf_get_loop('active', 'i');
571
572
573 // increase
574 $i++;
575
576
577 // update
578 acf_update_loop('active', 'i', $i);
579
580
581 // return
582 return get_row( $format );
583
584 }
585
586 function get_row( $format = false ) {
587
588 // vars
589 $loop = acf_get_loop('active');
590
591
592 // bail early if no loop
593 if( !$loop ) return false;
594
595
596 // get value
597 $value = acf_maybe_get( $loop['value'], $loop['i'] );
598
599
600 // bail early if no current value
601 // possible if get_row_layout() is called before the_row()
602 if( !$value ) return false;
603
604
605 // format
606 if( $format ) {
607
608 // vars
609 $field = $loop['field'];
610
611
612 // single row
613 if( acf_get_field_type_prop($field['type'], 'have_rows') === 'single' ) {
614
615 // format value
616 $value = acf_format_value( $value, $loop['post_id'], $field );
617
618 // multiple rows
619 } else {
620
621 // format entire value
622 // - solves problem where cached value is incomplete
623 // - no performance issues here thanks to cache
624 $value = acf_format_value( $loop['value'], $loop['post_id'], $field );
625 $value = acf_maybe_get( $value, $loop['i'] );
626
627 }
628
629 }
630
631
632 // return
633 return $value;
634
635 }
636
637 function get_row_index() {
638
639 // vars
640 $i = acf_get_loop('active', 'i');
641 $offset = acf_get_setting('row_index_offset');
642
643
644 // return
645 return $offset + $i;
646
647 }
648
649 function the_row_index() {
650
651 echo get_row_index();
652
653 }
654
655
656 /*
657 * get_row_sub_field
658 *
659 * This function is used inside a 'has_sub_field' while loop to return a sub field object
660 *
661 * @type function
662 * @date 16/05/2016
663 * @since 5.3.8
664 *
665 * @param $selector (string)
666 * @return (array)
667 */
668
669 function get_row_sub_field( $selector ) {
670
671 // vars
672 $row = acf_get_loop('active');
673
674
675 // bail early if no row
676 if( !$row ) return false;
677
678
679 // attempt to find sub field
680 $sub_field = acf_get_sub_field($selector, $row['field']);
681
682
683 // bail early if no field
684 if( !$sub_field ) return false;
685
686
687 // update field's name based on row data
688 $sub_field['name'] = "{$row['name']}_{$row['i']}_{$sub_field['name']}";
689
690
691 // return
692 return $sub_field;
693
694 }
695
696
697 /*
698 * get_row_sub_value
699 *
700 * This function is used inside a 'has_sub_field' while loop to return a sub field value
701 *
702 * @type function
703 * @date 16/05/2016
704 * @since 5.3.8
705 *
706 * @param $selector (string)
707 * @return (mixed)
708 */
709
710 function get_row_sub_value( $selector ) {
711
712 // vars
713 $row = acf_get_loop('active');
714
715
716 // bail early if no row
717 if( !$row ) return null;
718
719
720 // return value
721 if( isset($row['value'][ $row['i'] ][ $selector ]) ) {
722
723 return $row['value'][ $row['i'] ][ $selector ];
724
725 }
726
727
728 // return
729 return null;
730
731 }
732
733
734 /*
735 * reset_rows
736 *
737 * This function will find the current loop and unset it from the global array.
738 * To bo used when loop finishes or a break is used
739 *
740 * @type function
741 * @date 26/10/13
742 * @since 5.0.0
743 *
744 * @param $hard_reset (boolean) completely wipe the global variable, or just unset the active row
745 * @return (boolean)
746 */
747
748 function reset_rows() {
749
750 // remove last loop
751 acf_remove_loop('active');
752
753
754 // return
755 return true;
756
757 }
758
759
760 /*
761 * has_sub_field()
762 *
763 * This function is used inside a while loop to return either true or false (loop again or stop).
764 * When using a repeater or flexible content field, it will loop through the rows until
765 * there are none left or a break is detected
766 *
767 * @type function
768 * @since 1.0.3
769 * @date 29/01/13
770 *
771 * @param $field_name (string) the field name
772 * @param $post_id (mixed) the post_id of which the value is saved against
773 * @return (boolean)
774 */
775
776 function has_sub_field( $field_name, $post_id = false ) {
777
778 // vars
779 $r = have_rows( $field_name, $post_id );
780
781
782 // if has rows, progress through 1 row for the while loop to work
783 if( $r ) {
784
785 the_row();
786
787 }
788
789
790 // return
791 return $r;
792
793 }
794
795 function has_sub_fields( $field_name, $post_id = false ) {
796
797 return has_sub_field( $field_name, $post_id );
798
799 }
800
801
802 /*
803 * get_sub_field()
804 *
805 * This function is used inside a 'has_sub_field' while loop to return a sub field value
806 *
807 * @type function
808 * @since 1.0.3
809 * @date 29/01/13
810 *
811 * @param $field_name (string) the field name
812 * @return (mixed)
813 */
814
815 function get_sub_field( $selector = '', $format_value = true ) {
816
817 // get sub field
818 $sub_field = get_sub_field_object( $selector, $format_value );
819
820
821 // bail early if no sub field
822 if( !$sub_field ) return false;
823
824
825 // return
826 return $sub_field['value'];
827
828 }
829
830
831 /*
832 * the_sub_field()
833 *
834 * This function is the same as echo get_sub_field
835 *
836 * @type function
837 * @since 1.0.3
838 * @date 29/01/13
839 *
840 * @param $field_name (string) the field name
841 * @return n/a
842 */
843
844 function the_sub_field( $field_name, $format_value = true ) {
845
846 $value = get_sub_field( $field_name, $format_value );
847
848 if( is_array($value) ) {
849
850 $value = implode(', ',$value);
851
852 }
853
854 echo $value;
855 }
856
857
858 /*
859 * get_sub_field_object()
860 *
861 * This function is used inside a 'has_sub_field' while loop to return a sub field object
862 *
863 * @type function
864 * @since 3.5.8.1
865 * @date 29/01/13
866 *
867 * @param $child_name (string) the field name
868 * @return (array)
869 */
870
871 function get_sub_field_object( $selector, $format_value = true, $load_value = true ) {
872
873 // vars
874 $row = acf_get_loop('active');
875
876
877 // bail early if no row
878 if( !$row ) return false;
879
880
881 // attempt to find sub field
882 $sub_field = get_row_sub_field($selector);
883
884
885 // bail early if no sub field
886 if( !$sub_field ) return false;
887
888
889 // load value
890 if( $load_value ) {
891
892 $sub_field['value'] = get_row_sub_value( $sub_field['key'] );
893
894 }
895
896
897 // format value
898 if( $format_value ) {
899
900 // get value for field
901 $sub_field['value'] = acf_format_value( $sub_field['value'], $row['post_id'], $sub_field );
902
903 }
904
905
906 // return
907 return $sub_field;
908
909 }
910
911
912 /*
913 * get_row_layout()
914 *
915 * This function will return a string representation of the current row layout within a 'have_rows' loop
916 *
917 * @type function
918 * @since 3.0.6
919 * @date 29/01/13
920 *
921 * @param n/a
922 * @return (string)
923 */
924
925 function get_row_layout() {
926
927 // vars
928 $row = get_row();
929
930
931 // return
932 if( isset($row['acf_fc_layout']) ) {
933
934 return $row['acf_fc_layout'];
935
936 }
937
938
939 // return
940 return false;
941
942 }
943
944
945 /*
946 * acf_shortcode()
947 *
948 * This function is used to add basic shortcode support for the ACF plugin
949 * eg. [acf field="heading" post_id="123" format_value="1"]
950 *
951 * @type function
952 * @since 1.1.1
953 * @date 29/01/13
954 *
955 * @param $field (string) the field name or key
956 * @param $post_id (mixed) the post_id of which the value is saved against
957 * @param $format_value (boolean) whether or not to format the field value
958 * @return (string)
959 */
960
961 function acf_shortcode( $atts ) {
962
963 // extract attributs
964 extract( shortcode_atts( array(
965 'field' => '',
966 'post_id' => false,
967 'format_value' => true
968 ), $atts ) );
969
970
971 // get value and return it
972 $value = get_field( $field, $post_id, $format_value );
973
974
975 // array
976 if( is_array($value) ) {
977
978 $value = @implode( ', ', $value );
979
980 }
981
982
983 // return
984 return $value;
985
986 }
987
988 add_shortcode('acf', 'acf_shortcode');
989
990
991 /*
992 * update_field()
993 *
994 * This function will update a value in the database
995 *
996 * @type function
997 * @since 3.1.9
998 * @date 29/01/13
999 *
1000 * @param $selector (string) the field name or key
1001 * @param $value (mixed) the value to save in the database
1002 * @param $post_id (mixed) the post_id of which the value is saved against
1003 * @return (boolean)
1004 */
1005
1006 function update_field( $selector, $value, $post_id = false ) {
1007
1008 // filter post_id
1009 $post_id = acf_get_valid_post_id( $post_id );
1010
1011
1012 // get field
1013 $field = acf_maybe_get_field( $selector, $post_id, false );
1014
1015
1016 // create dummy field
1017 if( !$field ) {
1018
1019 $field = acf_get_valid_field(array(
1020 'name' => $selector,
1021 'key' => '',
1022 'type' => '',
1023 ));
1024
1025 }
1026
1027
1028 // save
1029 return acf_update_value( $value, $post_id, $field );
1030
1031 }
1032
1033
1034 /*
1035 * update_sub_field
1036 *
1037 * This function will update a value of a sub field in the database
1038 *
1039 * @type function
1040 * @date 2/04/2014
1041 * @since 5.0.0
1042 *
1043 * @param $selector (mixed) the sub field name or key, or an array of ancestors
1044 * @param $value (mixed) the value to save in the database
1045 * @param $post_id (mixed) the post_id of which the value is saved against
1046 * @return (boolean)
1047 */
1048
1049 function update_sub_field( $selector, $value, $post_id = false ) {
1050
1051 // vars
1052 $sub_field = false;
1053
1054
1055 // get sub field
1056 if( is_array($selector) ) {
1057
1058 $post_id = acf_get_valid_post_id( $post_id );
1059 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1060
1061 } else {
1062
1063 $post_id = acf_get_loop('active', 'post_id');
1064 $sub_field = get_row_sub_field( $selector );
1065
1066 }
1067
1068
1069 // bail early if no sub field
1070 if( !$sub_field ) return false;
1071
1072
1073 // update
1074 return acf_update_value( $value, $post_id, $sub_field );
1075
1076 }
1077
1078
1079 /*
1080 * delete_field()
1081 *
1082 * This function will remove a value from the database
1083 *
1084 * @type function
1085 * @since 3.1.9
1086 * @date 29/01/13
1087 *
1088 * @param $selector (string) the field name or key
1089 * @param $post_id (mixed) the post_id of which the value is saved against
1090 * @return (boolean)
1091 */
1092
1093 function delete_field( $selector, $post_id = false ) {
1094
1095 // filter post_id
1096 $post_id = acf_get_valid_post_id( $post_id );
1097
1098
1099 // get field
1100 $field = acf_maybe_get_field( $selector, $post_id );
1101
1102
1103 // delete
1104 return acf_delete_value( $post_id, $field );
1105
1106 }
1107
1108
1109 /*
1110 * delete_sub_field
1111 *
1112 * This function will delete a value of a sub field in the database
1113 *
1114 * @type function
1115 * @date 2/04/2014
1116 * @since 5.0.0
1117 *
1118 * @param $selector (mixed) the sub field name or key, or an array of ancestors
1119 * @param $value (mixed) the value to save in the database
1120 * @param $post_id (mixed) the post_id of which the value is saved against
1121 * @return (boolean)
1122 */
1123
1124 function delete_sub_field( $selector, $post_id = false ) {
1125
1126 return update_sub_field( $selector, null, $post_id );
1127
1128 }
1129
1130
1131 /*
1132 * add_row
1133 *
1134 * This function will add a row of data to a field
1135 *
1136 * @type function
1137 * @date 16/10/2015
1138 * @since 5.2.3
1139 *
1140 * @param $selector (string)
1141 * @param $row (array)
1142 * @param $post_id (mixed)
1143 * @return (boolean)
1144 */
1145
1146 function add_row( $selector, $row = false, $post_id = false ) {
1147
1148 // filter post_id
1149 $post_id = acf_get_valid_post_id( $post_id );
1150
1151
1152 // get field
1153 $field = acf_maybe_get_field( $selector, $post_id, false );
1154
1155
1156 // bail early if no field
1157 if( !$field ) return false;
1158
1159
1160 // get raw value
1161 $value = acf_get_value( $post_id, $field );
1162
1163
1164 // ensure array
1165 $value = acf_get_array($value);
1166
1167
1168 // append
1169 $value[] = $row;
1170
1171
1172 // update value
1173 acf_update_value( $value, $post_id, $field );
1174
1175
1176 // return
1177 return count($value);
1178
1179 }
1180
1181
1182 /*
1183 * add_sub_row
1184 *
1185 * This function will add a row of data to a field
1186 *
1187 * @type function
1188 * @date 16/10/2015
1189 * @since 5.2.3
1190 *
1191 * @param $selector (string)
1192 * @param $row (array)
1193 * @param $post_id (mixed)
1194 * @return (boolean)
1195 */
1196
1197 function add_sub_row( $selector, $row = false, $post_id = false ) {
1198
1199 // vars
1200 $sub_field = false;
1201
1202
1203 // get sub field
1204 if( is_array($selector) ) {
1205
1206 $post_id = acf_get_valid_post_id( $post_id );
1207 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1208
1209 } else {
1210
1211 $post_id = acf_get_loop('active', 'post_id');
1212 $sub_field = get_row_sub_field( $selector );
1213
1214 }
1215
1216
1217 // bail early if no sub field
1218 if( !$sub_field ) return false;
1219
1220
1221 // get raw value
1222 $value = acf_get_value( $post_id, $sub_field );
1223
1224
1225 // ensure array
1226 $value = acf_get_array( $value );
1227
1228
1229 // append
1230 $value[] = $row;
1231
1232
1233 // update
1234 acf_update_value( $value, $post_id, $sub_field );
1235
1236
1237 // return
1238 return count($value);
1239
1240 }
1241
1242
1243 /*
1244 * update_row
1245 *
1246 * This function will update a row of data to a field
1247 *
1248 * @type function
1249 * @date 19/10/2015
1250 * @since 5.2.3
1251 *
1252 * @param $selector (string)
1253 * @param $i (int)
1254 * @param $row (array)
1255 * @param $post_id (mixed)
1256 * @return (boolean)
1257 */
1258
1259 function update_row( $selector, $i = 1, $row = false, $post_id = false ) {
1260
1261 // vars
1262 $offset = acf_get_setting('row_index_offset');
1263 $i = $i - $offset;
1264
1265
1266 // filter post_id
1267 $post_id = acf_get_valid_post_id( $post_id );
1268
1269
1270 // get field
1271 $field = acf_maybe_get_field( $selector, $post_id, false );
1272
1273
1274 // bail early if no field
1275 if( !$field ) return false;
1276
1277
1278 // get raw value
1279 $value = acf_get_value( $post_id, $field );
1280
1281
1282 // ensure array
1283 $value = acf_get_array($value);
1284
1285
1286 // update
1287 $value[ $i ] = $row;
1288
1289
1290 // update value
1291 acf_update_value( $value, $post_id, $field );
1292
1293
1294 // return
1295 return true;
1296
1297 }
1298
1299
1300 /*
1301 * update_sub_row
1302 *
1303 * This function will add a row of data to a field
1304 *
1305 * @type function
1306 * @date 16/10/2015
1307 * @since 5.2.3
1308 *
1309 * @param $selector (string)
1310 * @param $row (array)
1311 * @param $post_id (mixed)
1312 * @return (boolean)
1313 */
1314
1315 function update_sub_row( $selector, $i = 1, $row = false, $post_id = false ) {
1316
1317 // vars
1318 $sub_field = false;
1319 $offset = acf_get_setting('row_index_offset');
1320 $i = $i - $offset;
1321
1322
1323 // get sub field
1324 if( is_array($selector) ) {
1325
1326 $post_id = acf_get_valid_post_id( $post_id );
1327 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1328
1329 } else {
1330
1331 $post_id = acf_get_loop('active', 'post_id');
1332 $sub_field = get_row_sub_field( $selector );
1333
1334 }
1335
1336
1337 // bail early if no sub field
1338 if( !$sub_field ) return false;
1339
1340
1341 // get raw value
1342 $value = acf_get_value( $post_id, $sub_field );
1343
1344
1345 // ensure array
1346 $value = acf_get_array( $value );
1347
1348
1349 // append
1350 $value[ $i ] = $row;
1351
1352
1353 // update
1354 acf_update_value( $value, $post_id, $sub_field );
1355
1356
1357 // return
1358 return true;
1359
1360 }
1361
1362
1363 /*
1364 * delete_row
1365 *
1366 * This function will delete a row of data from a field
1367 *
1368 * @type function
1369 * @date 19/10/2015
1370 * @since 5.2.3
1371 *
1372 * @param $selector (string)
1373 * @param $i (int)
1374 * @param $post_id (mixed)
1375 * @return (boolean)
1376 */
1377
1378 function delete_row( $selector, $i = 1, $post_id = false ) {
1379
1380 // vars
1381 $offset = acf_get_setting('row_index_offset');
1382 $i = $i - $offset;
1383
1384
1385 // filter post_id
1386 $post_id = acf_get_valid_post_id( $post_id );
1387
1388
1389 // get field
1390 $field = acf_maybe_get_field( $selector, $post_id );
1391
1392
1393 // bail early if no field
1394 if( !$field ) return false;
1395
1396
1397 // get value
1398 $value = acf_get_value( $post_id, $field );
1399
1400
1401 // ensure array
1402 $value = acf_get_array($value);
1403
1404
1405 // bail early if index doesn't exist
1406 if( !isset($value[ $i ]) ) return false;
1407
1408
1409 // unset
1410 unset( $value[ $i ] );
1411
1412
1413 // update
1414 acf_update_value( $value, $post_id, $field );
1415
1416
1417 // return
1418 return true;
1419
1420 }
1421
1422
1423 /*
1424 * delete_sub_row
1425 *
1426 * This function will add a row of data to a field
1427 *
1428 * @type function
1429 * @date 16/10/2015
1430 * @since 5.2.3
1431 *
1432 * @param $selector (string)
1433 * @param $row (array)
1434 * @param $post_id (mixed)
1435 * @return (boolean)
1436 */
1437
1438 function delete_sub_row( $selector, $i = 1, $post_id = false ) {
1439
1440 // vars
1441 $sub_field = false;
1442 $offset = acf_get_setting('row_index_offset');
1443 $i = $i - $offset;
1444
1445
1446 // get sub field
1447 if( is_array($selector) ) {
1448
1449 $post_id = acf_get_valid_post_id( $post_id );
1450 $sub_field = acf_maybe_get_sub_field( $selector, $post_id, false );
1451
1452 } else {
1453
1454 $post_id = acf_get_loop('active', 'post_id');
1455 $sub_field = get_row_sub_field( $selector );
1456
1457 }
1458
1459
1460 // bail early if no sub field
1461 if( !$sub_field ) return false;
1462
1463
1464 // get raw value
1465 $value = acf_get_value( $post_id, $sub_field );
1466
1467
1468 // ensure array
1469 $value = acf_get_array( $value );
1470
1471
1472 // bail early if index doesn't exist
1473 if( !isset($value[ $i ]) ) return false;
1474
1475
1476 // append
1477 unset( $value[ $i ] );
1478
1479
1480 // update
1481 acf_update_value( $value, $post_id, $sub_field );
1482
1483
1484 // return
1485 return true;
1486
1487 }
1488
1489
1490 /*
1491 * Depreceated Functions
1492 *
1493 * These functions are outdated
1494 *
1495 * @type function
1496 * @date 4/03/2014
1497 * @since 1.0.0
1498 *
1499 * @param n/a
1500 * @return n/a
1501 */
1502
1503 function create_field( $field ) {
1504
1505 acf_render_field( $field );
1506
1507 }
1508
1509 function render_field( $field ) {
1510
1511 acf_render_field( $field );
1512
1513 }
1514
1515 function reset_the_repeater_field() {
1516
1517 return reset_rows();
1518
1519 }
1520
1521 function the_repeater_field( $field_name, $post_id = false ) {
1522
1523 return has_sub_field( $field_name, $post_id );
1524
1525 }
1526
1527 function the_flexible_field( $field_name, $post_id = false ) {
1528
1529 return has_sub_field( $field_name, $post_id );
1530
1531 }
1532
1533 function acf_filter_post_id( $post_id ) {
1534
1535 return acf_get_valid_post_id( $post_id );
1536
1537 }
1538
1539 ?>
1540