Skip to content

Commit efc53a3

Browse files
authored
Version bump 4.1.0
Version 4.1.0
2 parents 0359261 + 87228b0 commit efc53a3

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
#CHANGELOG
2+
3+
## [4.1.0]
4+
- Prevent error `trying to get property of non-object` when no menu is set to a location using the walker.
5+
- Add `$depth` as 4th parameter passed to `nav_menu_link_attributes`.
6+
- Add support for `dropdown-item-text` linkmod type.
27
## [4.0.3]
38
- Drop composer class autoload statement.
49

class-wp-bootstrap-navwalker.php

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
1212
* Description: A custom WordPress nav walker class to implement the Bootstrap 4 navigation style in a custom theme using the WordPress built in menu manager.
1313
* Author: Edward McIntyre - @twittem, WP Bootstrap, William Patton - @pattonwebz
14-
* Version: 4.0.3
14+
* Version: 4.1.0
1515
* Author URI: https://github.com/wp-bootstrap
1616
* GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
1717
* GitHub Branch: master
@@ -133,7 +133,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
133133
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
134134

135135
// Add .dropdown or .active classes where they are needed.
136-
if ( $args->has_children ) {
136+
if ( isset( $args->has_children ) && $args->has_children ) {
137137
$classes[] = 'dropdown';
138138
}
139139
if ( in_array( 'current-menu-item', $classes, true ) || in_array( 'current-menu-parent', $classes, true ) ) {
@@ -181,7 +181,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
181181
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
182182
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
183183
// If item has_children add atts to <a>.
184-
if ( $args->has_children && 0 === $depth && $args->depth > 1 ) {
184+
if ( isset( $args->has_children ) && $args->has_children && 0 === $depth && $args->depth > 1 ) {
185185
$atts['href'] = '#';
186186
$atts['data-toggle'] = 'dropdown';
187187
$atts['aria-haspopup'] = 'true';
@@ -201,7 +201,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
201201
// update atts of this item based on any custom linkmod classes.
202202
$atts = self::update_atts_for_linkmod_type( $atts, $linkmod_classes );
203203
// Allow filtering of the $atts array before using it.
204-
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
204+
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
205205

206206
// Build a string of html containing all the atts for the item.
207207
$attributes = '';
@@ -220,8 +220,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
220220
/**
221221
* START appending the internal item contents to the output.
222222
*/
223-
$item_output = $args->before;
224-
223+
$item_output = isset( $args->before ) ? $args->before : '';
225224
/**
226225
* This is the start of the internal nav item. Depending on what
227226
* kind of linkmod we have we may need different wrapper elements.
@@ -272,8 +271,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
272271
}
273272

274273
// Put the item contents into $output.
275-
$item_output .= $args->link_before . $icon_html . $title . $args->link_after;
276-
274+
$item_output .= isset( $args->link_before ) ? $args->link_before . $icon_html . $title . $args->link_after : '';
277275
/**
278276
* This is the end of the internal nav item. We need to close the
279277
* correct element depending on the type of link or link mod.
@@ -286,11 +284,11 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
286284
$item_output .= '</a>';
287285
}
288286

289-
$item_output .= $args->after;
287+
$item_output .= isset( $args->after ) ? $args->after : '';
288+
290289
/**
291290
* END appending the internal item contents to the output.
292291
*/
293-
294292
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
295293

296294
}
@@ -407,7 +405,7 @@ private function seporate_linkmods_and_icons_from_classes( $classes, &$linkmod_c
407405
// Test for .disabled or .sr-only classes.
408406
$linkmod_classes[] = $class;
409407
unset( $classes[ $key ] );
410-
} elseif ( preg_match( '/^dropdown-header|^dropdown-divider/i', $class ) && $depth > 0 ) {
408+
} elseif ( preg_match( '/^dropdown-header|^dropdown-divider|^dropdown-item-text/i', $class ) && $depth > 0 ) {
411409
// Test for .dropdown-header or .dropdown-divider and a
412410
// depth greater than 0 - IE inside a dropdown.
413411
$linkmod_classes[] = $class;
@@ -448,6 +446,8 @@ private function get_linkmod_type( $linkmod_classes = array() ) {
448446
$linkmod_type = 'dropdown-header';
449447
} elseif ( 'dropdown-divider' === $link_class ) {
450448
$linkmod_type = 'dropdown-divider';
449+
} elseif ( 'dropdown-item-text' === $link_class ) {
450+
$linkmod_type = 'dropdown-item-text';
451451
}
452452
}
453453
}
@@ -479,7 +479,7 @@ private function update_atts_for_linkmod_type( $atts = array(), $linkmod_classes
479479
// Convert link to '#' and unset open targets.
480480
$atts['href'] = '#';
481481
unset( $atts['target'] );
482-
} elseif ( 'dropdown-header' === $link_class || 'dropdown-divider' === $link_class ) {
482+
} elseif ( 'dropdown-header' === $link_class || 'dropdown-divider' === $link_class || 'dropdown-item-text' === $link_class ) {
483483
// Store a type flag and unset href and target.
484484
unset( $atts['href'] );
485485
unset( $atts['target'] );
@@ -517,7 +517,9 @@ private function wrap_for_screen_reader( $text = '' ) {
517517
*/
518518
private function linkmod_element_open( $linkmod_type, $attributes = '' ) {
519519
$output = '';
520-
if ( 'dropdown-header' === $linkmod_type ) {
520+
if ( 'dropdown-item-text' === $linkmod_type ) {
521+
$output .= '<span class="dropdown-item-text"' . $attributes . '>';
522+
} elseif ( 'dropdown-header' === $linkmod_type ) {
521523
// For a header use a span with the .h6 class instead of a real
522524
// header tag so that it doesn't confuse screen readers.
523525
$output .= '<span class="dropdown-header h6"' . $attributes . '>';
@@ -539,7 +541,7 @@ private function linkmod_element_open( $linkmod_type, $attributes = '' ) {
539541
*/
540542
private function linkmod_element_close( $linkmod_type ) {
541543
$output = '';
542-
if ( 'dropdown-header' === $linkmod_type ) {
544+
if ( 'dropdown-header' === $linkmod_type || 'dropdown-item-text' === $linkmod_type ) {
543545
// For a header use a span with the .h6 class instead of a real
544546
// header tag so that it doesn't confuse screen readers.
545547
$output .= '</span>';

tests/test-navwalker.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public function setUp() {
3939
$this->valid_linkmod_typeflags = array(
4040
'dropdown-header',
4141
'dropdown-divider',
42+
'dropdown-item-text'
4243
);
4344

4445
// array of all possible linkmods, including the valid typeflags.
@@ -592,6 +593,8 @@ public function test_linkmod_elements_open_and_close_successfully() {
592593
$this->assertNotEmpty( $header_open, 'Got empty string for opener of ' . $this->valid_linkmod_typeflags[0] );
593594
$divider_open = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $this->valid_linkmod_typeflags[1], 'stringOfAttributes' ) );
594595
$this->assertNotEmpty( $divider_open, 'Got empty string for opener of ' . $this->valid_linkmod_typeflags[1] );
596+
$text_open = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( $this->valid_linkmod_typeflags[2], 'stringOfAttributes' ) );
597+
$this->assertNotEmpty( $divider_open, 'Got empty string for opener of ' . $this->valid_linkmod_typeflags[2] );
595598

596599
// test that that an unknown linkmod type being passed results in no output.
597600
$nonexistent_linkmod_type_open = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( 'nonexistentlinkmodtype', 'stringOfAttributes' ) );
@@ -605,21 +608,30 @@ public function test_linkmod_elements_open_and_close_successfully() {
605608
$this->assertNotEmpty( $header_close, 'Got empty string for closer of ' . $this->valid_linkmod_typeflags[0] );
606609
$divider_close = $method_close->invokeArgs( $wp_bootstrap_navwalker, array( $this->valid_linkmod_typeflags[1] ) );
607610
$this->assertNotEmpty( $divider_close, 'Got empty string for closer of ' . $this->valid_linkmod_typeflags[1] );
611+
$text_close = $method_close->invokeArgs( $wp_bootstrap_navwalker, array( $this->valid_linkmod_typeflags[2] ) );
612+
$this->assertNotEmpty( $divider_close, 'Got empty string for closer of ' . $this->valid_linkmod_typeflags[2] );
608613

609614
// test that that an unknown linkmod type being passed results in no output.
610615
$nonexistent_linkmod_type_close = $method_open->invokeArgs( $wp_bootstrap_navwalker, array( 'nonexistentlinkmodtype' ) );
611616
$this->assertEmpty( $nonexistent_linkmod_type_close, 'Expected empty string when using non-existent linkmod type.' );
612617

618+
// dropdown-header should be a span.
613619
$this->assertRegExp(
614620
'/^(<span(.*?)>)(.*?)(<\/span>)$/',
615621
$header_open . $header_close,
616622
'The opener and closer for ' . $this->valid_linkmod_typeflags[0] . ' does not seem to match expected elements.'
617623
);
618-
624+
// dropdown-divider should be a div.
619625
$this->assertRegExp(
620626
'/^(<div(.*?)>)(.*?)(<\/div>)$/',
621627
$divider_open . $divider_close,
622-
'The opener and closer for ' . $this->valid_linkmod_typeflags[1] . ' linkmods does not seem to match expected elements.'
628+
'The opener and closer for ' . $this->valid_linkmod_typeflags[1] . ' does not seem to match expected elements.'
629+
);
630+
// dropdown-item-text should be a span.
631+
$this->assertRegExp(
632+
'/^(<span(.*?)>)(.*?)(<\/span>)$/',
633+
$text_open . $text_close,
634+
'The opener and closer for ' . $this->valid_linkmod_typeflags[2] . ' does not seem to match expected elements.'
623635
);
624636
}
625637
}

0 commit comments

Comments
 (0)