*/ /** * * Taxonomy Module * * Detects if current content has/is: * a) any term of specific taxonomy or specific term * b) taxonomy archive or specific term archive * */ class CASModule_taxonomy extends CASModule { private $taxonomy_objects; private $post_terms; public function __construct() { parent::__construct(); $this->id = 'taxonomies'; $this->name = __('Taxonomies','content-aware-sidebars'); } public function is_content() { if(is_singular()) { // Check if content has any taxonomies supported $taxonomies = get_object_taxonomies(get_post_type()); if($taxonomies) { $this->post_terms = wp_get_object_terms(get_the_ID(),$taxonomies); // Check if content has any actual taxonomy terms if($this->post_terms) { return true; } } } else if(is_tax() || is_category() || is_tag()) { return true; } return false; } public function db_join() { global $wpdb; $joins = "LEFT JOIN $wpdb->term_relationships term ON term.object_id = posts.ID "; $joins .= "LEFT JOIN $wpdb->term_taxonomy taxonomy ON taxonomy.term_taxonomy_id = term.term_taxonomy_id "; $joins .= "LEFT JOIN $wpdb->terms terms ON terms.term_id = taxonomy.term_id "; $joins .= "LEFT JOIN $wpdb->postmeta taxonomies ON taxonomies.post_id = posts.ID AND taxonomies.meta_key = '".ContentAwareSidebars::prefix."taxonomies'"; return $joins; } public function db_where() { if(is_singular()) { $terms = array(); $taxonomies = array(); //Grab posts terms and make where rules for taxonomies. foreach($this->post_terms as $term) { $terms[$term->taxonomy][] = $term->slug; if(!isset($taxonomies[$term->taxonomy])) { $taxonomies[$term->taxonomy] = $term->taxonomy; } } foreach($terms as $taxonomy => $term_arr) { $testecho[] = "(taxonomy.taxonomy = '".$taxonomy."' AND terms.slug IN('".implode("','",$term_arr)."'))"; } return "(terms.slug IS NULL OR ".implode(" OR ",$testecho).") AND (taxonomies.meta_value IS NULL OR taxonomies.meta_value IN('".implode("','",$taxonomies)."'))"; //return "(terms.slug IS NULL OR terms.slug IN('".implode("','",$terms)."')) AND (taxonomies.meta_value IS NULL OR taxonomies.meta_value IN('".implode("','",$taxonomies)."'))"; } $term = get_queried_object(); return "(taxonomy.taxonomy = '".$term->taxonomy."' AND terms.slug = '".$term->slug."')"; } public function db_where2() { return "terms.slug IS NOT NULL OR taxonomies.meta_value IS NOT NULL"; } public function _get_content() { } public function meta_box_content() { global $post; foreach ($this->_get_taxonomies() as $taxonomy) { echo '

' . $taxonomy->label . '

'."\n"; echo '
'; $meta = get_post_meta($post->ID, ContentAwareSidebars::prefix . 'taxonomies', false); $current = $meta != '' ? $meta : array(); $terms = get_terms($taxonomy->name, array('get' => 'all','number' => 200)); echo '

' . "\n"; echo '' . "\n"; echo '

' . "\n"; if (!$terms || is_wp_error($terms)) { echo '

' . __('No items.') . '

'; } else { ?>
name]"); ?>" value="0" />
    ID, array('taxonomy' => $taxonomy, 'popular_terms' => $popular_ids, 'terms' => $terms)) ?>
'."\n"; } } private function _get_taxonomies() { // List public taxonomies if (empty($this->taxonomy_objects)) { foreach (get_taxonomies(array('public' => true), 'objects') as $tax) { $this->taxonomy_objects[$tax->name] = $tax; } } return $this->taxonomy_objects; } }