Make WordPress Core

Ticket #28620: 28620-alt.2.diff

File 28620-alt.2.diff, 1.8 KB (added by petitphp, 4 years ago)

Refresh of 28620-alt.diff

  • src/wp-includes/nav-menu-template.php

    diff --git src/wp-includes/nav-menu-template.php src/wp-includes/nav-menu-template.php
    index 592522878f..4b31148632 100644
    function wp_nav_menu( $args = array() ) {  
    196196        _wp_menu_item_classes_by_context( $menu_items );
    197197
    198198        $sorted_menu_items        = array();
     199        $menu_items_tree          = array();
    199200        $menu_items_with_children = array();
    200201        foreach ( (array) $menu_items as $menu_item ) {
    201202                $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
     203                $menu_items_tree[ $menu_item->ID ]           = $menu_item->menu_item_parent;
    202204                if ( $menu_item->menu_item_parent ) {
    203                         $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
     205                        $menu_items_with_children[ $menu_item->menu_item_parent ] = 1;
     206                }
     207        }
     208
     209        // Calculate the depth of each menu item with children
     210        foreach ( $menu_items_with_children as $menu_item_key => &$menu_item_depth ) {
     211                $menu_item_parent = $menu_items_tree[ $menu_item_key ];
     212                while ( $menu_item_parent ) {
     213                        $menu_item_depth  = $menu_item_depth + 1;
     214                        $menu_item_parent = $menu_items_tree[ $menu_item_parent ];
    204215                }
    205216        }
    206217
    207218        // Add the menu-item-has-children class where applicable.
    208219        if ( $menu_items_with_children ) {
    209220                foreach ( $sorted_menu_items as &$menu_item ) {
    210                         if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
     221                        if ( isset( $menu_items_with_children[ $menu_item->ID ] ) && ( $args->depth <= 0 || $menu_items_with_children[ $menu_item->ID ] < $args->depth ) ) {
    211222                                $menu_item->classes[] = 'menu-item-has-children';
    212223                        }
    213224                }
    214225        }
    215226
    216         unset( $menu_items, $menu_item );
     227        unset( $menu_items_tree, $menu_items_with_children, $menu_items, $menu_item );
    217228
    218229        /**
    219230         * Filters the sorted list of menu item objects before generating the menu's HTML.