Menus: Add white space option to wp_nav_menu() and wp_list_pages().

Adds an `item_spacing` option to the arguments array for the functions `wp_nav_menu()`, `wp_list_pages()`, and `wp_page_menu()`. `item_spacing` is a boolean accepting either `preserve` or `discard`.

Previously, certain CSS choices could result in a site's layout changing if `wp_nav_menu()` fell back to the default `wp_list_pages()` due to differences in the whitespace within the HTML. The new argument ensures a function outputs consistant HTML while maintaining backward compatibility.

Fixes #35206.

Built from https://develop.svn.wordpress.org/trunk@38523


git-svn-id: http://core.svn.wordpress.org/trunk@38464 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson
2016-09-06 09:06:31 +00:00
parent cfe941cd6f
commit 4131900722
5 changed files with 126 additions and 42 deletions

View File

@@ -50,8 +50,15 @@ class Walker_Nav_Menu extends Walker {
* @param array $args An array of wp_nav_menu() arguments.
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
if ( 'preserve' === $args->item_spacing ) {
$t = "\t";
$n = "\n";
} else {
$t = '';
$n = '';
}
$indent = str_repeat( $t, $depth );
$output .= "{$n}{$indent}<ul class=\"sub-menu\">{$n}";
}
/**
@@ -66,8 +73,15 @@ class Walker_Nav_Menu extends Walker {
* @param array $args An array of wp_nav_menu() arguments.
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
if ( 'preserve' === $args->item_spacing ) {
$t = "\t";
$n = "\n";
} else {
$t = '';
$n = '';
}
$indent = str_repeat( $t, $depth );
$output .= "$indent</ul>{$n}";
}
/**
@@ -85,7 +99,14 @@ class Walker_Nav_Menu extends Walker {
* @param int $id Current item ID.
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
if ( 'preserve' === $args->item_spacing ) {
$t = "\t";
$n = "\n";
} else {
$t = '';
$n = '';
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
@@ -216,7 +237,14 @@ class Walker_Nav_Menu extends Walker {
* @param array $args An array of wp_nav_menu() arguments.
*/
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>\n";
if ( 'preserve' === $args->item_spacing ) {
$t = "\t";
$n = "\n";
} else {
$t = '';
$n = '';
}
$output .= "</li>{$n}";
}
} // Walker_Nav_Menu