[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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              } else {
  42                  $t = '';
  43              }
  44              if ( $depth ) {
  45                  $indent = str_repeat( $t, $depth );
  46              } else {
  47                  $indent = '';
  48              }
  49  
  50              $css_class = array( 'page_item', 'page-item-' . $page->ID );
  51  
  52              if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
  53                  $css_class[] = 'page_item_has_children';
  54              }
  55  
  56              if ( ! empty( $current_page_id ) ) {
  57                  $_current_page = get_post( $current_page_id );
  58                  if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
  59                      $css_class[] = 'current_page_ancestor';
  60                  }
  61                  if ( $page->ID === $current_page_id ) {
  62                      $css_class[] = 'current_page_item';
  63                  } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
  64                      $css_class[] = 'current_page_parent';
  65                  }
  66              } elseif ( get_option( 'page_for_posts' ) === $page->ID ) {
  67                  $css_class[] = 'current_page_parent';
  68              }
  69  
  70              /** This filter is documented in wp-includes/class-walker-page.php */
  71              $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
  72              $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
  73  
  74              if ( '' === $page->post_title ) {
  75                  /* translators: %d: ID of a post. */
  76                  $page->post_title = sprintf( __( '#%d (no title)', 'twentytwenty' ), $page->ID );
  77              }
  78  
  79              $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
  80              $args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
  81  
  82              $atts                 = array();
  83              $atts['href']         = get_permalink( $page->ID );
  84              $atts['aria-current'] = ( $page->ID === $current_page_id ) ? 'page' : '';
  85  
  86              /** This filter is documented in wp-includes/class-walker-page.php */
  87              $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
  88  
  89              $attributes = '';
  90              foreach ( $atts as $attr => $value ) {
  91                  if ( ! empty( $value ) ) {
  92                      $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
  93                      $attributes .= ' ' . $attr . '="' . $value . '"';
  94                  }
  95              }
  96  
  97              $args['list_item_before'] = '';
  98              $args['list_item_after']  = '';
  99  
 100              // Wrap the link in a div and append a sub menu toggle.
 101              if ( isset( $args['show_toggles'] ) && true === $args['show_toggles'] ) {
 102                  // Wrap the menu item link contents in a div, used for positioning.
 103                  $args['list_item_before'] = '<div class="ancestor-wrapper">';
 104                  $args['list_item_after']  = '';
 105  
 106                  // Add a toggle to items with children.
 107                  if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
 108  
 109                      $toggle_target_string = '.menu-modal .page-item-' . $page->ID . ' > ul';
 110                      $toggle_duration      = twentytwenty_toggle_duration();
 111  
 112                      // Add the sub menu toggle.
 113                      $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">' .
 114                          /* translators: Hidden accessibility text. */
 115                          __( 'Show sub menu', 'twentytwenty' ) .
 116                      '</span>' . twentytwenty_get_theme_svg( 'chevron-down' ) . '</button>';
 117  
 118                  }
 119  
 120                  // Close the wrapper.
 121                  $args['list_item_after'] .= '</div><!-- .ancestor-wrapper -->';
 122              }
 123  
 124              // Add icons to menu items with children.
 125              if ( isset( $args['show_sub_menu_icons'] ) && true === $args['show_sub_menu_icons'] ) {
 126                  if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
 127                      $args['list_item_after'] = '<span class="icon"></span>';
 128                  }
 129              }
 130  
 131              $output .= $indent . sprintf(
 132                  '<li%s>%s<a%s>%s%s%s</a>%s',
 133                  $css_classes,
 134                  $args['list_item_before'],
 135                  $attributes,
 136                  $args['link_before'],
 137                  /** This filter is documented in wp-includes/post-template.php */
 138                  apply_filters( 'the_title', $page->post_title, $page->ID ),
 139                  $args['link_after'],
 140                  $args['list_item_after']
 141              );
 142  
 143              if ( ! empty( $args['show_date'] ) ) {
 144                  if ( 'modified' === $args['show_date'] ) {
 145                      $time = $page->post_modified;
 146                  } else {
 147                      $time = $page->post_date;
 148                  }
 149  
 150                  $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
 151                  $output     .= ' ' . mysql2date( $date_format, $time );
 152              }
 153          }
 154      }
 155  }


Generated : Fri Apr 26 08:20:02 2024 Cross-referenced by PHPXref