[ 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 string[]
  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       * @since 5.9.0 Renamed `$page` to `$data_object` and `$current_page` to `$current_object_id`
  97       *              to match parent class for PHP 8 named parameter support.
  98       *
  99       * @param string  $output            Used to append additional content. Passed by reference.
 100       * @param WP_Post $data_object       Page data object.
 101       * @param int     $depth             Optional. Depth of page. Used for padding. Default 0.
 102       * @param array   $args              Optional. Array of arguments. Default empty array.
 103       * @param int     $current_object_id Optional. ID of the current page. Default 0.
 104       */
 105  	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {
 106          // Restores the more descriptive, specific name for use within this method.
 107          $page            = $data_object;
 108          $current_page_id = $current_object_id;
 109  
 110          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
 111              $t = "\t";
 112              $n = "\n";
 113          } else {
 114              $t = '';
 115              $n = '';
 116          }
 117          if ( $depth ) {
 118              $indent = str_repeat( $t, $depth );
 119          } else {
 120              $indent = '';
 121          }
 122  
 123          $css_class = array( 'page_item', 'page-item-' . $page->ID );
 124  
 125          if ( isset( $args['pages_with_children'][ $page->ID ] ) ) {
 126              $css_class[] = 'page_item_has_children';
 127          }
 128  
 129          if ( ! empty( $current_page_id ) ) {
 130              $_current_page = get_post( $current_page_id );
 131  
 132              if ( $_current_page && in_array( $page->ID, $_current_page->ancestors, true ) ) {
 133                  $css_class[] = 'current_page_ancestor';
 134              }
 135  
 136              if ( $page->ID == $current_page_id ) {
 137                  $css_class[] = 'current_page_item';
 138              } elseif ( $_current_page && $page->ID === $_current_page->post_parent ) {
 139                  $css_class[] = 'current_page_parent';
 140              }
 141          } elseif ( get_option( 'page_for_posts' ) == $page->ID ) {
 142              $css_class[] = 'current_page_parent';
 143          }
 144  
 145          /**
 146           * Filters the list of CSS classes to include with each page item in the list.
 147           *
 148           * @since 2.8.0
 149           *
 150           * @see wp_list_pages()
 151           *
 152           * @param string[] $css_class       An array of CSS classes to be applied to each list item.
 153           * @param WP_Post  $page            Page data object.
 154           * @param int      $depth           Depth of page, used for padding.
 155           * @param array    $args            An array of arguments.
 156           * @param int      $current_page_id ID of the current page.
 157           */
 158          $css_classes = implode( ' ', apply_filters( 'page_css_class', $css_class, $page, $depth, $args, $current_page_id ) );
 159          $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
 160  
 161          if ( '' === $page->post_title ) {
 162              /* translators: %d: ID of a post. */
 163              $page->post_title = sprintf( __( '#%d (no title)' ), $page->ID );
 164          }
 165  
 166          $args['link_before'] = empty( $args['link_before'] ) ? '' : $args['link_before'];
 167          $args['link_after']  = empty( $args['link_after'] ) ? '' : $args['link_after'];
 168  
 169          $atts                 = array();
 170          $atts['href']         = get_permalink( $page->ID );
 171          $atts['aria-current'] = ( $page->ID == $current_page_id ) ? 'page' : '';
 172  
 173          /**
 174           * Filters the HTML attributes applied to a page menu item's anchor element.
 175           *
 176           * @since 4.8.0
 177           *
 178           * @param array $atts {
 179           *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 180           *
 181           *     @type string $href         The href attribute.
 182           *     @type string $aria-current The aria-current attribute.
 183           * }
 184           * @param WP_Post $page            Page data object.
 185           * @param int     $depth           Depth of page, used for padding.
 186           * @param array   $args            An array of arguments.
 187           * @param int     $current_page_id ID of the current page.
 188           */
 189          $atts = apply_filters( 'page_menu_link_attributes', $atts, $page, $depth, $args, $current_page_id );
 190  
 191          $attributes = '';
 192          foreach ( $atts as $attr => $value ) {
 193              if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
 194                  $value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
 195                  $attributes .= ' ' . $attr . '="' . $value . '"';
 196              }
 197          }
 198  
 199          $output .= $indent . sprintf(
 200              '<li%s><a%s>%s%s%s</a>',
 201              $css_classes,
 202              $attributes,
 203              $args['link_before'],
 204              /** This filter is documented in wp-includes/post-template.php */
 205              apply_filters( 'the_title', $page->post_title, $page->ID ),
 206              $args['link_after']
 207          );
 208  
 209          if ( ! empty( $args['show_date'] ) ) {
 210              if ( 'modified' === $args['show_date'] ) {
 211                  $time = $page->post_modified;
 212              } else {
 213                  $time = $page->post_date;
 214              }
 215  
 216              $date_format = empty( $args['date_format'] ) ? '' : $args['date_format'];
 217              $output     .= ' ' . mysql2date( $date_format, $time );
 218          }
 219      }
 220  
 221      /**
 222       * Outputs the end of the current element in the tree.
 223       *
 224       * @since 2.1.0
 225       * @since 5.9.0 Renamed `$page` to `$data_object` to match parent class for PHP 8 named parameter support.
 226       *
 227       * @see Walker::end_el()
 228       *
 229       * @param string  $output      Used to append additional content. Passed by reference.
 230       * @param WP_Post $data_object Page data object. Not used.
 231       * @param int     $depth       Optional. Depth of page. Default 0 (unused).
 232       * @param array   $args        Optional. Array of arguments. Default empty array.
 233       */
 234  	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {
 235          if ( isset( $args['item_spacing'] ) && 'preserve' === $args['item_spacing'] ) {
 236              $t = "\t";
 237              $n = "\n";
 238          } else {
 239              $t = '';
 240              $n = '';
 241          }
 242          $output .= "</li>{$n}";
 243      }
 244  
 245  }


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1