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


Generated : Thu Jan 30 08:20:01 2025 Cross-referenced by PHPXref