Advertisement
Guest User

ACF Rule - Oxygen Templates

a guest
Nov 21st, 2019
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) exit;
  4.  
  5. class Digitalis_OXY_ACF {
  6.    
  7.     public function __construct() {
  8.        
  9.         $this->add_filters();
  10.        
  11.     }
  12.    
  13.     private function add_filters() {
  14.        
  15.         add_filter('acf/location/rule_types', [$this, 'location_rules']);
  16.         add_filter('acf/location/rule_values/oxy-template', [$this, 'location_values_oxy_template']);
  17.         add_filter('acf/location/rule_match/oxy-template', [$this, 'location_match_oxy_template'], 10, 4);
  18.        
  19.     }
  20.    
  21.     public function location_rules ( $choices ) {
  22.    
  23.         $choices['Oxygen']['oxy-template'] = 'Oxygen Template';
  24.         return $choices;
  25.        
  26.     }
  27.    
  28.     function location_values_oxy_template ( $choices ) {
  29.        
  30.         global $wpdb;
  31.         $templates = $wpdb->get_results(
  32.             "SELECT id, post_title
  33.             FROM $wpdb->posts as post
  34.             WHERE post_type = 'ct_template'
  35.             AND post.post_status IN ('publish')"
  36.         );
  37.        
  38.        
  39.         if( $templates ) {
  40.             foreach($templates as $template) {
  41.                 $choices[ $template->id ] = $template->post_title;
  42.             }
  43.         }
  44.  
  45.         return $choices;
  46.     }
  47.    
  48.     public function location_match_oxy_template ( $match, $rule, $options, $field_group = false ) {
  49.        
  50.         $current_template = get_post_meta( $options['post_id'], 'ct_other_template', true );
  51.         $selected_template = (int) $rule['value'];
  52.        
  53.         if($rule['operator'] == "=="){
  54.             $match = ( $current_template == $selected_template );
  55.         } elseif($rule['operator'] == "!=") {
  56.             $match = ( $current_template != $selected_template );
  57.         }
  58.  
  59.         return $match;
  60.        
  61.     }
  62.    
  63. }
  64.  
  65. new Digitalis_OXY_ACF();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement