/core/controllers/location.php

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