Menus: Allow themes and plugins to pass HTML attributes to various Nav Walker outputs.

This introduces a new set of hooks that can be used to filter various HTML elements of the Nav Walker, in order to output the desired HTML attributes:

- List items: `nav_menu_item_attributes`
- Submenu `<ul>` element: `nav_menu_submenu_attributes`

Props davidwebca, danyk4, costdev, peterwilsoncc, audrasjb, oglekler.
Fixes #57140.




git-svn-id: https://develop.svn.wordpress.org/trunk@56067 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jb Audras
2023-06-27 15:25:00 +00:00
parent 1d543a59f6
commit b66fcb55e5
2 changed files with 177 additions and 14 deletions
+64 -14
View File
@@ -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}<ul$class_names>{$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 `<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,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' . $id . $class_names . '>';
$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.
*/
$li_atts = apply_filters( 'nav_menu_item_attributes', $li_atts, $menu_item, $args, $depth );
$li_attributes = $this->build_atts( $li_atts );
$output .= $indent . '<li' . $li_attributes . '>';
$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 .= "</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;
}
}
@@ -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&amp;goodbye"',
),
);
}
}