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