Advertisement
Guest User

Untitled

a guest
Jul 19th, 2018
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.40 KB | None | 0 0
  1. <?php
  2.  
  3.     new my_acf_extension();
  4.  
  5.     class my_acf_extension
  6.     {
  7.         public function __construct()
  8.         {
  9.             // state field on city
  10.             add_action('acf/load_field/key=field_579376941cecc', array($this, 'load_state_field_choices'));
  11.             // state field on post
  12.             add_action('acf/load_field/key=field_579376f522130', array($this, 'load_state_field_choices'));
  13.             // city field on post
  14.             add_action('acf/load_field/key=field_5793770922131', array($this, 'load_city_field_choices'));
  15.             // ajax action for loading city choices
  16.             add_action('wp_ajax_load_city_field_choices', array($this, 'ajax_load_city_field_choices'));
  17.             // enqueue js extension for acf
  18.             // do this when ACF in enqueuing scripts
  19.             add_action('acf/input/admin_enqueue_scripts', array($this, 'enqueue_script'));
  20.         } // end public function __construct
  21.  
  22.         public function load_state_field_choices($field)
  23.         {
  24.             // this funciton dynamically loads the state select field choices
  25.             // from the state post type
  26.  
  27.             // I only want to do this on Posts and the city CPT
  28.             global $post;
  29.             if (!$post ||
  30.                 !isset($post->ID) ||
  31.                 (get_post_type($post->ID) != 'post' && get_post_type($post->ID) != 'city')) {
  32.                 return $field;
  33.             }
  34.  
  35.             // get states
  36.             $args = array(
  37.                 'post_type' => 'state',
  38.                 'posts_per_page' => -1,
  39.                 'orderby' => 'title',
  40.                 'order' => 'ASC'
  41.             );
  42.             $query = new WP_Query($args);
  43.             $choices = array('' => '-- State --');
  44.             if (count($query->posts)) {
  45.                 // populate choices
  46.                 foreach ($query->posts as $state) {
  47.                     $choices[$state->ID] = $state->post_title;
  48.                 }
  49.             }
  50.             $field['choices'] = $choices;
  51.             return $field;
  52.         } // end public function load_state_field_choices
  53.  
  54.         public function load_city_field_choices($field)
  55.         {
  56.             // this function dynamically loads city field choices
  57.             // based on the currently saved state
  58.  
  59.             // I only want to do this on Posts
  60.             global $post;
  61.             if (!$post ||
  62.                 !isset($post->ID) ||
  63.                 get_post_type($post->ID) != 'post') {
  64.                 return $field;
  65.             }
  66.             // get the state post id
  67.             // I generally use get_post_meta() instead of get_field()
  68.             // when building functionality, but get_field() could be
  69.             // subsitited here
  70.             $state = intval(get_post_meta($post->ID, 'state', true));
  71.             $cities = $this->get_cities($state);
  72.             $field['choices'] = $cities;
  73.             return $field;
  74.         } // end public funciton load_city_field_choices
  75.  
  76.         public function enqueue_script()
  77.         {
  78.             // enqueue acf extenstion
  79.  
  80.             // only enqueue the script on the post page where it needs to run
  81.             /* *** THIS IS IMPORTANT
  82.                    ACF uses the same scripts as well as the same field identification
  83.                    markup (the data-key attribute) if the ACF field group editor
  84.                    because of this, if you load and run your custom javascript on
  85.                    the field group editor page it can have unintended side effects
  86.                    on this page. It is important to alway make sure you're only
  87.                    loading scripts where you need them.
  88.             */
  89.             global $post;
  90.             if (!$post ||
  91.                 !isset($post->ID) ||
  92.                 get_post_type($post->ID) != 'post') {
  93.                 return;
  94.             }
  95.  
  96.             $handle = 'my-acf-extension';
  97.  
  98.             // I'm using this method to set the src because
  99.             // I don't know where this file will be located
  100.             // you should alter this to use the correct fundtions
  101.             // to set the src value to point to the javascript file
  102.             $src = '/'.str_replace(ABSPATH, '', dirname(__FILE__)).'/my-acf-extension.js';
  103.             // make this script dependent on acf-input
  104.             $depends = array('acf-input');
  105.  
  106.             wp_enqueue_script($handle, $src, $depends);
  107.         } // end public function enqueue_script
  108.  
  109.         public function ajax_load_city_field_choices()
  110.         {
  111.             // this funtion is called by AJAX to load cities
  112.             // based on state selecteion
  113.  
  114.             // we can use the acf nonce to verify
  115.             if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
  116.                 die();
  117.             }
  118.             $state = 0;
  119.             if (isset($_POST['state'])) {
  120.                 $state = intval($_POST['state']);
  121.             }
  122.             $cities = $this->get_cities($state);
  123.             $choices = array();
  124.             foreach ($cities as $value => $label) {
  125.                 $choices[] = array('value' => $value, 'label' => $label);
  126.             }
  127.             echo json_encode($choices);
  128.             exit;
  129.         } // end public function ajax_load_city_field_choices
  130.  
  131.         private function get_cities($post_id)
  132.         {
  133.             // $post_id is post ID of state post
  134.             // get all cities related to the state
  135.             $args = array(
  136.                 'post_type' => 'city',
  137.                 'posts_per_page' => -1,
  138.                 'orderby' => 'title',
  139.                 'order' => 'ASC',
  140.                 'meta_query' => array(
  141.                     array(
  142.                         'key' => 'state',
  143.                         'value' => $post_id          // [if acf supports only one value]
  144.                       //  'value' => '"'.$post_id.'"',  // [if acf supports multiple values]
  145.                       //  'compare' => 'LIKE'           // [if acf supports multiple values]
  146.                     )
  147.                 )
  148.             );
  149.             $query = new WP_Query($args);
  150.             $choices = array('' => '-- City --');
  151.             if (count($query->posts)) {
  152.                 // populate choices
  153.                 foreach ($query->posts as $post) {
  154.                     $choices[$post->ID] = $post->post_title;
  155.                 }
  156.             }
  157.             return $choices;
  158.         } // end private function get_cities
  159.     } // end class my_acf_extension
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement