[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-walker-page.php (source)

   1  <?php
   2  /**
   3   * Post API: Walker_Page class
   4   *
   5   * @package WordPress
   6   * @subpackage Template
   7   * @since 4.4.0
   8   */
   9  
  10  /**
  11   * Core walker class used to create an HTML list of pages.
  12   *
  13   * @since 2.1.0
  14   *
  15   * @see Walker
  16   */
  17  class Walker_Page extends Walker {
  18  
  19      /**
  20       * What the class handles.
  21       *
  22       * @since 2.1.0
  23       * @var string
  24       *
  25       * @see Walker::$tree_type
  26       */
  27      public $tree_type = 'page';
  28  
  29      /**
  30       * Database fields to use.
  31       *
  32       * @since 2.1.0
  33       * @var array
  34       *
  35       * @see Walker::$db_fields
  36       * @todo Decouple this.
  37       */
  38      public $db_fields = array(
  39          'parent' => 'post_parent',
  40          'id'     => 'ID',
  41      );
  42  
  43      /**
  44       * Outputs the beginning of the current level in the tree before elements are output.
  45       *
  46       * @since 2.1.0
  47       *
  48       * @see Walker::start_lvl()
  49       *
  50       * @param string $output Used to append additional content (passed by reference).
  51       * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
  52       * @param array  $args   Optional. Arguments for outputting the next level.
  53       *                       Default empty array.
  54       */
  55  	public function start_lvl( &$output, $depth = 0, $args = array() ) {
  56          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  57              $t = "\t";
  58              $n = "\n";
  59          } else {
  60              $t = '';
  61              $n = '';
  62          }
  63          $indent  = str_repeat( $t, $depth );
  64          $output .= "{$n}{$indent}<ul class='children'>{$n}";
  65      }
  66  
  67      /**
  68       * Outputs the end of the current level in the tree after elements are output.
  69       *
  70       * @since 2.1.0
  71       *
  72       * @see Walker::end_lvl()
  73       *
  74       * @param string $output Used to append additional content (passed by reference).
  75       * @param int    $depth  Optional. Depth of page. Used for padding. Default 0.
  76       * @param array  $args   Optional. Arguments for outputting the end of the current level.
  77       *                       Default empty array.
  78       */
  79  	public function end_lvl( &$output, $depth = 0, $args = array() ) {
  80          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
  81              $t = "\t";
  82              $n = "\n";
  83          } else {
  84              $t = '';
  85              $n = '';
  86          }
  87          $indent  = str_repeat( $t, $depth );
  88          $output .= "{$indent}</ul>{$n}";
  89      }
  90  
  91      /**
  92       * Outputs the beginning of the current element in the tree.
  93       *
  94       * @see Walker::start_el()
  95       * @since 2.1.0
  96       *
  97       * @param string  $output       Used to append additional content. Passed by reference.
  98       * @param WP_Post $page         Page data object.
  99       * @param int     $depth        Optional. Depth of page. Used for padding. Default 0.
 100       * @param array   $args         Optional. Array of arguments. Default empty array.
 101       * @param int     $current_page Optional. Page ID. Default 0.
 102       */
 103  	public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) {
 104          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
 105              $t = "\t";
 106              $n = "\n";
 107          } else {
 108              $t = '';
 109              $n = '';
 110          }
 111          if ( $depth ) {
 112              $indent = str_repeat( $t, $depth );
 113          } else {
 114              $indent = '';
 115          }
 116  
 117          $css_class = array( 'page_item', 'page-item-' . $page->ID );
 118  
 119          if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
 120              $css_class[] = 'page_item_has_children';
 121          }
 122  
 123          if ( ! empty( $current_page ) ) {
 124              $_current_page = get_post( $current_page );
 125              if ( $_current_page && in_array( $page->ID, $_current_page->ancestors ) ) {
 126                  $css_class[] = 'current_page_ancestor';
 127              }
 128              if ( $page->ID == $current_page ) {
 129                  $css_class[] = 'current_page_item';
 130              } elseif ( $_current_page && $page->ID == $_current_page->post_parent ) {
 131                  $css_class[] = 'current_page_parent';
 132              }
 133          } elseif ( $page->ID == get_option( 'page_for_posts' ) ) {
 134              $css_class[] = 'current_page_parent';
 135          }
 136  
 137          /**
 138           * Filters the list of CSS classes to include with each page item in the list.
 139           *
 140           * @since 2.8.0
 141           *
 142           * @see wp_list_pages()
 143           *
 144           * @param string[] $css_class    An array of CSS classes to be applied to each list item.
 145           * @param WP_Post  $page         Page data object.
 146           * @param int      $depth        Depth of page, used for padding.
 147           * @param array    $args         An array of arguments.
 148           * @param int      $current_page ID of the current page.
 149           */
 150          $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page ) );
 151          $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
 152  
 153          if ( '' === $page->post_title ) {
 154              /* translators: %d: ID of a post. */
 155              $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
 156          }
 157  
 158          $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
 159          $args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
 160  
 161          $atts                 = array();
 162          $atts['href']         = get_permalink( $page->ID );
 163          $atts['aria-current'] = ( $page->ID == $current_page ) ? 'page' : '';
 164  
 165          /**
 166           * Filters the HTML attributes applied to a page menu item's anchor element.
 167           *
 168           * @since 4.8.0
 169           *
 170           * @param array $atts {
 171           *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 172           *
 173           *     @type string $href         The href attribute.
 174           *     @type string $aria_current The aria-current attribute.
 175           * }
 176           * @param WP_Post $page         Page data object.
 177           * @param int     $depth        Depth of page, used for padding.
 178           * @param array   $args         An array of arguments.
 179           * @param int     $current_page ID of the current page.
 180           */
 181          $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page );
 182  
 183          $attributes = '';
 184          foreach ( $atts as $attr => $value ) {
 185              if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
 186                  $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
 187                  $attributes .= ' ' . $attr . '="' . $value . '"';
 188              }
 189          }
 190  
 191          $output .= $indent . sprintf(
 192              '<li%s><a%s>%s%s%s</a>',
 193              $css_classes,
 194              $attributes,
 195              $args['link_before'],
 196              /** This filter is documented in wp-includes/post-template.php */
 197              apply_filters( 'the_title', $page->post_title, $page->ID ),
 198              $args['link_after']
 199          );
 200  
 201          if ( ! empty( $args['show_date'] ) ) {
 202              if ( 'modified' == $args['show_date'] ) {
 203                  $time = $page->post_modified;
 204              } else {
 205                  $time = $page->post_date;
 206              }
 207  
 208              $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
 209              $output     .= ' ' . mysql2date( $date_format, $time );
 210          }
 211      }
 212  
 213      /**
 214       * Outputs the end of the current element in the tree.
 215       *
 216       * @since 2.1.0
 217       *
 218       * @see Walker::end_el()
 219       *
 220       * @param string  $output Used to append additional content. Passed by reference.
 221       * @param WP_Post $page   Page data object. Not used.
 222       * @param int     $depth  Optional. Depth of page. Default 0 (unused).
 223       * @param array   $args   Optional. Array of arguments. Default empty array.
 224       */
 225  	public function end_el( &$output, $page, $depth = 0, $args = array() ) {
 226          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
 227              $t = "\t";
 228              $n = "\n";
 229          } else {
 230              $t = '';
 231              $n = '';
 232          }
 233          $output .= "</li>{$n}";
 234      }
 235  
 236  }


Generated: Tue Nov 19 01:00:03 2019 Cross-referenced by PHPXref 0.7.1