Make WordPress Core

Changeset 54809


Ignore:
Timestamp:
11/11/2022 02:40:54 AM (4 years ago)
Author:
desrosj
Message:

Menus: Apply menu-item-has-children class in sub-menus.

Ensure the menu-item-has-children class is added to sub-menu items when wp_nav_menu() is called with the depth parameter specified to a non-zero value.

Follow up to [54478].

Props davidvongries, fpodhorsky, hellofromTonya, innovext, larsmqller, LeonidasMilossis, mattkeys, mukesh27, nuvoPoint, ocean90, outrankjames, petitphp, SergeyBiryukov, sippis, webmandesign, peterwilsoncc.
Merges [54801] to the 6.1 branch.
Fixes #56946.
See #28620.

Location:
branches/6.1
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.1

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

    r54478 r54809  
    205205                        $menu_items_with_children[ $menu_item->menu_item_parent ] = 1;
    206206                }
    207 
    208                 // Calculate the depth of each menu item with children
    209                 foreach ( $menu_items_with_children as $menu_item_key => &$menu_item_depth ) {
    210                         $menu_item_parent = $menu_items_tree[ $menu_item_key ];
    211                         while ( $menu_item_parent ) {
    212                                 $menu_item_depth  = $menu_item_depth + 1;
    213                                 $menu_item_parent = $menu_items_tree[ $menu_item_parent ];
    214                         }
     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 ];
    215215                }
    216216        }
  • branches/6.1/tests/phpunit/tests/menu/wp-nav-menu.php

    r54808 r54809  
    1212        static $lvl1_menu_item = 0;
    1313        static $lvl2_menu_item = 0;
     14        static $lvl3_menu_item = 0;
    1415
    1516        public static function set_up_before_class() {
     
    5051                                'menu-item-url'       => '#',
    5152                                'menu-item-parent-id' => self::$lvl1_menu_item,
     53                                'menu-item-status'    => 'publish',
     54                        )
     55                );
     56
     57                // Create lvl3 menu item.
     58                self::$lvl3_menu_item = wp_update_nav_menu_item(
     59                        self::$menu_id,
     60                        0,
     61                        array(
     62                                'menu-item-title'     => 'Lvl3 menu item',
     63                                'menu-item-url'       => '#',
     64                                'menu-item-parent-id' => self::$lvl2_menu_item,
    5265                                'menu-item-status'    => 'publish',
    5366                        )
     
    8295         *
    8396         * @ticket 28620
     97         * @ticket 56946
    8498         */
    8599        public function test_wp_nav_menu_should_have_has_children_class_without_custom_depth() {
     
    113127                $this->assertStringContainsString(
    114128                        sprintf(
     129                                '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-%1$d">',
     130                                self::$lvl2_menu_item
     131                        ),
     132                        $menu_html,
     133                        'Level 2 should be present in the HTML output and have the `menu-item-has-children` class.'
     134                );
     135
     136                $this->assertStringContainsString(
     137                        sprintf(
    115138                                '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-%1$d">',
    116                                 self::$lvl2_menu_item
    117                         ),
    118                         $menu_html,
    119                         'Level 2 should be present in the HTML output and not have the `menu-item-has-children` class since it has no children.'
     139                                self::$lvl3_menu_item
     140                        ),
     141                        $menu_html,
     142                        'Level 3 should be present in the HTML output and not have the `menu-item-has-children` class since it has no children.'
    120143                );
    121144        }
     
    126149         *
    127150         * @ticket 28620
     151         * @ticket 56946
    128152         */
    129153        public function test_wp_nav_menu_should_not_have_has_children_class_with_custom_depth() {
     
    133157                        array(
    134158                                'menu'  => self::$menu_id,
    135                                 'depth' => 2,
     159                                'depth' => 3,
    136160                                'echo'  => false,
    137161                        )
     
    149173                $this->assertStringContainsString(
    150174                        sprintf(
     175                                '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-%1$d">',
     176                                self::$lvl1_menu_item
     177                        ),
     178                        $menu_html,
     179                        'Level 1 should be present in the HTML output and have the `menu-item-has-children` class.'
     180                );
     181
     182                $this->assertStringContainsString(
     183                        sprintf(
    151184                                '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-%1$d">',
    152                                 self::$lvl1_menu_item
    153                         ),
    154                         $menu_html,
    155                         'Level 1 should be present in the HTML output and not have the `menu-item-has-children` class since it is the last item to be rendered.'
     185                                self::$lvl2_menu_item
     186                        ),
     187                        $menu_html,
     188                        'Level 2 should be present in the HTML output and not have the `menu-item-has-children` class since it is the last item to be rendered.'
    156189                );
    157190
     
    159192                        sprintf(
    160193                                '<li id="menu-item-%d"',
    161                                 self::$lvl2_menu_item
    162                         ),
    163                         $menu_html,
    164                         'Level 2 should not be present in the HTML output.'
     194                                self::$lvl3_menu_item
     195                        ),
     196                        $menu_html,
     197                        'Level 3 should not be present in the HTML output.'
    165198                );
    166199        }
Note: See TracChangeset for help on using the changeset viewer.