[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref