Menus: Prevent infinite loop in menus.

This modifies how the `menu-item-has-children` class is removed from bottom level menu items. Instead of removing the class within `wp_nav_menu()` a filter is applied to the `nav_menu_css_class` hook to remove the class as required.

Introduces `wp_nav_menu_remove_menu_item_has_children_class()` for removing the class.

Reverts source code changes in [54478,54801], the tests are retained.

Props davidbinda, SergeyBiryukov, mhkuu, JeffPaul, jmdodd, priethor, desrosj, hellofromTonya, azaozz, peterwilsoncc.
Fixes #56926.
See #28620.


Built from https://develop.svn.wordpress.org/trunk@54999


git-svn-id: http://core.svn.wordpress.org/trunk@54532 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2022-12-16 02:40:15 +00:00
parent 72948dee1e
commit 4d4ddb80dc
3 changed files with 39 additions and 18 deletions

View File

@ -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 );

View File

@ -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 `<li>` element.
* @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.
* @return string[] Modified nav menu classes.
*/
function wp_nav_menu_remove_menu_item_has_children_class( $classes, $menu_item, $args, $depth ) {
// Max-depth is 1-based.
$max_depth = isset( $args->depth ) ? (int) $args->depth : 0;
// Depth is 0-based so needs to be increased by one.
$depth = $depth + 1;
// Complete menu tree is displayed.
if ( 0 === $max_depth ) {
return $classes;
}
/*
* Remove the `menu-item-has-children` class from bottom level menu items.
* -1 is used to display all menu items in one level so the class should
* be removed from all menu items.
*/
if ( -1 === $max_depth || $depth >= $max_depth ) {
$classes = array_diff( $classes, array( 'menu-item-has-children' ) );
}
return $classes;
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.2-alpha-54998';
$wp_version = '6.2-alpha-54999';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.