acf_location

Locations.

Defined (1)

The class is defined in the following location(s).

/core/controllers/location.php  
  1. class acf_location 
  2.  
  3. /** 
  4. * __construct 
  5. * @description:  
  6. * @since 3.1.8 
  7. * @created: 23/06/12 
  8. */ 
  9.  
  10. function __construct() 
  11. // ajax 
  12. add_action('wp_ajax_acf/location/match_field_groups_ajax', array($this, 'match_field_groups_ajax')); 
  13. add_action('wp_ajax_nopriv_acf/location/match_field_groups_ajax', array($this, 'match_field_groups_ajax')); 
  14.  
  15.  
  16. // filters 
  17. add_filter('acf/location/match_field_groups', array($this, 'match_field_groups'), 10, 2); 
  18.  
  19.  
  20. // Basic 
  21. add_filter('acf/location/rule_match/post_type', array($this, 'rule_match_post_type'), 10, 3); 
  22. add_filter('acf/location/rule_match/user_type', array($this, 'rule_match_user_type'), 10, 3); 
  23.  
  24. // Page 
  25. add_filter('acf/location/rule_match/page', array($this, 'rule_match_post'), 10, 3); 
  26. add_filter('acf/location/rule_match/page_type', array($this, 'rule_match_page_type'), 10, 3); 
  27. add_filter('acf/location/rule_match/page_parent', array($this, 'rule_match_page_parent'), 10, 3); 
  28. add_filter('acf/location/rule_match/page_template', array($this, 'rule_match_page_template'), 10, 3); 
  29.  
  30. // Post 
  31. add_filter('acf/location/rule_match/post', array($this, 'rule_match_post'), 10, 3); 
  32. add_filter('acf/location/rule_match/post_category', array($this, 'rule_match_post_category'), 10, 3); 
  33. add_filter('acf/location/rule_match/post_format', array($this, 'rule_match_post_format'), 10, 3); 
  34. add_filter('acf/location/rule_match/post_status', array($this, 'rule_match_post_status'), 10, 3); 
  35. add_filter('acf/location/rule_match/taxonomy', array($this, 'rule_match_taxonomy'), 10, 3); 
  36.  
  37. // Other 
  38. add_filter('acf/location/rule_match/ef_taxonomy', array($this, 'rule_match_ef_taxonomy'), 10, 3); 
  39. add_filter('acf/location/rule_match/ef_user', array($this, 'rule_match_ef_user'), 10, 3); 
  40. add_filter('acf/location/rule_match/ef_media', array($this, 'rule_match_ef_media'), 10, 3); 
  41.  
  42. // Options Page 
  43. add_filter('acf/location/rule_match/options_page', array($this, 'rule_match_options_page'), 10, 3); 
  44.  
  45.  
  46. /** 
  47. * match_field_groups_ajax 
  48. * @description:  
  49. * @since: 3.5.7 
  50. * @created: 3/01/13 
  51. */ 
  52.  
  53. function match_field_groups_ajax() 
  54.  
  55. // vars 
  56. $options = array( 
  57. 'nonce' => '',  
  58. 'ajax' => true 
  59. ); 
  60.  
  61.  
  62. // load post options 
  63. $options = array_merge($options, $_POST); 
  64.  
  65.  
  66. // verify nonce 
  67. if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) 
  68. die(0); 
  69.  
  70.  
  71. // return array 
  72. $return = apply_filters( 'acf/location/match_field_groups', array(), $options ); 
  73.  
  74.  
  75. // echo json 
  76. echo json_encode( $return ); 
  77.  
  78.  
  79. die();  
  80.  
  81.  
  82. /** 
  83. * match_field_groups 
  84. * @description:  
  85. * @since: 3.5.7 
  86. * @created: 3/01/13 
  87. */ 
  88.  
  89. function match_field_groups( $return, $options ) 
  90.  
  91. // vars 
  92. $defaults = array( 
  93. 'post_id' => 0,  
  94. 'post_type' => 0,  
  95. 'page_template' => 0,  
  96. 'page_parent' => 0,  
  97. 'page_type' => 0,  
  98. 'post_category' => array(),  
  99. 'post_format' => 0,  
  100. 'taxonomy' => array(),  
  101. 'ef_taxonomy' => 0,  
  102. 'ef_user' => 0,  
  103. 'ef_media' => 0,  
  104. 'lang' => 0,  
  105. 'ajax' => false 
  106. ); 
  107.  
  108.  
  109. // merge in $options 
  110. $options = array_merge($defaults, $options); 
  111.  
  112.  
  113. // Parse values 
  114. $options = apply_filters( 'acf/parse_types', $options ); 
  115.  
  116.  
  117. // WPML 
  118. if( defined('ICL_LANGUAGE_CODE') ) 
  119. $options['lang'] = ICL_LANGUAGE_CODE; 
  120.  
  121. //global $sitepress; 
  122. //$sitepress->switch_lang( $options['lang'] ); 
  123.  
  124.  
  125. // find all acf objects 
  126. $acfs = apply_filters('acf/get_field_groups', array()); 
  127.  
  128.  
  129. // blank array to hold acfs 
  130. $return = array(); 
  131.  
  132.  
  133. if( $acfs ) 
  134. foreach( $acfs as $acf ) 
  135. // load location 
  136. $acf['location'] = apply_filters('acf/field_group/get_location', array(), $acf['id']); 
  137.  
  138.  
  139. // vars 
  140. $add_box = false; 
  141.  
  142.  
  143. foreach( $acf['location'] as $group_id => $group ) 
  144. // start of as true, this way, any rule that doesn't match will cause this varaible to false 
  145. $match_group = true; 
  146.  
  147. if( is_array($group) ) 
  148. foreach( $group as $rule_id => $rule ) 
  149. // Hack for ef_media => now post_type = attachment 
  150. if( $rule['param'] == 'ef_media' ) 
  151. $rule['param'] = 'post_type'; 
  152. $rule['value'] = 'attachment'; 
  153.  
  154.  
  155. // $match = true / false 
  156. $match = apply_filters( 'acf/location/rule_match/' . $rule['param'] , false, $rule, $options ); 
  157.  
  158. if( !$match ) 
  159. $match_group = false; 
  160.  
  161.  
  162.  
  163. // all rules must havematched! 
  164. if( $match_group ) 
  165. $add_box = true; 
  166.  
  167.  
  168.  
  169. // add ID to array  
  170. if( $add_box ) 
  171. $return[] = $acf['id']; 
  172.  
  173.  
  174.  
  175.  
  176. return $return; 
  177.  
  178.  
  179. /** 
  180. * rule_match_post_type 
  181. * @description:  
  182. * @since: 3.5.7 
  183. * @created: 3/01/13 
  184. */ 
  185.  
  186. function rule_match_post_type( $match, $rule, $options ) 
  187. $post_type = $options['post_type']; 
  188.  
  189. if( !$post_type ) 
  190. if( !$options['post_id'] ) 
  191. return false; 
  192.  
  193. $post_type = get_post_type( $options['post_id'] ); 
  194.  
  195.  
  196. if( $rule['operator'] == "==" ) 
  197. $match = ( $post_type === $rule['value'] ); 
  198. elseif( $rule['operator'] == "!=" ) 
  199. $match = ( $post_type !== $rule['value'] ); 
  200.  
  201.  
  202. return $match; 
  203.  
  204.  
  205. /** 
  206. * rule_match_post 
  207. * @description:  
  208. * @since: 3.5.7 
  209. * @created: 3/01/13 
  210. */ 
  211.  
  212. function rule_match_post( $match, $rule, $options ) 
  213. // validation 
  214. if( !$options['post_id'] ) 
  215. return false; 
  216.  
  217.  
  218. // translate $rule['value'] 
  219. // - this variable will hold the original post_id, but $options['post_id'] will hold the translated version 
  220. //if( function_exists('icl_object_id') ) 
  221. //{ 
  222. // $rule['value'] = icl_object_id( $rule['value'], $options['post_type'], true ); 
  223. //} 
  224.  
  225.  
  226. if($rule['operator'] == "==") 
  227. $match = ( $options['post_id'] == $rule['value'] ); 
  228. elseif($rule['operator'] == "!=") 
  229. $match = ( $options['post_id'] != $rule['value'] ); 
  230.  
  231. return $match; 
  232.  
  233.  
  234.  
  235. /** 
  236. * rule_match_page_type 
  237. * @description:  
  238. * @since: 3.5.7 
  239. * @created: 3/01/13 
  240. */ 
  241.  
  242. function rule_match_page_type( $match, $rule, $options ) 
  243. // validation 
  244. if( !$options['post_id'] ) 
  245. return false; 
  246.  
  247. $post = get_post( $options['post_id'] ); 
  248.  
  249. if( $rule['value'] == 'front_page') 
  250.  
  251. $front_page = (int) get_option('page_on_front'); 
  252.  
  253.  
  254. if($rule['operator'] == "==") 
  255. $match = ( $front_page == $post->ID ); 
  256. elseif($rule['operator'] == "!=") 
  257. $match = ( $front_page != $post->ID ); 
  258.  
  259. elseif( $rule['value'] == 'posts_page') 
  260.  
  261. $posts_page = (int) get_option('page_for_posts'); 
  262.  
  263.  
  264. if($rule['operator'] == "==") 
  265. $match = ( $posts_page == $post->ID ); 
  266. elseif($rule['operator'] == "!=") 
  267. $match = ( $posts_page != $post->ID ); 
  268.  
  269. elseif( $rule['value'] == 'top_level') 
  270. $post_parent = $post->post_parent; 
  271. if( $options['page_parent'] ) 
  272. $post_parent = $options['page_parent']; 
  273.  
  274.  
  275. if($rule['operator'] == "==") 
  276. $match = ( $post_parent == 0 ); 
  277. elseif($rule['operator'] == "!=") 
  278. $match = ( $post_parent != 0 ); 
  279.  
  280. elseif( $rule['value'] == 'parent') 
  281.  
  282. $children = get_pages(array( 
  283. 'post_type' => $post->post_type,  
  284. 'child_of' => $post->ID,  
  285. )); 
  286.  
  287.  
  288. if($rule['operator'] == "==") 
  289. $match = ( count($children) > 0 ); 
  290. elseif($rule['operator'] == "!=") 
  291. $match = ( count($children) == 0 ); 
  292.  
  293. elseif( $rule['value'] == 'child') 
  294.  
  295. $post_parent = $post->post_parent; 
  296. if( $options['page_parent'] ) 
  297. $post_parent = $options['page_parent']; 
  298.  
  299.  
  300. if($rule['operator'] == "==") 
  301. $match = ( $post_parent != 0 ); 
  302. elseif($rule['operator'] == "!=") 
  303. $match = ( $post_parent == 0 ); 
  304.  
  305.  
  306. return $match; 
  307.  
  308.  
  309.  
  310. /** 
  311. * rule_match_page_parent 
  312. * @description:  
  313. * @since: 3.5.7 
  314. * @created: 3/01/13 
  315. */ 
  316.  
  317. function rule_match_page_parent( $match, $rule, $options ) 
  318. // validation 
  319. if( !$options['post_id'] ) 
  320. return false; 
  321.  
  322.  
  323. // vars 
  324. $post = get_post( $options['post_id'] ); 
  325.  
  326. $post_parent = $post->post_parent; 
  327. if( $options['page_parent'] ) 
  328. $post_parent = $options['page_parent']; 
  329.  
  330.  
  331. if($rule['operator'] == "==") 
  332. $match = ( $post_parent == $rule['value'] ); 
  333. elseif($rule['operator'] == "!=") 
  334. $match = ( $post_parent != $rule['value'] ); 
  335.  
  336.  
  337. return $match; 
  338.  
  339.  
  340.  
  341. /** 
  342. * rule_match_page_template 
  343. * @description:  
  344. * @since: 3.5.7 
  345. * @created: 3/01/13 
  346. */ 
  347.  
  348. function rule_match_page_template( $match, $rule, $options ) 
  349. $page_template = $options['page_template']; 
  350. if( ! $page_template ) 
  351. $page_template = get_post_meta( $options['post_id'], '_wp_page_template', true ); 
  352.  
  353.  
  354. if( ! $page_template ) 
  355. $post_type = $options['post_type']; 
  356.  
  357. if( !$post_type ) 
  358. $post_type = get_post_type( $options['post_id'] ); 
  359.  
  360. if( $post_type == 'page' ) 
  361. $page_template = "default"; 
  362.  
  363.  
  364.  
  365. if($rule['operator'] == "==") 
  366. $match = ( $page_template === $rule['value'] ); 
  367. elseif($rule['operator'] == "!=") 
  368. $match = ( $page_template !== $rule['value'] ); 
  369.  
  370. return $match; 
  371.  
  372.  
  373.  
  374. /** 
  375. * rule_match_post_category 
  376. * @description:  
  377. * @since: 3.5.7 
  378. * @created: 3/01/13 
  379. */ 
  380.  
  381. function rule_match_post_category( $match, $rule, $options ) 
  382. // validate 
  383. if( !$options['post_id'] ) 
  384. return false; 
  385.  
  386.  
  387. // post type 
  388. if( !$options['post_type'] ) 
  389. $options['post_type'] = get_post_type( $options['post_id'] ); 
  390.  
  391.  
  392. // vars 
  393. $taxonomies = get_object_taxonomies( $options['post_type'] ); 
  394. $terms = $options['post_category']; 
  395.  
  396.  
  397. // not AJAX  
  398. if( !$options['ajax'] ) 
  399. // no terms? Load them from the post_id 
  400. if( empty($terms) ) 
  401. $all_terms = get_the_terms( $options['post_id'], 'category' ); 
  402. if($all_terms) 
  403. foreach($all_terms as $all_term) 
  404. $terms[] = $all_term->term_id; 
  405.  
  406.  
  407. // no terms at all?  
  408. if( empty($terms) ) 
  409. // If no ters, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked 
  410. if( is_array($taxonomies) && in_array('category', $taxonomies) ) 
  411. $terms[] = '1'; 
  412.  
  413.  
  414.  
  415. if($rule['operator'] == "==") 
  416. $match = false; 
  417.  
  418. if($terms) 
  419. if( in_array($rule['value'], $terms) ) 
  420. $match = true;  
  421.  
  422. elseif($rule['operator'] == "!=") 
  423. $match = true; 
  424.  
  425. if($terms) 
  426. if( in_array($rule['value'], $terms) ) 
  427. $match = false;  
  428.  
  429.  
  430.  
  431. return $match; 
  432.  
  433.  
  434.  
  435. /** 
  436. * rule_match_user_type 
  437. * @description:  
  438. * @since: 3.5.7 
  439. * @created: 3/01/13 
  440. */ 
  441.  
  442. function rule_match_user_type( $match, $rule, $options ) 
  443. $user = wp_get_current_user(); 
  444.  
  445. if( $rule['operator'] == "==" ) 
  446. if( $rule['value'] == 'super_admin' ) 
  447. $match = is_super_admin( $user->ID ); 
  448. else  
  449. $match = in_array( $rule['value'], $user->roles ); 
  450.  
  451. elseif( $rule['operator'] == "!=" ) 
  452. if( $rule['value'] == 'super_admin' ) 
  453. $match = !is_super_admin( $user->ID ); 
  454. else  
  455. $match = ( ! in_array( $rule['value'], $user->roles ) ); 
  456.  
  457. return $match; 
  458.  
  459.  
  460.  
  461. /** 
  462. * rule_match_user_type 
  463. * @description:  
  464. * @since: 3.5.7 
  465. * @created: 3/01/13 
  466. */ 
  467.  
  468. function rule_match_options_page( $match, $rule, $options ) 
  469. global $plugin_page; 
  470.  
  471. // NOTE 
  472. // comment out below code as it was interfering with custom slugs 
  473.  
  474. // older location rules may be "options-pagename" 
  475. /** 
  476. if( substr($rule['value'], 0, 8) == 'options-' ) 
  477. $rule['value'] = 'acf-' . $rule['value']; 
  478. */ 
  479.  
  480.  
  481. // older location ruels may be "Pagename" 
  482. /** 
  483. if( substr($rule['value'], 0, 11) != 'acf-options' ) 
  484. $rule['value'] = 'acf-options-' . sanitize_title( $rule['value'] ); 
  485.  
  486. // value may now be wrong (acf-options-options) 
  487. if( $rule['value'] == 'acf-options-options' ) 
  488. $rule['value'] = 'acf-options'; 
  489. */ 
  490.  
  491.  
  492. if($rule['operator'] == "==") 
  493. $match = ( $plugin_page === $rule['value'] ); 
  494. elseif($rule['operator'] == "!=") 
  495. $match = ( $plugin_page !== $rule['value'] ); 
  496.  
  497.  
  498. return $match; 
  499.  
  500.  
  501.  
  502. /** 
  503. * rule_match_post_format 
  504. * @description:  
  505. * @since: 3.5.7 
  506. * @created: 3/01/13 
  507. */ 
  508.  
  509. function rule_match_post_format( $match, $rule, $options ) 
  510. // vars 
  511. $post_format = $options['post_format']; 
  512. if( !$post_format ) 
  513. {  
  514. // validate 
  515. if( !$options['post_id'] ) 
  516. return false; 
  517.  
  518.  
  519. // post type 
  520. if( !$options['post_type'] ) 
  521. $options['post_type'] = get_post_type( $options['post_id'] ); 
  522.  
  523.  
  524. // does post_type support 'post-format' 
  525. if( post_type_supports( $options['post_type'], 'post-formats' ) ) 
  526. $post_format = get_post_format( $options['post_id'] ); 
  527.  
  528. if( $post_format === false ) 
  529. $post_format = 'standard'; 
  530.  
  531.  
  532. if($rule['operator'] == "==") 
  533. $match = ( $post_format === $rule['value'] ); 
  534.  
  535. elseif($rule['operator'] == "!=") 
  536. $match = ( $post_format !== $rule['value'] ); 
  537.  
  538.  
  539.  
  540. return $match; 
  541.  
  542.  
  543.  
  544. /** 
  545. * rule_match_post_status 
  546. * @description:  
  547. * @since: 3.5.7 
  548. * @created: 3/01/13 
  549. */ 
  550.  
  551. function rule_match_post_status( $match, $rule, $options ) 
  552. // validate 
  553. if( !$options['post_id'] ) 
  554. return false; 
  555.  
  556.  
  557. // vars 
  558. $post_status = get_post_status( $options['post_id'] ); 
  559.  
  560.  
  561. // auto-draft = draft 
  562. if( $post_status == 'auto-draft' ) 
  563. $post_status = 'draft'; 
  564.  
  565.  
  566. // match 
  567. if($rule['operator'] == "==") 
  568. $match = ( $post_status === $rule['value'] ); 
  569.  
  570. elseif($rule['operator'] == "!=") 
  571. $match = ( $post_status !== $rule['value'] ); 
  572.  
  573.  
  574. // return 
  575. return $match; 
  576.  
  577.  
  578.  
  579. /** 
  580. * rule_match_taxonomy 
  581. * @description:  
  582. * @since: 3.5.7 
  583. * @created: 3/01/13 
  584. */ 
  585.  
  586. function rule_match_taxonomy( $match, $rule, $options ) 
  587. // validate 
  588. if( !$options['post_id'] ) 
  589. return false; 
  590.  
  591.  
  592. // post type 
  593. if( !$options['post_type'] ) 
  594. $options['post_type'] = get_post_type( $options['post_id'] ); 
  595.  
  596.  
  597. // vars 
  598. $taxonomies = get_object_taxonomies( $options['post_type'] ); 
  599. $terms = $options['taxonomy']; 
  600.  
  601.  
  602. // not AJAX  
  603. if( !$options['ajax'] ) 
  604. // no terms? Load them from the post_id 
  605. if( empty($terms) ) 
  606. if( is_array($taxonomies) ) 
  607. foreach( $taxonomies as $tax ) 
  608. $all_terms = get_the_terms( $options['post_id'], $tax ); 
  609. if($all_terms) 
  610. foreach($all_terms as $all_term) 
  611. $terms[] = $all_term->term_id; 
  612.  
  613.  
  614. // no terms at all?  
  615. if( empty($terms) ) 
  616. // If no ters, this is a new post and should be treated as if it has the "Uncategorized" (1) category ticked 
  617. if( is_array($taxonomies) && in_array('category', $taxonomies) ) 
  618. $terms[] = '1'; 
  619.  
  620.  
  621. if($rule['operator'] == "==") 
  622. $match = false; 
  623.  
  624. if($terms) 
  625. if( in_array($rule['value'], $terms) ) 
  626. $match = true;  
  627.  
  628. elseif($rule['operator'] == "!=") 
  629. $match = true; 
  630.  
  631. if($terms) 
  632. if( in_array($rule['value'], $terms) ) 
  633. $match = false;  
  634.  
  635.  
  636.  
  637. return $match; 
  638.  
  639.  
  640.  
  641. /** 
  642. * rule_match_ef_taxonomy 
  643. * @description:  
  644. * @since: 3.5.7 
  645. * @created: 3/01/13 
  646. */ 
  647.  
  648. function rule_match_ef_taxonomy( $match, $rule, $options ) 
  649.  
  650. $ef_taxonomy = $options['ef_taxonomy']; 
  651.  
  652.  
  653. if( $ef_taxonomy ) 
  654. if($rule['operator'] == "==") 
  655. $match = ( $ef_taxonomy == $rule['value'] ); 
  656.  
  657. // override for "all" 
  658. if( $rule['value'] == "all" ) 
  659. $match = true; 
  660.  
  661. elseif($rule['operator'] == "!=") 
  662. $match = ( $ef_taxonomy != $rule['value'] ); 
  663.  
  664. // override for "all" 
  665. if( $rule['value'] == "all" ) 
  666. $match = false; 
  667.  
  668.  
  669.  
  670.  
  671.  
  672.  
  673.  
  674. return $match; 
  675.  
  676.  
  677.  
  678. /** 
  679. * rule_match_ef_user 
  680. * @description:  
  681. * @since: 3.5.7 
  682. * @created: 3/01/13 
  683. */ 
  684.  
  685. function rule_match_ef_user( $match, $rule, $options ) 
  686.  
  687. $ef_user = $options['ef_user']; 
  688.  
  689.  
  690. if( $ef_user ) 
  691. if($rule['operator'] == "==") 
  692. $match = ( user_can($ef_user, $rule['value']) ); 
  693.  
  694. // override for "all" 
  695. if( $rule['value'] === "all" ) 
  696. $match = true; 
  697. elseif($rule['operator'] == "!=") 
  698. $match = ( !user_can($ef_user, $rule['value']) ); 
  699.  
  700. // override for "all" 
  701. if( $rule['value'] === "all" ) 
  702. $match = false; 
  703.  
  704.  
  705.  
  706. return $match; 
  707.  
  708. }  
  709.