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