From b02beaf298674695f18768a459212cea95be988a Mon Sep 17 00:00:00 2001 From: Konstantin Obenland Date: Tue, 10 Oct 2017 17:07:31 +0000 Subject: [PATCH] Menus: Limit mapping to registered locations Weeds out orphaned locations, so their information will not continue to be mapped to future themes. Fixes #42121. git-svn-id: https://develop.svn.wordpress.org/trunk@41811 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu.php | 3 ++- tests/phpunit/tests/menu/nav-menu.php | 21 +++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 38ef5c89bc..aff8a54373 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -1084,7 +1084,8 @@ function _wp_menus_changed() { * @return array Nav menus mapped to new nav menu locations. */ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locations ) { - $registered_nav_menus = get_registered_nav_menus(); + $registered_nav_menus = get_registered_nav_menus(); + $new_nav_menu_locations = array_intersect_key( $new_nav_menu_locations, $registered_nav_menus ); // Short-circuit if there are no old nav menu location assignments to map. if ( empty( $old_nav_menu_locations ) ) { diff --git a/tests/phpunit/tests/menu/nav-menu.php b/tests/phpunit/tests/menu/nav-menu.php index ec346da53b..562c866c23 100644 --- a/tests/phpunit/tests/menu/nav-menu.php +++ b/tests/phpunit/tests/menu/nav-menu.php @@ -47,6 +47,27 @@ class Tests_Nav_Menu_Theme_Change extends WP_UnitTestCase { $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations ); } + /** + * Only registered locations should be mapped and returned. + * + * @covers wp_map_nav_menu_locations() + */ + function test_filter_registered_locations() { + $this->register_nav_menu_locations( array( 'primary', 'secondary' ) ); + $old_next_theme_nav_menu_locations = $prev_theme_nav_menu_locations = array( + 'primary' => 1, + 'secondary' => 2, + 'social' => 3, + ); + $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $old_next_theme_nav_menu_locations, $prev_theme_nav_menu_locations ); + + $expected_nav_menu_locations = array( + 'primary' => 1, + 'secondary' => 2, + ); + $this->assertEquals( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations ); + } + /** * Locations with the same name should map, switching to a theme not previously-active. *