class-twentytwenty-walker-page.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. /**
  3. * Custom page walker for this theme.
  4. *
  5. * @package WordPress
  6. * @subpackage Twenty_Twenty
  7. * @since Twenty Twenty 1.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. /** This filter is documented in wp-includes/class-walker-page.php */
  58. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  59. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  60. if ( '' === $page->post_title ) {
  61. /* translators: %d: ID of a post. */
  62. $page->post_title = sprintf( __( '#%d (no title)', 'twentytwenty' ), $page->ID );
  63. }
  64. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  65. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  66. $atts = array();
  67. $atts['href'] = get_permalink( $page->ID );
  68. $atts['aria-current'] = ( $page->ID === $current_page ) ? 'page' : '';
  69. /** This filter is documented in wp-includes/class-walker-page.php */
  70. $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
  71. $attributes = '';
  72. foreach ( $atts as $attr => $value ) {
  73. if ( ! empty( $value ) ) {
  74. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  75. $attributes .= ' ' . $attr . '="' . $value . '"';
  76. }
  77. }
  78. $args['list_item_before'] = '';
  79. $args['list_item_after'] = '';
  80. // Wrap the link in a div and append a sub menu toggle.
  81. if ( isset( $args['show_toggles'] ) && true === $args['show_toggles'] ) {
  82. // Wrap the menu item link contents in a div, used for positioning.
  83. $args['list_item_before'] = '<div class="ancestor-wrapper">';
  84. $args['list_item_after'] = '';
  85. // Add a toggle to items with children.
  86. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  87. $toggle_target_string = '.menu-modal .page-item-' . $page->ID . ' > ul';
  88. $toggle_duration = twentytwenty_toggle_duration();
  89. // Add the sub menu toggle.
  90. $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>';
  91. }
  92. // Close the wrapper.
  93. $args['list_item_after'] .= '</div><!-- .ancestor-wrapper -->';
  94. }
  95. // Add icons to menu items with children.
  96. if ( isset( $args['show_sub_menu_icons'] ) && true === $args['show_sub_menu_icons'] ) {
  97. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  98. $args['list_item_after'] = '<span class="icon"></span>';
  99. }
  100. }
  101. $output .= $indent . sprintf(
  102. '<li%s>%s<a%s>%s%s%s</a>%s',
  103. $css_classes,
  104. $args['list_item_before'],
  105. $attributes,
  106. $args['link_before'],
  107. /** This filter is documented in wp-includes/post-template.php */
  108. apply_filters( 'the_title', $page->post_title, $page->ID ),
  109. $args['link_after'],
  110. $args['list_item_after']
  111. );
  112. if ( ! empty( $args['show_date'] ) ) {
  113. if ( 'modified' === $args['show_date'] ) {
  114. $time = $page->post_modified;
  115. } else {
  116. $time = $page->post_date;
  117. }
  118. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  119. $output .= ' ' . mysql2date( $date_format, $time );
  120. }
  121. }
  122. }
  123. }