class-walker-page.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * @access public
  22. * @var string
  23. *
  24. * @see Walker::$tree_type
  25. */
  26. public $tree_type = 'page';
  27. /**
  28. * Database fields to use.
  29. *
  30. * @since 2.1.0
  31. * @access private
  32. * @var array
  33. *
  34. * @see Walker::$db_fields
  35. * @todo Decouple this.
  36. */
  37. public $db_fields = array( 'parent' => 'post_parent', 'id' => 'ID' );
  38. /**
  39. * Outputs the beginning of the current level in the tree before elements are output.
  40. *
  41. * @since 2.1.0
  42. * @access public
  43. *
  44. * @see Walker::start_lvl()
  45. *
  46. * @param string $output Passed by reference. Used to append additional content.
  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. * @access public
  67. *
  68. * @see Walker::end_lvl()
  69. *
  70. * @param string $output Passed by reference. Used to append additional content.
  71. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  72. * @param array $args Optional. Arguments for outputting the end of the current level.
  73. * Default empty array.
  74. */
  75. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  76. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  77. $t = "\t";
  78. $n = "\n";
  79. } else {
  80. $t = '';
  81. $n = '';
  82. }
  83. $indent = str_repeat( $t, $depth );
  84. $output .= "{$indent}</ul>{$n}";
  85. }
  86. /**
  87. * Outputs the beginning of the current element in the tree.
  88. *
  89. * @see Walker::start_el()
  90. * @since 2.1.0
  91. * @access public
  92. *
  93. * @param string $output Used to append additional content. Passed by reference.
  94. * @param WP_Post $page 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_page Optional. Page ID. Default 0.
  98. */
  99. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  100. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  101. $t = "\t";
  102. $n = "\n";
  103. } else {
  104. $t = '';
  105. $n = '';
  106. }
  107. if ( $depth ) {
  108. $indent = str_repeat( $t, $depth );
  109. } else {
  110. $indent = '';
  111. }
  112. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  113. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  114. $css_class[] = 'page_item_has_children';
  115. }
  116. if ( ! empty( $current_page ) ) {
  117. $_current_page = get_post( $current_page );
  118. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
  119. $css_class[] = 'current_page_ancestor';
  120. }
  121. if ( $page->ID == $current_page ) {
  122. $css_class[] = 'current_page_item';
  123. } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
  124. $css_class[] = 'current_page_parent';
  125. }
  126. } elseif ( $page->ID == get_option('page_for_posts') ) {
  127. $css_class[] = 'current_page_parent';
  128. }
  129. /**
  130. * Filters the list of CSS classes to include with each page item in the list.
  131. *
  132. * @since 2.8.0
  133. *
  134. * @see wp_list_pages()
  135. *
  136. * @param array $css_class An array of CSS classes to be applied
  137. * to each list item.
  138. * @param WP_Post $page Page data object.
  139. * @param int $depth Depth of page, used for padding.
  140. * @param array $args An array of arguments.
  141. * @param int $current_page ID of the current page.
  142. */
  143. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  144. if ( '' === $page->post_title ) {
  145. /* translators: %d: ID of a post */
  146. $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
  147. }
  148. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  149. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  150. $atts = array();
  151. $atts['href'] = get_permalink( $page->ID );
  152. /**
  153. * Filters the HTML attributes applied to a page menu item's anchor element.
  154. *
  155. * @since 4.8.0
  156. *
  157. * @param array $atts {
  158. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  159. *
  160. * @type string $href The href 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 ( ! empty( $value ) ) {
  171. $value = esc_attr( $value );
  172. $attributes .= ' ' . $attr . '="' . $value . '"';
  173. }
  174. }
  175. $output .= $indent . sprintf(
  176. '<li class="%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. * @access public
  199. *
  200. * @see Walker::end_el()
  201. *
  202. * @param string $output Used to append additional content. Passed by reference.
  203. * @param WP_Post $page Page data object. Not used.
  204. * @param int $depth Optional. Depth of page. Default 0 (unused).
  205. * @param array $args Optional. Array of arguments. Default empty array.
  206. */
  207. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  208. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  209. $t = "\t";
  210. $n = "\n";
  211. } else {
  212. $t = '';
  213. $n = '';
  214. }
  215. $output .= "</li>{$n}";
  216. }
  217. }