PluginProbe
Advanced Custom Fields (ACF®) / 5.6.6
Advanced Custom Fields (ACF®) v5.6.6
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 / local.php
local.php
1,045 lines 15.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
4
5 if( ! class_exists('acf_local') ) :
6
7 class acf_local {
8
9 // vars
10 var $temp_groups = array(),
11 $temp_fields = array(),
12 $groups = array(),
13 $fields = array(),
14 $reference = array(),
15 $parents = array();
16
17
18 /*
19 * __construct
20 *
21 * This function will setup the class functionality
22 *
23 * @type function
24 * @date 5/03/2014
25 * @since 5.4.0
26 *
27 * @param n/a
28 * @return n/a
29 */
30
31 function __construct() {
32
33 // register filter
34 acf_enable_filter('local');
35
36
37 // actions
38 add_action('acf/include_fields', array($this, 'acf_include_fields'), 5, 0);
39
40
41 // filters
42 add_filter('acf/get_field_groups', array($this, 'acf_get_field_groups'), 20, 1);
43
44 }
45
46
47 /*
48 * get_key
49 *
50 * This function will check for references and modify the key
51 *
52 * @type function
53 * @date 30/06/2016
54 * @since 5.4.0
55 *
56 * @param $key (string)
57 * @return $key
58 */
59
60 function get_key( $key = '' ) {
61
62 // check for reference
63 if( isset($this->reference[ $key ]) ) {
64
65 $key = $this->reference[ $key ];
66
67 }
68
69
70 // return
71 return $key;
72
73 }
74
75
76 /*
77 * reset
78 *
79 * This function will remove (reset) all field group and fields
80 *
81 * @type function
82 * @date 2/06/2016
83 * @since 5.3.8
84 *
85 * @param n/a
86 * @return n/a
87 */
88
89 function reset() {
90
91 // vars
92 $this->temp_groups = array();
93 $this->temp_fields = array();
94 $this->groups = array();
95 $this->fields = array();
96 $this->reference = array();
97 $this->parents = array();
98
99 }
100
101
102 /*
103 * is_enabled
104 *
105 * This function will return true if acf_local functionality is enabled
106 *
107 * @type function
108 * @date 14/07/2016
109 * @since 5.4.0
110 *
111 * @param n/a
112 * @return n/a
113 */
114
115 function is_enabled() {
116
117 // bail early if no local allowed
118 if( !acf_get_setting('local') ) return false;
119
120
121 // return
122 return acf_is_filter_enabled('local');
123
124 }
125
126
127 /*
128 * is_ready
129 *
130 * This function will return true when ACF has included all field types and is ready to import
131 * Importing fields too early will cause issues where sub fields have not been extracted correctly
132 *
133 * @type function
134 * @date 13/3/17
135 * @since 5.5.10
136 *
137 * @param n/a
138 * @return (boolean)
139 */
140
141 function is_ready() {
142
143 return did_action('acf/include_fields');
144
145 }
146
147
148 /*
149 * acf_include_fields
150 *
151 * This function include any $temp data
152 *
153 * @type function
154 * @date 8/2/17
155 * @since 5.5.6
156 *
157 * @param n/a
158 * @return n/a
159 */
160
161 function acf_include_fields() {
162
163 // field groups
164 if( !empty($this->temp_groups) ) {
165
166 // loop
167 foreach( $this->temp_groups as $i => $temp ) {
168
169 // add
170 $this->add_field_group($temp);
171
172
173 // unset
174 unset($this->temp_groups[ $i ]);
175
176 }
177
178 }
179
180
181 // fields
182 if( !empty($this->temp_fields) ) {
183
184 // prepare
185 $this->temp_fields = acf_prepare_fields_for_import( $this->temp_fields );
186
187
188 // loop
189 foreach( $this->temp_fields as $i => $temp ) {
190
191 // add
192 $this->add_field($temp);
193
194
195 // unset
196 unset($this->temp_fields[ $i ]);
197
198 }
199
200 }
201
202 }
203
204
205 /*
206 * add_parent_reference
207 *
208 * This function will add a child reference for a given parent
209 *
210 * @type function
211 * @date 14/07/2016
212 * @since 5.4.0
213 *
214 * @param $parent_key (string)
215 * @param $field_key (string)
216 * @return (mixed)
217 */
218
219 function add_parent_reference( $parent_key = '', $field_key = '' ) {
220
221 // create array if doesn't exist
222 if( empty($this->parents[ $parent_key ]) ) {
223
224 $this->parents[ $parent_key ] = array();
225
226 }
227
228
229 // append
230 $this->parents[ $parent_key ][ $field_key ] = $field_key;
231
232 }
233
234
235 /*
236 * remove_parent_reference
237 *
238 * This function will remove a child reference for a given parent
239 *
240 * @type function
241 * @date 14/07/2016
242 * @since 5.4.0
243 *
244 * @param $parent_key (string)
245 * @param $field_key (string)
246 * @return (mixed)
247 */
248
249 function remove_parent_reference( $parent_key = '', $field_key = '' ) {
250
251 // bail early if no parent
252 if( empty($this->parents[ $parent_key ]) ) return false;
253
254
255 // remove
256 unset( $this->parents[ $parent_key ][ $field_key ] );
257
258 }
259
260
261 /*
262 * maybe_add_field
263 *
264 * This function will either import or add to temp
265 *
266 * @type function
267 * @date 9/2/17
268 * @since 5.5.6
269 *
270 * @param $field (array)
271 * @return n/a
272 */
273
274 function maybe_add_field( $field ) {
275
276 // add
277 if( $this->is_ready() ) {
278
279 $this->add_field( $field );
280
281 // add to temp
282 } else {
283
284 $this->temp_fields[] = $field;
285
286 }
287
288 }
289
290
291 /*
292 * add_field
293 *
294 * This function will add a $field
295 *
296 * @type function
297 * @date 10/03/2014
298 * @since 5.0.0
299 *
300 * @param $field (array)
301 * @return n/a
302 */
303
304 function add_field( $field ) {
305
306 // defaults
307 $field = wp_parse_args($field, array(
308 'key' => '',
309 'name' => '',
310 'parent' => 0
311 ));
312
313
314 // add parent reference
315 $this->add_parent_reference( $field['parent'], $field['key'] );
316
317
318 // add in menu order
319 $field['menu_order'] = $this->count_fields( $field['parent'] ) - 1;
320
321
322 // add field
323 $this->fields[ $field['key'] ] = $field;
324
325
326 // add reference for field name
327 $this->reference[ $field['name'] ] = $field['key'];
328
329 }
330
331
332 /*
333 * is_field
334 *
335 * This function will return true if a field exists for a given key
336 *
337 * @type function
338 * @date 10/03/2014
339 * @since 5.0.0
340 *
341 * @param $key (string)
342 * @return (bolean)
343 */
344
345 function is_field( $key = '' ) {
346
347 // vars
348 $key = $this->get_key($key);
349
350
351 // bail early if not enabled
352 if( !$this->is_enabled() ) return false;
353
354
355 // return
356 return isset( $this->fields[ $key ] );
357
358 }
359
360 function is_field_key( $key ) {
361
362 // bail early if not enabled
363 if( !$this->is_enabled() ) return false;
364
365
366 // return
367 return isset( $this->fields[ $key ] );
368
369 }
370
371 function is_field_name( $name ) {
372
373 // bail early if not enabled
374 if( !$this->is_enabled() ) return false;
375
376
377 // return
378 return isset( $this->reference[ $name ] );
379
380 }
381
382
383 /*
384 * get_field
385 *
386 * This function will return a local field for a given key
387 *
388 * @type function
389 * @date 10/03/2014
390 * @since 5.0.0
391 *
392 * @param $key (string)
393 * @return (bolean)
394 */
395
396 function get_field( $key = '' ) {
397
398 // vars
399 $key = $this->get_key($key);
400
401
402 // bail early if no group
403 if( !$this->is_field($key) ) return false;
404
405
406 // return
407 return $this->fields[ $key ];
408
409 }
410
411
412 /*
413 * remove_field
414 *
415 * This function will remove a $field
416 *
417 * @type function
418 * @date 10/03/2014
419 * @since 5.0.0
420 *
421 * @param $key (string)
422 * @return n/a
423 */
424
425 function remove_field( $key = '' ) {
426
427 // get field
428 $field = $this->get_field( $key );
429
430
431 // bail early if no field
432 if( !$field ) return false;
433
434
435 // remove parent reference
436 $this->remove_parent_reference( $field['parent'], $field['key'] );
437
438
439 // remove field
440 unset( $this->fields[ $field['key'] ] );
441
442
443 // remove reference for field name
444 unset( $this->reference[ $field['name'] ] );
445
446
447 // remove children
448 if( $this->have_fields($key) ) {
449
450 $this->remove_fields( $key );
451
452 }
453
454 }
455
456
457 /*
458 * maybe_add_field_group
459 *
460 * This function will either import or add to temp
461 *
462 * @type function
463 * @date 9/2/17
464 * @since 5.5.6
465 *
466 * @param $field_group (array)
467 * @return n/a
468 */
469
470 function maybe_add_field_group( $field_group ) {
471
472 // add
473 if( $this->is_ready() ) {
474
475 $this->add_field_group( $field_group );
476
477 // add to temp
478 } else {
479
480 $this->temp_groups[] = $field_group;
481
482 }
483
484 }
485
486
487 /*
488 * add_field_group
489 *
490 * This function will add a $field group to the local placeholder
491 *
492 * @type function
493 * @date 10/03/2014
494 * @since 5.0.0
495 *
496 * @param $field_group (array)
497 * @return n/a
498 */
499
500 function add_field_group( $field_group ) {
501
502 // vars
503 $fields = acf_extract_var($field_group, 'fields');
504
505
506 // validate
507 $field_group = acf_get_valid_field_group($field_group);
508
509
510 // don't allow overrides
511 if( $this->is_field_group($field_group['key']) ) return;
512
513
514 // add local (may be set to json)
515 if( empty($field_group['local']) ) $field_group['local'] = 'php';
516
517
518 // add field group
519 $this->groups[ $field_group['key'] ] = $field_group;
520
521
522 // bail ealry if no fields
523 if( !$fields ) return;
524
525
526 // format fields
527 $fields = acf_prepare_fields_for_import( $fields );
528
529
530 // add fields
531 foreach( $fields as $field ) {
532
533 // add parent
534 if( empty($field['parent']) ) $field['parent'] = $field_group['key'];
535
536
537 // add field
538 $this->add_field( $field );
539
540 }
541
542 }
543
544
545 /*
546 * is_field_group
547 *
548 * This function will return true if a field group exists for a given key
549 *
550 * @type function
551 * @date 10/03/2014
552 * @since 5.0.0
553 *
554 * @param $key (string)
555 * @return (bolean)
556 */
557
558 function is_field_group( $key = '' ) {
559
560 // bail early if not enabled
561 if( !$this->is_enabled() ) return false;
562
563
564 // return
565 return isset( $this->groups[ $key ] );
566
567 }
568
569
570 /*
571 * get_field_group
572 *
573 * This function will return a local field group for a given key
574 *
575 * @type function
576 * @date 10/03/2014
577 * @since 5.0.0
578 *
579 * @param $key (string)
580 * @return (bolean)
581 */
582
583 function get_field_group( $key = '' ) {
584
585 // bail early if no group
586 if( !$this->is_field_group($key) ) return false;
587
588
589 // return
590 return $this->groups[ $key ];
591
592 }
593
594
595 /*
596 * count_field_groups
597 *
598 * This function will return the number of field groups
599 *
600 * @type function
601 * @date 10/03/2014
602 * @since 5.0.0
603 *
604 * @param $key (string)
605 * @return (bolean)
606 */
607
608
609 function count_field_groups() {
610
611 // return
612 return count( $this->groups );
613
614 }
615
616
617 /*
618 * have_field_groups
619 *
620 * This function will true if local field groups exist
621 *
622 * @type function
623 * @date 10/03/2014
624 * @since 5.0.0
625 *
626 * @param n/a
627 * @return (int)
628 */
629
630 function have_field_groups() {
631
632 // bail early if not enabled
633 if( !$this->is_enabled() ) return 0;
634
635
636 // return
637 return $this->count_field_groups();
638
639 }
640
641
642 /*
643 * get_field_groups
644 *
645 * This function will return an array of field groups
646 *
647 * @type function
648 * @date 10/03/2014
649 * @since 5.0.0
650 *
651 * @param $key (string)
652 * @return (bolean)
653 */
654
655 function get_field_groups() {
656
657 // bail early if no parent
658 if( !$this->have_field_groups() ) return false;
659
660
661 // vars
662 $field_groups = array();
663
664
665 // append
666 foreach( array_keys($this->groups) as $field_group_key ) {
667
668 $field_groups[] = acf_get_field_group( $field_group_key );
669
670 }
671
672
673 // return
674 return $field_groups;
675
676 }
677
678
679
680 /*
681 * count_fields
682 *
683 * This function will return the number of fields for a given parent
684 *
685 * @type function
686 * @date 10/03/2014
687 * @since 5.0.0
688 *
689 * @param $key (string)
690 * @return (bolean)
691 */
692
693
694 function count_fields( $parent_key = '' ) {
695
696 // check
697 if( isset($this->parents[ $parent_key ]) ) {
698
699 return count($this->parents[ $parent_key ]);
700
701 }
702
703
704 // return
705 return 0;
706
707 }
708
709
710 /*
711 * have_fields
712 *
713 * This function will true if local fields exist
714 *
715 * @type function
716 * @date 10/03/2014
717 * @since 5.0.0
718 *
719 * @param n/a
720 * @return (int)
721 */
722
723 function have_fields( $parent_key = '' ) {
724
725 // bail early if not enabled
726 if( !$this->is_enabled() ) return 0;
727
728
729 // return
730 return $this->count_fields( $parent_key );
731
732 }
733
734
735 /*
736 * get_fields
737 *
738 * This function will return an array of fields for a given 'parent' key (field group key or field key)
739 *
740 * @type function
741 * @date 10/03/2014
742 * @since 5.0.0
743 *
744 * @param $key (string)
745 * @return (bolean)
746 */
747
748 function get_fields( $parent_key = '' ) {
749
750 // bail early if no parent
751 if( !$this->have_fields($parent_key) ) return false;
752
753
754 // vars
755 $fields = array();
756
757
758 // append
759 foreach( $this->parents[ $parent_key ] as $field_key ) {
760
761 $fields[] = acf_get_field( $field_key );
762
763 }
764
765
766 // return
767 return $fields;
768
769 }
770
771
772 /*
773 * remove_fields
774 *
775 * This function will remove the field reference for a field group
776 *
777 * @type function
778 * @date 10/03/2014
779 * @since 5.0.0
780 *
781 * @param $key (string)
782 * @return (bolean)
783 */
784
785 function remove_fields( $parent_key = '' ) {
786
787 // bail early if no parent
788 if( !$this->have_fields($parent_key) ) return false;
789
790
791 // loop
792 foreach( $this->parents[ $parent_key ] as $field_key ) {
793
794 $this->remove_field( $field_key );
795
796 }
797
798
799 // return
800 return true;
801 }
802
803
804 /*
805 * acf_get_field_groups
806 *
807 * This function will override and add field groups to the `acf_get_field_groups()` results
808 *
809 * @type filter (acf/get_field_groups)
810 * @date 5/12/2013
811 * @since 5.0.0
812 *
813 * @param $field_groups (array)
814 * @return $field_groups
815 */
816
817 function acf_get_field_groups( $field_groups ) {
818
819 // bail early if no local field groups
820 if( !$this->have_field_groups() ) return $field_groups;
821
822
823 // vars
824 $ignore = array();
825 $added = false;
826
827
828 // populate ignore list
829 if( !empty($field_groups) ) {
830
831 foreach( $field_groups as $k => $group ) {
832
833 $ignore[] = $group['key'];
834
835 }
836
837 }
838
839
840 // append field groups
841 $groups = $this->get_field_groups();
842
843 foreach( $groups as $group ) {
844
845 // is ignore
846 if( in_array($group['key'], $ignore) ) continue;
847
848
849 // append
850 $field_groups[] = $group;
851 $added = true;
852
853 }
854
855
856 // order field groups based on menu_order, title
857 if( $added ) {
858
859 $menu_order = array();
860 $title = array();
861
862 foreach( $field_groups as $key => $row ) {
863
864 $menu_order[ $key ] = $row['menu_order'];
865 $title[ $key ] = $row['title'];
866 }
867
868
869 // sort the array with menu_order ascending
870 array_multisort( $menu_order, SORT_ASC, $title, SORT_ASC, $field_groups );
871
872 }
873
874
875 // return
876 return $field_groups;
877
878 }
879
880 }
881
882
883 // initialize
884 acf()->local = new acf_local();
885
886 endif; // class_exists check
887
888
889 /*
890 * Helpers
891 *
892 * alias of acf()->local->functions
893 *
894 * @type function
895 * @date 11/06/2014
896 * @since 5.0.0
897 *
898 * @param n/a
899 * @return n/a
900 */
901
902 function acf_local() {
903
904 return acf()->local;
905
906 }
907
908 function acf_disable_local() {
909
910 acf_disable_filter('local');
911
912 }
913
914 function acf_enable_local() {
915
916 acf_enable_filter('local');
917
918 }
919
920 function acf_reset_local() {
921
922 return acf_local()->reset();
923
924 }
925
926
927 // field group
928 function acf_add_local_field_group( $field_group ) {
929
930 return acf_local()->maybe_add_field_group( $field_group );
931
932 }
933
934 function acf_remove_local_field_group( $key = '' ) {
935
936 return false;
937
938 }
939
940 function acf_is_local_field_group( $key = '' ) {
941
942 return acf_local()->is_field_group( $key );
943
944 }
945
946 function acf_get_local_field_group( $key = '' ) {
947
948 return acf_local()->get_field_group( $key );
949
950 }
951
952
953 // field groups
954 function acf_count_local_field_groups() {
955
956 return acf_local()->count_field_groups();
957
958 }
959
960 function acf_have_local_field_groups() {
961
962 return acf_local()->have_field_groups();
963
964 }
965
966 function acf_get_local_field_groups() {
967
968 return acf_local()->get_field_groups();
969
970 }
971
972
973 // field
974 function acf_add_local_field( $field ) {
975
976 return acf_local()->maybe_add_field( $field );
977
978 }
979
980 function acf_remove_local_field( $key = '' ) {
981
982 return acf_local()->remove_field( $key );
983
984 }
985
986 function acf_is_local_field( $key = '' ) {
987
988 return acf_local()->is_field( $key );
989
990 }
991
992 function acf_is_local_field_key( $key = '' ) {
993
994 return acf_local()->is_field_key( $key );
995
996 }
997
998 function acf_is_local_field_name( $name = '' ) {
999
1000 return acf_local()->is_field_name( $name );
1001
1002 }
1003
1004 function acf_get_local_field( $key = '' ) {
1005
1006 return acf_local()->get_field( $key );
1007
1008 }
1009
1010
1011 // fields
1012 function acf_count_local_fields( $key = '' ) {
1013
1014 return acf_local()->count_fields( $key );
1015
1016 }
1017
1018 function acf_have_local_fields( $key = '' ) {
1019
1020 return acf_local()->have_fields( $key );
1021
1022 }
1023
1024 function acf_get_local_fields( $key = '' ) {
1025
1026 return acf_local()->get_fields( $key );
1027
1028 }
1029
1030 function acf_remove_local_fields( $key = '' ) {
1031
1032 return acf_local()->remove_fields( $key );
1033
1034 }
1035
1036
1037 // deprecated
1038 function register_field_group( $field_group ) {
1039
1040 acf_add_local_field_group( $field_group );
1041
1042 }
1043
1044
1045 ?>