WordPress.org

Make WordPress Core

Changeset 7737


Ignore:
Timestamp:
04/18/2008 05:41:10 PM (10 years ago)
Author:
ryan
Message:

wp_category_checklist() and wp_link_category_checklist() to replace the nested cats code. see #2621

Location:
trunk/wp-admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/edit-form-advanced.php

    r7720 r7737  
    242242<div id="categories-all" class="ui-tabs-panel">
    243243    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
    244         <?php dropdown_categories( 0, 0, $popular_ids ); ?>
     244        <?php wp_category_checklist($post->ID) ?>
    245245    </ul>
    246246</div>
  • trunk/wp-admin/edit-link-form.php

    r7385 r7737  
    119119<div id="categories-all" class="ui-tabs-panel">
    120120    <ul id="categorychecklist" class="list:category categorychecklist form-no-clear">
    121         <?php dropdown_link_categories(); ?>
     121        <?php wp_link_category_checklist($link_id); ?>
    122122    </ul>
    123123</div>
  • trunk/wp-admin/includes/template.php

    r7732 r7737  
    114114
    115115//
    116 // Nasty Category Stuff
     116// Category Checklists
    117117//
    118118
    119 function sort_cats( $cat1, $cat2 ) {
    120     if ( $cat1['checked'] || $cat2['checked'] )
    121         return ( $cat1['checked'] && !$cat2['checked'] ) ? -1 : 1;
    122     else
    123         return strcasecmp( $cat1['cat_name'], $cat2['cat_name'] );
    124 }
    125 
    126 function wp_set_checked_post_categories( $default = 0 ) {
    127     global $post_ID, $checked_categories;
    128 
    129     if ( empty($checked_categories) ) {
    130         if ( $post_ID ) {
    131             $checked_categories = wp_get_post_categories($post_ID);
    132 
    133             if ( count( $checked_categories ) == 0 ) {
    134                 // No selected categories, strange
    135             $checked_categories[] = $default;
    136             }
    137         } else {
    138             $checked_categories[] = $default;
    139         }
    140     }
    141 
    142 }
    143 function get_nested_categories( $default = 0, $parent = 0 ) {
    144     global $checked_categories;
    145 
    146     wp_set_checked_post_categories( $default = 0 );
    147 
    148     if ( is_object($parent) ) { // Hack: if passed a category object, will return nested cats with parent as root
    149         $root = array(
    150             'children' => get_nested_categories( $default, $parent->term_id ),
    151             'cat_ID' => $parent->term_id,
    152             'checked' => in_array( $parent->term_id, $checked_categories ),
    153             'cat_name' => get_the_category_by_ID( $parent->term_id )
    154         );
    155         $result = array( $parent->term_id => $root );
    156     } else {
    157         $parent = (int) $parent;
    158 
    159         $cats = get_categories("parent=$parent&hide_empty=0&fields=ids");
    160 
    161         $result = array();
    162         if ( is_array( $cats ) ) {
    163             foreach ( $cats as $cat ) {
    164                 $result[$cat]['children'] = get_nested_categories( $default, $cat );
    165                 $result[$cat]['cat_ID'] = $cat;
    166                 $result[$cat]['checked'] = in_array( $cat, $checked_categories );
    167                 $result[$cat]['cat_name'] = get_the_category_by_ID( $cat );
    168             }
    169         }
    170     }
    171 
    172     $result = apply_filters('get_nested_categories', $result);
    173     usort( $result, 'sort_cats' );
    174 
    175     return $result;
    176 }
    177 
    178 function write_nested_categories( $categories, $popular_ids = array() ) {
    179     foreach ( $categories as $category ) {
    180         $class = in_array( $category['cat_ID'], $popular_ids ) ? ' class="popular-category"' : '';
    181         echo "\n", "<li id='category-$category[cat_ID]'$class>", '<label for="in-category-', $category['cat_ID'], '" class="selectit"><input value="', $category['cat_ID'], '" type="checkbox" name="post_category[]" id="in-category-', $category['cat_ID'], '"', ($category['checked'] ? ' checked="checked"' : "" ), '/> ', wp_specialchars( apply_filters('the_category', $category['cat_name'] )), '</label>';
    182 
    183         if ( $category['children'] ) {
    184             echo "\n<ul>";
    185             write_nested_categories( $category['children'] );
    186             echo "\n</ul>";
    187         }
    188         echo '</li>';
    189     }
    190 }
    191 
     119// Deprecated. Use wp_link_category_checklist
    192120function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
    193     write_nested_categories( get_nested_categories( $default, $parent ), $popular_ids );
     121    global $post_ID;
     122    wp_category_checklist($post_ID);
     123}
     124
     125class Walker_Category_Checklist extends Walker {
     126    var $tree_type = 'category';
     127    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this
     128
     129    function start_lvl($output, $depth, $args) {
     130        $indent = str_repeat("\t", $depth);
     131        $output .= "$indent<ul class='children'>\n";
     132        return $output;
     133    }
     134
     135    function end_lvl($output, $depth, $args) {
     136        $indent = str_repeat("\t", $depth);
     137        $output .= "$indent</ul>\n";
     138        return $output;
     139    }
     140
     141    function start_el($output, $category, $depth, $args) {
     142        extract($args);
     143
     144        $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
     145        $output .= "\n<li id='category-$category->term_id'$class>" . '<label for="in-category-' . $category->term_id . '" class="selectit"><input value="' . $category->term_id . '" type="checkbox" name="post_category[]" id="in-category-' . $category->term_id . '"' . (in_array( $category->term_id, $selected_cats ) ? ' checked="checked"' : "" ) . '/> ' . wp_specialchars( apply_filters('the_category', $category->name )) . '</label>';
     146
     147        return $output;
     148    }
     149
     150    function end_el($output, $category, $depth, $args) {
     151        if ( 'list' != $args['style'] )
     152            return $output;
     153
     154        $output .= "</li>\n";
     155        return $output;
     156    }
     157}
     158
     159function wp_category_checklist( $post_id ) {
     160    $walker = new Walker_Category_Checklist;
     161
     162    $args = array();
     163    $args['selected_cats'] = wp_get_post_categories($post_id);
     164    $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10 ) );
     165    $categories = get_categories('get=all');
     166    $args = array($categories, 0, $args);
     167    $output = call_user_func_array(array(&$walker, 'walk'), $args);
     168
     169    echo $output;
    194170}
    195171
     
    215191}
    216192
     193// Deprecated. Use wp_link_category_checklist
    217194function dropdown_link_categories( $default = 0 ) {
    218195    global $link_id;
    219196
     197    wp_link_category_checklist($link_id);
     198}
     199
     200function wp_link_category_checklist( $link_id = 0 ) {
    220201    if ( $link_id ) {
    221202        $checked_categories = wp_get_link_cats($link_id);
Note: See TracChangeset for help on using the changeset viewer.