Advertisement
Guest User

taxonomy.php

a guest
Sep 30th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.18 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @package Content Aware Sidebars
  4.  * @author Joachim Jensen <jv@intox.dk>
  5.  */
  6.  
  7. /**
  8.  *
  9.  * Taxonomy Module
  10.  *
  11.  * Detects if current content has/is:
  12.  * a) any term of specific taxonomy or specific term
  13.  * b) taxonomy archive or specific term archive
  14.  *
  15.  */
  16. class CASModule_taxonomy extends CASModule {
  17.    
  18.     private $taxonomy_objects;
  19.     private $post_terms;
  20.    
  21.     public function __construct() {
  22.         parent::__construct();
  23.         $this->id = 'taxonomies';
  24.         $this->name = __('Taxonomies','content-aware-sidebars');
  25.     }
  26.    
  27.     public function is_content() {     
  28.         if(is_singular()) {
  29.             // Check if content has any taxonomies supported
  30.             $taxonomies = get_object_taxonomies(get_post_type());
  31.             if($taxonomies) {
  32.                 $this->post_terms = wp_get_object_terms(get_the_ID(),$taxonomies);
  33.                 // Check if content has any actual taxonomy terms
  34.                 if($this->post_terms) {
  35.                     return true;
  36.                 }
  37.             }
  38.         } else if(is_tax() || is_category() || is_tag()) {
  39.             return true;
  40.         }
  41.         return false;
  42.     }
  43.    
  44.     public function db_join() {
  45.         global $wpdb;
  46.        
  47.         $joins = "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID ";
  48.         $joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id ";
  49.         $joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id ";
  50.         $joins .= "LEFT JOIN $wpdb->postmeta taxonomies ON taxonomies.post_id = posts.ID AND taxonomies.meta_key = '".ContentAwareSidebars::prefix."taxonomies'";
  51.        
  52.         return $joins;
  53.    
  54.     }
  55.    
  56.     public function db_where() {
  57.        
  58.         if(is_singular()) {
  59.             $terms = array();
  60.             $taxonomies = array();
  61.                        
  62.             //Grab posts terms and make where rules for taxonomies.
  63.             foreach($this->post_terms as $term) {
  64.                 $terms[$term->taxonomy][] = $term->slug;
  65.                 if(!isset($taxonomies[$term->taxonomy])) {
  66.                     $taxonomies[$term->taxonomy] = $term->taxonomy;
  67.                 }
  68.             }
  69.            
  70.             foreach($terms as $taxonomy => $term_arr) {
  71.                 $testecho[] = "(taxonomy.taxonomy = '".$taxonomy."' AND terms.slug IN('".implode("','",$term_arr)."'))";
  72.             }
  73.  
  74.             return "(terms.slug IS NULL OR ".implode(" OR ",$testecho).") AND (taxonomies.meta_value IS NULL OR taxonomies.meta_value IN('".implode("','",$taxonomies)."'))";
  75.        
  76.             //return "(terms.slug IS NULL OR terms.slug IN('".implode("','",$terms)."')) AND (taxonomies.meta_value IS NULL OR taxonomies.meta_value IN('".implode("','",$taxonomies)."'))";
  77.         }
  78.         $term = get_queried_object();
  79.        
  80.         return "(taxonomy.taxonomy = '".$term->taxonomy."' AND terms.slug = '".$term->slug."')";
  81.        
  82.     }
  83.    
  84.     public function db_where2() {
  85.         return "terms.slug IS NOT NULL OR taxonomies.meta_value IS NOT NULL";
  86.     }
  87.  
  88.     public function _get_content() {
  89.        
  90.     }
  91.  
  92.     public function meta_box_content() {
  93.         global $post;
  94.  
  95.         foreach ($this->_get_taxonomies() as $taxonomy) {
  96.             echo '<h4><a href="#">' . $taxonomy->label . '</a></h4>'."\n";
  97.             echo '<div class="cas-rule-content" id="cas-' . $this->id . '-' . $taxonomy->name . '">';
  98.  
  99.             $meta = get_post_meta($post->ID, ContentAwareSidebars::prefix . 'taxonomies', false);
  100.             $current = $meta != '' ? $meta : array();
  101.  
  102.             $terms = get_terms($taxonomy->name, array('get' => 'all','number' => 200));
  103.  
  104.             echo '<p>' . "\n";
  105.             echo '<label><input type="checkbox" name="taxonomies[]" value="' . $taxonomy->name . '"' . checked(in_array($taxonomy->name, $current), true, false) . ' /> ' . sprintf(__('Show with %s', 'content-aware-sidebars'), $taxonomy->labels->all_items) . '</label>' . "\n";
  106.             echo '</p>' . "\n";
  107.             if (!$terms || is_wp_error($terms)) {
  108.                 echo '<p>' . __('No items.') . '</p>';
  109.             } else {
  110.                 ?>
  111.                     <div id="taxonomy-<?php echo $taxonomy->name; ?>" class="categorydiv" style="min-height:100%;">
  112.                         <ul id="<?php echo $taxonomy->name; ?>-tabs" class="category-tabs">
  113.                             <li class="hide-if-no-js"><a href="#<?php echo $taxonomy->name; ?>-pop" tabindex="3"><?php _e('Most Used'); ?></a></li>
  114.                             <li class="tabs"><a href="#<?php echo $taxonomy->name; ?>-all" tabindex="3"><?php _e('View All'); ?></a></li>
  115.                         </ul>
  116.  
  117.                         <div id="<?php echo $taxonomy->name; ?>-pop" class="tabs-panel" style="display: none;min-height:100%;">
  118.                             <ul id="<?php echo $taxonomy->name; ?>checklist-pop" class="categorychecklist form-no-clear" >
  119.                 <?php $popular_ids = cas_popular_terms_checklist($taxonomy); ?>
  120.                             </ul>
  121.                         </div>
  122.                        
  123.                         <div id="<?php echo $taxonomy->name; ?>-all" class="tabs-panel" style="min-height:100%;">
  124.                             <input type="hidden" name="<?php echo ($taxonomy->name == "category" ? "post_category[]" : "tax_input[$taxonomy->name]"); ?>" value="0" />
  125.                             <ul id="<?php echo $taxonomy->name; ?>checklist" class="list:<?php echo $taxonomy->name ?> categorychecklist form-no-clear">
  126.                 <?php cas_terms_checklist($post->ID, array('taxonomy' => $taxonomy, 'popular_terms' => $popular_ids, 'terms' => $terms)) ?>
  127.                             </ul>
  128.                         </div>
  129.                     </div>
  130.                 <?php
  131.             }
  132.             echo '</div>'."\n";
  133.         }
  134.     }
  135.    
  136.     private function _get_taxonomies() {
  137.         // List public taxonomies
  138.         if (empty($this->taxonomy_objects)) {
  139.             foreach (get_taxonomies(array('public' => true), 'objects') as $tax) {
  140.                 $this->taxonomy_objects[$tax->name] = $tax;
  141.             }
  142.         }
  143.         return $this->taxonomy_objects;
  144.     }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement