Advertisement
manchumahara

Wordpress checklist for taxonomy

Aug 2nd, 2012
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.34 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_CBChecklist')){
  12. class Walker_Category_CBChecklist 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 nonbulletlist'>\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="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>';
  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. /*
  63. if(!function_exists('wp_category_checklist')){
  64. function wp_category_checklist( $post_id = 0, $descendants_and_self = 0, $selected_cats = false, $popular_cats = false, $walker = null, $checked_ontop = true ) {
  65.     wp_terms_checklist($post_id,
  66.         array(
  67.             'taxonomy' => 'category',
  68.             'descendants_and_self' => $descendants_and_self,
  69.             'selected_cats' => $selected_cats,
  70.             'popular_cats' => $popular_cats,
  71.             'walker' => $walker,
  72.             'checked_ontop' => $checked_ontop
  73.   ));
  74. }
  75. }
  76. */
  77. /**
  78.  * Taxonomy independent version of wp_category_checklist
  79.  *
  80.  * @since 3.0.0
  81.  *
  82.  * @param int $post_id
  83.  * @param array $args
  84.  */
  85. if(!function_exists('wp_terms_cbchecklist')){
  86. function wp_terms_cbchecklist($post_id = 0, $args = array()) {
  87.     $defaults = array(
  88.         'descendants_and_self' => 0,
  89.         'selected_cats' => false,
  90.         'popular_cats' => false,
  91.         'walker' => null,
  92.         'taxonomy' => 'category',
  93.         'checked_ontop' => false
  94.     );
  95.     extract( wp_parse_args($args, $defaults), EXTR_SKIP );
  96.  
  97.     if ( empty($walker) || !is_a($walker, 'Walker') )
  98.         $walker = new Walker_Category_CBChecklist;
  99.  
  100.     $descendants_and_self = (int) $descendants_and_self;
  101.  
  102.     $args = array('taxonomy' => $taxonomy);
  103.  
  104.     $tax = get_taxonomy($taxonomy);
  105.     $args['disabled'] = !current_user_can($tax->cap->assign_terms);
  106.  
  107.     if ( is_array( $selected_cats ) )
  108.         $args['selected_cats'] = $selected_cats;
  109.     elseif ( $post_id )
  110.         $args['selected_cats'] = wp_get_object_terms($post_id, $taxonomy, array_merge($args, array('fields' => 'ids')));
  111.     else
  112.         $args['selected_cats'] = array();
  113.  
  114.     if ( is_array( $popular_cats ) )
  115.         $args['popular_cats'] = $popular_cats;
  116.     else
  117.         $args['popular_cats'] = get_terms( $taxonomy, array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10, 'hierarchical' => false ) );
  118.  
  119.     if ( $descendants_and_self ) {
  120.         $categories = (array) get_terms($taxonomy, array( 'child_of' => $descendants_and_self, 'hierarchical' => 0, 'hide_empty' => 0 ) );
  121.         $self = get_term( $descendants_and_self, $taxonomy );
  122.         array_unshift( $categories, $self );
  123.     } else {
  124.         $categories = (array) get_terms($taxonomy, array('get' => 'all'));
  125.     }
  126.  
  127.     if ( $checked_ontop ) {
  128.         // 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)
  129.         $checked_categories = array();
  130.         $keys = array_keys( $categories );
  131.  
  132.         foreach( $keys as $k ) {
  133.             if ( in_array( $categories[$k]->term_id, $args['selected_cats'] ) ) {
  134.                 $checked_categories[] = $categories[$k];
  135.                 unset( $categories[$k] );
  136.             }
  137.         }
  138.  
  139.         // Put checked cats on top
  140.         echo call_user_func_array(array(&$walker, 'walk'), array($checked_categories, 0, $args));
  141.     }
  142.     // Then the rest of them
  143.     echo call_user_func_array(array(&$walker, 'walk'), array($categories, 0, $args));
  144. }
  145. }
  146.  
  147. /**
  148.  * Display or retrieve the HTML dropdown list of categories.
  149.  *
  150.  * The list of arguments is below:
  151.  *     'show_option_all' (string) - Text to display for showing all categories.
  152.  *     'show_option_none' (string) - Text to display for showing no categories.
  153.  *     'orderby' (string) default is 'ID' - What column to use for ordering the
  154.  * categories.
  155.  *     'order' (string) default is 'ASC' - What direction to order categories.
  156.  *     'show_last_update' (bool|int) default is 0 - See {@link get_categories()}
  157.  *     'show_count' (bool|int) default is 0 - Whether to show how many posts are
  158.  * in the category.
  159.  *     'hide_empty' (bool|int) default is 1 - Whether to hide categories that
  160.  * don't have any posts attached to them.
  161.  *     'child_of' (int) default is 0 - See {@link get_categories()}.
  162.  *     'exclude' (string) - See {@link get_categories()}.
  163.  *     'echo' (bool|int) default is 1 - Whether to display or retrieve content.
  164.  *     'depth' (int) - The max depth.
  165.  *     'tab_index' (int) - Tab index for select element.
  166.  *     'name' (string) - The name attribute value for select element.
  167.  *     'id' (string) - The ID attribute value for select element. Defaults to name if omitted.
  168.  *     'class' (string) - The class attribute value for select element.
  169.  *     'selected' (int) - Which category ID is selected.
  170.  *     'taxonomy' (string) - The name of the taxonomy to retrieve. Defaults to category.
  171.  *
  172.  * The 'hierarchical' argument, which is disabled by default, will override the
  173.  * depth argument, unless it is true. When the argument is false, it will
  174.  * display all of the categories. When it is enabled it will use the value in
  175.  * the 'depth' argument.
  176.  *
  177.  * @since 2.1.0
  178.  *
  179.  * @param string|array $args Optional. Override default arguments.
  180.  * @return string HTML content only if 'echo' argument is 0.
  181.  */
  182. function wp_terms_cbchecklist_retrieve( $args = '' ) {
  183.     $defaults = array(
  184.         'show_option_all' => '', 'show_option_none' => '',
  185.         'orderby' => 'id', 'order' => 'ASC',
  186.         'show_last_update' => 0, 'show_count' => 0,
  187.         'hide_empty' => 0, 'child_of' => 0,
  188.         'exclude' => '', 'echo' => 1,
  189.         'selected' => 0, 'hierarchical' => 0,
  190.         'name' => 'cat', 'id' => '',
  191.         'class' => 'postform', 'depth' => 0,
  192.         'tab_index' => 0, 'taxonomy' => 'category',
  193.         'hide_if_empty' => false
  194.     );
  195.  
  196.     $defaults['selected'] = ( is_category() ) ? get_query_var( 'cat' ) : 0;
  197.  
  198.     // Back compat.
  199.     if ( isset( $args['type'] ) && 'link' == $args['type'] ) {
  200.         _deprecated_argument( __FUNCTION__, '3.0', '' );
  201.         $args['taxonomy'] = 'link_category';
  202.     }
  203.  
  204.     $r = wp_parse_args( $args, $defaults );
  205.  
  206.     if ( !isset( $r['pad_counts'] ) && $r['show_count'] && $r['hierarchical'] ) {
  207.         $r['pad_counts'] = true;
  208.     }
  209.  
  210.     $r['include_last_update_time'] = $r['show_last_update'];
  211.     extract( $r );
  212.  
  213.     $tab_index_attribute = '';
  214.     if ( (int) $tab_index > 0 )
  215.         $tab_index_attribute = " tabindex=\"$tab_index\"";
  216.  
  217.     $categories = get_terms( $taxonomy, $r );
  218.     $name = esc_attr( $name );
  219.     $class = esc_attr( $class );
  220.     $id = $id ? esc_attr( $id ) : $name;
  221.  
  222.     if ( ! $r['hide_if_empty'] || ! empty($categories) )                
  223.         //$output = "<select name='$name' id='$id' class='$class' $tab_index_attribute>\n";
  224.                 //$output = '<select name="'.$name.'[]" id="'.$id.'" class="'.$class.'" '.$tab_index_attribute.'>';
  225.                 $output = '<ul class="'.$class.'" id="'.$id.'">';
  226.     else
  227.         $output = '';
  228.  
  229.        
  230.         /*
  231.     if ( empty($categories) && ! $r['hide_if_empty'] && !empty($show_option_none) ) {
  232.         $show_option_none = apply_filters( 'list_cats', $show_option_none );
  233.         $output .= "\t<option value='-1' selected='selected'>$show_option_none</option>\n";
  234.     }
  235.         */
  236.         //var_dump($r['selected']);
  237.     $cbselect = $r['selected'];
  238.         if(!is_array($cbselect)){
  239.             $cbselect = array();
  240.         }
  241.         //var_dump($cbselect[0]);
  242.         //$r['selected'] = $cbselect[0];
  243.        
  244.         if ( ! empty( $categories ) ) {
  245.                 /*
  246.         if ( $show_option_all ) {
  247.             $show_option_all = apply_filters( 'list_cats', $show_option_all );
  248.             $selected = ( '0' === strval($r['selected']) ) ? " selected='selected'" : '';
  249.             $output .= "\t<option value='0'$selected>$show_option_all</option>\n";
  250.         }
  251.                 */
  252.                 /*
  253.         if ( $show_option_none ) {
  254.             $show_option_none = apply_filters( 'list_cats', $show_option_none );
  255.             $selected = ( '-1' === strval($r['selected']) ) ? " selected='selected'" : '';
  256.             $output .= "\t<option value='-1'$selected>$show_option_none</option>\n";
  257.         }
  258.                 */
  259.                 /*
  260.         if ( $hierarchical )
  261.             $depth = $r['depth'];  // Walk the full depth.
  262.         else
  263.             $depth = -1; // Flat.
  264.                 */
  265.                 //print_r($categories);
  266.                 foreach($categories as $ct){
  267.                   $output .= "\n<li id='{$taxonomy}-{$ct->term_id}' class='$class'>" . '<label class="selectit"><input value="' . $ct->term_id . '" type="checkbox" name="'.$name.'[]" id="in-'.$taxonomy.'-' . $ct->term_id . '"' . checked( in_array( $ct->term_id, $cbselect ), true, false ) . disabled( empty( $args['disabled'] ), false, false ) . ' /> ' . esc_html( apply_filters('the_category', $ct->name )) . '</label>';
  268.                 }
  269.         //$output .= walk_category_dropdown_tree( $categories, $depth, $r );
  270.     }
  271.     if ( ! $r['hide_if_empty'] || ! empty($categories) )
  272.         $output .= "</ul>\n";
  273.  
  274.  
  275.     $output = apply_filters( 'wp_dropdown_cats', $output );
  276.  
  277.     if ( $echo )
  278.         echo $output;
  279.  
  280.     return $output;
  281. }
  282.  
  283. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement