From 5ed53d6479485c0714c1e6f6ae898c479851316b Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Mon, 8 Nov 2021 21:59:23 +0100 Subject: [PATCH 1/5] Fix placement of .active class for dropdown items --- class-wp-bootstrap-navwalker.php | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/class-wp-bootstrap-navwalker.php b/class-wp-bootstrap-navwalker.php index 9b0d424..b836aa0 100644 --- a/class-wp-bootstrap-navwalker.php +++ b/class-wp-bootstrap-navwalker.php @@ -152,6 +152,30 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { // Join any icon classes plucked from $classes into a string. $icon_class_string = join( ' ', $icon_classes ); + // Whether the current item is a dropdown. + $is_dropdown = false; + if ( $this->has_children && 1 !== $args->depth ) { + $is_dropdown = true; + if ( $depth >= $args->depth - 1 && $args->depth !== 0 ) { + $is_dropdown = false; + } + } + + // Whether the current item is a dropdown item. + $is_dropdown_item = false; + if ( ! ( $this->has_children && 0 === $depth ) && $depth > 0 ) { + $is_dropdown_item = true; + } + + // Whether the current item is active or the item is an ancestor of + // the current item. + $is_active = false; + if ( $item->current || $item->current_item_ancestor ) { + if ( ! ( $item->current_item_ancestor && 1 === $args->depth ) ) { + $is_active = true; + } + } + /** * Filters the arguments for a single nav menu item. * @@ -165,11 +189,12 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { */ $args = apply_filters( 'nav_menu_item_args', $args, $item, $depth ); - // Add .dropdown or .active classes where they are needed. - if ( $this->has_children ) { + if ( $is_dropdown ) { $classes[] = 'dropdown'; } - if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) { + + if ( $is_active && ! $is_dropdown_item ) { + // For dropdown items the .active class is set on the a tag. $classes[] = 'active'; } @@ -226,6 +251,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { // For items in dropdowns use .dropdown-item instead of .nav-link. if ( $depth > 0 ) { $atts['class'] = 'dropdown-item'; + $atts['class'] .= $is_active ? ' active' : ''; } else { $atts['class'] = 'nav-link'; } From a19dba8b4a7c20637cc4ece1bfbb1a1b4e2fd8b0 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Mon, 8 Nov 2021 22:03:17 +0100 Subject: [PATCH 2/5] Fix dropdown being set when children are not displayed --- class-wp-bootstrap-navwalker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-wp-bootstrap-navwalker.php b/class-wp-bootstrap-navwalker.php index b836aa0..1156d1f 100644 --- a/class-wp-bootstrap-navwalker.php +++ b/class-wp-bootstrap-navwalker.php @@ -236,7 +236,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { } // If the item has_children add atts to . - if ( $this->has_children && 0 === $depth ) { + if ( $is_dropdown ) { $atts['href'] = '#'; $atts['data-toggle'] = 'dropdown'; $atts['aria-expanded'] = 'false'; From a369503b6b3cc8005f42c5eae0708af38c8aa8f9 Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Mon, 8 Nov 2021 23:00:05 +0100 Subject: [PATCH 3/5] Use $is_dropdown_item --- class-wp-bootstrap-navwalker.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/class-wp-bootstrap-navwalker.php b/class-wp-bootstrap-navwalker.php index 1156d1f..679b3c4 100644 --- a/class-wp-bootstrap-navwalker.php +++ b/class-wp-bootstrap-navwalker.php @@ -248,8 +248,9 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { } $atts['href'] = ! empty( $item->url ) ? $item->url : '#'; + // For items in dropdowns use .dropdown-item instead of .nav-link. - if ( $depth > 0 ) { + if ( $is_dropdown_item ) { $atts['class'] = 'dropdown-item'; $atts['class'] .= $is_active ? ' active' : ''; } else { From 979e3a0d16f575fd577bea79c2c4063ed8aa38ca Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Mon, 8 Nov 2021 23:00:29 +0100 Subject: [PATCH 4/5] Fix wpcs --- class-wp-bootstrap-navwalker.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/class-wp-bootstrap-navwalker.php b/class-wp-bootstrap-navwalker.php index 679b3c4..8165399 100644 --- a/class-wp-bootstrap-navwalker.php +++ b/class-wp-bootstrap-navwalker.php @@ -156,7 +156,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { $is_dropdown = false; if ( $this->has_children && 1 !== $args->depth ) { $is_dropdown = true; - if ( $depth >= $args->depth - 1 && $args->depth !== 0 ) { + if ( $depth >= $args->depth - 1 && 0 !== $args->depth ) { $is_dropdown = false; } } @@ -251,7 +251,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { // For items in dropdowns use .dropdown-item instead of .nav-link. if ( $is_dropdown_item ) { - $atts['class'] = 'dropdown-item'; + $atts['class'] = 'dropdown-item'; $atts['class'] .= $is_active ? ' active' : ''; } else { $atts['class'] = 'nav-link'; From e6f21f80d3db6e55607d42dcaa85843d80cc3cff Mon Sep 17 00:00:00 2001 From: IanDelMar <42134098+IanDelMar@users.noreply.github.com> Date: Tue, 9 Nov 2021 06:16:09 +0100 Subject: [PATCH 5/5] Fix variable description --- class-wp-bootstrap-navwalker.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/class-wp-bootstrap-navwalker.php b/class-wp-bootstrap-navwalker.php index 8165399..53c9978 100644 --- a/class-wp-bootstrap-navwalker.php +++ b/class-wp-bootstrap-navwalker.php @@ -168,7 +168,7 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) { } // Whether the current item is active or the item is an ancestor of - // the current item. + // an active item. $is_active = false; if ( $item->current || $item->current_item_ancestor ) { if ( ! ( $item->current_item_ancestor && 1 === $args->depth ) ) {