class-walker-page.php 6.6 KB

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