/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. * Ends the list of after the elements are added. 
  64. * 
  65. * @since 2.1.0 
  66. * @access public 
  67. * 
  68. * @see Walker::end_lvl() 
  69. * 
  70. * @param string $output Used to append additional content. Passed by reference. 
  71. * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0. 
  72. * @param array $args Optional. An array of arguments. Will only append content if style argument 
  73. * value is 'list'. See wp_list_categories(). Default empty array. 
  74. */ 
  75. public function end_lvl( &$output, $depth = 0, $args = array() ) { 
  76. if ( 'list' != $args['style'] ) 
  77. return; 
  78.  
  79. $indent = str_repeat("\t", $depth); 
  80. $output .= "$indent</ul>\n"; 
  81.  
  82. /** 
  83. * Starts the element output. 
  84. * 
  85. * @since 2.1.0 
  86. * @access public 
  87. * 
  88. * @see Walker::start_el() 
  89. * 
  90. * @param string $output Passed by reference. Used to append additional content. 
  91. * @param object $category Category data object. 
  92. * @param int $depth Optional. Depth of category in reference to parents. Default 0. 
  93. * @param array $args Optional. An array of arguments. See wp_list_categories(). Default empty array. 
  94. * @param int $id Optional. ID of the current category. Default 0. 
  95. */ 
  96. public function start_el( &$output, $category, $depth = 0, $args = array(), $id = 0 ) { 
  97. /** This filter is documented in wp-includes/category-template.php */ 
  98. $cat_name = apply_filters( 
  99. 'list_cats',  
  100. esc_attr( $category->name ),  
  101. $category 
  102. ); 
  103.  
  104. // Don't generate an element if the category name is empty. 
  105. if ( ! $cat_name ) { 
  106. return; 
  107.  
  108. $link = '<a href="' . esc_url( get_term_link( $category ) ) . '" '; 
  109. if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) { 
  110. /** 
  111. * Filters the category description for display. 
  112. * 
  113. * @since 1.2.0 
  114. * 
  115. * @param string $description Category description. 
  116. * @param object $category Category object. 
  117. */ 
  118. $link .= 'title="' . esc_attr( strip_tags( apply_filters( 'category_description', $category->description, $category ) ) ) . '"'; 
  119.  
  120. $link .= '>'; 
  121. $link .= $cat_name . '</a>'; 
  122.  
  123. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) { 
  124. $link .= ' '; 
  125.  
  126. if ( empty( $args['feed_image'] ) ) { 
  127. $link .= '('; 
  128.  
  129. $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"'; 
  130.  
  131. if ( empty( $args['feed'] ) ) { 
  132. $alt = ' alt="' . sprintf(__( 'Feed for all posts filed under %s' ), $cat_name ) . '"'; 
  133. } else { 
  134. $alt = ' alt="' . $args['feed'] . '"'; 
  135. $name = $args['feed']; 
  136. $link .= empty( $args['title'] ) ? '' : $args['title']; 
  137.  
  138. $link .= '>'; 
  139.  
  140. if ( empty( $args['feed_image'] ) ) { 
  141. $link .= $name; 
  142. } else { 
  143. $link .= "<img src='" . $args['feed_image'] . "'$alt" . ' />'; 
  144. $link .= '</a>'; 
  145.  
  146. if ( empty( $args['feed_image'] ) ) { 
  147. $link .= ')'; 
  148.  
  149. if ( ! empty( $args['show_count'] ) ) { 
  150. $link .= ' (' . number_format_i18n( $category->count ) . ')'; 
  151. if ( 'list' == $args['style'] ) { 
  152. $output .= "\t<li"; 
  153. $css_classes = array( 
  154. 'cat-item',  
  155. 'cat-item-' . $category->term_id,  
  156. ); 
  157.  
  158. if ( ! empty( $args['current_category'] ) ) { 
  159. // 'current_category' can be an array, so we use `get_terms()`. 
  160. $_current_terms = get_terms( $category->taxonomy, array( 
  161. 'include' => $args['current_category'],  
  162. 'hide_empty' => false,  
  163. ) ); 
  164.  
  165. foreach ( $_current_terms as $_current_term ) { 
  166. if ( $category->term_id == $_current_term->term_id ) { 
  167. $css_classes[] = 'current-cat'; 
  168. } elseif ( $category->term_id == $_current_term->parent ) { 
  169. $css_classes[] = 'current-cat-parent'; 
  170. while ( $_current_term->parent ) { 
  171. if ( $category->term_id == $_current_term->parent ) { 
  172. $css_classes[] = 'current-cat-ancestor'; 
  173. break; 
  174. $_current_term = get_term( $_current_term->parent, $category->taxonomy ); 
  175.  
  176. /** 
  177. * Filters 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. * Ends the element output, if needed. 
  199. * 
  200. * @since 2.1.0 
  201. * @access public 
  202. * 
  203. * @see Walker::end_el() 
  204. * 
  205. * @param string $output Passed by reference. Used to append additional content. 
  206. * @param object $page Not used. 
  207. * @param int $depth Optional. Depth of category. Not used. 
  208. * @param array $args Optional. An array of arguments. Only uses 'list' for whether should append 
  209. * to output. See wp_list_categories(). Default empty array. 
  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.  
.