| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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|stdClass $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 * @phpstan-param object{ 157 * ID: int|string, 158 * title: string, 159 * classes?: string[], 160 * target?: string, 161 * xfn?: string, 162 * url?: string|false, 163 * attr_title?: string, 164 * current?: bool, 165 * } $data_object 166 */ 167 public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) { 168 // Restores the more descriptive, specific name for use within this method. 169 $menu_item = $data_object; 170 171 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 172 $t = ''; 173 $n = ''; 174 } else { 175 $t = "\t"; 176 $n = "\n"; 177 } 178 $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; 179 180 $classes = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes; 181 $classes[] = 'menu-item-' . $menu_item->ID; 182 183 /** 184 * Filters the arguments for a single nav menu item. 185 * 186 * @since 4.4.0 187 * 188 * @param stdClass $args An object of wp_nav_menu() arguments. 189 * @param WP_Post|stdClass $menu_item Menu item data object. 190 * @param int $depth Depth of menu item. Used for padding. 191 */ 192 $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth ); 193 194 /** 195 * Filters the CSS classes applied to a menu item's list item element. 196 * 197 * @since 3.0.0 198 * @since 4.1.0 The `$depth` parameter was added. 199 * 200 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. 201 * @param WP_Post|stdClass $menu_item The current menu item object. 202 * @param stdClass $args An object of wp_nav_menu() arguments. 203 * @param int $depth Depth of menu item. Used for padding. 204 */ 205 $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) ); 206 207 /** 208 * Filters the ID attribute applied to a menu item's list item element. 209 * 210 * @since 3.0.1 211 * @since 4.1.0 The `$depth` parameter was added. 212 * 213 * @param string $menu_item_id The ID attribute applied to the menu item's `<li>` element. 214 * @param WP_Post|stdClass $menu_item The current menu item. 215 * @param stdClass $args An object of wp_nav_menu() arguments. 216 * @param int $depth Depth of menu item. Used for padding. 217 */ 218 $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth ); 219 220 $li_atts = array(); 221 $li_atts['id'] = ! empty( $id ) ? $id : ''; 222 $li_atts['class'] = ! empty( $class_names ) ? $class_names : ''; 223 224 /** 225 * Filters the HTML attributes applied to a menu's list item element. 226 * 227 * @since 6.3.0 228 * 229 * @param array $li_atts { 230 * The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored. 231 * 232 * @type string $class HTML CSS class attribute. 233 * @type string $id HTML id attribute. 234 * } 235 * @param WP_Post|stdClass $menu_item The current menu item object. 236 * @param stdClass $args An object of wp_nav_menu() arguments. 237 * @param int $depth Depth of menu item. Used for padding. 238 */ 239 $li_atts = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth ); 240 $li_attributes = $this->build_atts( $li_atts ); 241 242 $output .= $indent . '<li' . $li_attributes . '>'; 243 244 /** This filter is documented in wp-includes/post-template.php */ 245 $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); 246 247 // Save filtered value before filtering again. 248 $the_title_filtered = $title; 249 250 /** 251 * Filters a menu item's title. 252 * 253 * @since 4.4.0 254 * 255 * @param string $title The menu item's title. 256 * @param WP_Post|stdClass $menu_item The current menu item object. 257 * @param stdClass $args An object of wp_nav_menu() arguments. 258 * @param int $depth Depth of menu item. Used for padding. 259 */ 260 $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); 261 262 $atts = array(); 263 $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : ''; 264 $atts['rel'] = ! empty( $menu_item->xfn ) ? $menu_item->xfn : ''; 265 266 if ( ! empty( $menu_item->url ) ) { 267 if ( $this->privacy_policy_url === $menu_item->url ) { 268 $atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy'; 269 } 270 271 $atts['href'] = $menu_item->url; 272 } else { 273 $atts['href'] = ''; 274 } 275 276 $atts['aria-current'] = ! empty( $menu_item->current ) ? 'page' : ''; 277 278 // Add title attribute only if it does not match the link text (before or after filtering). 279 if ( ! empty( $menu_item->attr_title ) 280 && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $menu_item->title ) ) 281 && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $the_title_filtered ) ) 282 && trim( strtolower( $menu_item->attr_title ) ) !== trim( strtolower( $title ) ) 283 ) { 284 $atts['title'] = $menu_item->attr_title; 285 } else { 286 $atts['title'] = ''; 287 } 288 289 /** 290 * Filters the HTML attributes applied to a menu item's anchor element. 291 * 292 * @since 3.6.0 293 * @since 4.1.0 The `$depth` parameter was added. 294 * 295 * @param array $atts { 296 * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. 297 * 298 * @type string $title Title attribute. 299 * @type string $target Target attribute. 300 * @type string $rel The rel attribute. 301 * @type string $href The href attribute. 302 * @type string $aria-current The aria-current attribute. 303 * } 304 * @param WP_Post|stdClass $menu_item The current menu item object. 305 * @param stdClass $args An object of wp_nav_menu() arguments. 306 * @param int $depth Depth of menu item. Used for padding. 307 */ 308 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); 309 $attributes = $this->build_atts( $atts ); 310 311 $item_output = $args->before; 312 $item_output .= '<a' . $attributes . '>'; 313 $item_output .= $args->link_before . $title . $args->link_after; 314 $item_output .= '</a>'; 315 $item_output .= $args->after; 316 317 /** 318 * Filters a menu item's starting output. 319 * 320 * The menu item's starting output only includes `$args->before`, the opening `<a>`, 321 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is 322 * no filter for modifying the opening and closing `<li>` for a menu item. 323 * 324 * @since 3.0.0 325 * 326 * @param string $item_output The menu item's starting HTML output. 327 * @param WP_Post|stdClass $menu_item Menu item data object. 328 * @param int $depth Depth of menu item. Used for padding. 329 * @param stdClass $args An object of wp_nav_menu() arguments. 330 */ 331 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args ); 332 } 333 334 /** 335 * Ends the element output, if needed. 336 * 337 * @since 3.0.0 338 * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support. 339 * 340 * @see Walker::end_el() 341 * 342 * @param string $output Used to append additional content (passed by reference). 343 * @param WP_Post|stdClass $data_object Menu item data object. Not used. 344 * @param int $depth Depth of page. Not Used. 345 * @param stdClass $args An object of wp_nav_menu() arguments. 346 */ 347 public function end_el( &$output, $data_object, $depth = 0, $args = null ) { 348 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 349 $t = ''; 350 $n = ''; 351 } else { 352 $t = "\t"; 353 $n = "\n"; 354 } 355 $output .= "</li>{$n}"; 356 } 357 358 /** 359 * Builds a string of HTML attributes from an array of key/value pairs. 360 * Empty values are ignored. 361 * 362 * @since 6.3.0 363 * 364 * @param array $atts Optional. An array of HTML attribute key/value pairs. Default empty array. 365 * @return string A string of HTML attributes. 366 */ 367 protected function build_atts( $atts = array() ) { 368 $attribute_string = ''; 369 foreach ( $atts as $attr => $value ) { 370 if ( false !== $value && '' !== $value && is_scalar( $value ) ) { 371 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 372 $attribute_string .= ' ' . $attr . '="' . $value . '"'; 373 } 374 } 375 return $attribute_string; 376 } 377 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sun Nov 1 08:20:35 2026 | Cross-referenced by PHPXref |