]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/class-walker-category.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-includes / class-walker-category.php
1 <?php
2 /**
3  * Taxonomy API: Walker_Category class
4  *
5  * @package WordPress
6  * @subpackage Template
7  * @since 4.4.0
8  */
9
10 /**
11  * Core class used to create an HTML list of categories.
12  *
13  * @since 2.1.0
14  *
15  * @see Walker
16  */
17 class Walker_Category extends Walker {
18         /**
19          * What the class handles.
20          *
21          * @see Walker::$tree_type
22          * @since 2.1.0
23          * @var string
24          */
25         public $tree_type = 'category';
26
27         /**
28          * Database fields to use.
29          *
30          * @see Walker::$db_fields
31          * @since 2.1.0
32          * @todo Decouple this
33          * @var array
34          */
35         public $db_fields = array ('parent' => 'parent', 'id' => 'term_id');
36
37         /**
38          * Starts the list before the elements are added.
39          *
40          * @see Walker::start_lvl()
41          *
42          * @since 2.1.0
43          *
44          * @param string $output Passed by reference. Used to append additional content.
45          * @param int    $depth  Depth of category. Used for tab indentation.
46          * @param array  $args   An array of arguments. Will only append content if style argument value is 'list'.
47          *                       @see wp_list_categories()
48          */
49         public function start_lvl( &$output, $depth = 0, $args = array() ) {
50                 if ( 'list' != $args['style'] )
51                         return;
52
53                 $indent = str_repeat("\t", $depth);
54                 $output .= "$indent<ul class='children'>\n";
55         }
56
57         /**
58          * Ends the list of after the elements are added.
59          *
60          * @see Walker::end_lvl()
61          *
62          * @since 2.1.0
63          *
64          * @param string $output Passed by reference. Used to append additional content.
65          * @param int    $depth  Depth of category. Used for tab indentation.
66          * @param array  $args   An array of arguments. Will only append content if style argument value is 'list'.
67          *                       @wsee wp_list_categories()
68          */
69         public function end_lvl( &$output, $depth = 0, $args = array() ) {
70                 if ( 'list' != $args['style'] )
71                         return;
72
73                 $indent = str_repeat("\t", $depth);
74                 $output .= "$indent</ul>\n";
75         }
76
77         /**
78          * Start the element output.
79          *
80          * @see Walker::start_el()
81          *
82          * @since 2.1.0
83          *
84          * @param string $output   Passed by reference. Used to append additional content.
85          * @param object $category Category data object.
86          * @param int    $depth    Depth of category in reference to parents. Default 0.
87          * @param array  $args     An array of arguments. @see wp_list_categories()
88          * @param int    $id       ID of the current category.
89          */
90         public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) {
91                 /** This filter is documented in wp-includes/category-template.php */
92                 $cat_name = apply_filters(
93                         'list_cats',
94                         esc_attr( $category->name ),
95                         $category
96                 );
97
98                 // Don't generate an element if the category name is empty.
99                 if ( ! $cat_name ) {
100                         return;
101                 }
102
103                 $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" ';
104                 if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
105                         /**
106                          * Filter the category description for display.
107                          *
108                          * @since 1.2.0
109                          *
110                          * @param string $description Category description.
111                          * @param object $category    Category object.
112                          */
113                         $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"';
114                 }
115
116                 $link .= '>';
117                 $link .= $cat_name . '</a>';
118
119                 if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
120                         $link .= ' ';
121
122                         if ( empty( $args['feed_image'] ) ) {
123                                 $link .= '(';
124                         }
125
126                         $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
127
128                         if ( empty( $args['feed'] ) ) {
129                                 $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
130                         } else {
131                                 $alt = ' alt="' . $args['feed'] . '"';
132                                 $name = $args['feed'];
133                                 $link .= empty( $args['title'] ) ? '' : $args['title'];
134                         }
135
136                         $link .= '>';
137
138                         if ( empty( $args['feed_image'] ) ) {
139                                 $link .= $name;
140                         } else {
141                                 $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />';
142                         }
143                         $link .= '</a>';
144
145                         if ( empty( $args['feed_image'] ) ) {
146                                 $link .= ')';
147                         }
148                 }
149
150                 if ( ! empty( $args['show_count'] ) ) {
151                         $link .= ' (' . number_format_i18n( $category->count ) . ')';
152                 }
153                 if ( 'list' == $args['style'] ) {
154                         $output .= "\t<li";
155                         $css_classes = array(
156                                 'cat-item',
157                                 'cat-item-' . $category->term_id,
158                         );
159
160                         if ( ! empty( $args['current_category'] ) ) {
161                                 // 'current_category' can be an array, so we use `get_terms()`.
162                                 $_current_terms = get_terms( $category->taxonomy, array(
163                                         'include' => $args['current_category'],
164                                         'hide_empty' => false,
165                                 ) );
166
167                                 foreach ( $_current_terms as $_current_term ) {
168                                         if ( $category->term_id == $_current_term->term_id ) {
169                                                 $css_classes[] = 'current-cat';
170                                         } elseif ( $category->term_id == $_current_term->parent ) {
171                                                 $css_classes[] = 'current-cat-parent';
172                                         }
173                                 }
174                         }
175
176                         /**
177                          * Filter the list of CSS classes to include with each category in the list.
178                          *
179                          * @since 4.2.0
180                          *
181                          * @see wp_list_categories()
182                          *
183                          * @param array  $css_classes An array of CSS classes to be applied to each list item.
184                          * @param object $category    Category data object.
185                          * @param int    $depth       Depth of page, used for padding.
186                          * @param array  $args        An array of wp_list_categories() arguments.
187                          */
188                         $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
189
190                         $output .=  ' class="' . $css_classes . '"';
191                         $output .= ">$link\n";
192                 } elseif ( isset( $args['separator'] ) ) {
193                         $output .= "\t$link" . $args['separator'] . "\n";
194                 } else {
195                         $output .= "\t$link<br />\n";
196                 }
197         }
198
199         /**
200          * Ends the element output, if needed.
201          *
202          * @see Walker::end_el()
203          *
204          * @since 2.1.0
205          *
206          * @param string $output Passed by reference. Used to append additional content.
207          * @param object $page   Not used.
208          * @param int    $depth  Depth of category. Not used.
209          * @param array  $args   An array of arguments. Only uses 'list' for whether should append to output. @see wp_list_categories()
210          */
211         public function end_el( &$output, $page, $depth = 0, $args = array() ) {
212                 if ( 'list' != $args['style'] )
213                         return;
214
215                 $output .= "</li>\n";
216         }
217
218 }