class-twentytwenty-walker-page.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Custom page walker for this theme.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Twenty
  7. * @since 1.0.0
  8. */
  9. if ( ! class_exists( 'TwentyTwenty_Walker_Page' ) ) {
  10. /**
  11. * CUSTOM PAGE WALKER
  12. * A custom walker for pages.
  13. */
  14. class TwentyTwenty_Walker_Page extends Walker_Page {
  15. /**
  16. * Outputs the beginning of the current element in the tree.
  17. *
  18. * @see Walker::start_el()
  19. * @since 2.1.0
  20. *
  21. * @param string $output Used to append additional content. Passed by reference.
  22. * @param WP_Post $page Page data object.
  23. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  24. * @param array $args Optional. Array of arguments. Default empty array.
  25. * @param int $current_page Optional. Page ID. Default 0.
  26. */
  27. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  28. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  29. $t = "\t";
  30. $n = "\n";
  31. } else {
  32. $t = '';
  33. $n = '';
  34. }
  35. if ( $depth ) {
  36. $indent = str_repeat( $t, $depth );
  37. } else {
  38. $indent = '';
  39. }
  40. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  41. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  42. $css_class[] = 'page_item_has_children';
  43. }
  44. if ( ! empty( $current_page ) ) {
  45. $_current_page = get_post( $current_page );
  46. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
  47. $css_class[] = 'current_page_ancestor';
  48. }
  49. if ( $page->ID === $current_page ) {
  50. $css_class[] = 'current_page_item';
  51. } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
  52. $css_class[] = 'current_page_parent';
  53. }
  54. } elseif ( get_option( 'page_for_posts' ) === $page->ID ) {
  55. $css_class[] = 'current_page_parent';
  56. }
  57. /**
  58. * Filters the list of CSS classes to include with each page item in the list.
  59. *
  60. * @since 2.8.0
  61. *
  62. * @see wp_list_pages()
  63. *
  64. * @param string[] $css_class An array of CSS classes to be applied to each list item.
  65. * @param WP_Post $page Page data object.
  66. * @param int $depth Depth of page, used for padding.
  67. * @param array $args An array of arguments.
  68. * @param int $current_page ID of the current page.
  69. */
  70. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  71. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  72. if ( '' === $page->post_title ) {
  73. /* translators: %d: ID of a post */
  74. $page->post_title = sprintf( __( '#%d (no title)', 'twentytwenty' ), $page->ID );
  75. }
  76. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  77. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  78. $atts = array();
  79. $atts['href'] = get_permalink( $page->ID );
  80. $atts['aria-current'] = ( $page->ID === $current_page ) ? 'page' : '';
  81. /**
  82. * Filters the HTML attributes applied to a page menu item's anchor element.
  83. *
  84. * @since 4.8.0
  85. *
  86. * @param array $atts {
  87. * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
  88. *
  89. * @type string $href The href attribute.
  90. * @type string $aria_current The aria-current attribute.
  91. * }
  92. * @param WP_Post $page Page data object.
  93. * @param int $depth Depth of page, used for padding.
  94. * @param array $args An array of arguments.
  95. * @param int $current_page ID of the current page.
  96. */
  97. $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
  98. $attributes = '';
  99. foreach ( $atts as $attr => $value ) {
  100. if ( ! empty( $value ) ) {
  101. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  102. $attributes .= ' ' . $attr . '="' . $value . '"';
  103. }
  104. }
  105. $args['list_item_before'] = '';
  106. $args['list_item_after'] = '';
  107. // Wrap the link in a div and append a sub menu toggle.
  108. if ( isset( $args['show_toggles'] ) && true === $args['show_toggles'] ) {
  109. // Wrap the menu item link contents in a div, used for positioning.
  110. $args['list_item_before'] = '<div class="ancestor-wrapper">';
  111. $args['list_item_after'] = '';
  112. // Add a toggle to items with children.
  113. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  114. $toggle_target_string = '.menu-modal .page-item-' . $page->ID . ' > ul';
  115. $toggle_duration = twentytwenty_toggle_duration();
  116. // Add the sub menu toggle.
  117. $args['list_item_after'] .= '<button class="toggle sub-menu-toggle fill-children-current-color" data-toggle-target="' . $toggle_target_string . '" data-toggle-type="slidetoggle" data-toggle-duration="' . absint( $toggle_duration ) . '" aria-expanded="false"><span class="screen-reader-text">' . __( 'Show sub menu', 'twentytwenty' ) . '</span>' . twentytwenty_get_theme_svg( 'chevron-down' ) . '</button>';
  118. }
  119. // Close the wrapper.
  120. $args['list_item_after'] .= '</div><!-- .ancestor-wrapper -->';
  121. }
  122. // Add icons to menu items with children.
  123. if ( isset( $args['show_sub_menu_icons'] ) && true === $args['show_sub_menu_icons'] ) {
  124. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  125. $args['list_item_after'] = '<span class="icon"></span>';
  126. }
  127. }
  128. $output .= $indent . sprintf(
  129. '<li%s>%s<a%s>%s%s%s</a>%s',
  130. $css_classes,
  131. $args['list_item_before'],
  132. $attributes,
  133. $args['link_before'],
  134. /** This filter is documented in wp-includes/post-template.php */
  135. apply_filters( 'the_title', $page->post_title, $page->ID ),
  136. $args['link_after'],
  137. $args['list_item_after']
  138. );
  139. if ( ! empty( $args['show_date'] ) ) {
  140. if ( 'modified' === $args['show_date'] ) {
  141. $time = $page->post_modified;
  142. } else {
  143. $time = $page->post_date;
  144. }
  145. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  146. $output .= ' ' . mysql2date( $date_format, $time );
  147. }
  148. }
  149. }
  150. }