wp/wp-includes/class-walker-nav-menu.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
--- a/wp/wp-includes/class-walker-nav-menu.php	Thu Sep 29 08:06:27 2022 +0200
+++ b/wp/wp-includes/class-walker-nav-menu.php	Fri Sep 05 18:40:08 2025 +0200
@@ -73,9 +73,27 @@
 		 * @param int      $depth   Depth of menu item. Used for padding.
 		 */
 		$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
-		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
+
+		$atts          = array();
+		$atts['class'] = ! empty( $class_names ) ? $class_names : '';
 
-		$output .= "{$n}{$indent}<ul$class_names>{$n}";
+		/**
+		 * Filters the HTML attributes applied to a menu list element.
+		 *
+		 * @since 6.3.0
+		 *
+		 * @param array $atts {
+		 *     The HTML attributes applied to the `<ul>` element, empty strings are ignored.
+		 *
+		 *     @type string $class    HTML CSS class attribute.
+		 * }
+		 * @param stdClass $args      An object of `wp_nav_menu()` arguments.
+		 * @param int      $depth     Depth of menu item. Used for padding.
+		 */
+		$atts       = apply_filters( 'nav_menu_submenu_attributes', $atts, $args, $depth );
+		$attributes = $this->build_atts( $atts );
+
+		$output .= "{$n}{$indent}<ul{$attributes}>{$n}";
 	}
 
 	/**
@@ -156,23 +174,43 @@
 		 * @param int      $depth     Depth of menu item. Used for padding.
 		 */
 		$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
-		$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
 
 		/**
-		 * Filters the ID applied to a menu item's list item element.
+		 * Filters the ID attribute applied to a menu item's list item element.
 		 *
 		 * @since 3.0.1
 		 * @since 4.1.0 The `$depth` parameter was added.
 		 *
-		 * @param string   $menu_id   The ID that is applied to the menu item's `<li>` element.
-		 * @param WP_Post  $menu_item The current menu item.
+		 * @param string   $menu_item_id The ID attribute applied to the menu item's `<li>` element.
+		 * @param WP_Post  $menu_item    The current menu item.
+		 * @param stdClass $args         An object of wp_nav_menu() arguments.
+		 * @param int      $depth        Depth of menu item. Used for padding.
+		 */
+		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
+
+		$li_atts          = array();
+		$li_atts['id']    = ! empty( $id ) ? $id : '';
+		$li_atts['class'] = ! empty( $class_names ) ? $class_names : '';
+
+		/**
+		 * Filters the HTML attributes applied to a menu's list item element.
+		 *
+		 * @since 6.3.0
+		 *
+		 * @param array $li_atts {
+		 *     The HTML attributes applied to the menu item's `<li>` element, empty strings are ignored.
+		 *
+		 *     @type string $class        HTML CSS class attribute.
+		 *     @type string $id           HTML id attribute.
+		 * }
+		 * @param WP_Post  $menu_item The current menu item object.
 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
 		 * @param int      $depth     Depth of menu item. Used for padding.
 		 */
-		$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
-		$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
+		$li_atts       = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
+		$li_attributes = $this->build_atts( $li_atts );
 
-		$output .= $indent . '<li' . $id . $class_names . '>';
+		$output .= $indent . '<li' . $li_attributes . '>';
 
 		$atts           = array();
 		$atts['title']  = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
@@ -182,7 +220,17 @@
 		} else {
 			$atts['rel'] = $menu_item->xfn;
 		}
-		$atts['href']         = ! empty( $menu_item->url ) ? $menu_item->url : '';
+
+		if ( ! empty( $menu_item->url ) ) {
+			if ( get_privacy_policy_url() === $menu_item->url ) {
+				$atts['rel'] = empty( $atts['rel'] ) ? 'privacy-policy' : $atts['rel'] . ' privacy-policy';
+			}
+
+			$atts['href'] = $menu_item->url;
+		} else {
+			$atts['href'] = '';
+		}
+
 		$atts['aria-current'] = $menu_item->current ? 'page' : '';
 
 		/**
@@ -204,15 +252,8 @@
 		 * @param stdClass $args      An object of wp_nav_menu() arguments.
 		 * @param int      $depth     Depth of menu item. Used for padding.
 		 */
-		$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
-
-		$attributes = '';
-		foreach ( $atts as $attr => $value ) {
-			if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
-				$value       = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
-				$attributes .= ' ' . $attr . '="' . $value . '"';
-			}
-		}
+		$atts       = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
+		$attributes = $this->build_atts( $atts );
 
 		/** This filter is documented in wp-includes/post-template.php */
 		$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
@@ -276,4 +317,23 @@
 		$output .= "</li>{$n}";
 	}
 
+	/**
+	 * Builds a string of HTML attributes from an array of key/value pairs.
+	 * Empty values are ignored.
+	 *
+	 * @since 6.3.0
+	 *
+	 * @param  array $atts Optional. An array of HTML attribute key/value pairs. Default empty array.
+	 * @return string A string of HTML attributes.
+	 */
+	protected function build_atts( $atts = array() ) {
+		$attribute_string = '';
+		foreach ( $atts as $attr => $value ) {
+			if ( false !== $value && '' !== $value && is_scalar( $value ) ) {
+				$value             = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
+				$attribute_string .= ' ' . $attr . '="' . $value . '"';
+			}
+		}
+		return $attribute_string;
+	}
 }