You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

210 lines
5.8 KiB

  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. $output .= $indent . sprintf(
  151. '<li class="%s"><a href="%s">%s%s%s</a>',
  152. $css_classes,
  153. get_permalink( $page->ID ),
  154. $args['link_before'],
  155. /** This filter is documented in wp-includes/post-template.php */
  156. apply_filters( 'the_title', $page->post_title, $page->ID ),
  157. $args['link_after']
  158. );
  159. if ( ! empty( $args['show_date'] ) ) {
  160. if ( 'modified' == $args['show_date'] ) {
  161. $time = $page->post_modified;
  162. } else {
  163. $time = $page->post_date;
  164. }
  165. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  166. $output .= " " . mysql2date( $date_format, $time );
  167. }
  168. }
  169. /**
  170. * Outputs the end of the current element in the tree.
  171. *
  172. * @since 2.1.0
  173. * @access public
  174. *
  175. * @see Walker::end_el()
  176. *
  177. * @param string $output Used to append additional content. Passed by reference.
  178. * @param WP_Post $page Page data object. Not used.
  179. * @param int $depth Optional. Depth of page. Default 0 (unused).
  180. * @param array $args Optional. Array of arguments. Default empty array.
  181. */
  182. public function end_el( &$output, $page, $depth = 0, $args = array() ) {
  183. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  184. $t = "\t";
  185. $n = "\n";
  186. } else {
  187. $t = '';
  188. $n = '';
  189. }
  190. $output .= "</li>{$n}";
  191. }
  192. }