<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[54478] trunk: Menus: Remove `.menu-item-has-children` on `wp_nav_menu` last level menu items when `$depth` arg is used.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/54478">54478</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/54478","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>audrasjb</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-10-11 14:02:55 +0000 (Tue, 11 Oct 2022)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Menus: Remove `.menu-item-has-children` on `wp_nav_menu` last level menu items when `$depth` arg is used.
This changeset prevents `wp_nav_menu` last level menu items from having the `.menu-item-has-children` class when the `$depth` argument is used. It adds a loop in `wp_nav_menu()` to calculate the depth of each menu item with children to make sure the class is added only when applicable.
Props slobodanmanic, kucrut, iCaspar, mdgl, petitphp, audrasjb, costdev.
Fixes <a href="https://core.trac.wordpress.org/ticket/28620">#28620</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesnavmenutemplatephp">trunk/src/wp-includes/nav-menu-template.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsmenuwpnavmenuphp">trunk/tests/phpunit/tests/menu/wp-nav-menu.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesnavmenutemplatephp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/nav-menu-template.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/nav-menu-template.php 2022-10-11 13:43:20 UTC (rev 54477)
+++ trunk/src/wp-includes/nav-menu-template.php 2022-10-11 14:02:55 UTC (rev 54478)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -196,24 +196,38 @@
</span><span class="cx" style="display: block; padding: 0 10px"> _wp_menu_item_classes_by_context( $menu_items );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $sorted_menu_items = array();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $menu_items_tree = array();
</ins><span class="cx" style="display: block; padding: 0 10px"> $menu_items_with_children = array();
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( (array) $menu_items as $menu_item ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $menu_items_tree[ $menu_item->ID ] = $menu_item->menu_item_parent;
</ins><span class="cx" style="display: block; padding: 0 10px"> if ( $menu_item->menu_item_parent ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $menu_items_with_children[ $menu_item->menu_item_parent ] = true;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $menu_items_with_children[ $menu_item->menu_item_parent ] = 1;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ // 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 ];
+ }
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Add the menu-item-has-children class where applicable.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( $menu_items_with_children ) {
</span><span class="cx" style="display: block; padding: 0 10px"> foreach ( $sorted_menu_items as &$menu_item ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( isset( $menu_items_with_children[ $menu_item->ID ] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if (
+ isset( $menu_items_with_children[ $menu_item->ID ] ) &&
+ ( $args->depth <= 0 || $menu_items_with_children[ $menu_item->ID ] < $args->depth )
+ ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> $menu_item->classes[] = 'menu-item-has-children';
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- unset( $menu_items, $menu_item );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ unset( $menu_items_tree, $menu_items_with_children, $menu_items, $menu_item );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters the sorted list of menu item objects before generating the menu's HTML.
</span></span></pre></div>
<a id="trunktestsphpunittestsmenuwpnavmenuphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/menu/wp-nav-menu.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/menu/wp-nav-menu.php (rev 0)
+++ trunk/tests/phpunit/tests/menu/wp-nav-menu.php 2022-10-11 14:02:55 UTC (rev 54478)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,173 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+
+/**
+ * @group menu
+ *
+ * @covers ::wp_nav_menu
+ */
+class Tests_Menu_wpNavMenu extends WP_UnitTestCase {
+
+ static $menu_id = 0;
+ static $lvl0_menu_item = 0;
+ static $lvl1_menu_item = 0;
+ static $lvl2_menu_item = 0;
+
+ public static function set_up_before_class() {
+ parent::set_up_before_class();
+
+ // Create nav menu.
+ self::$menu_id = wp_create_nav_menu( 'test' );
+
+ // Create lvl0 menu item.
+ self::$lvl0_menu_item = wp_update_nav_menu_item(
+ self::$menu_id,
+ 0,
+ array(
+ 'menu-item-title' => 'Root menu item',
+ 'menu-item-url' => '#',
+ 'menu-item-status' => 'publish',
+ )
+ );
+
+ // Create lvl1 menu item.
+ self::$lvl1_menu_item = wp_update_nav_menu_item(
+ self::$menu_id,
+ 0,
+ array(
+ 'menu-item-title' => 'Lvl1 menu item',
+ 'menu-item-url' => '#',
+ 'menu-item-parent-id' => self::$lvl0_menu_item,
+ 'menu-item-status' => 'publish',
+ )
+ );
+
+ // Create lvl2 menu item.
+ self::$lvl2_menu_item = wp_update_nav_menu_item(
+ self::$menu_id,
+ 0,
+ array(
+ 'menu-item-title' => 'Lvl2 menu item',
+ 'menu-item-url' => '#',
+ 'menu-item-parent-id' => self::$lvl1_menu_item,
+ 'menu-item-status' => 'publish',
+ )
+ );
+
+ /**
+ * This filter is used to prevent reusing a menu item ID more that once. It cause the tests to failed
+ * after the first one since the IDs are missing from the HTML generated by `wp_nav_menu`.
+ *
+ * To allow the tests to pass, we remove the filter before running them and add it back after
+ * they ran ({@see Tests_Menu_wpNavMenu::tear_down_after_class()}).
+ */
+ remove_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once' );
+ }
+
+ public static function tear_down_after_class() {
+ wp_delete_nav_menu( self::$menu_id );
+
+ /**
+ * This filter was removed to let the tests pass and need to be added back ({@see Tests_Menu_wpNavMenu::set_up_before_class}).
+ */
+ add_filter( 'nav_menu_item_id', '_nav_menu_item_id_use_once', 10, 2 );
+
+ parent::tear_down_after_class();
+ }
+
+ /**
+ * Test all menu items containing children have the CSS class `menu-item-has-children` when displaying the menu
+ * without specifying a custom depth.
+ *
+ * @ticket 28620
+ */
+ public function test_wp_nav_menu_should_have_has_children_class_without_custom_depth() {
+
+ // Render the menu with all its hierarchy.
+ $menu_html = wp_nav_menu(
+ array(
+ 'menu' => self::$menu_id,
+ 'echo' => false,
+ )
+ );
+
+ // Level 0 should be present in the HTML output and have the `menu-item-has-children` class.
+ $this->assertStringContainsString(
+ sprintf(
+ '<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">',
+ self::$lvl0_menu_item
+ ),
+ $menu_html,
+ 'Level 0 should be present in the HTML output and have the menu-item-has-children class'
+ );
+
+ // Level 1 should be present in the HTML output and have the `menu-item-has-children` class.
+ $this->assertStringContainsString(
+ sprintf(
+ '<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">',
+ self::$lvl1_menu_item
+ ),
+ $menu_html,
+ 'Level 1 should be present in the HTML output and have the menu-item-has-children class'
+ );
+
+ // Level 2 should be present in the HTML output and not have the `menu-item-has-children` class since it has no
+ // children.
+ $this->assertStringContainsString(
+ sprintf(
+ '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-%1$d">',
+ self::$lvl2_menu_item
+ ),
+ $menu_html,
+ 'Level 2 should be present in the HTML output and not have the `menu-item-has-children` class since it has no children'
+ );
+ }
+
+ /**
+ * Tests that when displaying a menu with a custom depth, the last menu item doesn't have the CSS class
+ * `menu-item-has-children` even if it's the case when displaying the full menu.
+ *
+ * @ticket 28620
+ */
+ public function test_wp_nav_menu_should_not_have_has_children_class_with_custom_depth() {
+
+ // Render the menu limited to 1 level of hierarchy (Lvl0 + Lvl1).
+ $menu_html = wp_nav_menu(
+ array(
+ 'menu' => self::$menu_id,
+ 'depth' => 2,
+ 'echo' => false,
+ )
+ );
+
+ // Level 0 should be present in the HTML output and have the `menu-item-has-children` class.
+ $this->assertStringContainsString(
+ sprintf(
+ '<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">',
+ self::$lvl0_menu_item
+ ),
+ $menu_html,
+ 'Level 0 should be present in the HTML output and have the menu-item-has-children class'
+ );
+
+ // Level 1 should be present in the HTML output and not have the `menu-item-has-children` class since its the
+ // last item to be rendered.
+ $this->assertStringContainsString(
+ sprintf(
+ '<li id="menu-item-%1$d" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-%1$d">',
+ self::$lvl1_menu_item
+ ),
+ $menu_html,
+ 'Level 1 should be present in the HTML output and not have the `menu-item-has-children` class since its the last item to be rendered'
+ );
+
+ // Level 2 should not be present in the HTML output.
+ $this->assertStringNotContainsString(
+ sprintf(
+ '<li id="menu-item-%d"',
+ self::$lvl2_menu_item
+ ),
+ $menu_html,
+ 'Level 2 should not be present in the HTML output'
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/menu/wp-nav-menu.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span></div>
</body>
</html>