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

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

wp_list_categories() and Walker_Category

Line 
1Index: wp-includes/classes.php
2===================================================================
3--- wp-includes/classes.php     (revision 5143)
4+++ wp-includes/classes.php     (working copy)
5@@ -560,96 +560,97 @@
6        var $db_fields = array ('parent' => 'category_parent', 'id' => 'cat_ID'); //TODO: decouple this
7 
8        function start_lvl($output, $depth, $args) {
9-               if ( 'list' != $args['style'] )
10-                       return $output;
11-
12-               $indent = str_repeat("\t", $depth);
13-               $output .= "$indent<ul class='children'>\n";
14+               $output .= str_repeat("\t", $depth);
15+               
16+               switch ($args['format']) {
17+                       case 'html':
18+                               $output .= '<ul>';
19+                               break;
20+               }
21+               
22+               $output .= "\n";
23+               
24                return $output;
25        }
26 
27        function end_lvl($output, $depth, $args) {
28-               if ( 'list' != $args['style'] )
29-                       return $output;
30-
31-               $indent = str_repeat("\t", $depth);
32-               $output .= "$indent</ul>\n";
33+               $output .= str_repeat("\t", $depth);
34+               
35+               switch ($args['format']) {
36+                       case 'html':
37+                               $output .= '</ul>';
38+                               break;
39+               }
40+               
41+               $output .= "\n";
42+               
43                return $output;
44        }
45 
46        function start_el($output, $category, $depth, $args) {
47                extract($args);
48-
49-               $cat_name = attribute_escape( $category->cat_name);
50-               $cat_name = apply_filters( 'list_cats', $cat_name, $category );
51-               $link = '<a href="' . get_category_link( $category->cat_ID ) . '" ';
52-               if ( $use_desc_for_title == 0 || empty($category->category_description) )
53-                       $link .= 'title="' . sprintf(__( 'View all posts filed under %s' ), $cat_name) . '"';
54-               else
55-                       $link .= 'title="' . attribute_escape( apply_filters( 'category_description', $category->category_description, $category )) . '"';
56-               $link .= '>';
57-               $link .= $cat_name . '</a>';
58-
59-               if ( (! empty($feed_image)) || (! empty($feed)) ) {
60-                       $link .= ' ';
61-
62-                       if ( empty($feed_image) )
63-                               $link .= '(';
64-
65-                       $link .= '<a href="' . get_category_rss_link( 0, $category->cat_ID, $category->category_nicename ) . '"';
66-
67-                       if ( empty($feed) )
68-                               $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
69-                       else {
70-                               $title = ' title="' . $feed . '"';
71-                               $alt = ' alt="' . $feed . '"';
72-                               $name = $feed;
73-                               $link .= $title;
74+               
75+               $output .= str_repeat("\t", $depth + 1);
76+               
77+               if ($current_category) {
78+                       $_current_category = get_category($current_category);
79+                       
80+                       if ($_current_category->cat_ID == $category->cat_ID) {
81+                               $current_class = ' class="current-cat"';
82+                       } elseif ($_current_category->category_parent == $category->cat_ID) {
83+                               $current_class = ' class="current-cat-parent"';
84                        }
85-
86-                       $link .= '>';
87-
88-                       if ( empty($feed_image) )
89-                               $link .= $name;
90-                       else
91-                               $link .= "<img src='$feed_image'$alt$title" . ' />';
92-                       $link .= '</a>';
93-                       if ( empty($feed_image) )
94-                               $link .= ')';
95+               
96+               switch ($format) {
97+                       case 'html':
98+                               $output .= "<li$current_class>";
99+                               break;
100                }
101-
102-               if ( isset($show_count) && $show_count )
103-                       $link .= ' (' . intval($category->category_count) . ')';
104-
105-               if ( isset($show_date) && $show_date ) {
106-                       $link .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
107-               }
108-
109-               if ( $current_category )
110-                       $_current_category = get_category( $current_category );
111-
112-               if ( 'list' == $args['style'] ) {
113-                       $output .= "\t<li";
114-                       if ( $current_category && ($category->cat_ID == $current_category) )
115-                               $output .=  ' class="current-cat"';
116-                       elseif ( $_current_category && ($category->cat_ID == $_current_category->category_parent) )
117-                               $output .=  ' class="current-cat-parent"';
118-                       $output .= ">$link\n";
119+               
120+               $output .= $before;
121+               
122+               $output .= '<a href="' . get_category_link($category->cat_ID) . '" ';
123+               
124+               $cat_name = apply_filters('list_cats', $category->cat_name, $category);
125+               
126+               if (!$use_desc_for_title || empty($category->category_description)) {
127+                       $output .= 'title="' . sprintf(__('View all posts filed under %'), attribute_escape($cat_name)) . '"';
128                } else {
129-                       $output .= "\t$link<br />\n";
130+                       $output .= 'title="' . attribute_escape(apply_filters('category_description', $category->category_description, $category)) . '"';
131                }
132-
133+               
134+               $output .= '>' . $cat_name . '</a>';
135+               
136+               if (!empty($feed) || !empty($feed_image)) {
137+                       $output .= ' ';
138+                       
139+                       if (empty($feed_image)) {
140+                               $link .= '(<a href="' . get_category_rss_link(0, $category->cat_ID, $category->category_nicename) . '" title="' . attribute_escape($feed) . '">' . $feed . '</a>)';
141+                       } else {
142+                               $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>';
143+                       }
144+               }
145+               
146+               if ($show_count) {
147+                       $output .= ' (' . intval($category->category_count) . ')';
148+               }
149+               
150+               if ($show_last_update) {
151+                       $output .= ' ' . gmdate('Y-m-d', $category->last_update_timestamp);
152+               }
153+               
154+               $output .= $after;
155+               
156+               switch ($format) {
157+                       case 'html':
158+                               $output .= '</li>';
159+                               break;
160+               }
161+               
162+               $output .= "\n";
163+               
164                return $output;
165        }
166-
167-       function end_el($output, $page, $depth, $args) {
168-               if ( 'list' != $args['style'] )
169-                       return $output;
170-
171-               $output .= "</li>\n";
172-               return $output;
173-       }
174-
175 }
176 
177 class Walker_CategoryDropdown extends Walker {
178Index: wp-includes/category-template.php
179===================================================================
180--- wp-includes/category-template.php   (revision 5143)
181+++ wp-includes/category-template.php   (working copy)
182@@ -259,29 +259,68 @@
183        else
184                parse_str($args, $r);
185 
186-       $defaults = array('show_option_all' => '', 'orderby' => 'name',
187-               'order' => 'ASC', 'show_last_update' => 0, 'style' => 'list',
188-               'show_count' => 0, 'hide_empty' => 1, 'use_desc_for_title' => 1,
189-               'child_of' => 0, 'feed' => '', 'feed_image' => '', 'exclude' => '',
190-               'hierarchical' => true, 'title_li' => __('Categories'));
191+       $defaults = array(
192+               'show_option_all' => '',
193+               'orderby' => 'name',
194+               'order' => 'ASC',
195+               'show_last_update' => false,
196+               'style' => 'list',
197+               'show_count' => false,
198+               'hide_empty' => true,
199+               'use_desc_for_title' => true,
200+               'child_of' => false,
201+               'feed' => '',
202+               'feed_image' => '',
203+               'exclude' => '',
204+               'hierarchical' => true,
205+               'title_li' => __('Categories'),
206+               'before' => '',
207+               'after' => '',
208+               'format' => 'html'
209+       );
210+       
211        $r = array_merge($defaults, $r);
212+       
213        if ( !isset($r['pad_counts']) && $r['show_count'] && $r['hierarchical'] )
214                $r['pad_counts'] = true;
215+       
216        if ( isset($r['show_date']) )
217                $r['include_last_update_time'] = $r['show_date'];
218+       
219+       // Backwards compatability
220+       if (empty($r['format']) && !empty($r['style'])) {
221+               $r['format'] = $r['style'];
222+               
223+               // 'list' is the old version of 'html'
224+               if ($r['format'] == 'list') {
225+                       $r['format'] = 'html';
226+               }
227+       }
228+       
229        extract($r);
230 
231        $categories = get_categories($r);
232 
233        $output = '';
234-       if ( $title_li && 'list' == $style )
235-                       $output = '<li class="categories">' . $r['title_li'] . '<ul>';
236+       
237+       if ($style == 'list' && !empty($title_li)) {
238+               $output .= '<li class="categories">' . $title_li . "\n";
239+       }
240 
241        if ( empty($categories) ) {
242-               if ( 'list' == $style )
243-                       $output .= '<li>' . __("No categories") . '</li>';
244-               else
245-                       $output .= __("No categories");
246+               switch ($format) {
247+                       case 'html':
248+                               $output .= "<ul>\n<li>";
249+                               break;
250+               }
251+               
252+               $output .= $before . __('No categories') . $after;
253+               
254+               switch ($format) {
255+                       case 'html':
256+                               $output .= "</li>\n</ul>";
257+                               break;
258+               }
259        } else {
260                global $wp_query;
261 
262@@ -292,12 +331,29 @@
263                        $depth = 0;  // Walk the full depth.
264                else
265                        $depth = -1; // Flat.
266+               
267+               switch ($format) {
268+                       case 'html':
269+                               $output .= '<ul>';
270+                               break;
271+               }
272+               
273+               $output .= "\n";
274 
275                $output .= walk_category_tree($categories, $depth, $r);
276+               
277+               switch ($format) {
278+                       case 'html':
279+                               $output .= '</ul>';
280+                               break;
281+               }
282+               
283+               $output .= "\n";
284        }
285 
286+       // Backwards compatability
287        if ( $title_li && 'list' == $style )
288-               $output .= '</ul></li>';
289+               $output .= "</li>\n";
290 
291        echo apply_filters('wp_list_categories', $output);
292 }