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

https://gitlab.com/VTTE/sitios-vtte · PHP · 236 lines · 106 code · 20 blank · 110 comment · 32 complexity · 135e8673caff119a96889039c8cfba8a MD5 · raw file

  1. <?php
  2. /**
  3. * Post API: Walker_Page class
  4. *
  5. * @package WordPress
  6. * @subpackage Template
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core walker class used to create an HTML list of pages.
  11. *
  12. * @since 2.1.0
  13. *
  14. * @see Walker
  15. */
  16. class Walker_Page 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 = 'page';
  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' => 'post_parent',
  37. 'id' => 'ID',
  38. );
  39. /**
  40. * Outputs the beginning of the current level in the tree before elements are output.
  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 page. Used for padding. Default 0.
  48. * @param array $args Optional. Arguments for outputting the next level.
  49. * Default empty array.
  50. */
  51. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  52. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  53. $t = "\t";
  54. $n = "\n";
  55. } else {
  56. $t = '';
  57. $n = '';
  58. }
  59. $indent = str_repeat( $t, $depth );
  60. $output .= "{$n}{$indent}<ul class='children'>{$n}";
  61. }
  62. /**
  63. * Outputs the end of the current level in the tree after elements are output.
  64. *
  65. * @since 2.1.0
  66. *
  67. * @see Walker::end_lvl()
  68. *
  69. * @param string $output Used to append additional content (passed by reference).
  70. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  71. * @param array $args Optional. Arguments for outputting the end of the current level.
  72. * Default empty array.
  73. */
  74. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  75. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  76. $t = "\t";
  77. $n = "\n";
  78. } else {
  79. $t = '';
  80. $n = '';
  81. }
  82. $indent = str_repeat( $t, $depth );
  83. $output .= "{$indent}</ul>{$n}";
  84. }
  85. /**
  86. * Outputs the beginning of the current element in the tree.
  87. *
  88. * @see Walker::start_el()
  89. * @since 2.1.0
  90. *
  91. * @param string $output Used to append additional content. Passed by reference.
  92. * @param WP_Post $page Page data object.
  93. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  94. * @param array $args Optional. Array of arguments. Default empty array.
  95. * @param int $current_page Optional. Page ID. Default 0.
  96. */
  97. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  98. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  99. $t = "\t";
  100. $n = "\n";
  101. } else {
  102. $t = '';
  103. $n = '';
  104. }
  105. if ( $depth ) {
  106. $indent = str_repeat( $t, $depth );
  107. } else {
  108. $indent = '';
  109. }
  110. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  111. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  112. $css_class[] = 'page_item_has_children';
  113. }
  114. if ( ! empty( $current_page ) ) {
  115. $_current_page = get_post( $current_page );
  116. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
  117. $css_class[] = 'current_page_ancestor';
  118. }
  119. if ( $page->ID == $current_page ) {
  120. $css_class[] = 'current_page_item';
  121. } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
  122. $css_class[] = 'current_page_parent';
  123. }
  124. } elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
  125. $css_class[] = 'current_page_parent';
  126. }
  127. /**
  128. * Filters the list of CSS classes to include with each page item in the list.
  129. *
  130. * @since 2.8.0
  131. *
  132. * @see wp_list_pages()
  133. *
  134. * @param string[] $css_class An array of CSS classes to be applied to each list item.
  135. * @param WP_Post $page Page data object.
  136. * @param int $depth Depth of page, used for padding.
  137. * @param array $args An array of arguments.
  138. * @param int $current_page ID of the current page.
  139. */
  140. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  141. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  142. if ( '' === $page->post_title ) {
  143. /* translators: %d: ID of a post. */
  144. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
  145. }
  146. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  147. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  148. $atts = array();
  149. $atts['href'] = get_permalink( $page->ID );
  150. $atts['aria-current'] = ( $page->ID == $current_page ) ? 'page' : '';
  151. /**
  152. * Filters the HTML attributes applied to a page menu item's anchor element.
  153. *
  154. * @since 4.8.0
  155. *
  156. * @param array $atts {
  157. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  158. *
  159. * @type string $href The href attribute.
  160. * @type string $aria_current The aria-current attribute.
  161. * }
  162. * @param WP_Post $page Page data object.
  163. * @param int $depth Depth of page, used for padding.
  164. * @param array $args An array of arguments.
  165. * @param int $current_page ID of the current page.
  166. */
  167. $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
  168. $attributes = '';
  169. foreach ( $atts as $attr => $value ) {
  170. if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
  171. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  172. $attributes .= ' ' . $attr . '="' . $value . '"';
  173. }
  174. }
  175. $output .= $indent . sprintf(
  176. '<li%s><a%s>%s%s%s</a>',
  177. $css_classes,
  178. $attributes,
  179. $args['link_before'],
  180. /** This filter is documented in wp-includes/post-template.php */
  181. apply_filters( 'the_title', $page->post_title, $page->ID ),
  182. $args['link_after']
  183. );
  184. if ( ! empty( $args['show_date'] ) ) {
  185. if ( 'modified' == $args['show_date'] ) {
  186. $time = $page->post_modified;
  187. } else {
  188. $time = $page->post_date;
  189. }
  190. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  191. $output .= ' ' . mysql2date( $date_format, $time );
  192. }
  193. }
  194. /**
  195. * Outputs the end of the current element in the tree.
  196. *
  197. * @since 2.1.0
  198. *
  199. * @see Walker::end_el()
  200. *
  201. * @param string $output Used to append additional content. Passed by reference.
  202. * @param WP_Post $page Page data object. Not used.
  203. * @param int $depth Optional. Depth of page. Default 0 (unused).
  204. * @param array $args Optional. Array of arguments. Default empty array.
  205. */
  206. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  207. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  208. $t = "\t";
  209. $n = "\n";
  210. } else {
  211. $t = '';
  212. $n = '';
  213. }
  214. $output .= "</li>{$n}";
  215. }
  216. }