[ Index ] |
PHP Cross Reference of WordPress |
[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 * 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 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; 77 78 $output .= "{$n}{$indent}<ul$class_names>{$n}"; 79 } 80 81 /** 82 * Ends the list of after the elements are added. 83 * 84 * @since 3.0.0 85 * 86 * @see Walker::end_lvl() 87 * 88 * @param string $output Used to append additional content (passed by reference). 89 * @param int $depth Depth of menu item. Used for padding. 90 * @param stdClass $args An object of wp_nav_menu() arguments. 91 */ 92 public function end_lvl( &$output, $depth = 0, $args = null ) { 93 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 94 $t = ''; 95 $n = ''; 96 } else { 97 $t = "\t"; 98 $n = "\n"; 99 } 100 $indent = str_repeat( $t, $depth ); 101 $output .= "$indent</ul>{$n}"; 102 } 103 104 /** 105 * Starts the element output. 106 * 107 * @since 3.0.0 108 * @since 4.4.0 The {@see 'nav_menu_item_args'} filter was added. 109 * @since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id` 110 * to match parent class for PHP 8 named parameter support. 111 * 112 * @see Walker::start_el() 113 * 114 * @param string $output Used to append additional content (passed by reference). 115 * @param WP_Post $data_object Menu item data object. 116 * @param int $depth Depth of menu item. Used for padding. 117 * @param stdClass $args An object of wp_nav_menu() arguments. 118 * @param int $current_object_id Optional. ID of the current menu item. Default 0. 119 */ 120 public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) { 121 // Restores the more descriptive, specific name for use within this method. 122 $menu_item = $data_object; 123 124 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 125 $t = ''; 126 $n = ''; 127 } else { 128 $t = "\t"; 129 $n = "\n"; 130 } 131 $indent = ( $depth ) ? str_repeat( $t, $depth ) : ''; 132 133 $classes = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes; 134 $classes[] = 'menu-item-' . $menu_item->ID; 135 136 /** 137 * Filters the arguments for a single nav menu item. 138 * 139 * @since 4.4.0 140 * 141 * @param stdClass $args An object of wp_nav_menu() arguments. 142 * @param WP_Post $menu_item Menu item data object. 143 * @param int $depth Depth of menu item. Used for padding. 144 */ 145 $args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth ); 146 147 /** 148 * Filters the CSS classes applied to a menu item's list item element. 149 * 150 * @since 3.0.0 151 * @since 4.1.0 The `$depth` parameter was added. 152 * 153 * @param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element. 154 * @param WP_Post $menu_item The current menu item object. 155 * @param stdClass $args An object of wp_nav_menu() arguments. 156 * @param int $depth Depth of menu item. Used for padding. 157 */ 158 $class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) ); 159 $class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : ''; 160 161 /** 162 * Filters the ID applied to a menu item's list item element. 163 * 164 * @since 3.0.1 165 * @since 4.1.0 The `$depth` parameter was added. 166 * 167 * @param string $menu_id The ID that is applied to the menu item's `<li>` element. 168 * @param WP_Post $menu_item The current menu item. 169 * @param stdClass $args An object of wp_nav_menu() arguments. 170 * @param int $depth Depth of menu item. Used for padding. 171 */ 172 $id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth ); 173 $id = $id ? ' id="' . esc_attr( $id ) . '"' : ''; 174 175 $output .= $indent . '<li' . $id . $class_names . '>'; 176 177 $atts = array(); 178 $atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : ''; 179 $atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : ''; 180 if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) { 181 $atts['rel'] = 'noopener'; 182 } else { 183 $atts['rel'] = $menu_item->xfn; 184 } 185 $atts['href'] = ! empty( $menu_item->url ) ? $menu_item->url : ''; 186 $atts['aria-current'] = $menu_item->current ? 'page' : ''; 187 188 /** 189 * Filters the HTML attributes applied to a menu item's anchor element. 190 * 191 * @since 3.6.0 192 * @since 4.1.0 The `$depth` parameter was added. 193 * 194 * @param array $atts { 195 * The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored. 196 * 197 * @type string $title Title attribute. 198 * @type string $target Target attribute. 199 * @type string $rel The rel attribute. 200 * @type string $href The href attribute. 201 * @type string $aria-current The aria-current attribute. 202 * } 203 * @param WP_Post $menu_item The current menu item object. 204 * @param stdClass $args An object of wp_nav_menu() arguments. 205 * @param int $depth Depth of menu item. Used for padding. 206 */ 207 $atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth ); 208 209 $attributes = ''; 210 foreach ( $atts as $attr => $value ) { 211 if ( is_scalar( $value ) && '' !== $value && false !== $value ) { 212 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value ); 213 $attributes .= ' ' . $attr . '="' . $value . '"'; 214 } 215 } 216 217 /** This filter is documented in wp-includes/post-template.php */ 218 $title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID ); 219 220 /** 221 * Filters a menu item's title. 222 * 223 * @since 4.4.0 224 * 225 * @param string $title The menu item's title. 226 * @param WP_Post $menu_item The current menu item object. 227 * @param stdClass $args An object of wp_nav_menu() arguments. 228 * @param int $depth Depth of menu item. Used for padding. 229 */ 230 $title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth ); 231 232 $item_output = $args->before; 233 $item_output .= '<a' . $attributes . '>'; 234 $item_output .= $args->link_before . $title . $args->link_after; 235 $item_output .= '</a>'; 236 $item_output .= $args->after; 237 238 /** 239 * Filters a menu item's starting output. 240 * 241 * The menu item's starting output only includes `$args->before`, the opening `<a>`, 242 * the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is 243 * no filter for modifying the opening and closing `<li>` for a menu item. 244 * 245 * @since 3.0.0 246 * 247 * @param string $item_output The menu item's starting HTML output. 248 * @param WP_Post $menu_item Menu item data object. 249 * @param int $depth Depth of menu item. Used for padding. 250 * @param stdClass $args An object of wp_nav_menu() arguments. 251 */ 252 $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args ); 253 } 254 255 /** 256 * Ends the element output, if needed. 257 * 258 * @since 3.0.0 259 * @since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support. 260 * 261 * @see Walker::end_el() 262 * 263 * @param string $output Used to append additional content (passed by reference). 264 * @param WP_Post $data_object Menu item data object. Not used. 265 * @param int $depth Depth of page. Not Used. 266 * @param stdClass $args An object of wp_nav_menu() arguments. 267 */ 268 public function end_el( &$output, $data_object, $depth = 0, $args = null ) { 269 if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) { 270 $t = ''; 271 $n = ''; 272 } else { 273 $t = "\t"; 274 $n = "\n"; 275 } 276 $output .= "</li>{$n}"; 277 } 278 279 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Tue Oct 15 01:00:02 2024 | Cross-referenced by PHPXref 0.7.1 |