diff --git a/wp-includes/default-filters.php b/wp-includes/default-filters.php index e9ca31f5f7..5c623e371b 100644 --- a/wp-includes/default-filters.php +++ b/wp-includes/default-filters.php @@ -617,6 +617,7 @@ add_filter( 'plupload_default_settings', 'wp_show_heic_upload_error' ); // Nav menu. add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 ); +add_filter( 'nav_menu_css_class', 'wp_nav_menu_remove_menu_item_has_children_class', 10, 4 ); // Widgets. add_action( 'after_setup_theme', 'wp_setup_widgets_block_editor', 1 ); diff --git a/wp-includes/nav-menu-template.php b/wp-includes/nav-menu-template.php index b92cc0c7a8..e1a58b0668 100644 --- a/wp-includes/nav-menu-template.php +++ b/wp-includes/nav-menu-template.php @@ -196,7 +196,6 @@ 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 ) { // Fix invalid `menu_item_parent`. See: https://core.trac.wordpress.org/ticket/56926. @@ -205,34 +204,21 @@ function wp_nav_menu( $args = array() ) { } $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 ] = 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 ]; + $menu_items_with_children[ $menu_item->menu_item_parent ] = true; } } // 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 ] ) && - ( $args->depth <= 0 || $menu_items_with_children[ $menu_item->ID ] < $args->depth ) - ) { + if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) { $menu_item->classes[] = 'menu-item-has-children'; } } } - unset( $menu_items_tree, $menu_items_with_children, $menu_items, $menu_item ); + unset( $menu_items, $menu_item ); /** * Filters the sorted list of menu item objects before generating the menu's HTML. @@ -648,3 +634,37 @@ function _nav_menu_item_id_use_once( $id, $item ) { return $id; } + +/** + * Remove the `menu-item-has-children` class from bottom level menu items. + * + * @since 6.1.2 + * + * @param string[] $classes Array of the CSS classes that are applied to the menu item's `