Skip to content

Commit e3af960

Browse files
authored
Merge pull request #446 from IanDelMar/fallback
Rewrite fallback()
2 parents 7227c95 + 2de6bbf commit e3af960

2 files changed

Lines changed: 33 additions & 31 deletions

File tree

class-wp-bootstrap-navwalker.php

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -317,60 +317,62 @@ public function start_el( &$output, $item, $depth = 0, $args = null, $id = 0 ) {
317317
}
318318

319319
/**
320-
* Menu Fallback
320+
* Menu fallback.
321321
*
322322
* If this function is assigned to the wp_nav_menu's fallback_cb variable
323323
* and a menu has not been assigned to the theme location in the WordPress
324-
* menu manager the function with display nothing to a non-logged in user,
324+
* menu manager the function will display nothing to a non-logged in user,
325325
* and will add a link to the WordPress menu manager if logged in as an admin.
326326
*
327327
* @param array $args passed from the wp_nav_menu function.
328-
* @return string|void
328+
* @return string|void String when echo is false.
329329
*/
330330
public static function fallback( $args ) {
331331
if ( ! current_user_can( 'edit_theme_options' ) ) {
332332
return;
333333
}
334334

335-
// Get Arguments.
336-
$container = $args['container'];
337-
$container_id = $args['container_id'];
338-
$container_class = $args['container_class'];
339-
$menu_class = $args['menu_class'];
340-
$menu_id = $args['menu_id'];
341-
342335
// Initialize var to store fallback html.
343336
$fallback_output = '';
344337

345-
if ( $container ) {
346-
$fallback_output .= '<' . esc_attr( $container );
347-
if ( $container_id ) {
348-
$fallback_output .= ' id="' . esc_attr( $container_id ) . '"';
349-
}
350-
if ( $container_class ) {
351-
$fallback_output .= ' class="' . esc_attr( $container_class ) . '"';
338+
// Menu container opening tag.
339+
$show_container = false;
340+
if ( $args['container'] ) {
341+
/**
342+
* Filters the list of HTML tags that are valid for use as menu containers.
343+
*
344+
* @since WP 3.0.0
345+
*
346+
* @param array $tags The acceptable HTML tags for use as menu containers.
347+
* Default is array containing 'div' and 'nav'.
348+
*/
349+
$allowed_tags = apply_filters( 'wp_nav_menu_container_allowedtags', array( 'div', 'nav' ) );
350+
if ( is_string( $args['container'] ) && in_array( $args['container'], $allowed_tags, true ) ) {
351+
$show_container = true;
352+
$class = $args['container_class'] ? ' class="menu-fallback-container ' . esc_attr( $args['container_class'] ) . '"' : ' class="menu-fallback-container"';
353+
$id = $args['container_id'] ? ' id="' . esc_attr( $args['container_id'] ) . '"' : '';
354+
$fallback_output .= '<' . $args['container'] . $id . $class . '>';
352355
}
353-
$fallback_output .= '>';
354356
}
355-
$fallback_output .= '<ul';
356-
if ( $menu_id ) {
357-
$fallback_output .= ' id="' . esc_attr( $menu_id ) . '"'; }
358-
if ( $menu_class ) {
359-
$fallback_output .= ' class="' . esc_attr( $menu_class ) . '"'; }
360-
$fallback_output .= '>';
357+
358+
// The fallback menu.
359+
$class = $args['menu_class'] ? ' class="menu-fallback-menu ' . esc_attr( $args['menu_class'] ) . '"' : ' class="menu-fallback-menu"';
360+
$id = $args['menu_id'] ? ' id="' . esc_attr( $args['menu_id'] ) . '"' : '';
361+
$fallback_output .= '<ul' . $id . $class . '>';
361362
$fallback_output .= '<li class="nav-item"><a href="' . esc_url( admin_url( 'nav-menus.php' ) ) . '" class="nav-link" title="' . esc_attr__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '">' . esc_html__( 'Add a menu', 'wp-bootstrap-navwalker' ) . '</a></li>';
362363
$fallback_output .= '</ul>';
363-
if ( $container ) {
364-
$fallback_output .= '</' . esc_attr( $container ) . '>';
364+
365+
// Menu container closing tag.
366+
if ( $show_container ) {
367+
$fallback_output .= '</' . $args['container'] . '>';
365368
}
366369

367-
// If $args has 'echo' key and it's true echo, otherwise return.
370+
// if $args has 'echo' key and it's true echo, otherwise return.
368371
if ( array_key_exists( 'echo', $args ) && $args['echo'] ) {
369372
echo $fallback_output; // WPCS: XSS OK.
370-
return;
373+
} else {
374+
return $fallback_output;
371375
}
372-
373-
return $fallback_output;
374376
}
375377

376378
/**

tests/test-navwalker.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ public function test_fallback_function_output_loggedin() {
220220

221221
// rudimentary content test - confirm it opens a div with 2 expected
222222
// values and ends by closing a div.
223-
$match = ( preg_match( '/^(<div id="a_container_id" class="a_container_class">)(.*?)(<\/div>)$/', $fallback_output_echo ) ) ? true : false;
223+
$match = ( preg_match( '/^(<div id="a_container_id" class="menu-fallback-container a_container_class">)(.*?)(<\/div>)$/', $fallback_output_echo ) ) ? true : false;
224224
$this->assertTrue(
225225
$match,
226226
'Fallback method seems to create unexpected html for logged in users in echo mode.'

0 commit comments

Comments
 (0)