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
This commit is contained in:
Jonathan Desrosiers 2019-09-13 17:57:57 +00:00
parent 84f073cc52
commit 50a0d6c7a9
2 changed files with 11 additions and 0 deletions

View File

@ -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 );
}

View File

@ -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() {