Advertisement
jpscoggan

Custom Tribe Events filter

Apr 21st, 2014
315
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.49 KB | None | 0 0
  1. /**
  2.  * Class TribeEventsFilter_CustomTag
  3.  */
  4. class TribeEventsFilter_CustomTag extends TribeEventsFilter{
  5.     public $type = 'checkbox';
  6.  
  7.     public function get_admin_form() {
  8.         $title = $this->get_title_field();
  9.         $type = $this->get_type_field();
  10.         return $title.$type;
  11.     }
  12.  
  13.     protected function get_type_field() {
  14.         $name = $this->get_admin_field_name('type');
  15.         $field = sprintf( __( 'Type: %s %s', 'tribe-events-filter-view' ),
  16.             sprintf( '<label><input type="radio" name="%s" value="select" %s /> %s</label>',
  17.                 $name,
  18.                 checked( $this->type, 'select', false ),
  19.                 __( 'Dropdown', 'tribe-events-filter-view' )
  20.             ),
  21.             sprintf( '<label><input type="radio" name="%s" value="checkbox" %s /> %s</label>',
  22.                 $name,
  23.                 checked( $this->type, 'checkbox', false ),
  24.                 __( 'Checkboxes', 'tribe-events-filter-view' )
  25.             )
  26.         );
  27.         return '<div class="tribe_events_active_filter_type_options">'.$field.'</div>';
  28.     }
  29.  
  30.     protected function get_values() {
  31.         $custom_tags = array();
  32.         $custom_tags_term = get_terms( 'custom_tags', array( 'orderby' => 'name', 'order' => 'DESC' ) );
  33.         $custom_tags_by_id = array();
  34.         foreach( $custom_tags_term as $term ) {
  35.             $custom_tags_by_id[$term->term_id] = $term;
  36.         }
  37.         $custom_tags_by_id_reverse = array_reverse( $custom_tags_by_id );
  38.         $parents = array( '0' );
  39.         while ( !empty( $parents ) ) {
  40.             $parents_copy = $parents;
  41.             foreach ( $custom_tags_by_id_reverse as $term ) {
  42.                 if ( in_array( $term->parent, $parents_copy ) ) {
  43.                     $parents[] = $term->term_id;
  44.                     unset( $custom_tags_by_id[$term->term_id] );
  45.                     $custom_tags_by_id = TribeEvents::array_insert_after_key( $term->parent, $custom_tags_by_id, array( $term->term_id => $term ) );
  46.                 }
  47.             }
  48.             $parents = array_diff( $parents, $parents_copy );
  49.         }
  50.         $child_spacer = '&nbsp;&nbsp;';
  51.         foreach( $custom_tags_by_id as $cat ) {
  52.             $child_depth = 0;
  53.             $parent_id = $cat->parent;
  54.             while ( $parent_id != 0 ) {
  55.                 $child_depth++;
  56.                 $parent_id = $custom_tags_by_id[$parent_id]->parent;
  57.             }
  58.             $child_indent = str_repeat($child_spacer, $child_depth);
  59.             $custom_tags[] = array(
  60.                 'name' => $child_indent . $cat->name,
  61.                 'value' => $cat->term_id,
  62.                 'data' => array(
  63.                     'slug' => $cat->slug,
  64.                 ),
  65.             );
  66.         }
  67.         return $custom_tags;
  68.     }
  69.  
  70.     protected function setup_query_args() {
  71.         $this->queryArgs = array( 'tax_query' => array( array(
  72.             'taxonomy' => 'custom_tags',
  73.             'field' => 'id',
  74.             'terms' => $this->currentValue,
  75.             'include_children' => false,
  76.         ) ) );
  77.     }
  78.  
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement