Advertisement
brook-tribe

Add Filter to Filterbar Example

Aug 13th, 2014
1,165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.05 KB | None | 0 0
  1. /**
  2.  * Quick example of adding a custom filter
  3.  * A simple copy and paste of the existing Category filter
  4.  * Refer to the last line for how it adds itself to the interface
  5.  * Find this new filter in: WP Admin > Events > Settings > Filterbar
  6.  * Docs for TribeEventsFilter: http://docs.tri.be/Filter-Bar/class-TribeEventsFilter.html
  7.  */
  8. class TribeEventsFilter_Custom extends TribeEventsFilter {
  9.     public $type = 'select';
  10.  
  11.     public function get_admin_form() {
  12.         $title = $this->get_title_field();
  13.         $type = $this->get_type_field();
  14.         return $title.$type;
  15.     }
  16.  
  17.     protected function get_type_field() {
  18.         $name = $this->get_admin_field_name('type');
  19.         $field = sprintf( __( 'Type: %s %s', 'tribe-events-filter-view' ),
  20.             sprintf( '<label><input type="radio" name="%s" value="select" %s /> %s</label>',
  21.                 $name,
  22.                 checked( $this->type, 'select', false ),
  23.                 __( 'Dropdown', 'tribe-events-filter-view' )
  24.             ),
  25.             sprintf( '<label><input type="radio" name="%s" value="checkbox" %s /> %s</label>',
  26.                 $name,
  27.                 checked( $this->type, 'checkbox', false ),
  28.                 __( 'Checkboxes', 'tribe-events-filter-view' )
  29.             )
  30.         );
  31.         return '<div class="tribe_events_active_filter_type_options">'.$field.'</div>';
  32.     }
  33.  
  34.     protected function get_values() {
  35.         $event_categories = array();
  36.         $event_categories_term = get_terms( TribeEvents::TAXONOMY, array( 'orderby' => 'name', 'order' => 'DESC' ) );
  37.         $event_categories_by_id = array();
  38.         foreach( $event_categories_term as $term ) {
  39.             $event_categories_by_id[$term->term_id] = $term;
  40.         }
  41.         $event_categories_by_id_reverse = array_reverse( $event_categories_by_id );
  42.         $parents = array( '0' );
  43.         while ( !empty( $parents ) ) {
  44.             $parents_copy = $parents;
  45.             foreach ( $event_categories_by_id_reverse as $term ) {
  46.                 if ( in_array( $term->parent, $parents_copy ) ) {
  47.                     $parents[] = $term->term_id;
  48.                     unset( $event_categories_by_id[$term->term_id] );
  49.                     $event_categories_by_id = TribeEvents::array_insert_after_key( $term->parent, $event_categories_by_id, array( $term->term_id => $term ) );
  50.                 }
  51.             }
  52.             $parents = array_diff( $parents, $parents_copy );
  53.         }
  54.         $child_spacer = '&nbsp;&nbsp;';
  55.         foreach( $event_categories_by_id as $cat ) {
  56.             $child_depth = 0;
  57.             $parent_id = $cat->parent;
  58.             while ( $parent_id != 0 ) {
  59.                 $child_depth++;
  60.                 $parent_id = $event_categories_by_id[$parent_id]->parent;
  61.             }
  62.             $child_indent = str_repeat($child_spacer, $child_depth);
  63.             $event_categories[] = array(
  64.                 'name' => $child_indent . $cat->name,
  65.                 'value' => $cat->term_id,
  66.                 'data' => array(
  67.                     'slug' => $cat->slug,
  68.                 ),
  69.             );
  70.         }
  71.         return $event_categories;
  72.     }
  73.  
  74.     protected function setup_query_args() {
  75.         $this->queryArgs = array( 'tax_query' => array( array(
  76.             'taxonomy' => TribeEvents::TAXONOMY,
  77.             'field' => 'id',
  78.             'terms' => $this->currentValue,
  79.             'include_children' => false,
  80.         ) ) );
  81.     }
  82.  
  83. }
  84.  
  85. // This adds our new filter to the Filterbar options
  86. // Invokes TribeEventsFilter::__construct($name, $slug);
  87. $taxonomy_example = new TribeEventsFilter_Custom('Taxonomy', 'taxonomy_filter');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement