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


Generated: Sun May 31 01:00:03 2020 Cross-referenced by PHPXref 0.7.1