Advertisement
manchumahara

Wordpress radio optins for taxonomy

Aug 2nd, 2012
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.97 KB | None | 0 0
  1. <?php
  2. //
  3. // Category Checklists
  4. //
  5.  
  6. /**
  7.  * {@internal Missing Short Description}}
  8.  *
  9.  * @since 2.5.1
  10.  */
  11. if(!class_exists('Walker_Category_Radiolist')){
  12. class Walker_Category_Radiolist extends Walker {
  13.     var $tree_type = 'category';
  14.     var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
  15.  
  16.     function start_lvl(&$output, $depth, $args) {
  17.         $indent = str_repeat("\t", $depth);
  18.         $output .= "$indent<ul class='children'>\n";
  19.                 //$output .= "$indent\n";
  20.     }
  21.  
  22.     function end_lvl(&$output, $depth, $args) {
  23.         $indent = str_repeat("\t", $depth);
  24.         $output .= "$indent</ul>\n";
  25.                 //$output .= "$indent\n";
  26.     }
  27.  
  28.     function start_el(&$output, $category, $depth, $args) {
  29.         extract($args);
  30.         if ( empty($taxonomy) )
  31.             $taxonomy = 'category';
  32.  
  33.         if ( $taxonomy == 'category' )
  34.             $name = 'post_category';
  35.         else
  36.                         $name = $taxonomy;
  37.             //$name = 'tax_input['.$taxonomy.']';
  38.  
  39.         $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
  40.         //$output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
  41.                 $output .= "\n<li id='{$taxonomy}-{$category->term_id}'$class>" . '<label class="selectit"><input value="' . $category->term_id . '" type="radio" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $category->term_id . '"' . checked( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '</label>';
  42.                 //$output .= '<label class="fullsize customcheckboxl" for="'.esc_html( apply_filters('the_category', $category->name )).$category->term_id.'"><input type="checkbox" id="'.esc_html( apply_filters('the_category', $category->name )).$category->term_id.'" class="customcheckbox" name="'.$name.'[]" '.checked( in_array( $category->term_id, $selected_cats ), true, false ).' value="'.$category->term_id.'" /> '.esc_html( apply_filters('the_category', $category->name )).'</label>';
  43.                 //'<label class="fullsize customcheckboxl" for="'.$language_list->cat_name.$language_list->cat_ID.'"><input type="checkbox" id="'.$language_list->cat_name.$language_list->cat_ID.'" class="customcheckbox" name="language[]" '.$check.' value="'.$language_list->cat_ID.'" /> '.$language_list->cat_name.'</label>';
  44.     }
  45.  
  46.     function end_el(&$output, $category, $depth, $args) {
  47.         $output .= "</li>\n";
  48.                 //$output .= "\n";
  49.     }
  50. }
  51. }
  52. /**
  53.  * {@internal Missing Short Description}}
  54.  *
  55.  * @since 2.5.1
  56.  *
  57.  * @param unknown_type $post_id
  58.  * @param unknown_type $descendants_and_self
  59.  * @param unknown_type $selected_cats
  60.  * @param unknown_type $popular_cats
  61.  */
  62. if(!function_exists('wp_category_radiolist')){
  63. function wp_category_radiolist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
  64.         wp_terms_radiolist($post_id,
  65.             array(
  66.                     'taxonomy' => 'category',
  67.                     'descendants_and_self' => $descendants_and_self,
  68.                     'selected_cats' => $selected_cats,
  69.                     'popular_cats' => $popular_cats,
  70.                     'walker' => $walker,
  71.                     'checked_ontop' => $checked_ontop
  72.         ));
  73.     }
  74. }
  75. /**
  76.  * Taxonomy independent version of wp_category_checklist
  77.  *
  78.  * @since 3.0.0
  79.  *
  80.  * @param int $post_id
  81.  * @param array $args
  82.  */
  83. if(!function_exists('wp_terms_radiolist')){
  84. function wp_terms_radiolist($post_id = 0, $args = array()) {
  85.     $defaults = array(
  86.         'descendants_and_self' => 0,
  87.         'selected_cats' => false,
  88.         'popular_cats' => false,
  89.         'walker' => null,
  90.         'taxonomy' => 'category',
  91.         'checked_ontop' => false
  92.     );
  93.     extract( wp_parse_args($args, $defaults), EXTR_SKIP );
  94.  
  95.     if ( empty($walker) || !is_a($walker, 'Walker') )
  96.         $walker = new Walker_Category_Radiolist;
  97.  
  98.     $descendants_and_self = (int) $descendants_and_self;
  99.  
  100.     $args = array('taxonomy' => $taxonomy);
  101.  
  102.     $tax = get_taxonomy($taxonomy);
  103.     $args['disabled'] = !current_user_can($tax->cap->assign_terms);
  104.  
  105.     if ( is_array( $selected_cats ) )
  106.         $args['selected_cats'] = $selected_cats;
  107.     elseif ( $post_id )
  108.         $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
  109.     else
  110.         $args['selected_cats'] = array();
  111.  
  112.     if ( is_array( $popular_cats ) )
  113.         $args['popular_cats'] = $popular_cats;
  114.     else
  115.         $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
  116.  
  117.     if ( $descendants_and_self ) {
  118.         $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
  119.         $self = get_term( $descendants_and_self, $taxonomy );
  120.         array_unshift( $categories, $self );
  121.     } else {
  122.         $categories = (array) get_terms($taxonomy, array('get' => 'all'));
  123.     }
  124.  
  125.     if ( $checked_ontop ) {
  126.         // Post process $categories rather than adding an exclude to the get_terms() query to keep the query the same across all posts (for any query cache)
  127.         $checked_categories = array();
  128.         $keys = array_keys( $categories );
  129.  
  130.         foreach( $keys as $k ) {
  131.             if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
  132.                 $checked_categories[] = $categories[$k];
  133.                 unset( $categories[$k] );
  134.             }
  135.         }
  136.  
  137.         // Put checked cats on top
  138.         echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
  139.     }
  140.     // Then the rest of them
  141.     echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
  142. }
  143. }
  144. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement