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.


git-svn-id: https://develop.svn.wordpress.org/trunk@38523 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2016-09-06 09:05:45 +00:00
parent 18654d2778
commit 0c88ec217d
7 changed files with 213 additions and 41 deletions
+49 -28
View File
@@ -1138,6 +1138,7 @@ function wp_dropdown_pages( $args = '' ) {
* 'menu_order', 'post_parent', 'ID', 'rand', or 'comment_count'. Default 'post_title'.
* @type string $title_li List heading. Passing a null or empty value will result in no heading, and the list
* will not be wrapped with unordered list `<ul>` tags. Default 'Pages'.
* @type string $item_spacing Whether whitespace format the menu's HTML: 'discard' or 'preserve' (default).
* @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
* }
* @return string|void HTML list of pages.
@@ -1149,11 +1150,16 @@ function wp_list_pages( $args = '' ) {
'child_of' => 0, 'exclude' => '',
'title_li' => __( 'Pages' ), 'echo' => 1,
'authors' => '', 'sort_column' => 'menu_order, post_title',
'link_before' => '', 'link_after' => '', 'walker' => '',
'link_before' => '', 'link_after' => '', 'item_spacing' => 'preserve', 'walker' => '',
);
$r = wp_parse_args( $args, $defaults );
if ( ! in_array( $r['item_spacing'], array( 'preserve', 'discard' ), true ) ) {
// invalid value, fall back to default.
$r['item_spacing'] = $defaults['item_spacing'];
}
$output = '';
$current_page = 0;
@@ -1230,38 +1236,53 @@ function wp_list_pages( $args = '' ) {
* @param array|string $args {
* Optional. Arguments to generate a page menu. See wp_list_pages() for additional arguments.
*
* @type string $sort_column How to short the list of pages. Accepts post column names.
* Default 'menu_order, post_title'.
* @type string $menu_id ID for the div containing the page list. Default is empty string.
* @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
* @type string $container Element to use for the element containing the page list. Default 'div'.
* @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
* Default true.
* @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
* you'd like shown for the home link. 1|true defaults to 'Home'.
* @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
* @type string $link_after The HTML or text to append to $show_home text. Default empty.
* @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
* @type string $after The HTML or text to append to the menu. Default is '</ul>'.
* @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
* @type string $sort_column How to short the list of pages. Accepts post column names.
* Default 'menu_order, post_title'.
* @type string $menu_id ID for the div containing the page list. Default is empty string.
* @type string $menu_class Class to use for the element containing the page list. Default 'menu'.
* @type string $container Element to use for the element containing the page list. Default 'div'.
* @type bool $echo Whether to echo the list or return it. Accepts true (echo) or false (return).
* Default true.
* @type int|bool|string $show_home Whether to display the link to the home page. Can just enter the text
* you'd like shown for the home link. 1|true defaults to 'Home'.
* @type string $link_before The HTML or text to prepend to $show_home text. Default empty.
* @type string $link_after The HTML or text to append to $show_home text. Default empty.
* @type string $before The HTML or text to prepend to the menu. Default is '<ul>'.
* @type string $after The HTML or text to append to the menu. Default is '</ul>'.
* @type string $item_spacing Whether whitespace format the menu's HTML: 'discard' or 'preserve' (default).
* @type Walker $walker Walker instance to use for listing pages. Default empty (Walker_Page).
* }
* @return string|void HTML menu
*/
function wp_page_menu( $args = array() ) {
$defaults = array(
'sort_column' => 'menu_order, post_title',
'menu_id' => '',
'menu_class' => 'menu',
'container' => 'div',
'echo' => true,
'link_before' => '',
'link_after' => '',
'before' => '<ul>',
'after' => '</ul>',
'walker' => '',
'sort_column' => 'menu_order, post_title',
'menu_id' => '',
'menu_class' => 'menu',
'container' => 'div',
'echo' => true,
'link_before' => '',
'link_after' => '',
'before' => '<ul>',
'after' => '</ul>',
'item_spacing' => 'discard',
'walker' => '',
);
$args = wp_parse_args( $args, $defaults );
if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ) ) ) {
// invalid value, fall back to default.
$args['item_spacing'] = $defaults['item_spacing'];
}
if ( 'preserve' === $args['item_spacing'] ) {
$t = "\t";
$n = "\n";
} else {
$t = '';
$n = '';
}
/**
* Filters the arguments used to generate a page-based menu.
*
@@ -1300,7 +1321,7 @@ function wp_page_menu( $args = array() ) {
$list_args['echo'] = false;
$list_args['title_li'] = '';
$menu .= str_replace( array( "\r", "\n", "\t" ), '', wp_list_pages($list_args) );
$menu .= wp_list_pages( $list_args );
$container = sanitize_text_field( $args['container'] );
@@ -1315,7 +1336,7 @@ function wp_page_menu( $args = array() ) {
if ( isset( $args['fallback_cb'] ) &&
'wp_page_menu' === $args['fallback_cb'] &&
'ul' !== $container ) {
$args['before'] = '<ul>';
$args['before'] = "<ul>{$n}";
$args['after'] = '</ul>';
}
@@ -1331,7 +1352,7 @@ function wp_page_menu( $args = array() ) {
$attrs .= ' class="' . esc_attr( $args['menu_class'] ) . '"';
}
$menu = "<{$container}{$attrs}>" . $menu . "</{$container}>\n";
$menu = "<{$container}{$attrs}>" . $menu . "</{$container}>{$n}";
/**
* Filters the HTML output of a page-based menu.