[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-content/themes/twentytwenty/classes/ -> class-twentytwenty-walker-page.php (source)

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


Generated: Fri Apr 19 01:00:02 2024 Cross-referenced by PHPXref 0.7.1