Make WordPress Core


Ignore:
Timestamp:
04/22/2008 09:06:00 PM (17 years ago)
Author:
ryan
Message:

Convert Walker classes to pass as reference. Props mdawaffe. fixes #6796 for 2.5

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.5/wp-admin/includes/template.php

    r7767 r7770  
    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;
     119// Deprecated. Use wp_link_category_checklist
     120function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
     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    }
     133
     134    function end_lvl(&$output, $depth, $args) {
     135        $indent = str_repeat("\t", $depth);
     136        $output .= "$indent</ul>\n";
     137    }
     138
     139    function start_el(&$output, $category, $depth, $args) {
     140        extract($args);
     141
     142        $class = in_array( $category->term_id, $popular_cats ) ? ' class="popular-category"' : '';
     143        $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>';
     144    }
     145
     146    function end_el(&$output, $category, $depth, $args) {
     147        $output .= "</li>\n";
     148    }
     149}
     150
     151function wp_category_checklist( $post_id = 0 ) {
     152    $walker = new Walker_Category_Checklist;
     153
     154    $args = array();
     155   
     156    if ( $post_id )
     157        $args['selected_cats'] = wp_get_post_categories($post_id);
    122158    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 
    192 function dropdown_categories( $default = 0, $parent = 0, $popular_ids = array() ) {
    193     write_nested_categories( get_nested_categories( $default, $parent ), $popular_ids );
     159        $args['selected_cats'] = array();
     160    $args['popular_cats'] = get_terms( 'category', array( 'fields' => 'ids', 'orderby' => 'count', 'order' => 'DESC', 'number' => 10 ) );
     161    $categories = get_categories('get=all');
     162    $args = array($categories, 0, $args);
     163    $output = call_user_func_array(array(&$walker, 'walk'), $args);
     164
     165    echo $output;
    194166}
    195167
     
    215187}
    216188
     189// Deprecated. Use wp_link_category_checklist
    217190function dropdown_link_categories( $default = 0 ) {
    218191    global $link_id;
    219192
     193    wp_link_category_checklist($link_id);
     194}
     195
     196function wp_link_category_checklist( $link_id = 0 ) {
    220197    if ( $link_id ) {
    221198        $checked_categories = wp_get_link_cats($link_id);
Note: See TracChangeset for help on using the changeset viewer.