From 2e781774670b391676dac979985807a3fa0a2769 Mon Sep 17 00:00:00 2001 From: Jonathan Desrosiers Date: Fri, 14 Dec 2018 05:15:54 +0000 Subject: [PATCH] Nav Menus: Fix a PHP 7.3 error when switching themes. When switching themes, `wp_map_nav_menu_locations()` is used to ensure nav menus are placed in the relevant menu location. Occasionally, menus are registered to locations with numeric slugs, rather than strings. `wp_map_nav_menu_locations()` assumed it would be the latter, and ran `stripos()` on those numeric slugs. This behavior is deprecated in PHP 7.3. As this is the last PHP 7.3 error in unit tests, this commit also removes PHP 7.3 from Travis' `allowed_failures` list. Props pento, desrosj, jorbin. Merges [43899] to trunk. See #45018. git-svn-id: https://develop.svn.wordpress.org/trunk@44167 602fd350-edb4-49c9-b593-d223f7449a82 --- .travis.yml | 3 +-- src/wp-includes/nav-menu.php | 8 ++++++-- tests/phpunit/tests/menu/nav-menu.php | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 4 deletions(-) diff --git a/.travis.yml b/.travis.yml index e8a677d3af..39d61d6416 100644 --- a/.travis.yml +++ b/.travis.yml @@ -12,11 +12,11 @@ env: - WP_TRAVISCI=travis:phpunit matrix: include: - - php: 7.3 - php: 7.2 env: WP_TRAVISCI=travis:format - php: 7.1 env: WP_TRAVISCI=travis:js + - php: 7.3 - php: 7.2 - php: 7.1 - php: 7.0 @@ -34,7 +34,6 @@ matrix: - php: nightly allow_failures: - php: nightly - - php: 7.3 before_install: - | if [[ "$WP_TRAVISCI" == "travis:phpunit" ]]; then diff --git a/src/wp-includes/nav-menu.php b/src/wp-includes/nav-menu.php index 79f89d62d1..9b6eeba9b3 100644 --- a/src/wp-includes/nav-menu.php +++ b/src/wp-includes/nav-menu.php @@ -1186,7 +1186,9 @@ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locat foreach ( $registered_nav_menus as $new_location => $name ) { // ...actually match! - if ( false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) { + if ( is_string( $new_location ) && false === stripos( $new_location, $slug ) && false === stripos( $slug, $new_location ) ) { + continue; + } elseif ( is_numeric( $new_location ) && $new_location !== $slug ) { continue; } @@ -1197,7 +1199,9 @@ function wp_map_nav_menu_locations( $new_nav_menu_locations, $old_nav_menu_locat foreach ( $slug_group as $slug ) { // ... have a match as well. - if ( false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) { + if ( is_string( $location ) && false === stripos( $location, $slug ) && false === stripos( $slug, $location ) ) { + continue; + } elseif ( is_numeric( $location ) && $location !== $slug ) { continue; } diff --git a/tests/phpunit/tests/menu/nav-menu.php b/tests/phpunit/tests/menu/nav-menu.php index 57086afc28..b0b5c446fb 100644 --- a/tests/phpunit/tests/menu/nav-menu.php +++ b/tests/phpunit/tests/menu/nav-menu.php @@ -200,4 +200,29 @@ class Tests_Nav_Menu_Theme_Change extends WP_UnitTestCase { ); $this->assertEqualSets( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations ); } + + /** + * Technically possible old nav menu locations were registered numerically. + * + * @covers wp_map_nav_menu_locations() + */ + public function test_numerical_old_locations() { + $this->register_nav_menu_locations( array( 'primary', 1 ) ); + + $old_nav_menu_locations = array( + 'primary' => 1, + 'tertiary' => 2, + 0 => 3, + ); + + $next_theme_nav_menu_locations = array(); + $new_next_theme_nav_menu_locations = wp_map_nav_menu_locations( $next_theme_nav_menu_locations, $old_nav_menu_locations ); + + $expected_nav_menu_locations = array( + 'primary' => 1, + 0 => 3, + ); + + $this->assertEqualSets( $expected_nav_menu_locations, $new_next_theme_nav_menu_locations ); + } }