Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.84 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Adds a taxonomy to the native 'post' object type.
  5.  *
  6.  * Also adds a rewrite endpoint to test the issue. Visiting any posts URL with
  7.  * the taxonomy slug segment prepended will trigger output of the custom
  8.  * taxonomy's terms assigned to the post; best to view page source.
  9.  *
  10.  * @link https://wordpress.org/support/topic/turning-off-frontend-actions-and-filter
  11.  */
  12. class OrgSupportTopic862744
  13. {
  14.     const TAXONOMY_SLUG = 'mytax';
  15.     const OBJECT_TYPE = 'post';
  16.     const PERMASTRUCT_TYPE = EP_ALL;
  17.     const TEMPLATE_FILE = 'org-862744.php';
  18.    
  19.     const TAXONOMY_HOOK_PRIORITY = 11;
  20.     const TEMPLAGE_HOOK_PRIORITY = 99;
  21.    
  22.     public function __construct()
  23.     {
  24.         $this->_construct();
  25.     }
  26.    
  27.     protected function _construct()
  28.     {
  29.        
  30.     }
  31.    
  32.     public function init()
  33.     {
  34.         $this->_hook();
  35.     }
  36.    
  37.     protected function _hook()
  38.     {
  39.         $me = $this;
  40.         add_action('init', function() use ($me) {
  41.             $me->_registerTaxonomy();
  42.         }, static::TAXONOMY_HOOK_PRIORITY);
  43.        
  44.     }
  45.    
  46.     public function _registerTaxonomy()
  47.     {
  48.         register_taxonomy(static::TAXONOMY_SLUG, static::OBJECT_TYPE, array(
  49.             'label'             => 'My Tax',
  50.             'hierarchical'      => true
  51.         ));
  52.        
  53.         var_dump(static::PERMASTRUCT_TYPE);
  54.         add_rewrite_endpoint(static::TAXONOMY_SLUG, static::PERMASTRUCT_TYPE);
  55.         flush_rewrite_rules();
  56.        
  57.         add_filter('template_include', array($this, '_overrideTemplate'), static::TEMPLAGE_HOOK_PRIORITY);
  58.     }
  59.    
  60.     public function generateTermList($objectId)
  61.     {
  62.         return get_the_term_list($objectId, static::TAXONOMY_SLUG);
  63.     }
  64.    
  65.     public function _overrideTemplate($template)
  66.     {
  67.         global $wp_query;
  68.         if ($this->_getQueryVar(static::TAXONOMY_SLUG) !== null && ($objectId = get_post()->ID)) {
  69.             echo $this->generateTermList($objectId);
  70.         }
  71.        
  72.         return $template;
  73. //        return $this->_locateTemplateFile(static::TEMPLATE_FILE);
  74.     }
  75.    
  76.     protected function _locateTemplateFile($files)
  77.     {
  78.         if (!is_array($files)) {
  79.             $files = array($files);
  80.         }
  81.        
  82.         return strlen($path = locate_template($files, false))
  83.                 ? $path
  84.                 : null;
  85.     }
  86.    
  87.     protected function _getQueryVar($key)
  88.     {
  89.         return get_query_var($key, null);
  90.     }
  91. }
  92.  
  93. /**
  94.  * @staticvar type $instance
  95.  * @return \OrgSupportTopic862744
  96.  * @link https://wordpress.org/support/topic/turning-off-frontend-actions-and-filter
  97.  */
  98. function orgSupportTopic862744()
  99. {
  100.     static $instance = null;
  101.    
  102.     if (is_null($instance)) {
  103.         $instance = new OrgSupportTopic862744();
  104.         $instance->init();
  105.     }
  106.    
  107.     return $instance;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement