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

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