[ 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       *
 130       * @see Walker::start_el()
 131       *
 132       * @param string   $output            Used to append additional content (passed by reference).
 133       * @param WP_Post  $data_object       Menu item data object.
 134       * @param int      $depth             Depth of menu item. Used for padding.
 135       * @param stdClass $args              An object of wp_nav_menu() arguments.
 136       * @param int      $current_object_id Optional. ID of the current menu item. Default 0.
 137       */
 138  	public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
 139          // Restores the more descriptive, specific name for use within this method.
 140          $menu_item = $data_object;
 141  
 142          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
 143              $t = '';
 144              $n = '';
 145          } else {
 146              $t = "\t";
 147              $n = "\n";
 148          }
 149          $indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
 150  
 151          $classes   = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes;
 152          $classes[] = 'menu-item-' . $menu_item->ID;
 153  
 154          /**
 155           * Filters the arguments for a single nav menu item.
 156           *
 157           * @since 4.4.0
 158           *
 159           * @param stdClass $args      An object of wp_nav_menu() arguments.
 160           * @param WP_Post  $menu_item Menu item data object.
 161           * @param int      $depth     Depth of menu item. Used for padding.
 162           */
 163          $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth );
 164  
 165          /**
 166           * Filters the CSS classes applied to a menu item's list item element.
 167           *
 168           * @since 3.0.0
 169           * @since 4.1.0 The `$depth` parameter was added.
 170           *
 171           * @param string[] $classes   Array of the CSS classes that are applied to the menu item's `<li>` element.
 172           * @param WP_Post  $menu_item The current menu item object.
 173           * @param stdClass $args      An object of wp_nav_menu() arguments.
 174           * @param int      $depth     Depth of menu item. Used for padding.
 175           */
 176          $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
 177  
 178          /**
 179           * Filters the ID attribute applied to a menu item's list item element.
 180           *
 181           * @since 3.0.1
 182           * @since 4.1.0 The `$depth` parameter was added.
 183           *
 184           * @param string   $menu_item_id The ID attribute applied to the menu item's `<li>` element.
 185           * @param WP_Post  $menu_item    The current menu item.
 186           * @param stdClass $args         An object of wp_nav_menu() arguments.
 187           * @param int      $depth        Depth of menu item. Used for padding.
 188           */
 189          $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
 190  
 191          $li_atts          = array();
 192          $li_atts['id']    = ! empty( $id ) ? $id : '';
 193          $li_atts['class'] = ! empty( $class_names ) ? $class_names : '';
 194  
 195          /**
 196           * Filters the HTML attributes applied to a menu's list item element.
 197           *
 198           * @since 6.3.0
 199           *
 200           * @param array $li_atts {
 201           *     The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored.
 202           *
 203           *     @type string $class        HTML CSS class attribute.
 204           *     @type string $id           HTML id attribute.
 205           * }
 206           * @param WP_Post  $menu_item The current menu item object.
 207           * @param stdClass $args      An object of wp_nav_menu() arguments.
 208           * @param int      $depth     Depth of menu item. Used for padding.
 209           */
 210          $li_atts       = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
 211          $li_attributes = $this->build_atts( $li_atts );
 212  
 213          $output .= $indent . '<li' . $li_attributes . '>';
 214  
 215          $atts           = array();
 216          $atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
 217          $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
 218          if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
 219              $atts['rel'] = 'noopener';
 220          } else {
 221              $atts['rel'] = $menu_item->xfn;
 222          }
 223  
 224          if ( ! empty( $menu_item->url ) ) {
 225              if ( get_privacy_policy_url() === $menu_item->url ) {
 226                  $atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
 227              }
 228  
 229              $atts['href'] = $menu_item->url;
 230          } else {
 231              $atts['href'] = '';
 232          }
 233  
 234          $atts['aria-current'] = $menu_item->current ? 'page' : '';
 235  
 236          /**
 237           * Filters the HTML attributes applied to a menu item's anchor element.
 238           *
 239           * @since 3.6.0
 240           * @since 4.1.0 The `$depth` parameter was added.
 241           *
 242           * @param array $atts {
 243           *     The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
 244           *
 245           *     @type string $title        Title attribute.
 246           *     @type string $target       Target attribute.
 247           *     @type string $rel          The rel attribute.
 248           *     @type string $href         The href attribute.
 249           *     @type string $aria-current The aria-current attribute.
 250           * }
 251           * @param WP_Post  $menu_item The current menu item object.
 252           * @param stdClass $args      An object of wp_nav_menu() arguments.
 253           * @param int      $depth     Depth of menu item. Used for padding.
 254           */
 255          $atts       = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
 256          $attributes = $this->build_atts( $atts );
 257  
 258          /** This filter is documented in wp-includes/post-template.php */
 259          $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
 260  
 261          /**
 262           * Filters a menu item's title.
 263           *
 264           * @since 4.4.0
 265           *
 266           * @param string   $title     The menu item's title.
 267           * @param WP_Post  $menu_item The current menu item object.
 268           * @param stdClass $args      An object of wp_nav_menu() arguments.
 269           * @param int      $depth     Depth of menu item. Used for padding.
 270           */
 271          $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
 272  
 273          $item_output  = $args->before;
 274          $item_output .= '<a' . $attributes . '>';
 275          $item_output .= $args->link_before . $title . $args->link_after;
 276          $item_output .= '</a>';
 277          $item_output .= $args->after;
 278  
 279          /**
 280           * Filters a menu item's starting output.
 281           *
 282           * The menu item's starting output only includes `$args->before`, the opening `<a>`,
 283           * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
 284           * no filter for modifying the opening and closing `<li>` for a menu item.
 285           *
 286           * @since 3.0.0
 287           *
 288           * @param string   $item_output The menu item's starting HTML output.
 289           * @param WP_Post  $menu_item   Menu item data object.
 290           * @param int      $depth       Depth of menu item. Used for padding.
 291           * @param stdClass $args        An object of wp_nav_menu() arguments.
 292           */
 293          $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args );
 294      }
 295  
 296      /**
 297       * Ends the element output, if needed.
 298       *
 299       * @since 3.0.0
 300       * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
 301       *
 302       * @see Walker::end_el()
 303       *
 304       * @param string   $output      Used to append additional content (passed by reference).
 305       * @param WP_Post  $data_object Menu item data object. Not used.
 306       * @param int      $depth       Depth of page. Not Used.
 307       * @param stdClass $args        An object of wp_nav_menu() arguments.
 308       */
 309  	public function end_el( &$output, $data_object, $depth = 0, $args = null ) {
 310          if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
 311              $t = '';
 312              $n = '';
 313          } else {
 314              $t = "\t";
 315              $n = "\n";
 316          }
 317          $output .= "</li>{$n}";
 318      }
 319  
 320      /**
 321       * Builds a string of HTML attributes from an array of key/value pairs.
 322       * Empty values are ignored.
 323       *
 324       * @since 6.3.0
 325       *
 326       * @param  array $atts Optional. An array of HTML attribute key/value pairs. Default empty array.
 327       * @return string A string of HTML attributes.
 328       */
 329  	protected function build_atts( $atts = array() ) {
 330          $attribute_string = '';
 331          foreach ( $atts as $attr => $value ) {
 332              if ( false !== $value && '' !== $value && is_scalar( $value ) ) {
 333                  $value             = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
 334                  $attribute_string .= ' ' . $attr . '="' . $value . '"';
 335              }
 336          }
 337          return $attribute_string;
 338      }
 339  }


Generated : Sat Apr 20 08:20:01 2024 Cross-referenced by PHPXref