[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Nav Menu API: Walker_Nav_Menu class
   4   *
   5   * @package WordPress
   6   * @subpackage Nav_Menus
   7   * @since 4.6.0
   8   */
   9  
  10  /**
  11   * Core class used to implement an HTML list of nav menu items.
  12   *
  13   * @since 3.0.0
  14   *
  15   * @see Walker
  16   */
  17  class Walker_Nav_Menu extends Walker {
  18      /**
  19       * What the class handles.
  20       *
  21       * @since 3.0.0
  22       * @var string
  23       *
  24       * @see Walker::$tree_type
  25       */
  26      public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
  27  
  28      /**
  29       * Database fields to use.
  30       *
  31       * @since 3.0.0
  32       * @todo Decouple this.
  33       * @var string[]
  34       *
  35       * @see Walker::$db_fields
  36       */
  37      public $db_fields = array(
  38          'parent' => 'menu_item_parent',
  39          'id'     => 'db_id',
  40      );
  41  
  42      /**
  43       * Starts the list before the elements are added.
  44       *
  45       * @since 3.0.0
  46       *
  47       * @see Walker::start_lvl()
  48       *
  49       * @param string   $output Used to append additional content (passed by reference).
  50       * @param int      $depth  Depth of menu item. Used for padding.
  51       * @param stdClass $args   An object of wp_nav_menu() arguments.
  52       */
  53  	public function start_lvl( &$output, $depth = 0, $args = null ) {
  54          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
  55              $t = '';
  56              $n = '';
  57          } else {
  58              $t = "\t";
  59              $n = "\n";
  60          }
  61          $indent = str_repeat( $t, $depth );
  62  
  63          // Default class.
  64          $classes = array( 'sub-menu' );
  65  
  66          /**
  67           * Filters the CSS class(es) applied to a menu list element.
  68           *
  69           * @since 4.8.0
  70           *
  71           * @param string[] $classes Array of the CSS classes that are applied to the menu `<ul>` element.
  72           * @param stdClass $args    An object of `wp_nav_menu()` arguments.
  73           * @param int      $depth   Depth of menu item. Used for padding.
  74           */
  75          $class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
  76  
  77          $atts          = array();
  78          $atts['class'] = ! empty( $class_names ) ? $class_names : '';
  79  
  80          /**
  81           * Filters the HTML attributes applied to a menu list element.
  82           *
  83           * @since 6.3.0
  84           *
  85           * @param array $atts {
  86           *     The HTML attributes applied to the `<ul>` element, empty strings are ignored.
  87           *
  88           *     @type string $class    HTML CSS class attribute.
  89           * }
  90           * @param stdClass $args      An object of `wp_nav_menu()` arguments.
  91           * @param int      $depth     Depth of menu item. Used for padding.
  92           */
  93          $atts       = apply_filters( 'nav_menu_submenu_attributes', $atts, $args, $depth );
  94          $attributes = $this->build_atts( $atts );
  95  
  96          $output .= "{$n}{$indent}<ul{$attributes}>{$n}";
  97      }
  98  
  99      /**
 100       * Ends the list of after the elements are added.
 101       *
 102       * @since 3.0.0
 103       *
 104       * @see Walker::end_lvl()
 105       *
 106       * @param string   $output Used to append additional content (passed by reference).
 107       * @param int      $depth  Depth of menu item. Used for padding.
 108       * @param stdClass $args   An object of wp_nav_menu() arguments.
 109       */
 110  	public function end_lvl( &$output, $depth = 0, $args = null ) {
 111          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
 112              $t = '';
 113              $n = '';
 114          } else {
 115              $t = "\t";
 116              $n = "\n";
 117          }
 118          $indent  = str_repeat( $t, $depth );
 119          $output .= "$indent</ul>{$n}";
 120      }
 121  
 122      /**
 123       * Starts the element output.
 124       *
 125       * @since 3.0.0
 126       * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added.
 127       * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id`
 128       *              to match parent class for PHP 8 named parameter support.
 129       * @since 6.7.0 Removed redundant title attributes.
 130       *
 131       * @see Walker::start_el()
 132       *
 133       * @param string   $output            Used to append additional content (passed by reference).
 134       * @param WP_Post  $data_object       Menu item data object.
 135       * @param int      $depth             Depth of menu item. Used for padding.
 136       * @param stdClass $args              An object of wp_nav_menu() arguments.
 137       * @param int      $current_object_id Optional. ID of the current menu item. Default 0.
 138       */
 139  	public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
 140          // Restores the more descriptive, specific name for use within this method.
 141          $menu_item = $data_object;
 142  
 143          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
 144              $t = '';
 145              $n = '';
 146          } else {
 147              $t = "\t";
 148              $n = "\n";
 149          }
 150          $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
 151  
 152          $classes   = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes;
 153          $classes[] = 'menu-item-' . $menu_item->ID;
 154  
 155          /**
 156           * Filters the arguments for a single nav menu item.
 157           *
 158           * @since 4.4.0
 159           *
 160           * @param stdClass $args      An object of wp_nav_menu() arguments.
 161           * @param WP_Post  $menu_item Menu item data object.
 162           * @param int      $depth     Depth of menu item. Used for padding.
 163           */
 164          $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth );
 165  
 166          /**
 167           * Filters the CSS classes applied to a menu item's list item element.
 168           *
 169           * @since 3.0.0
 170           * @since 4.1.0 The `$depth` parameter was added.
 171           *
 172           * @param string[] $classes   Array of the CSS classes that are applied to the menu item's `<li>` element.
 173           * @param WP_Post  $menu_item The current menu item object.
 174           * @param stdClass $args      An object of wp_nav_menu() arguments.
 175           * @param int      $depth     Depth of menu item. Used for padding.
 176           */
 177          $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
 178  
 179          /**
 180           * Filters the ID attribute applied to a menu item's list item element.
 181           *
 182           * @since 3.0.1
 183           * @since 4.1.0 The `$depth` parameter was added.
 184           *
 185           * @param string   $menu_item_id The ID attribute applied to the menu item's `<li>` element.
 186           * @param WP_Post  $menu_item    The current menu item.
 187           * @param stdClass $args         An object of wp_nav_menu() arguments.
 188           * @param int      $depth        Depth of menu item. Used for padding.
 189           */
 190          $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
 191  
 192          $li_atts          = array();
 193          $li_atts['id']    = ! empty( $id ) ? $id : '';
 194          $li_atts['class'] = ! empty( $class_names ) ? $class_names : '';
 195  
 196          /**
 197           * Filters the HTML attributes applied to a menu's list item element.
 198           *
 199           * @since 6.3.0
 200           *
 201           * @param array $li_atts {
 202           *     The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored.
 203           *
 204           *     @type string $class        HTML CSS class attribute.
 205           *     @type string $id           HTML id attribute.
 206           * }
 207           * @param WP_Post  $menu_item The current menu item object.
 208           * @param stdClass $args      An object of wp_nav_menu() arguments.
 209           * @param int      $depth     Depth of menu item. Used for padding.
 210           */
 211          $li_atts       = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
 212          $li_attributes = $this->build_atts( $li_atts );
 213  
 214          $output .= $indent . '<li' . $li_attributes . '>';
 215  
 216          /** This filter is documented in wp-includes/post-template.php */
 217          $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
 218  
 219          // Save filtered value before filtering again.
 220          $the_title_filtered = $title;
 221  
 222          /**
 223           * Filters a menu item's title.
 224           *
 225           * @since 4.4.0
 226           *
 227           * @param string   $title     The menu item's title.
 228           * @param WP_Post  $menu_item The current menu item object.
 229           * @param stdClass $args      An object of wp_nav_menu() arguments.
 230           * @param int      $depth     Depth of menu item. Used for padding.
 231           */
 232          $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
 233  
 234          $atts           = array();
 235          $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
 236          $atts['rel']    = ! empty( $menu_item->xfn ) ? $menu_item->xfn : '';
 237  
 238          if ( ! empty( $menu_item->url ) ) {
 239              if ( get_privacy_policy_url() === $menu_item->url ) {
 240                  $atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
 241              }
 242  
 243              $atts['href'] = $menu_item->url;
 244          } else {
 245              $atts['href'] = '';
 246          }
 247  
 248          $atts['aria-current'] = $menu_item->current ? 'page' : '';
 249  
 250          // Add title attribute only if it does not match the link text (before or after filtering).
 251          if ( ! empty( $menu_item->attr_title )
 252              && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) )
 253              && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $the_title_filtered ) )
 254              && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) )
 255          ) {
 256              $atts['title'] = $menu_item->attr_title;
 257          } else {
 258              $atts['title'] = '';
 259          }
 260  
 261          /**
 262           * Filters the HTML attributes applied to a menu item's anchor element.
 263           *
 264           * @since 3.6.0
 265           * @since 4.1.0 The `$depth` parameter was added.
 266           *
 267           * @param array $atts {
 268           *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 269           *
 270           *     @type string $title        Title attribute.
 271           *     @type string $target       Target attribute.
 272           *     @type string $rel          The rel attribute.
 273           *     @type string $href         The href attribute.
 274           *     @type string $aria-current The aria-current attribute.
 275           * }
 276           * @param WP_Post  $menu_item The current menu item object.
 277           * @param stdClass $args      An object of wp_nav_menu() arguments.
 278           * @param int      $depth     Depth of menu item. Used for padding.
 279           */
 280          $atts       = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
 281          $attributes = $this->build_atts( $atts );
 282  
 283          $item_output  = $args->before;
 284          $item_output .= '<a' . $attributes . '>';
 285          $item_output .= $args->link_before . $title . $args->link_after;
 286          $item_output .= '</a>';
 287          $item_output .= $args->after;
 288  
 289          /**
 290           * Filters a menu item's starting output.
 291           *
 292           * The menu item's starting output only includes `$args->before`, the opening `<a>`,
 293           * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
 294           * no filter for modifying the opening and closing `<li>` for a menu item.
 295           *
 296           * @since 3.0.0
 297           *
 298           * @param string   $item_output The menu item's starting HTML output.
 299           * @param WP_Post  $menu_item   Menu item data object.
 300           * @param int      $depth       Depth of menu item. Used for padding.
 301           * @param stdClass $args        An object of wp_nav_menu() arguments.
 302           */
 303          $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args );
 304      }
 305  
 306      /**
 307       * Ends the element output, if needed.
 308       *
 309       * @since 3.0.0
 310       * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
 311       *
 312       * @see Walker::end_el()
 313       *
 314       * @param string   $output      Used to append additional content (passed by reference).
 315       * @param WP_Post  $data_object Menu item data object. Not used.
 316       * @param int      $depth       Depth of page. Not Used.
 317       * @param stdClass $args        An object of wp_nav_menu() arguments.
 318       */
 319  	public function end_el( &$output, $data_object, $depth = 0, $args = null ) {
 320          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
 321              $t = '';
 322              $n = '';
 323          } else {
 324              $t = "\t";
 325              $n = "\n";
 326          }
 327          $output .= "</li>{$n}";
 328      }
 329  
 330      /**
 331       * Builds a string of HTML attributes from an array of key/value pairs.
 332       * Empty values are ignored.
 333       *
 334       * @since 6.3.0
 335       *
 336       * @param  array $atts Optional. An array of HTML attribute key/value pairs. Default empty array.
 337       * @return string A string of HTML attributes.
 338       */
 339  	protected function build_atts( $atts = array() ) {
 340          $attribute_string = '';
 341          foreach ( $atts as $attr => $value ) {
 342              if ( false !== $value && '' !== $value && is_scalar( $value ) ) {
 343                  $value             = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
 344                  $attribute_string .= ' ' . $attr . '="' . $value . '"';
 345              }
 346          }
 347          return $attribute_string;
 348      }
 349  }


Generated : Tue Dec 24 08:20:01 2024 Cross-referenced by PHPXref