Advertisement
cw17s0n

Faceted Search Widget

Jun 22nd, 2012
267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.61 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Faceted Search Widget
  4. Description: Sidebar Widget to allow filtering indexes by builtin and custom taxonomies
  5. Version: 2.0
  6. Author: The Federal Communications Commission
  7. Author URI: http://fcc.gov/developers
  8. License: GPL2
  9. */
  10.  
  11. class FCC_Refine_Widget extends WP_Widget {
  12.  
  13.     private $defaults = array(
  14.             'title' => '',
  15.             'depth' => '0',
  16.             'contentdiv' => false,
  17.             'orderby' => 'name',
  18.         );
  19.        
  20.     /**
  21.      * Constructor
  22.      */
  23.     Function FCC_Refine_Widget() {
  24.    
  25.         parent::WP_Widget(false, $name = 'Faceted Search Widget');
  26.        
  27.         //can't i18n outside of function
  28.         $this->defaults['title'] = __( 'Refine', 'faceted-search-widget' );
  29.                        
  30.     }
  31.    
  32.     /**
  33.      * Widget to propegate sidebar list of taxonomies and terms
  34.      * @param array $args args passed to widget
  35.      * @param reference $instance the widget instance
  36.      */
  37.     function widget( $args, $instance ) {
  38.    
  39.         //verify that this is either an archive or search results
  40.         if ( is_404() || is_single() || is_attachment() || is_page() )
  41.             return;
  42.    
  43.         //enqueue ajax scripts, if necessary
  44.         if ( $instance['contentdiv'] ) {
  45.        
  46.             $file = ( WP_DEBUG ) ? 'js/faceted-search-widget.dev.js' : 'js/faceted-search-widget.js';
  47.        
  48.             wp_enqueue_script( 'faceted-search-widget', plugins_url( $file, __FILE__ ), array( 'jquery' ), filemtime( dirname( __FILE__ ) . '/' . $file ), true );
  49.            
  50.             $data = array( '.' . $this->widget_options['classname'], $instance['contentdiv'] );
  51.            
  52.             wp_localize_script( 'faceted-search-widget', 'faceted_search_widget', $data );
  53.        
  54.         }
  55.    
  56.         //grab widget args and wp_query
  57.         global $wp_query;
  58.         extract( $args );
  59.  
  60.         $title = apply_filters( 'widget_title', $instance['title'] );
  61.         ?>
  62.             <?php echo $before_widget; ?>
  63.                 <?php if ( $title )
  64.                     echo $before_title . $title . $after_title;
  65.  
  66.         $taxs = get_taxonomies( array( 'public' => true, 'query_var' => true ),  'objects' ); ?>
  67.         <ul>
  68.         <?php foreach ($taxs as $tax) {
  69.  
  70.         //Non-Hierarchical taxonomy with term already filtered (no futher filtering)
  71.         if ( !$tax->hierarchical && $this->tax_in_query( $tax->name ) ) {
  72.             continue;
  73.                        
  74.         //Hierarchical taxonomy with term filtered (filter down to children)
  75.         } else if ( $tax->hierarchical && $this->tax_in_query( $tax->name ) ) {
  76.             $termID = term_exists( get_query_var( $tax->query_var ) );         
  77.             $terms = get_terms( $tax->name, array( 'child_of' => $termID ) );
  78.  
  79.         //No filters, get all terms
  80.         } else {
  81.             $terms = get_terms( $tax->name );
  82.         }  
  83.  
  84.         //verify taxonomy has terms associated with it
  85.         if ( sizeof( $terms ) == 0 )
  86.             continue;
  87.        
  88.         add_filter( 'term_link', array( &$this, 'term_link_filter'), 10, 3 );
  89.         add_filter( 'get_terms', array( &$this, 'get_terms_filter'), 10, 3 );
  90.        
  91.         wp_list_categories( array(
  92.                                 'taxonomy' => $tax->name,
  93.                                 'show_count' => true,
  94.                                 'depth' => $instance['depth'],
  95.                                 'title_li' => $tax->labels->name,
  96.                                 'orderby' => $instance['orderby'],
  97.                                 'order' => ( $instance['orderby'] == 'count' ) ? 'DESC' : 'ASC',
  98.                             ) );
  99.  
  100.         remove_filter( 'term_link', array( &$this, 'term_link_filter' ) );
  101.         remove_filter( 'get_terms', array( &$this, 'get_terms_filter') );
  102.  
  103.         } ?>
  104.         </ul>
  105.         <?php  echo $after_widget;
  106.     }
  107.    
  108.     /**
  109.      * Function to process changes to the widge title
  110.      * @param array $old the old title
  111.      * @param array $new the new title
  112.      * @return array the new title array
  113.      */
  114.     function update( $new, $old ) {
  115.         $instance = $old;
  116.         $instance['title'] = strip_tags( $new['title'] );
  117.         $instance['contentdiv'] = strip_tags( $new['contentdiv'] );
  118.         $instance['depth'] = (int) $new['depth'];
  119.         $instance['orderby'] = $new['orderby'];
  120.         return $instance;
  121.     }
  122.    
  123.     /**
  124.      * Callback to generate the title form for widgets.php
  125.      * @param reference $instance the widget instance
  126.      */
  127.     function form( $instance ) {
  128.    
  129.         foreach ( $this->defaults as $key => $value )
  130.             if ( !isset( $instance[ $key ] ) )
  131.                 $instance[ $key ] = $value;
  132.     ?>
  133.          <p>
  134.           <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', 'faceted-search-widget' ); ?></label>
  135.           <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr( $instance['title'] ); ?>" /><br />
  136.           <span class="description"><?php _e( 'Title to display above the widget', 'faceted-search-widget' ); ?></span>
  137.         </p>
  138.         <p>
  139.           <label for="<?php echo $this->get_field_id('depth'); ?>"><?php _e('Depth:', 'faceted-search-widget' ); ?></label>
  140.           <input class="small-text" id="<?php echo $this->get_field_id('depth'); ?>" name="<?php echo $this->get_field_name('depth'); ?>" type="text" value="<?php echo esc_attr( $instance['depth'] );; ?>" /><br />
  141.           <span class="description"><?php _e( 'Maximum number of levels to show within hierarchical taxonomies like categories', 'faceted-search-widget' ); ?></span>
  142.         </p>
  143.         <p>
  144.             <label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e('Order terms by:', 'faceted-search-widget' ); ?></label>
  145.             <select class="widefat" id="<?php echo $this->get_field_id('orderby'); ?>" name="<?php echo $this->get_field_name('orderby'); ?>">
  146.                 <?php foreach ( array( 'name' => __( 'Name', 'faceted-search-widget' ), 'count' => __( 'Count', 'faceted-search-widget' )) as $option => $label ) { ?>
  147.                     <option value="<?php echo $option; ?>" <?php selected( $option, $instance['orderby'] ); ?>><?php echo $label; ?></option>
  148.                 <?php } ?>
  149.             </select>
  150.         </p>
  151.         <p>
  152.           <label for="<?php echo $this->get_field_id('contentdiv'); ?>"><?php _e('Content Div:', 'faceted-search-widget' ); ?></label>
  153.           <input class="widefat" id="<?php echo $this->get_field_id('contentdiv'); ?>" name="<?php echo $this->get_field_name('contentdiv'); ?>" type="text" value="<?php echo esc_attr( $instance['contentdiv'] );; ?>" /><br />
  154.           <span class="description"><?php _e( 'jQuery selector of DIV containing your template\'s posts, leave blank to disable ajax search', 'faceted-search-widget' ); ?></span>
  155.         </p>
  156.        
  157.         <?php
  158.     }
  159.    
  160.     /**
  161.      * Makes term links query args rather than absolute links
  162.      * @param string $termlink the original link
  163.      * @param array $term the term object
  164.      * @param string $taxonomy the taxonomy slug
  165.      * @return string the modified link
  166.      */
  167.     function term_link_filter( $termlink, $term, $taxonomy ) {
  168.         $tax = get_taxonomy( $taxonomy );
  169.         return esc_url( add_query_arg( $tax->query_var, $term->slug ) );
  170.     }
  171.    
  172.     /**
  173.      * Filters term list to only terms within current view, and modifies post count
  174.      * @param array $terms the original terms list
  175.      * @param array $taxonomies the taxonomy
  176.      * @param array $args args originally passed
  177.      * @return array the modified terms list
  178.      */
  179.     function get_terms_filter( $terms, $taxonomies, $args ) {
  180.          
  181.         global $wp_query;
  182.  
  183.         //safe to assume one because filter is added immediately before use
  184.         $tax = get_taxonomy( $taxonomies[0] );
  185.        
  186.         $post_type = get_post_type();
  187.  
  188.         foreach ( $terms as $id => &$term ) {
  189.        
  190.             $tax_query = $wp_query->tax_query->queries;
  191.             $tax_query['relationship'] = 'AND';
  192.  
  193.             $tax_query[] =  array(  
  194.                                 'taxonomy'  =>  $tax->name,
  195.                                 'field'     =>  'slug',
  196.                                 'terms'     =>  $term->slug,
  197.                                 );
  198.             /*Added by Chuck - Adds post type to query*/                   
  199.             $query = new WP_Query( array( 'post_type' => $post_type, 'tax_query' => $tax_query ) );
  200.  
  201.             //If this term has no posts, don't display the link
  202.             if ( !$query->found_posts )
  203.                 unset( $terms[ $id ] );
  204.             else       
  205.                 $terms[$id]->count = $query->found_posts;
  206.            
  207.         }
  208.    
  209.         return $terms;
  210.     }
  211.    
  212.     /**
  213.      * Checks the global WP_Query Object to see if the taxonomy query is being queried
  214.      * Used to prevent already filtered taxonomies from being displayed
  215.      * @param string $tax the taxonomy slug
  216.      * @return bool true if in the query, otherwise false
  217.      * @since 1.4
  218.      */
  219.     function tax_in_query( $tax ) {
  220.         global $wp_query;
  221.        
  222.         if ( !isset( $wp_query->tax_query ) || !isset( $wp_query->tax_query->queries ) )
  223.             return false;
  224.        
  225.         foreach ( $wp_query->tax_query->queries as $query )
  226.             if ( $query['taxonomy'] == $tax )
  227.                 return true;
  228.                
  229.         return false;
  230.        
  231.     }
  232. }
  233.  
  234. /**
  235.  * Register the sidebar widget
  236.  */
  237. add_action('widgets_init', create_function('', 'return register_widget("FCC_Refine_Widget");'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement