class-walker-category.php 8.3 KB

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