From 50a0d6c7a96fc82dc5fa9105ee8040ac41067452 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 13 Sep 2019 17:57:57 +0000 Subject: [PATCH] Menus: Nav menu locations should not be integers. When nav menu location slugs are integers, some hard to debug results can occur. `register_nav_menus()` utilizes `array_merge()` which renumbers numeric indexes, starting from 0. Because of this, numeric menu locations will almost always be changed. This change introduces a `_doing_it_wrong()` notice to inform developers that nav menu locations should always be strings. Props audrasjb, desrosj, welcher. Fixes #45361. git-svn-id: https://develop.svn.wordpress.org/trunk@46102 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu.php | 7 +++++++ tests/phpunit/tests/menu/nav-menu.php | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index f130d2f3e1..b618f96e9b 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -92,6 +92,13 @@ function register_nav_menus( $locations = array() ) { add_theme_support( 'menus' ); + foreach ( $locations as $key => $value ) { + if ( is_int( $key ) ) { + _doing_it_wrong( __FUNCTION__, __( 'Nav menu locations must be strings.' ), '5.3' ); + break; + } + } + $_wp_registered_nav_menus = array_merge( (array) $_wp_registered_nav_menus, $locations ); } diff --git a/tests/phpunit/tests/menu/nav-menu.php b/tests/phpunit/tests/menu/nav-menu.php index 2627f9f21a..4577f5e9a9 100644 --- a/tests/phpunit/tests/menu/nav-menu.php +++ b/tests/phpunit/tests/menu/nav-menu.php @@ -182,6 +182,8 @@ class Tests_Nav_Menu_Theme_Change extends WP_UnitTestCase { /** * Technically possible to register menu locations numerically. * + * @expectedIncorrectUsage register_nav_menus + * * @covers ::wp_map_nav_menu_locations() */ function test_numerical_locations() { @@ -205,6 +207,8 @@ class Tests_Nav_Menu_Theme_Change extends WP_UnitTestCase { /** * Technically possible old nav menu locations were registered numerically. * + * @expectedIncorrectUsage register_nav_menus + * * @covers wp_map_nav_menu_locations() */ public function test_numerical_old_locations() {