Нема описа

class-twentytwenty-walker-page.php 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. * @since Twenty Twenty 1.0
  15. */
  16. class TwentyTwenty_Walker_Page extends Walker_Page {
  17. /**
  18. * Outputs the beginning of the current element in the tree.
  19. *
  20. * @since Twenty Twenty 1.0
  21. *
  22. * @see Walker::start_el()
  23. *
  24. * @param string $output Used to append additional content. Passed by reference.
  25. * @param WP_Post $page Page data object.
  26. * @param int $depth Optional. Depth of page. Used for padding. Default 0.
  27. * @param array $args Optional. Array of arguments. Default empty array.
  28. * @param int $current_page Optional. Page ID. Default 0.
  29. */
  30. public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
  31. if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  32. $t = "\t";
  33. $n = "\n";
  34. } else {
  35. $t = '';
  36. $n = '';
  37. }
  38. if ( $depth ) {
  39. $indent = str_repeat( $t, $depth );
  40. } else {
  41. $indent = '';
  42. }
  43. $css_class = array( 'page_item', 'page-item-' . $page->ID );
  44. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  45. $css_class[] = 'page_item_has_children';
  46. }
  47. if ( ! empty( $current_page ) ) {
  48. $_current_page = get_post( $current_page );
  49. if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
  50. $css_class[] = 'current_page_ancestor';
  51. }
  52. if ( $page->ID === $current_page ) {
  53. $css_class[] = 'current_page_item';
  54. } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
  55. $css_class[] = 'current_page_parent';
  56. }
  57. } elseif ( get_option( 'page_for_posts' ) === $page->ID ) {
  58. $css_class[] = 'current_page_parent';
  59. }
  60. /** This filter is documented in wp-includes/class-walker-page.php */
  61. $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
  62. $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  63. if ( '' === $page->post_title ) {
  64. /* translators: %d: ID of a post. */
  65. $page->post_title = sprintf( __( '#%d (no title)', 'twentytwenty' ), $page->ID );
  66. }
  67. $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  68. $args['link_after'] = empty( $args['link_after'] ) ? '' : $args['link_after'];
  69. $atts = array();
  70. $atts['href'] = get_permalink( $page->ID );
  71. $atts['aria-current'] = ( $page->ID === $current_page ) ? 'page' : '';
  72. /** This filter is documented in wp-includes/class-walker-page.php */
  73. $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
  74. $attributes = '';
  75. foreach ( $atts as $attr => $value ) {
  76. if ( ! empty( $value ) ) {
  77. $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  78. $attributes .= ' ' . $attr . '="' . $value . '"';
  79. }
  80. }
  81. $args['list_item_before'] = '';
  82. $args['list_item_after'] = '';
  83. // Wrap the link in a div and append a sub menu toggle.
  84. if ( isset( $args['show_toggles'] ) && true === $args['show_toggles'] ) {
  85. // Wrap the menu item link contents in a div, used for positioning.
  86. $args['list_item_before'] = '<div class="ancestor-wrapper">';
  87. $args['list_item_after'] = '';
  88. // Add a toggle to items with children.
  89. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  90. $toggle_target_string = '.menu-modal .page-item-' . $page->ID . ' > ul';
  91. $toggle_duration = twentytwenty_toggle_duration();
  92. // Add the sub menu toggle.
  93. $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>';
  94. }
  95. // Close the wrapper.
  96. $args['list_item_after'] .= '</div><!-- .ancestor-wrapper -->';
  97. }
  98. // Add icons to menu items with children.
  99. if ( isset( $args['show_sub_menu_icons'] ) && true === $args['show_sub_menu_icons'] ) {
  100. if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  101. $args['list_item_after'] = '<span class="icon"></span>';
  102. }
  103. }
  104. $output .= $indent . sprintf(
  105. '<li%s>%s<a%s>%s%s%s</a>%s',
  106. $css_classes,
  107. $args['list_item_before'],
  108. $attributes,
  109. $args['link_before'],
  110. /** This filter is documented in wp-includes/post-template.php */
  111. apply_filters( 'the_title', $page->post_title, $page->ID ),
  112. $args['link_after'],
  113. $args['list_item_after']
  114. );
  115. if ( ! empty( $args['show_date'] ) ) {
  116. if ( 'modified' === $args['show_date'] ) {
  117. $time = $page->post_modified;
  118. } else {
  119. $time = $page->post_date;
  120. }
  121. $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
  122. $output .= ' ' . mysql2date( $date_format, $time );
  123. }
  124. }
  125. }
  126. }