class-walker-category.php 6.5 KB

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