Skip to content

Commit

Permalink
Add $depth param to a filter and prevent a possible error message sit…
Browse files Browse the repository at this point in the history
…uation when no menu is set to the location.
  • Loading branch information
pattonwebz committed Apr 15, 2018
1 parent 3c7924f commit d590c37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#CHANGELOG

## [3.1.0]

- Backport 2 improvements from v4.x branch:
- Prevent error `trying to get property of non-object` when no menu is set to a location using the walker.
- Add `$depth` as 4th parameter passed to `nav_menu_link_attributes`.

## [3.0.3]

- Revert composer autoload changes.
Expand Down
18 changes: 10 additions & 8 deletions wp-bootstrap-navwalker.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
* Description: A custom WordPress nav walker class to implement the Bootstrap 3 navigation style in a custom theme using the WordPress built in menu manager.
* Author: Edward McIntyre - @twittem, WP Bootstrap, William Patton - @pattonwebz
* Version: 3.0.3
* Version: 3.1.0
* Author URI: https://github.com/wp-bootstrap
* GitHub Plugin URI: https://github.com/wp-bootstrap/wp-bootstrap-navwalker
* GitHub Branch: master
Expand Down Expand Up @@ -84,7 +84,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
if ( $args->has_children ) {
if ( isset( $args->has_children ) && $args->has_children ) {
$class_names .= ' dropdown';
}
if ( in_array( 'current-menu-item', $classes, true ) ) {
Expand All @@ -105,15 +105,15 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
// If item has_children add atts to a.
if ( $args->has_children && 0 === $depth ) {
if ( isset( $args->has_children ) && $args->has_children && 0 === $depth ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
$atts['aria-haspopup'] = 'true';
} else {
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
}
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args );
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$icon_attributes = '';
$attributes = '';
foreach ( $atts as $attr => $value ) {
Expand All @@ -127,7 +127,7 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
}
}
}
$item_output = $args->before;
$item_output = isset( $args->before ) ? $args->before : '';

/*
* Glyphicons/Font-Awesome
Expand All @@ -146,9 +146,11 @@ public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0
} else {
$item_output .= '<a' . $attributes . '>';
}
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
$item_output .= ( $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
$item_output .= $args->after;
$item_output .= isset( $args->link_before ) ? $args->link_before : ''
$item_output .= apply_filters( 'the_title', $item->title, $item->ID );
$item_output .= isset( $args->link_after ) ? $args->link_after: '';
$item_output .= ( isset( $args->has_children ) && $args->has_children && 0 === $depth ) ? ' <span class="caret"></span></a>' : '</a>';
$item_output .= isset( $args->after ) ? $args->after : '';
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
} // End if().
}
Expand Down

0 comments on commit d590c37

Please sign in to comment.