PluginProbe
Advanced Custom Fields (ACF®) / 4.1.1
Advanced Custom Fields (ACF®) v4.1.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 / core / controllers / location.php

location.php in Advanced Custom Fields (ACF®) 4.1.1, at core/controllers/location.php

959 lines 17.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * Locations
5 *
6 * @description: controller for location match functionality
7 * @since: 3.6
8 * @created: 25/01/13
9 */
10
11 class acf_location
12 {
13
14 /*
15 * __construct
16 *
17 * @description:
18 * @since 3.1.8
19 * @created: 23/06/12
20 */
21
22 function __construct()
23 {
24 // ajax
25 add_action('wp_ajax_acf/location/match_field_groups_ajax', array($this, 'match_field_groups_ajax'));
26
27
28 // filters
29 add_filter('acf/location/match_field_groups', array($this, 'match_field_groups'), 10, 2);
30
31
32 // Basic
33 add_filter('acf/location/rule_match/post_type', array($this, 'rule_match_post_type'), 10, 3);
34 add_filter('acf/location/rule_match/user_type', array($this, 'rule_match_user_type'), 10, 3);
35
36 // Page
37 add_filter('acf/location/rule_match/page', array($this, 'rule_match_post'), 10, 3);
38 add_filter('acf/location/rule_match/page_type', array($this, 'rule_match_page_type'), 10, 3);
39 add_filter('acf/location/rule_match/page_parent', array($this, 'rule_match_page_parent'), 10, 3);
40 add_filter('acf/location/rule_match/page_template', array($this, 'rule_match_page_template'), 10, 3);
41
42 // Post
43 add_filter('acf/location/rule_match/post', array($this, 'rule_match_post'), 10, 3);
44 add_filter('acf/location/rule_match/post_category', array($this, 'rule_match_post_category'), 10, 3);
45 add_filter('acf/location/rule_match/post_format', array($this, 'rule_match_post_format'), 10, 3);
46 add_filter('acf/location/rule_match/taxonomy', array($this, 'rule_match_taxonomy'), 10, 3);
47
48 // Other
49 add_filter('acf/location/rule_match/ef_taxonomy', array($this, 'rule_match_ef_taxonomy'), 10, 3);
50 add_filter('acf/location/rule_match/ef_user', array($this, 'rule_match_ef_user'), 10, 3);
51 add_filter('acf/location/rule_match/ef_media', array($this, 'rule_match_ef_media'), 10, 3);
52
53 // Options Page
54 add_filter('acf/location/rule_match/options_page', array($this, 'rule_match_options_page'), 10, 3);
55 }
56
57
58 /*
59 * match_field_groups_ajax
60 *
61 * @description:
62 * @since: 3.5.7
63 * @created: 3/01/13
64 */
65
66 function match_field_groups_ajax()
67 {
68
69 // vars
70 $options = array(
71 'nonce' => '',
72 'ajax' => true
73 );
74
75
76 // load post options
77 $options = array_merge($options, $_POST);
78
79
80 // verify nonce
81 if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') )
82 {
83 die(0);
84 }
85
86
87 // return array
88 $return = apply_filters( 'acf/location/match_field_groups', array(), $options );
89
90
91 // echo json
92 echo json_encode( $return );
93
94
95 die();
96 }
97
98
99 /*
100 * match_field_groups
101 *
102 * @description:
103 * @since: 3.5.7
104 * @created: 3/01/13
105 */
106
107 function match_field_groups( $return, $options )
108 {
109
110 // vars
111 $defaults = array(
112 'post_id' => 0,
113 'post_type' => 0,
114 'page_template' => 0,
115 'page_parent' => 0,
116 'page_type' => 0,
117 'post_category' => array(),
118 'post_format' => 0,
119 'taxonomy' => array(),
120 'ef_taxonomy' => 0,
121 'ef_user' => 0,
122 'ef_media' => 0,
123 'lang' => 0,
124 'ajax' => false
125 );
126
127
128 // merge in $options
129 $options = array_merge($defaults, $options);
130
131
132 // Parse values
133 $options = apply_filters( 'acf/parse_types', $options );
134
135
136 // WPML
137 if( defined('ICL_LANGUAGE_CODE') )
138 {
139 $options['lang'] = ICL_LANGUAGE_CODE;
140
141 //global $sitepress;
142 //$sitepress->switch_lang( $options['lang'] );
143 }
144
145
146 // find all acf objects
147 $acfs = apply_filters('acf/get_field_groups', array());
148
149
150 // blank array to hold acfs
151 $return = array();
152
153
154 if( $acfs )
155 {
156 foreach( $acfs as $acf )
157 {
158 // load location
159 $acf['location'] = apply_filters('acf/field_group/get_location', array(), $acf['id']);
160
161
162 // vars
163 $add_box = false;
164
165
166 foreach( $acf['location'] as $group_id => $group )
167 {
168 // start of as true, this way, any rule that doesn't match will cause this varaible to false
169 $match_group = true;
170
171 if( is_array($group) )
172 {
173 foreach( $group as $rule_id => $rule )
174 {
175 // $match = true / false
176 $match = apply_filters( 'acf/location/rule_match/' . $rule['param'] , false, $rule, $options );
177
178 if( !$match )
179 {
180 $match_group = false;
181 }
182
183 }
184 }
185
186
187 // all rules must havematched!
188 if( $match_group )
189 {
190 $add_box = true;
191 }
192
193 }
194
195
196 // add ID to array
197 if( $add_box )
198 {
199 $return[] = $acf['id'];
200
201 }
202
203 }
204 }
205
206
207 return $return;
208 }
209
210
211 /*
212 * rule_match_post_type
213 *
214 * @description:
215 * @since: 3.5.7
216 * @created: 3/01/13
217 */
218
219 function rule_match_post_type( $match, $rule, $options )
220 {
221 $post_type = $options['post_type'];
222
223 if( !$post_type )
224 {
225 if( !$options['post_id'] )
226 {
227 return false;
228 }
229
230 $post_type = get_post_type( $options['post_id'] );
231 }
232
233
234 if( $rule['operator'] == "==" )
235 {
236 $match = ( $post_type === $rule['value'] );
237 }
238 elseif( $rule['operator'] == "!=" )
239 {
240 $match = ( $post_type !== $rule['value'] );
241 }
242
243
244 return $match;
245 }
246
247
248 /*
249 * rule_match_post
250 *
251 * @description:
252 * @since: 3.5.7
253 * @created: 3/01/13
254 */
255
256 function rule_match_post( $match, $rule, $options )
257 {
258 // validation
259 if( !$options['post_id'] )
260 {
261 return false;
262 }
263
264
265 // translate $rule['value']
266 // - this variable will hold the origional post_id, but $options['post_id'] will hold the translated version
267 //if( function_exists('icl_object_id') )
268 //{
269 // $rule['value'] = icl_object_id( $rule['value'], $options['post_type'], true );
270 //}
271
272
273 if($rule['operator'] == "==")
274 {
275 $match = ( $options['post_id'] == $rule['value'] );
276 }
277 elseif($rule['operator'] == "!=")
278 {
279 $match = ( $options['post_id'] != $rule['value'] );
280 }
281
282 return $match;
283
284 }
285
286
287 /*
288 * rule_match_page_type
289 *
290 * @description:
291 * @since: 3.5.7
292 * @created: 3/01/13
293 */
294
295 function rule_match_page_type( $match, $rule, $options )
296 {
297 // validation
298 if( !$options['post_id'] )
299 {
300 return false;
301 }
302
303 $post = get_post( $options['post_id'] );
304
305 if( $rule['value'] == 'front_page')
306 {
307
308 $front_page = (int) get_option('page_on_front');
309
310
311 if($rule['operator'] == "==")
312 {
313 $match = ( $front_page == $post->ID );
314 }
315 elseif($rule['operator'] == "!=")
316 {
317 $match = ( $front_page != $post->ID );
318 }
319
320 }
321 elseif( $rule['value'] == 'posts_page')
322 {
323
324 $posts_page = (int) get_option('page_for_posts');
325
326
327 if($rule['operator'] == "==")
328 {
329 $match = ( $posts_page == $post->ID );
330 }
331 elseif($rule['operator'] == "!=")
332 {
333 $match = ( $posts_page != $post->ID );
334 }
335
336 }
337 elseif( $rule['value'] == 'top_level')
338 {
339 $post_parent = $post->post_parent;
340 if( $options['page_parent'] )
341 {
342 $post_parent = $options['page_parent'];
343 }
344
345
346 if($rule['operator'] == "==")
347 {
348 $match = ( $post_parent == 0 );
349 }
350 elseif($rule['operator'] == "!=")
351 {
352 $match = ( $post_parent != 0 );
353 }
354
355 }
356 elseif( $rule['value'] == 'parent')
357 {
358
359 $children = get_pages(array(
360 'post_type' => $post->post_type,
361 'child_of' => $post->ID,
362 ));
363
364
365 if($rule['operator'] == "==")
366 {
367 $match = ( count($children) > 0 );
368 }
369 elseif($rule['operator'] == "!=")
370 {
371 $match = ( count($children) == 0 );
372 }
373
374 }
375 elseif( $rule['value'] == 'child')
376 {
377
378 $post_parent = $post->post_parent;
379 if( $options['page_parent'] )
380 {
381 $post_parent = $options['page_parent'];
382 }
383
384
385 if($rule['operator'] == "==")
386 {
387 $match = ( $post_parent != 0 );
388 }
389 elseif($rule['operator'] == "!=")
390 {
391 $match = ( $post_parent == 0 );
392 }
393
394 }
395
396 return $match;
397
398 }
399
400
401 /*
402 * rule_match_page_parent
403 *
404 * @description:
405 * @since: 3.5.7
406 * @created: 3/01/13
407 */
408
409 function rule_match_page_parent( $match, $rule, $options )
410 {
411 // validation
412 if( !$options['post_id'] )
413 {
414 return false;
415 }
416
417
418 // vars
419 $post = get_post( $options['post_id'] );
420
421 $post_parent = $post->post_parent;
422 if( $options['page_parent'] )
423 {
424 $post_parent = $options['page_parent'];
425 }
426
427
428 if($rule['operator'] == "==")
429 {
430 $match = ( $post_parent == $rule['value'] );
431 }
432 elseif($rule['operator'] == "!=")
433 {
434 $match = ( $post_parent != $rule['value'] );
435 }
436
437
438 return $match;
439
440 }
441
442
443 /*
444 * rule_match_page_template
445 *
446 * @description:
447 * @since: 3.5.7
448 * @created: 3/01/13
449 */
450
451 function rule_match_page_template( $match, $rule, $options )
452 {
453 $page_template = $options['page_template'];
454 if( ! $page_template )
455 {
456 $page_template = get_post_meta( $options['post_id'], '_wp_page_template', true );
457 }
458
459
460 if( ! $page_template )
461 {
462 $post_type = $options['post_type'];
463
464 if( !$post_type )
465 {
466 $post_type = get_post_type( $options['post_id'] );
467 }
468
469 if( $post_type == 'page' )
470 {
471 $page_template = "default";
472 }
473 }
474
475
476
477 if($rule['operator'] == "==")
478 {
479 $match = ( $page_template === $rule['value'] );
480 }
481 elseif($rule['operator'] == "!=")
482 {
483 $match = ( $page_template !== $rule['value'] );
484 }
485
486 return $match;
487
488 }
489
490
491 /*
492 * rule_match_post_category
493 *
494 * @description:
495 * @since: 3.5.7
496 * @created: 3/01/13
497 */
498
499 function rule_match_post_category( $match, $rule, $options )
500 {
501 // validate
502 if( !$options['post_id'] )
503 {
504 return false;
505 }
506
507
508 // post type
509 if( !$options['post_type'] )
510 {
511 $options['post_type'] = get_post_type( $options['post_id'] );
512 }
513
514
515 // vars
516 $taxonomies = get_object_taxonomies( $options['post_type'] );
517 $terms = $options['post_category'];
518
519
520 // not AJAX
521 if( !$options['ajax'] )
522 {
523 // no terms? Load them from the post_id
524 if( empty($terms) )
525 {
526 $all_terms = get_the_terms( $options['post_id'], 'category' );
527 if($all_terms)
528 {
529 foreach($all_terms as $all_term)
530 {
531 $terms[] = $all_term->term_id;
532 }
533 }
534 }
535
536
537 // no terms at all?
538 if( empty($terms) )
539 {
540 // If no ters, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked
541 if( is_array($taxonomies) && in_array('category', $taxonomies) )
542 {
543 $terms[] = '1';
544 }
545 }
546 }
547
548
549
550 if($rule['operator'] == "==")
551 {
552 $match = false;
553
554 if($terms)
555 {
556 if( in_array($rule['value'], $terms) )
557 {
558 $match = true;
559 }
560 }
561
562 }
563 elseif($rule['operator'] == "!=")
564 {
565 $match = true;
566
567 if($terms)
568 {
569 if( in_array($rule['value'], $terms) )
570 {
571 $match = false;
572 }
573 }
574
575 }
576
577
578 return $match;
579
580 }
581
582
583 /*
584 * rule_match_user_type
585 *
586 * @description:
587 * @since: 3.5.7
588 * @created: 3/01/13
589 */
590
591 function rule_match_user_type( $match, $rule, $options )
592 {
593 if($rule['operator'] == "==")
594 {
595 $match = ( current_user_can($rule['value']) );
596 }
597 elseif($rule['operator'] == "!=")
598 {
599 $match = ( ! current_user_can($rule['value']) );
600 }
601
602 return $match;
603
604 }
605
606
607 /*
608 * rule_match_user_type
609 *
610 * @description:
611 * @since: 3.5.7
612 * @created: 3/01/13
613 */
614
615 function rule_match_options_page( $match, $rule, $options )
616 {
617 global $plugin_page;
618
619
620 // older location rules may be "options-pagename"
621 if( substr($rule['value'], 0, 8) == 'options-' )
622 {
623 $rule['value'] = 'acf-' . $rule['value'];
624 }
625
626
627 // older location ruels may be "Pagename"
628 if( substr($rule['value'], 0, 11) != 'acf-options' )
629 {
630 $rule['value'] = 'acf-options-' . sanitize_title( $rule['value'] );
631
632 // value may now be wrong (acf-options-options)
633 if( $rule['value'] == 'acf-options-options' )
634 {
635 $rule['value'] = 'acf-options';
636 }
637 }
638
639
640 if($rule['operator'] == "==")
641 {
642 $match = ( $plugin_page === $rule['value'] );
643 }
644 elseif($rule['operator'] == "!=")
645 {
646 $match = ( $plugin_page !== $rule['value'] );
647 }
648
649
650 return $match;
651
652 }
653
654
655 /*
656 * rule_match_post_format
657 *
658 * @description:
659 * @since: 3.5.7
660 * @created: 3/01/13
661 */
662
663 function rule_match_post_format( $match, $rule, $options )
664 {
665 // vars
666 $post_format = $options['post_format'];
667 if( !$post_format )
668 {
669 // validate
670 if( !$options['post_id'] )
671 {
672 return false;
673 }
674
675
676 // post type
677 if( !$options['post_type'] )
678 {
679 $options['post_type'] = get_post_type( $options['post_id'] );
680 }
681
682
683 // does post_type support 'post-format'
684 if( post_type_supports( $options['post_type'], 'post-formats' ) )
685 {
686 $post_format = get_post_format( $options['post_id'] );
687
688 if( $post_format === false )
689 {
690 $post_format = 'standard';
691 }
692 }
693 }
694
695
696 if($rule['operator'] == "==")
697 {
698 $match = ( $post_format === $rule['value'] );
699
700 }
701 elseif($rule['operator'] == "!=")
702 {
703 $match = ( $post_format !== $rule['value'] );
704 }
705
706
707
708 return $match;
709
710 }
711
712
713 /*
714 * rule_match_taxonomy
715 *
716 * @description:
717 * @since: 3.5.7
718 * @created: 3/01/13
719 */
720
721 function rule_match_taxonomy( $match, $rule, $options )
722 {
723 // validate
724 if( !$options['post_id'] )
725 {
726 return false;
727 }
728
729
730 // post type
731 if( !$options['post_type'] )
732 {
733 $options['post_type'] = get_post_type( $options['post_id'] );
734 }
735
736
737 // vars
738 $taxonomies = get_object_taxonomies( $options['post_type'] );
739 $terms = $options['taxonomy'];
740
741
742 // not AJAX
743 if( !$options['ajax'] )
744 {
745 // no terms? Load them from the post_id
746 if( empty($terms) )
747 {
748 if( is_array($taxonomies) )
749 {
750 foreach( $taxonomies as $tax )
751 {
752 $all_terms = get_the_terms( $options['post_id'], $tax );
753 if($all_terms)
754 {
755 foreach($all_terms as $all_term)
756 {
757 $terms[] = $all_term->term_id;
758 }
759 }
760 }
761 }
762 }
763
764
765 // no terms at all?
766 if( empty($terms) )
767 {
768 // If no ters, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked
769 if( is_array($taxonomies) && in_array('category', $taxonomies) )
770 {
771 $terms[] = '1';
772 }
773 }
774 }
775
776
777 if($rule['operator'] == "==")
778 {
779 $match = false;
780
781 if($terms)
782 {
783 if( in_array($rule['value'], $terms) )
784 {
785 $match = true;
786 }
787 }
788
789 }
790 elseif($rule['operator'] == "!=")
791 {
792 $match = true;
793
794 if($terms)
795 {
796 if( in_array($rule['value'], $terms) )
797 {
798 $match = false;
799 }
800 }
801
802 }
803
804
805 return $match;
806
807 }
808
809
810 /*
811 * rule_match_ef_taxonomy
812 *
813 * @description:
814 * @since: 3.5.7
815 * @created: 3/01/13
816 */
817
818 function rule_match_ef_taxonomy( $match, $rule, $options )
819 {
820
821 $ef_taxonomy = $options['ef_taxonomy'];
822
823
824 if( $ef_taxonomy )
825 {
826 if($rule['operator'] == "==")
827 {
828 $match = ( $ef_taxonomy == $rule['value'] );
829
830 // override for "all"
831 if( $rule['value'] == "all" )
832 {
833 $match = true;
834 }
835
836 }
837 elseif($rule['operator'] == "!=")
838 {
839 $match = ( $ef_taxonomy != $rule['value'] );
840
841 // override for "all"
842 if( $rule['value'] == "all" )
843 {
844 $match = false;
845 }
846
847 }
848
849
850
851
852 }
853
854
855 return $match;
856
857 }
858
859
860 /*
861 * rule_match_ef_user
862 *
863 * @description:
864 * @since: 3.5.7
865 * @created: 3/01/13
866 */
867
868 function rule_match_ef_user( $match, $rule, $options )
869 {
870
871 $ef_user = $options['ef_user'];
872
873
874 if( $ef_user )
875 {
876 if($rule['operator'] == "==")
877 {
878 $match = ( user_can($ef_user, $rule['value']) );
879
880 // override for "all"
881 if( $rule['value'] === "all" )
882 {
883 $match = true;
884 }
885 }
886 elseif($rule['operator'] == "!=")
887 {
888 $match = ( !user_can($ef_user, $rule['value']) );
889
890 // override for "all"
891 if( $rule['value'] === "all" )
892 {
893 $match = false;
894 }
895 }
896
897 }
898
899
900 return $match;
901
902 }
903
904
905 /*
906 * rule_match_ef_media
907 *
908 * @description:
909 * @since: 3.5.7
910 * @created: 3/01/13
911 */
912
913 function rule_match_ef_media( $match, $rule, $options )
914 {
915 global $pagenow;
916
917
918 if( $pagenow == 'post.php' )
919 {
920 // in 3.5, the media rule should check the post type
921 $rule['param'] = 'post_type';
922 $rule['value'] = 'attachment';
923 return $this->rule_match_post_type( $match, $rule, $options );
924 }
925
926
927 $ef_media = $options['ef_media'];
928
929 if( $ef_media )
930 {
931 if($rule['operator'] == "==")
932 {
933 // override for "all"
934 if( $rule['value'] === "all" )
935 {
936 $match = true;
937 }
938 }
939 elseif($rule['operator'] == "!=")
940 {
941 // override for "all"
942 if( $rule['value'] === "all" )
943 {
944 $match = false;
945 }
946 }
947
948 }
949
950 return $match;
951
952 }
953
954
955 }
956
957 new acf_location();
958
959 ?>