[wp-trac] [WordPress Trac] #57045: Menu Item Depth calculation logic is not correct

WordPress Trac noreply at wordpress.org
Wed Nov 9 12:14:35 UTC 2022


#57045: Menu Item Depth calculation logic is not correct
--------------------------+-----------------------------
 Reporter:  moha12351     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Menus         |    Version:  6.1
 Severity:  minor         |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/57045>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list