Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.76 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.             // kalliergeia field on poikilia
  10.             add_action('acf/load_field/key=field_579376941cecc', array($this, 'load_kalliergeia_field_choices'));
  11.             // kalliergeia field on post
  12.             add_action('acf/load_field/key=field_5b5e20e06334b', array($this, 'load_kalliergeia_field_choices'));
  13.             // poikilia field on post
  14.             add_action('acf/load_field/key=field_5b5e20cb6334a', array($this, 'load_poikilia_field_choices'));
  15.             // ajax action for loading poikilia choices
  16.             add_action('wp_ajax_load_poikilia_field_choices', array($this, 'ajax_load_poikilia_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_kalliergeia_field_choices($field)
  23.         {
  24.             // this funciton dynamically loads the kalliergeia select field choices
  25.             // from the kalliergeia post type
  26.  
  27.             // I only want to do this on paragogos and poikilia CPT
  28.             global $post;
  29.             if (!$post ||
  30.                 !isset($post->ID) ||
  31.                 (get_post_type($post->ID) != 'paragogos' && get_post_type($post->ID) != 'poikilia')) {
  32.                 return $field;
  33.             }
  34.  
  35.             // get kalliergeies
  36.             $args = array(
  37.                 'post_type' => 'kalliergeia',
  38.                 'posts_per_page' => -1,
  39.                 'orderby' => 'title',
  40.                 'order' => 'ASC'
  41.             );
  42.             $query = new WP_Query($args);
  43.             if (get_post_type($post->ID) == 'paragogos') {
  44.                 $choices = array('' => '-- Καλλιέργεια --');
  45.             } // Default value on paragogos CPT
  46.             if (get_post_type($post->ID) == 'poikilia') {
  47.                 $choices = array();
  48.             }                           // Empty default value on poikilia CPT
  49.             if (count($query->posts)) {
  50.                 // populate choices
  51.                 foreach ($query->posts as $kalliergeia) {
  52.                     $choices[$kalliergeia->ID] = $kalliergeia->post_title;
  53.                 }
  54.             }
  55.             $field['choices'] = $choices;
  56.             //debug_to_console($field);
  57.             return $field;
  58.         } // end public function load_kalliergeia_field_choices
  59.  
  60.         public function load_poikilia_field_choices($field)
  61.         {
  62.             // this function dynamically loads poikilia field choices
  63.             // based on the currently saved kalliergeia
  64.  
  65.             // I only want to do this on paragogos CPT
  66.             global $post;
  67.             if (!$post ||
  68.                 !isset($post->ID) ||
  69.                 get_post_type($post->ID) != 'paragogos') {
  70.                 return $field;
  71.             }
  72.             if (have_rows('kalliergeies')){
  73.               debug_to_console('Have rows');
  74.             }
  75.  
  76.             return $field;
  77.         } // end public funciton load_poikilia_field_choices
  78.  
  79.         public function enqueue_script()
  80.         {
  81.             // enqueue acf extenstion
  82.  
  83.             // only enqueue the script on the post page where it needs to run
  84.             /* *** THIS IS IMPORTANT
  85.                    ACF uses the same scripts as well as the same field identification
  86.                    markup (the data-key attribute) if the ACF field group editor
  87.                    because of this, if you load and run your custom javascript on
  88.                    the field group editor page it can have unintended side effects
  89.                    on this page. It is important to alway make sure you're only
  90.                    loading scripts where you need them.
  91.             */
  92.             global $post;
  93.             if (!$post ||
  94.                 !isset($post->ID) ||
  95.                 get_post_type($post->ID) != 'paragogos') {
  96.                 return;
  97.             }
  98.  
  99.             $handle = 'acf-kalliergeies';
  100.  
  101.             // I'm using this method to set the src because
  102.             // I don't know where this file will be located
  103.             // you should alter this to use the correct fundtions
  104.             // to set the src value to point to the javascript file
  105.             $src = '/'.str_replace(ABSPATH, '', dirname(__FILE__)).'/acf-kalliergeies.js';
  106.             // make this script dependent on acf-input
  107.             $depends = array('acf-input');
  108.  
  109.             wp_enqueue_script($handle, $src, $depends);
  110.         } // end public function enqueue_script
  111.  
  112.         public function ajax_load_poikilia_field_choices()
  113.         {
  114.             // this funtion is called by AJAX to load poikilies
  115.             // based on kalliergeia selecteion
  116.  
  117.             // we can use the acf nonce to verify
  118.             if (!wp_verify_nonce($_POST['nonce'], 'acf_nonce')) {
  119.                 die();
  120.             }
  121.             $kalliergeia = 0;
  122.             if (isset($_POST['kalliergeia'])) {
  123.                 $kalliergeia = intval($_POST['kalliergeia']);
  124.             }
  125.             $poikilies = $this->get_poikilies($kalliergeia);
  126.             $choices = array();
  127.             foreach ($poikilies as $value => $label) {
  128.                 $choices[] = array('value' => $value, 'label' => $label);
  129.             }
  130.             echo json_encode($choices);
  131.             exit;
  132.         } // end public function ajax_load_poikilia_field_choices
  133.  
  134.         private function get_poikilies($post_id)
  135.         {
  136.             // $post_id is post ID of kalliergeia post
  137.             // get all poikilies related to the kalliergeia
  138.             $args = array(
  139.                 'post_type' => 'poikilia',
  140.                 'posts_per_page' => -1,
  141.                 'orderby' => 'title',
  142.                 'order' => 'ASC',
  143.                 'meta_query' => array(
  144.                     array(
  145.                         'key' => 'kalliergeia',
  146.                          //'value' => $post_id,          // [if acf supports only one value]
  147.                          'value' => '"'.$post_id.'"',  // [if acf supports multiple values]
  148.                          'compare' => 'LIKE'           // [if acf supports multiple values]
  149.                     )
  150.                 )
  151.             );
  152.             $query = new WP_Query($args);
  153.             //$choices = array('' => '-- Ποικιλία --');
  154.             $choices = array();
  155.             if (count($query->posts)) {
  156.                 // populate choices
  157.                 foreach ($query->posts as $post) {
  158.                     $choices[$post->ID] = $post->post_title;
  159.                 }
  160.             }
  161.             return $choices;
  162.         } // end private function get_poikilies
  163.     } // end class my_acf_extension
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement