Make WordPress Core

Opened 23 months ago

Closed 23 months ago

Last modified 23 months ago

#57045 closed defect (bug) (duplicate)

Menu Item Depth calculation logic is not correct

Reported by: moha12351's profile moha12351 Owned by:
Milestone: Priority: normal
Severity: minor Version: 6.1
Component: Menus Keywords: has-patch
Focuses: Cc:

Description

I have checked the WordPress core wp-includes/nav-menu-template.php file and found some issues in the menu item depth calculation login

Current code from lines 201 to 216

  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 ] = 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 ];
			}
		}
	}

My Solution

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 ] = 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 ];
		}
	}

screenshots: https://drive.google.com/file/d/1h0KPw8VKLSHN2ODiL6u4Fbm8VbjNejHA/view

screenshots: https://drive.google.com/file/d/1KjPsKANbE6pgxMC9-dJ9O8TdMYK6VX8o/view

Attachments (1)

nav-menu-template.php (23.3 KB) - added by moha12351 23 months ago.

Download all attachments as: .zip

Change History (4)

This ticket was mentioned in PR #3591 on WordPress/wordpress-develop by @moha12351.


23 months ago
#1

  • Keywords has-patch added

I just fixed the menu item depth calculation logic

Fixes: https://core.trac.wordpress.org/ticket/57045

#2 @petitphp
23 months ago

  • Resolution set to duplicate
  • Status changed from new to closed

Hello @moha12351,

Welcome to WordPress Core Trac! Thank you for reporting this issue.

The issue is known and being tracked in #56946. It's currently milestoned for WP 6.1.1.

A fix as be provided and is being tested.

To keep discussions, testing, and fixes centralized in one place, I've closed this ticket as a duplicate.

I invite you to contribute in #56946. Any additional information you can provide is helpful and appreciated.

Thanks again

#3 @SergeyBiryukov
23 months ago

  • Milestone Awaiting Review deleted
Note: See TracTickets for help on using tickets.