PageRenderTime 40ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/wp-includes/class-walker-category.php

https://gitlab.com/VTTE/sitios-vtte
PHP | 268 lines | 118 code | 29 blank | 121 comment | 31 complexity | f8480c4b969f278d448f18f927e0580b MD5 | raw file
  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(
  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. *
  82. * @see Walker::start_el()
  83. *
  84. * @param string $output Used to append additional content (passed by reference).
  85. * @param object $category Category data object.
  86. * @param int $depth Optional. Depth of category in reference to parents. Default 0.
  87. * @param array $args Optional. An array of arguments. See wp_list_categories(). Default empty array.
  88. * @param int $id Optional. ID of the current category. Default 0.
  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( 'list_cats', esc_attr( $category->name ), $category );
  93. // Don't generate an element if the category name is empty.
  94. if ( '' === $cat_name ) {
  95. return;
  96. }
  97. $atts = array();
  98. $atts['href'] = get_term_link( $category );
  99. if ( $args['use_desc_for_title'] && ! empty( $category->description ) ) {
  100. /**
  101. * Filters the category description for display.
  102. *
  103. * @since 1.2.0
  104. *
  105. * @param string $description Category description.
  106. * @param object $category Category object.
  107. */
  108. $atts['title'] = strip_tags( apply_filters( 'category_description', $category->description, $category ) );
  109. }
  110. /**
  111. * Filters the HTML attributes applied to a category list item's anchor element.
  112. *
  113. * @since 5.2.0
  114. *
  115. * @param array $atts {
  116. * The HTML attributes applied to the list item's `<a>` element, empty strings are ignored.
  117. *
  118. * @type string $href The href attribute.
  119. * @type string $title The title attribute.
  120. * }
  121. * @param WP_Term $category Term data object.
  122. * @param int $depth Depth of category, used for padding.
  123. * @param array $args An array of arguments.
  124. * @param int $id ID of the current category.
  125. */
  126. $atts = apply_filters( 'category_list_link_attributes', $atts, $category, $depth, $args, $id );
  127. $attributes = '';
  128. foreach ( $atts as $attr => $value ) {
  129. if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
  130. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  131. $attributes .= ' ' . $attr . '="' . $value . '"';
  132. }
  133. }
  134. $link = sprintf(
  135. '<a%s>%s</a>',
  136. $attributes,
  137. $cat_name
  138. );
  139. if ( ! empty( $args['feed_image'] ) || ! empty( $args['feed'] ) ) {
  140. $link .= ' ';
  141. if ( empty( $args['feed_image'] ) ) {
  142. $link .= '(';
  143. }
  144. $link .= '<a href="' . esc_url( get_term_feed_link( $category->term_id, $category->taxonomy, $args['feed_type'] ) ) . '"';
  145. if ( empty( $args['feed'] ) ) {
  146. /* translators: %s: Category name. */
  147. $alt = ' alt="' . sprintf( __( 'Feed for all posts filed under %s' ), $cat_name ) . '"';
  148. } else {
  149. $alt = ' alt="' . $args['feed'] . '"';
  150. $name = $args['feed'];
  151. $link .= empty( $args['title'] ) ? '' : $args['title'];
  152. }
  153. $link .= '>';
  154. if ( empty( $args['feed_image'] ) ) {
  155. $link .= $name;
  156. } else {
  157. $link .= "<img src='" . esc_url( $args['feed_image'] ) . "'$alt" . ' />';
  158. }
  159. $link .= '</a>';
  160. if ( empty( $args['feed_image'] ) ) {
  161. $link .= ')';
  162. }
  163. }
  164. if ( ! empty( $args['show_count'] ) ) {
  165. $link .= ' (' . number_format_i18n( $category->count ) . ')';
  166. }
  167. if ( 'list' == $args['style'] ) {
  168. $output .= "\t<li";
  169. $css_classes = array(
  170. 'cat-item',
  171. 'cat-item-' . $category->term_id,
  172. );
  173. if ( ! empty( $args['current_category'] ) ) {
  174. // 'current_category' can be an array, so we use `get_terms()`.
  175. $_current_terms = get_terms(
  176. array(
  177. 'taxonomy' => $category->taxonomy,
  178. 'include' => $args['current_category'],
  179. 'hide_empty' => false,
  180. )
  181. );
  182. foreach ( $_current_terms as $_current_term ) {
  183. if ( $category->term_id == $_current_term->term_id ) {
  184. $css_classes[] = 'current-cat';
  185. $link = str_replace( '<a', '<a aria-current="page"', $link );
  186. } elseif ( $category->term_id == $_current_term->parent ) {
  187. $css_classes[] = 'current-cat-parent';
  188. }
  189. while ( $_current_term->parent ) {
  190. if ( $category->term_id == $_current_term->parent ) {
  191. $css_classes[] = 'current-cat-ancestor';
  192. break;
  193. }
  194. $_current_term = get_term( $_current_term->parent, $category->taxonomy );
  195. }
  196. }
  197. }
  198. /**
  199. * Filters the list of CSS classes to include with each category in the list.
  200. *
  201. * @since 4.2.0
  202. *
  203. * @see wp_list_categories()
  204. *
  205. * @param array $css_classes An array of CSS classes to be applied to each list item.
  206. * @param object $category Category data object.
  207. * @param int $depth Depth of page, used for padding.
  208. * @param array $args An array of wp_list_categories() arguments.
  209. */
  210. $css_classes = implode( ' ', apply_filters( 'category_css_class', $css_classes, $category, $depth, $args ) );
  211. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  212. $output .= $css_classes;
  213. $output .= ">$link\n";
  214. } elseif ( isset( $args['separator'] ) ) {
  215. $output .= "\t$link" . $args['separator'] . "\n";
  216. } else {
  217. $output .= "\t$link<br />\n";
  218. }
  219. }
  220. /**
  221. * Ends the element output, if needed.
  222. *
  223. * @since 2.1.0
  224. *
  225. * @see Walker::end_el()
  226. *
  227. * @param string $output Used to append additional content (passed by reference).
  228. * @param object $page Not used.
  229. * @param int $depth Optional. Depth of category. Not used.
  230. * @param array $args Optional. An array of arguments. Only uses 'list' for whether should append
  231. * to output. See wp_list_categories(). Default empty array.
  232. */
  233. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  234. if ( 'list' != $args['style'] ) {
  235. return;
  236. }
  237. $output .= "</li>\n";
  238. }
  239. }