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