class-walker-page.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 string[]
  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. * @since 5.9.0 Renamed `$page` to `$data_object` and `$current_page` to `$current_object_id`
  91. * to match parent class for PHP 8 named parameter support.
  92. *
  93. * @param string $output Used to append additional content. Passed by reference.
  94. * @param WP_Post $data_object Page data object.
  95. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  96. * @param array $args Optional. Array of arguments. Default empty array.
  97. * @param int $current_object_id Optional. ID of the current page. Default 0.
  98. */
  99. public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
  100. // Restores the more descriptive, specific name for use within this method.
  101. $page = $data_object;
  102. $current_page_id = $current_object_id;
  103. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  104. $t = "\t";
  105. $n = "\n";
  106. } else {
  107. $t = '';
  108. $n = '';
  109. }
  110. if ( $depth ) {
  111. $indent = str_repeat( $t, $depth );
  112. } else {
  113. $indent = '';
  114. }
  115. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  116. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  117. $css_class[] = 'page_item_has_children';
  118. }
  119. if ( ! empty( $current_page_id ) ) {
  120. $_current_page = get_post( $current_page_id );
  121. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
  122. $css_class[] = 'current_page_ancestor';
  123. }
  124. if ( $page->ID == $current_page_id ) {
  125. $css_class[] = 'current_page_item';
  126. } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
  127. $css_class[] = 'current_page_parent';
  128. }
  129. } elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
  130. $css_class[] = 'current_page_parent';
  131. }
  132. /**
  133. * Filters the list of CSS classes to include with each page item in the list.
  134. *
  135. * @since 2.8.0
  136. *
  137. * @see wp_list_pages()
  138. *
  139. * @param string[] $css_class An array of CSS classes to be applied to each list item.
  140. * @param WP_Post $page Page data object.
  141. * @param int $depth Depth of page, used for padding.
  142. * @param array $args An array of arguments.
  143. * @param int $current_page_id ID of the current page.
  144. */
  145. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
  146. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  147. if ( '' === $page->post_title ) {
  148. /* translators: %d: ID of a post. */
  149. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
  150. }
  151. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  152. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  153. $atts = array();
  154. $atts['href'] = get_permalink( $page->ID );
  155. $atts['aria-current'] = ( $page->ID == $current_page_id ) ? 'page' : '';
  156. /**
  157. * Filters the HTML attributes applied to a page menu item's anchor element.
  158. *
  159. * @since 4.8.0
  160. *
  161. * @param array $atts {
  162. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  163. *
  164. * @type string $href The href attribute.
  165. * @type string $aria-current The aria-current attribute.
  166. * }
  167. * @param WP_Post $page Page data object.
  168. * @param int $depth Depth of page, used for padding.
  169. * @param array $args An array of arguments.
  170. * @param int $current_page_id ID of the current page.
  171. */
  172. $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
  173. $attributes = '';
  174. foreach ( $atts as $attr => $value ) {
  175. if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
  176. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  177. $attributes .= ' ' . $attr . '="' . $value . '"';
  178. }
  179. }
  180. $output .= $indent . sprintf(
  181. '<li%s><a%s>%s%s%s</a>',
  182. $css_classes,
  183. $attributes,
  184. $args['link_before'],
  185. /** This filter is documented in wp-includes/post-template.php */
  186. apply_filters( 'the_title', $page->post_title, $page->ID ),
  187. $args['link_after']
  188. );
  189. if ( ! empty( $args['show_date'] ) ) {
  190. if ( 'modified' === $args['show_date'] ) {
  191. $time = $page->post_modified;
  192. } else {
  193. $time = $page->post_date;
  194. }
  195. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  196. $output .= ' ' . mysql2date( $date_format, $time );
  197. }
  198. }
  199. /**
  200. * Outputs the end of the current element in the tree.
  201. *
  202. * @since 2.1.0
  203. * @since 5.9.0 Renamed `$page` to `$data_object` to match parent class for PHP 8 named parameter support.
  204. *
  205. * @see Walker::end_el()
  206. *
  207. * @param string $output Used to append additional content. Passed by reference.
  208. * @param WP_Post $data_object Page data object. Not used.
  209. * @param int $depth Optional. Depth of page. Default 0 (unused).
  210. * @param array $args Optional. Array of arguments. Default empty array.
  211. */
  212. public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
  213. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  214. $t = "\t";
  215. $n = "\n";
  216. } else {
  217. $t = '';
  218. $n = '';
  219. }
  220. $output .= "</li>{$n}";
  221. }
  222. }