From 3f4fe54b436eb2f2748cfcc281edaebee4ed38b8 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Sat, 24 Feb 2018 13:43:07 +0000 Subject: [PATCH] Menus: When checking if a Custom Link matches the current URL to add the `current-menu-item` class, check for decoded URL as well. Props soulseekah, campusboy1987. Fixes #43401. git-svn-id: https://develop.svn.wordpress.org/trunk@42732 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/nav-menu-template.php | 9 +++++- tests/phpunit/tests/post/nav-menu.php | 41 +++++++++++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/nav-menu-template.php b/src/wp-includes/nav-menu-template.php index 25b320e564..8624b85043 100644 --- a/src/wp-includes/nav-menu-template.php +++ b/src/wp-includes/nav-menu-template.php @@ -441,12 +441,19 @@ function _wp_menu_item_classes_by_context( &$menu_items ) { if ( is_customize_preview() ) { $_root_relative_current = strtok( untrailingslashit( $_SERVER['REQUEST_URI'] ), '?' ); } + $current_url = set_url_scheme( 'http://' . $_SERVER['HTTP_HOST'] . $_root_relative_current ); $raw_item_url = strpos( $menu_item->url, '#' ) ? substr( $menu_item->url, 0, strpos( $menu_item->url, '#' ) ) : $menu_item->url; $item_url = set_url_scheme( untrailingslashit( $raw_item_url ) ); $_indexless_current = untrailingslashit( preg_replace( '/' . preg_quote( $wp_rewrite->index, '/' ) . '$/', '', $current_url ) ); - if ( $raw_item_url && in_array( $item_url, array( $current_url, $_indexless_current, $_root_relative_current ) ) ) { + $matches = array( + $current_url, urldecode( $current_url ), + $_indexless_current, urldecode( $_indexless_current ), + $_root_relative_current, urldecode( $_root_relative_current ), + ); + + if ( $raw_item_url && in_array( $item_url, $matches ) ) { $classes[] = 'current-menu-item'; $menu_items[ $key ]->current = true; $_anc_id = (int) $menu_item->db_id; diff --git a/tests/phpunit/tests/post/nav-menu.php b/tests/phpunit/tests/post/nav-menu.php index e575f36623..d99b1a91a9 100644 --- a/tests/phpunit/tests/post/nav-menu.php +++ b/tests/phpunit/tests/post/nav-menu.php @@ -762,4 +762,45 @@ class Test_Nav_Menus extends WP_UnitTestCase { $this->assertNotContains( 'current-menu-ancestor', $post_archive_menu_item->classes ); } + /** + * Provides IRI matching data for _wp_menu_item_classes_by_context() test. + */ + function get_iri_current_menu_items() { + return array( + array( site_url( '/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82/' ) ), + array( site_url( '/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82' ) ), + array( '/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82/' ), + array( '/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82' ), + array( '/привет/' ), + array( '/привет' ), + ); + } + + /** + * @ticket 43401 + * @dataProvider get_iri_current_menu_items + */ + function test_iri_current_menu_item( $custom_link, $current = true ) { + wp_update_nav_menu_item( + $this->menu_id, 0, array( + 'menu-item-status' => 'publish', + 'menu-item-type' => 'custom', + 'menu-item-url' => $custom_link, + ) + ); + + $this->go_to( site_url( '/%D0%BF%D1%80%D0%B8%D0%B2%D0%B5%D1%82/' ) ); + + $menu_items = wp_get_nav_menu_items( $this->menu_id ); + _wp_menu_item_classes_by_context( $menu_items ); + + $classes = $menu_items[0]->classes; + + if ( $current ) { + $this->assertContains( 'current-menu-item', $classes ); + } else { + $this->assertNotContains( 'current-menu-item', $classes ); + } + } + }