From 88745395b829d1651f03e78dfd0a6b6d3788d79c Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 11 Nov 2022 02:40:54 +0000 Subject: [PATCH] Menus: Apply `menu-item-has-children` class in sub-menus. Ensure the `menu-item-has-children` class is added to sub-menu items when `wp_nav_menu()` is called with the `depth` parameter specified to a non-zero value. Follow up to [54478]. Props davidvongries, fpodhorsky, hellofromTonya, innovext, larsmqller, LeonidasMilossis, mattkeys, mukesh27, nuvoPoint, ocean90, outrankjames, petitphp, SergeyBiryukov, sippis, webmandesign, peterwilsoncc. Merges [54801] to the 6.1 branch. Fixes #56946. See #28620. git-svn-id: https://develop.svn.wordpress.org/branches/6.1@54809 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu-template.php | 14 +++---- tests/phpunit/tests/menu/wp-nav-menu.php | 47 ++++++++++++++++++++---- 2 files changed, 47 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/nav-menu-template.php b/src/wp-includes/nav-menu-template.php index df478a5fa4..0673bf36ed 100644 --- a/src/wp-includes/nav-menu-template.php +++ b/src/wp-includes/nav-menu-template.php @@ -204,14 +204,14 @@ function wp_nav_menu( $args = array() ) { if ( $menu_item->menu_item_parent ) { $menu_items_with_children[ $menu_item->menu_item_parent ] = 1; } + } - // Calculate the depth of each menu item with children - foreach ( $menu_items_with_children as $menu_item_key => &$menu_item_depth ) { - $menu_item_parent = $menu_items_tree[ $menu_item_key ]; - while ( $menu_item_parent ) { - $menu_item_depth = $menu_item_depth + 1; - $menu_item_parent = $menu_items_tree[ $menu_item_parent ]; - } + // Calculate the depth of each menu item with children. + foreach ( $menu_items_with_children as $menu_item_key => &$menu_item_depth ) { + $menu_item_parent = $menu_items_tree[ $menu_item_key ]; + while ( $menu_item_parent ) { + $menu_item_depth = $menu_item_depth + 1; + $menu_item_parent = $menu_items_tree[ $menu_item_parent ]; } } diff --git a/tests/phpunit/tests/menu/wp-nav-menu.php b/tests/phpunit/tests/menu/wp-nav-menu.php index 60a523bfd1..a077766567 100644 --- a/tests/phpunit/tests/menu/wp-nav-menu.php +++ b/tests/phpunit/tests/menu/wp-nav-menu.php @@ -11,6 +11,7 @@ class Tests_Menu_wpNavMenu extends WP_UnitTestCase { static $lvl0_menu_item = 0; static $lvl1_menu_item = 0; static $lvl2_menu_item = 0; + static $lvl3_menu_item = 0; public static function set_up_before_class() { parent::set_up_before_class(); @@ -53,6 +54,18 @@ class Tests_Menu_wpNavMenu extends WP_UnitTestCase { ) ); + // Create lvl3 menu item. + self::$lvl3_menu_item = wp_update_nav_menu_item( + self::$menu_id, + 0, + array( + 'menu-item-title' => 'Lvl3 menu item', + 'menu-item-url' => '#', + 'menu-item-parent-id' => self::$lvl2_menu_item, + 'menu-item-status' => 'publish', + ) + ); + /* * This filter is used to prevent reusing a menu item ID more that once. * It caused the tests to fail after the first one since the IDs are missing @@ -81,6 +94,7 @@ class Tests_Menu_wpNavMenu extends WP_UnitTestCase { * when displaying the menu without specifying a custom depth. * * @ticket 28620 + * @ticket 56946 */ public function test_wp_nav_menu_should_have_has_children_class_without_custom_depth() { @@ -112,11 +126,20 @@ class Tests_Menu_wpNavMenu extends WP_UnitTestCase { $this->assertStringContainsString( sprintf( - '