From c3645d036d5145c383c8d7e695e0cdd3e2dca27c Mon Sep 17 00:00:00 2001 From: audrasjb Date: Tue, 11 Oct 2022 14:04:13 +0000 Subject: [PATCH] Menus: Remove `.menu-item-has-children` on `wp_nav_menu` last level menu items when `$depth` arg is used. This changeset prevents `wp_nav_menu` last level menu items from having the `.menu-item-has-children` class when the `$depth` argument is used. It adds a loop in `wp_nav_menu()` to calculate the depth of each menu item with children to make sure the class is added only when applicable. Props slobodanmanic, kucrut, iCaspar, mdgl, petitphp, audrasjb, costdev. Fixes #28620. Built from https://develop.svn.wordpress.org/trunk@54478 git-svn-id: http://core.svn.wordpress.org/trunk@54037 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/nav-menu-template.php | 20 +++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/wp-includes/nav-menu-template.php b/wp-includes/nav-menu-template.php index 592522878f..df478a5fa4 100644 --- a/wp-includes/nav-menu-template.php +++ b/wp-includes/nav-menu-template.php @@ -196,24 +196,38 @@ function wp_nav_menu( $args = array() ) { _wp_menu_item_classes_by_context( $menu_items ); $sorted_menu_items = array(); + $menu_items_tree = array(); $menu_items_with_children = array(); foreach ( (array) $menu_items as $menu_item ) { $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; + $menu_items_tree[ $menu_item->ID ] = $menu_item->menu_item_parent; if ( $menu_item->menu_item_parent ) { - $menu_items_with_children[ $menu_item->menu_item_parent ] = true; + $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 ]; + } } } // Add the menu-item-has-children class where applicable. if ( $menu_items_with_children ) { foreach ( $sorted_menu_items as &$menu_item ) { - if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) { + if ( + isset( $menu_items_with_children[ $menu_item->ID ] ) && + ( $args->depth <= 0 || $menu_items_with_children[ $menu_item->ID ] < $args->depth ) + ) { $menu_item->classes[] = 'menu-item-has-children'; } } } - unset( $menu_items, $menu_item ); + unset( $menu_items_tree, $menu_items_with_children, $menu_items, $menu_item ); /** * Filters the sorted list of menu item objects before generating the menu's HTML. diff --git a/wp-includes/version.php b/wp-includes/version.php index 4191ca739f..f09be3e304 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.1-beta3-54477'; +$wp_version = '6.1-beta3-54478'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.