Make WordPress Core

Ticket #3567: 3567.category-template.php.diff

File 3567.category-template.php.diff, 7.9 KB (added by rob1n, 18 years ago)

wp_list_categories() and Walker_Category

  • wp-includes/classes.php

     
    560560        var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
    561561
    562562        function start_lvl($output, $depth, $args) {
    563                 if ( 'list' != $args['style'] )
    564                         return $output;
    565 
    566                 $indent = str_repeat("\t", $depth);
    567                 $output .= "$indent<ul class='children'>\n";
     563                $output .= str_repeat("\t", $depth);
     564               
     565                switch ($args['format']) {
     566                        case 'html':
     567                                $output .= '<ul>';
     568                                break;
     569                }
     570               
     571                $output .= "\n";
     572               
    568573                return $output;
    569574        }
    570575
    571576        function end_lvl($output, $depth, $args) {
    572                 if ( 'list' != $args['style'] )
    573                         return $output;
    574 
    575                 $indent = str_repeat("\t", $depth);
    576                 $output .= "$indent</ul>\n";
     577                $output .= str_repeat("\t", $depth);
     578               
     579                switch ($args['format']) {
     580                        case 'html':
     581                                $output .= '</ul>';
     582                                break;
     583                }
     584               
     585                $output .= "\n";
     586               
    577587                return $output;
    578588        }
    579589
    580590        function start_el($output, $category, $depth, $args) {
    581591                extract($args);
    582 
    583                 $cat_name = attribute_escape( $category->cat_name);
    584                 $cat_name = apply_filters( 'list_cats', $cat_name, $category );
    585                 $link = '<a href="' . get_category_link( $category->cat_ID ) . '" ';
    586                 if ( $use_desc_for_title == 0 || empty($category->category_description) )
    587                         $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
    588                 else
    589                         $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->category_description, $category )) . '"';
    590                 $link .= '>';
    591                 $link .= $cat_name . '</a>';
    592 
    593                 if ( (! empty($feed_image)) || (! empty($feed)) ) {
    594                         $link .= ' ';
    595 
    596                         if ( empty($feed_image) )
    597                                 $link .= '(';
    598 
    599                         $link .= '<a href="' . get_category_rss_link( 0, $category->cat_ID, $category->category_nicename ) . '"';
    600 
    601                         if ( empty($feed) )
    602                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
    603                         else {
    604                                 $title = ' title="' . $feed . '"';
    605                                 $alt = ' alt="' . $feed . '"';
    606                                 $name = $feed;
    607                                 $link .= $title;
     592               
     593                $output .= str_repeat("\t", $depth + 1);
     594               
     595                if ($current_category) {
     596                        $_current_category = get_category($current_category);
     597                       
     598                        if ($_current_category->cat_ID == $category->cat_ID) {
     599                                $current_class = ' class="current-cat"';
     600                        } elseif ($_current_category->category_parent == $category->cat_ID) {
     601                                $current_class = ' class="current-cat-parent"';
    608602                        }
    609 
    610                         $link .= '>';
    611 
    612                         if ( empty($feed_image) )
    613                                 $link .= $name;
    614                         else
    615                                 $link .= "<img src='$feed_image'$alt$title" . ' />';
    616                         $link .= '</a>';
    617                         if ( empty($feed_image) )
    618                                 $link .= ')';
     603               
     604                switch ($format) {
     605                        case 'html':
     606                                $output .= "<li$current_class>";
     607                                break;
    619608                }
    620 
    621                 if ( isset($show_count) && $show_count )
    622                         $link .= ' (' . intval($category->category_count) . ')';
    623 
    624                 if ( isset($show_date) && $show_date ) {
    625                         $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
    626                 }
    627 
    628                 if ( $current_category )
    629                         $_current_category = get_category( $current_category );
    630 
    631                 if ( 'list' == $args['style'] ) {
    632                         $output .= "\t<li";
    633                         if ( $current_category && ($category->cat_ID == $current_category) )
    634                                 $output .=  ' class="current-cat"';
    635                         elseif ( $_current_category && ($category->cat_ID == $_current_category->category_parent) )
    636                                 $output .=  ' class="current-cat-parent"';
    637                         $output .= ">$link\n";
     609               
     610                $output .= $before;
     611               
     612                $output .= '<a href="' . get_category_link($category->cat_ID) . '" ';
     613               
     614                $cat_name = apply_filters('list_cats', $category->cat_name, $category);
     615               
     616                if (!$use_desc_for_title || empty($category->category_description)) {
     617                        $output .= 'title="' . sprintf(__('View all posts filed under %'), attribute_escape($cat_name)) . '"';
    638618                } else {
    639                         $output .= "\t$link<br />\n";
     619                        $output .= 'title="' . attribute_escape(apply_filters('category_description', $category->category_description, $category)) . '"';
    640620                }
    641 
     621               
     622                $output .= '>' . $cat_name . '</a>';
     623               
     624                if (!empty($feed) || !empty($feed_image)) {
     625                        $output .= ' ';
     626                       
     627                        if (empty($feed_image)) {
     628                                $link .= '(<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '" title="' . attribute_escape($feed) . '">' . $feed . '</a>)';
     629                        } else {
     630                                $link .= '<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '" title="' . attribute_escape(sprintf(__('Feed for all posts filed under %s'), $cat_name)) . '"><img src="' . $feed_image . '" alt="' . attribute_escape(sprintf(__('Feed for all posts filed under %s'), $cat_name)) . '" /></a>';
     631                        }
     632                }
     633               
     634                if ($show_count) {
     635                        $output .= ' (' . intval($category->category_count) . ')';
     636                }
     637               
     638                if ($show_last_update) {
     639                        $output .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
     640                }
     641               
     642                $output .= $after;
     643               
     644                switch ($format) {
     645                        case 'html':
     646                                $output .= '</li>';
     647                                break;
     648                }
     649               
     650                $output .= "\n";
     651               
    642652                return $output;
    643653        }
    644 
    645         function end_el($output, $page, $depth, $args) {
    646                 if ( 'list' != $args['style'] )
    647                         return $output;
    648 
    649                 $output .= "</li>\n";
    650                 return $output;
    651         }
    652 
    653654}
    654655
    655656class Walker_CategoryDropdown extends Walker {
  • wp-includes/category-template.php

     
    259259        else
    260260                parse_str($args, $r);
    261261
    262         $defaults = array('show_option_all' => '', 'orderby' => 'name',
    263                 'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list',
    264                 'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1,
    265                 'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '',
    266                 'hierarchical' => true, 'title_li' => __('Categories'));
     262        $defaults = array(
     263                'show_option_all' => '',
     264                'orderby' => 'name',
     265                'order' => 'ASC',
     266                'show_last_update' => false,
     267                'style' => 'list',
     268                'show_count' => false,
     269                'hide_empty' => true,
     270                'use_desc_for_title' => true,
     271                'child_of' => false,
     272                'feed' => '',
     273                'feed_image' => '',
     274                'exclude' => '',
     275                'hierarchical' => true,
     276                'title_li' => __('Categories'),
     277                'before' => '',
     278                'after' => '',
     279                'format' => 'html'
     280        );
     281       
    267282        $r = array_merge($defaults, $r);
     283       
    268284        if ( !isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical'] )
    269285                $r['pad_counts'] = true;
     286       
    270287        if ( isset($r['show_date']) )
    271288                $r['include_last_update_time'] = $r['show_date'];
     289       
     290        // Backwards compatability
     291        if (empty($r['format']) && !empty($r['style'])) {
     292                $r['format'] = $r['style'];
     293               
     294                // 'list' is the old version of 'html'
     295                if ($r['format'] == 'list') {
     296                        $r['format'] = 'html';
     297                }
     298        }
     299       
    272300        extract($r);
    273301
    274302        $categories = get_categories($r);
    275303
    276304        $output = '';
    277         if ( $title_li && 'list' == $style )
    278                         $output = '<li class="categories">' . $r['title_li'] . '<ul>';
     305       
     306        if ($style == 'list' && !empty($title_li)) {
     307                $output .= '<li class="categories">' . $title_li . "\n";
     308        }
    279309
    280310        if ( empty($categories) ) {
    281                 if ( 'list' == $style )
    282                         $output .= '<li>' . __("No categories") . '</li>';
    283                 else
    284                         $output .= __("No categories");
     311                switch ($format) {
     312                        case 'html':
     313                                $output .= "<ul>\n<li>";
     314                                break;
     315                }
     316               
     317                $output .= $before . __('No categories') . $after;
     318               
     319                switch ($format) {
     320                        case 'html':
     321                                $output .= "</li>\n</ul>";
     322                                break;
     323                }
    285324        } else {
    286325                global $wp_query;
    287326
     
    292331                        $depth = 0;  // Walk the full depth.
    293332                else
    294333                        $depth = -1; // Flat.
     334               
     335                switch ($format) {
     336                        case 'html':
     337                                $output .= '<ul>';
     338                                break;
     339                }
     340               
     341                $output .= "\n";
    295342
    296343                $output .= walk_category_tree($categories, $depth, $r);
     344               
     345                switch ($format) {
     346                        case 'html':
     347                                $output .= '</ul>';
     348                                break;
     349                }
     350               
     351                $output .= "\n";
    297352        }
    298353
     354        // Backwards compatability
    299355        if ( $title_li && 'list' == $style )
    300                 $output .= '</ul></li>';
     356                $output .= "</li>\n";
    301357
    302358        echo apply_filters('wp_list_categories', $output);
    303359}