From 4131900722ae5f77260a80965b94c74d8f53409f Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 6 Sep 2016 09:06:31 +0000 Subject: [PATCH] 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 --- wp-includes/class-walker-nav-menu.php | 40 +++++++++++--- wp-includes/class-walker-page.php | 40 +++++++++++--- wp-includes/nav-menu-template.php | 9 +++- wp-includes/post-template.php | 77 +++++++++++++++++---------- wp-includes/version.php | 2 +- 5 files changed, 126 insertions(+), 42 deletions(-) diff --git a/wp-includes/class-walker-nav-menu.php b/wp-includes/class-walker-nav-menu.php index 43eaa91c66..edc6f2a607 100644 --- a/wp-includes/class-walker-nav-menu.php +++ b/wp-includes/class-walker-nav-menu.php @@ -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{$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 .= "\n"; + if ( 'preserve' === $args->item_spacing ) { + $t = "\t"; + $n = "\n"; + } else { + $t = ''; + $n = ''; + } + $output .= "{$n}"; } } // Walker_Nav_Menu diff --git a/wp-includes/class-walker-page.php b/wp-includes/class-walker-page.php index fd55ccee4d..d2ad1694c7 100644 --- a/wp-includes/class-walker-page.php +++ b/wp-includes/class-walker-page.php @@ -53,8 +53,15 @@ class Walker_Page extends Walker { * Default empty array. */ public function start_lvl( &$output, $depth = 0, $args = array() ) { - $indent = str_repeat("\t", $depth); - $output .= "\n$indent{$n}"; } /** @@ -89,8 +103,15 @@ class Walker_Page extends Walker { * @param int $current_page Optional. Page ID. Default 0. */ public function start_el( &$output, $page, $depth = 0, $args = array(), $current_page = 0 ) { + if ( 'preserve' === $args['item_spacing'] ) { + $t = "\t"; + $n = "\n"; + } else { + $t = ''; + $n = ''; + } if ( $depth ) { - $indent = str_repeat( "\t", $depth ); + $indent = str_repeat( $t, $depth ); } else { $indent = ''; } @@ -175,7 +196,14 @@ class Walker_Page extends Walker { * @param array $args Optional. Array of arguments. Default empty array. */ public function end_el( &$output, $page, $depth = 0, $args = array() ) { - $output .= "\n"; + if ( 'preserve' === $args['item_spacing'] ) { + $t = "\t"; + $n = "\n"; + } else { + $t = ''; + $n = ''; + } + $output .= "{$n}"; } } diff --git a/wp-includes/nav-menu-template.php b/wp-includes/nav-menu-template.php index 47e7039430..fc62c94a91 100644 --- a/wp-includes/nav-menu-template.php +++ b/wp-includes/nav-menu-template.php @@ -40,6 +40,7 @@ require_once ABSPATH . WPINC . '/class-walker-nav-menu.php'; * in order to be selectable by the user. * @type string $items_wrap How the list items should be wrapped. Default is a ul with an id and class. * Uses printf() format with numbered placeholders. + * @type string $item_spacing Whether whitespace format the menu's HTML: 'discard' or 'preserve' (default). * } * @return object|false|void Menu output if $echo is false, false if there are no items or no menu was found. */ @@ -47,10 +48,16 @@ function wp_nav_menu( $args = array() ) { static $menu_id_slugs = array(); $defaults = array( 'menu' => '', 'container' => 'div', 'container_class' => '', 'container_id' => '', 'menu_class' => 'menu', 'menu_id' => '', - 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '', + 'echo' => true, 'fallback_cb' => 'wp_page_menu', 'before' => '', 'after' => '', 'link_before' => '', 'link_after' => '', 'items_wrap' => '', 'item_spacing' => 'preserve', 'depth' => 0, 'walker' => '', 'theme_location' => '' ); $args = wp_parse_args( $args, $defaults ); + + if ( ! in_array( $args['item_spacing'], array( 'preserve', 'discard' ), true ) ) { + // invalid value, fall back to default. + $args['item_spacing'] = $defaults['item_spacing']; + } + /** * Filters the arguments used to display a navigation menu. * diff --git a/wp-includes/post-template.php b/wp-includes/post-template.php index 7994f89c87..64732d910b 100644 --- a/wp-includes/post-template.php +++ b/wp-includes/post-template.php @@ -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 `