Changeset 7770

Show
Ignore:
Timestamp:
04/22/08 21:06:00 (7 months ago)
Author:
ryan
Message:

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

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/2.5/wp-admin/edit-form-advanced.php

    r7762 r7770  
    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> 
  • branches/2.5/wp-admin/edit-link-form.php

    r7385 r7770  
    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> 
  • 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); 
  • branches/2.5/wp-admin/post.php

    r7760 r7770  
    3636    } elseif (!empty($referredby) && $referredby != $referer) { 
    3737        $location = $_POST['referredby']; 
     38        $location = remove_query_arg('_wp_original_http_referer', $location); 
    3839        if ( $_POST['referredby'] == 'redo' ) 
    3940            $location = get_permalink( $post_ID ); 
  • branches/2.5/wp-includes/classes.php

    r7551 r7770  
    396396 
    397397    //abstract callbacks 
    398     function start_lvl($output) { return $output;
    399     function end_lvl($output)   { return $output;
    400     function start_el($output)  { return $output;
    401     function end_el($output)    { return $output;
     398    function start_lvl(&$output) {
     399    function end_lvl(&$output)   {
     400    function start_el(&$output)  {
     401    function end_el(&$output)    {
    402402 
    403403    /* 
     
    405405     * otherwise, display the element and its children 
    406406     */ 
    407     function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, $output ) { 
    408  
    409         if ( !$element
    410             return $output
     407    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) { 
     408 
     409        if ( !$element
     410            return
    411411 
    412412        $id_field = $this->db_fields['id']; 
     
    414414 
    415415        //display this element 
    416         $cb_args = array_merge( array($output, $element, $depth), $args); 
    417         $output = call_user_func_array(array(&$this, 'start_el'), $cb_args); 
     416        $cb_args = array_merge( array(&$output, $element, $depth), $args); 
     417        call_user_func_array(array(&$this, 'start_el'), $cb_args); 
    418418 
    419419        if ( $max_depth == 0 || 
     
    428428                        $newlevel = true; 
    429429                        //start the child delimiter 
    430                         $cb_args = array_merge( array($output, $depth), $args); 
    431                         $output = call_user_func_array(array(&$this, 'start_lvl'), $cb_args); 
     430                        $cb_args = array_merge( array(&$output, $depth), $args); 
     431                        call_user_func_array(array(&$this, 'start_lvl'), $cb_args); 
    432432                    } 
    433433 
    434434                    array_splice( $children_elements, $i, 1 ); 
    435                     $output = $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); 
     435                    $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output ); 
    436436                    $i = -1; 
    437437                } 
     
    441441        if ( isset($newlevel) && $newlevel ){ 
    442442            //end the child delimiter 
    443             $cb_args = array_merge( array($output, $depth), $args); 
    444             $output = call_user_func_array(array(&$this, 'end_lvl'), $cb_args); 
     443            $cb_args = array_merge( array(&$output, $depth), $args); 
     444            call_user_func_array(array(&$this, 'end_lvl'), $cb_args); 
    445445        } 
    446446 
    447447        //end this element 
    448         $cb_args = array_merge( array($output, $element, $depth), $args); 
    449         $output = call_user_func_array(array(&$this, 'end_el'), $cb_args); 
    450  
    451         return $output; 
     448        $cb_args = array_merge( array(&$output, $element, $depth), $args); 
     449        call_user_func_array(array(&$this, 'end_el'), $cb_args); 
    452450    } 
    453451 
     
    477475            $empty_array = array(); 
    478476            foreach ( $elements as $e ) 
    479                 $output = $this->display_element( $e, $empty_array, 1, 0, $args, $output ); 
     477                $this->display_element( $e, $empty_array, 1, 0, $args, $output ); 
    480478            return $output; 
    481479        } 
     
    513511 
    514512        foreach ( $top_level_elements as $e ) 
    515             $output = $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); 
     513            $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output ); 
    516514 
    517515        /* 
     
    522520            $empty_array = array(); 
    523521            foreach ( $children_elements as $orphan_e ) 
    524                 $output = $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output ); 
     522                $this->display_element( $orphan_e, $empty_array, 1, 0, $args, $output ); 
    525523         } 
    526524         return $output; 
     
    532530    var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this 
    533531 
    534     function start_lvl($output, $depth) { 
     532    function start_lvl(&$output, $depth) { 
    535533        $indent = str_repeat("\t", $depth); 
    536534        $output .= "\n$indent<ul>\n"; 
    537         return $output; 
    538     } 
    539  
    540     function end_lvl($output, $depth) { 
     535    } 
     536 
     537    function end_lvl(&$output, $depth) { 
    541538        $indent = str_repeat("\t", $depth); 
    542539        $output .= "$indent</ul>\n"; 
    543         return $output; 
    544     } 
    545  
    546     function start_el($output, $page, $depth, $current_page, $args) { 
     540    } 
     541 
     542    function start_el(&$output, $page, $depth, $current_page, $args) { 
    547543        if ( $depth ) 
    548544            $indent = str_repeat("\t", $depth); 
     
    572568            $output .= " " . mysql2date($date_format, $time); 
    573569        } 
    574  
    575         return $output; 
    576     } 
    577  
    578     function end_el($output, $page, $depth) { 
     570    } 
     571 
     572    function end_el(&$output, $page, $depth) { 
    579573        $output .= "</li>\n"; 
    580  
    581         return $output; 
    582574    } 
    583575 
     
    588580    var $db_fields = array ('parent' => 'post_parent', 'id' => 'ID'); //TODO: decouple this 
    589581 
    590     function start_el($output, $page, $depth, $args) { 
    591                 $pad = str_repeat('&nbsp;', $depth * 3); 
    592  
    593                 $output .= "\t<option value=\"$page->ID\""; 
    594                 if ( $page->ID == $args['selected'] ) 
    595                                 $output .= ' selected="selected"'; 
    596                 $output .= '>'; 
    597                 $title = wp_specialchars($page->post_title); 
    598                 $output .= "$pad$title"; 
    599                 $output .= "</option>\n"; 
    600  
    601                 return $output; 
     582    function start_el(&$output, $page, $depth, $args) { 
     583        $pad = str_repeat('&nbsp;', $depth * 3); 
     584 
     585        $output .= "\t<option value=\"$page->ID\""; 
     586        if ( $page->ID == $args['selected'] ) 
     587            $output .= ' selected="selected"'; 
     588        $output .= '>'; 
     589        $title = wp_specialchars($page->post_title); 
     590        $output .= "$pad$title"; 
     591        $output .= "</option>\n"; 
    602592    } 
    603593} 
     
    607597    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this 
    608598 
    609     function start_lvl($output, $depth, $args) { 
     599    function start_lvl(&$output, $depth, $args) { 
    610600        if ( 'list' != $args['style'] ) 
    611             return $output
     601            return
    612602 
    613603        $indent = str_repeat("\t", $depth); 
    614604        $output .= "$indent<ul class='children'>\n"; 
    615         return $output; 
    616     } 
    617  
    618     function end_lvl($output, $depth, $args) { 
     605    } 
     606 
     607    function end_lvl(&$output, $depth, $args) { 
    619608        if ( 'list' != $args['style'] ) 
    620             return $output
     609            return
    621610 
    622611        $indent = str_repeat("\t", $depth); 
    623612        $output .= "$indent</ul>\n"; 
    624         return $output; 
    625     } 
    626  
    627     function start_el($output, $category, $depth, $args) { 
     613    } 
     614 
     615    function start_el(&$output, $category, $depth, $args) { 
    628616        extract($args); 
    629617 
     
    688676            $output .= "\t$link<br />\n"; 
    689677        } 
    690  
    691         return $output; 
    692     } 
    693  
    694     function end_el($output, $page, $depth, $args) { 
     678    } 
     679 
     680    function end_el(&$output, $page, $depth, $args) { 
    695681        if ( 'list' != $args['style'] ) 
    696             return $output
     682            return
    697683 
    698684        $output .= "</li>\n"; 
    699         return $output; 
    700685    } 
    701686 
     
    706691    var $db_fields = array ('parent' => 'parent', 'id' => 'term_id'); //TODO: decouple this 
    707692 
    708     function start_el($output, $category, $depth, $args) { 
     693    function start_el(&$output, $category, $depth, $args) { 
    709694        $pad = str_repeat('&nbsp;', $depth * 3); 
    710695 
     
    722707        } 
    723708        $output .= "</option>\n"; 
    724  
    725         return $output; 
    726709    } 
    727710}