diff --git a/src/wp-includes/class-walker-nav-menu.php b/src/wp-includes/class-walker-nav-menu.php
index e683a3d5c8..4683970c61 100644
--- a/src/wp-includes/class-walker-nav-menu.php
+++ b/src/wp-includes/class-walker-nav-menu.php
@@ -73,9 +73,27 @@ class Walker_Nav_Menu extends Walker {
* @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 ) . '"' : '';
- $output .= "{$n}{$indent}
{$n}";
+ $atts = array();
+ $atts['class'] = ! empty( $class_names ) ? $class_names : '';
+
+ /**
+ * Filters the HTML attributes applied to a menu list element.
+ *
+ * @since 6.3.0
+ *
+ * @param array $atts {
+ * The HTML attributes applied to the `` 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}{$n}";
}
/**
@@ -156,7 +174,6 @@ class Walker_Nav_Menu extends Walker {
* @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 attribute applied to a menu item's list item element.
@@ -170,9 +187,30 @@ class Walker_Nav_Menu extends Walker {
* @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 ) . '"' : '';
- $output .= $indent . '- ';
+ $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 `
- ` 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.
+ */
+ $li_atts = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
+ $li_attributes = $this->build_atts( $li_atts );
+
+ $output .= $indent . '
- ';
$atts = array();
$atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
@@ -214,15 +252,8 @@ class Walker_Nav_Menu extends Walker {
* @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 );
@@ -286,4 +317,23 @@ class Walker_Nav_Menu extends Walker {
$output .= "
{$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;
+ }
}
diff --git a/tests/phpunit/tests/menu/walker-nav-menu.php b/tests/phpunit/tests/menu/walker-nav-menu.php
index 0c38502f9e..1fa255ef61 100644
--- a/tests/phpunit/tests/menu/walker-nav-menu.php
+++ b/tests/phpunit/tests/menu/walker-nav-menu.php
@@ -365,4 +365,117 @@ class Tests_Menu_Walker_Nav_Menu extends WP_UnitTestCase {
$this->assertStringContainsString( 'rel="privacy-policy"', $output );
}
+
+ /**
+ * Tests that `Walker_Nav_Menu::start_lvl()` applies 'nav_menu_submenu_attributes' filters.
+ *
+ * @ticket 57278
+ *
+ * @covers Walker_Nav_Menu::start_lvl
+ */
+ public function test_start_lvl_should_apply_nav_menu_submenu_attributes_filters() {
+ $output = '';
+ $args = (object) array(
+ 'before' => '',
+ 'after' => '',
+ 'link_before' => '',
+ 'link_after' => '',
+ );
+
+ $filter = new MockAction();
+ add_filter( 'nav_menu_submenu_attributes', array( $filter, 'filter' ) );
+
+ $this->walker->start_lvl( $output, 0, $args );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+ /**
+ * Tests that `Walker_Nav_Menu::start_el()` applies 'nav_menu_item_attributes' filters.
+ *
+ * @ticket 57278
+ *
+ * @covers Walker_Nav_Menu::start_el
+ */
+ public function test_start_el_should_apply_nav_menu_item_attributes_filters() {
+ $output = '';
+ $post_id = self::factory()->post->create();
+ $item = (object) array(
+ 'ID' => $post_id,
+ 'object_id' => $post_id,
+ 'title' => get_the_title( $post_id ),
+ 'target' => '',
+ 'xfn' => '',
+ 'current' => false,
+ );
+ $args = (object) array(
+ 'before' => '',
+ 'after' => '',
+ 'link_before' => '',
+ 'link_after' => '',
+ );
+
+ $filter = new MockAction();
+ add_filter( 'nav_menu_item_attributes', array( $filter, 'filter' ) );
+
+ $this->walker->start_el( $output, $item, 0, $args );
+
+ $this->assertSame( 1, $filter->get_call_count() );
+ }
+
+ /**
+ * Tests that `Walker_Nav_Menu::build_atts()` builds attributes correctly.
+ *
+ * @ticket 57278
+ *
+ * @covers Walker_Nav_Menu::build_atts
+ *
+ * @dataProvider data_build_atts_should_build_attributes
+ *
+ * @param array $atts An array of HTML attribute key/value pairs.
+ * @param string $expected The expected built attributes.
+ */
+ public function test_build_atts_should_build_attributes( $atts, $expected ) {
+ $build_atts_reflection = new ReflectionMethod( $this->walker, 'build_atts' );
+
+ $build_atts_reflection->setAccessible( true );
+ $actual = $build_atts_reflection->invoke( $this->walker, $atts );
+ $build_atts_reflection->setAccessible( false );
+
+ $this->assertSame( $expected, $actual );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array[]
+ */
+ public function data_build_atts_should_build_attributes() {
+ return array(
+ 'an empty attributes array' => array(
+ 'atts' => array(),
+ 'expected' => '',
+ ),
+ 'attributes containing a (bool) false value' => array(
+ 'atts' => array( 'disabled' => false ),
+ 'expected' => '',
+ ),
+ 'attributes containing an empty string value' => array(
+ 'atts' => array( 'id' => '' ),
+ 'expected' => '',
+ ),
+ 'attributes containing a non-scalar value' => array(
+ 'atts' => array( 'data-items' => new stdClass() ),
+ 'expected' => '',
+ ),
+ 'attributes containing a "href" -> should escape the URL' => array(
+ 'atts' => array( 'href' => 'https://example.org/A File With Spaces.pdf' ),
+ 'expected' => ' href="https://example.org/A%20File%20With%20Spaces.pdf"',
+ ),
+ 'attributes containing a non-"href" attribute -> should escape the value' => array(
+ 'atts' => array( 'id' => 'hello&goodbye' ),
+ 'expected' => ' id="hello&goodbye"',
+ ),
+ );
+ }
}