Advertisement
manchumahara

Wp Taxonomy dropdown List

Aug 2nd, 2012
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.17 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_Dropdownlist')){
  12. class Walker_Category_Dropdownlist 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<optgroup 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</optgroup>\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<option id='{$taxonomy}-{$category->term_id}'$class " . ' value="' . $category->term_id . '"  ' . selected( in_array( $category->term_id, $selected_cats ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $category->name )) . '';
  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 .= "</option>\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_dropdownlist')){
  63. function wp_category_dropdownlist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
  64.         wp_terms_dropdownlist($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_dropdownlist')){
  84. function wp_terms_dropdownlist($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_Dropdownlist;
  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.  
  145.  
  146. /**
  147.  * Display or retrieve the HTML dropdown list of categories.
  148.  *
  149.  * The list of arguments is below:
  150.  *     'show_option_all' (string) - Text to display for showing all categories.
  151.  *     'show_option_none' (string) - Text to display for showing no categories.
  152.  *     'orderby' (string) default is 'ID' - What column to use for ordering the
  153.  * categories.
  154.  *     'order' (string) default is 'ASC' - What direction to order categories.
  155.  *     'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
  156.  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
  157.  * in the category.
  158.  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
  159.  * don't have any posts attached to them.
  160.  *     'child_of' (int) default is 0 - See {@link get_categories()}.
  161.  *     'exclude' (string) - See {@link get_categories()}.
  162.  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
  163.  *     'depth' (int) - The max depth.
  164.  *     'tab_index' (int) - Tab index for select element.
  165.  *     'name' (string) - The name attribute value for select element.
  166.  *     'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
  167.  *     'class' (string) - The class attribute value for select element.
  168.  *     'selected' (int) - Which category ID is selected.
  169.  *     'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
  170.  *
  171.  * The 'hierarchical' argument, which is disabled by default, will override the
  172.  * depth argument, unless it is true. When the argument is false, it will
  173.  * display all of the categories. When it is enabled it will use the value in
  174.  * the 'depth' argument.
  175.  *
  176.  * @since 2.1.0
  177.  *
  178.  * @param string|array $args Optional. Override default arguments.
  179.  * @return string HTML content only if 'echo' argument is 0.
  180.  */
  181. function wp_terms_dropdownlist_retrieve( $args = '' ) {
  182.     $defaults = array(
  183.         'show_option_all' => '', 'show_option_none' => '',
  184.         'orderby' => 'id', 'order' => 'ASC',
  185.         'show_last_update' => 0, 'show_count' => 0,
  186.         'hide_empty' => 0, 'child_of' => 0,
  187.         'exclude' => '', 'echo' => 1,
  188.         'selected' => 0, 'hierarchical' => 0,
  189.         'name' => 'cat', 'id' => '',
  190.         'class' => 'postform', 'depth' => 0,
  191.         'tab_index' => 0, 'taxonomy' => 'category',
  192.         'hide_if_empty' => false
  193.     );
  194.  
  195.     $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
  196.  
  197.     // Back compat.
  198.     if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
  199.         _deprecated_argument( __FUNCTION__, '3.0', '' );
  200.         $args['taxonomy'] = 'link_category';
  201.     }
  202.  
  203.     $r = wp_parse_args( $args, $defaults );
  204.  
  205.     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  206.         $r['pad_counts'] = true;
  207.     }
  208.  
  209.     $r['include_last_update_time'] = $r['show_last_update'];
  210.     extract( $r );
  211.  
  212.     $tab_index_attribute = '';
  213.     if ( (int) $tab_index > 0 )
  214.         $tab_index_attribute = " tabindex=\"$tab_index\"";
  215.  
  216.     $categories = get_terms( $taxonomy, $r );
  217.     $name = esc_attr( $name );
  218.     $class = esc_attr( $class );
  219.     $id = $id ? esc_attr( $id ) : $name;
  220.  
  221.     if ( ! $r['hide_if_empty'] || ! empty($categories) )                
  222.         //$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
  223.                 $output = '<select name="'.$name.'[]" id="'.$id.'" class="'.$class.'" '.$tab_index_attribute.'>';
  224.     else
  225.         $output = '';
  226.  
  227.        
  228.         /*
  229.     if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
  230.         $show_option_none = apply_filters( 'list_cats', $show_option_none );
  231.         $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
  232.     }
  233.         */
  234.         //var_dump($r['selected']);
  235.     $cbselect = $r['selected'];
  236.         //var_dump($cbselect[0]);
  237.         $r['selected'] = $cbselect[0];
  238.        
  239.         if ( ! empty( $categories ) ) {
  240.                 /*
  241.         if ( $show_option_all ) {
  242.             $show_option_all = apply_filters( 'list_cats', $show_option_all );
  243.             $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
  244.             $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
  245.         }                                
  246.         if ( $show_option_none ) {
  247.                        
  248.             $show_option_none = apply_filters( 'list_cats', $show_option_none );
  249.             $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
  250.             $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
  251.                        
  252.                         //$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>';
  253.         }
  254.                 */
  255.                
  256.         if ( $hierarchical )
  257.             $depth = $r['depth'];  // Walk the full depth.
  258.         else
  259.             $depth = -1; // Flat.
  260.  
  261.         $output .= walk_category_dropdown_tree( $categories, $depth, $r );
  262.     }
  263.     if ( ! $r['hide_if_empty'] || ! empty($categories) )
  264.         $output .= "</select>\n";
  265.  
  266.  
  267.     $output = apply_filters( 'wp_dropdown_cats', $output );
  268.  
  269.     if ( $echo )
  270.         echo $output;
  271.  
  272.     return $output;
  273. }
  274.  
  275. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement