diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index 7480fd5c64..675c545ce1 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -2818,6 +2818,11 @@ function rest_preload_api_request( $memo, $path ) { } } + $path = untrailingslashit( $path ); + if ( empty( $path ) ) { + $path = '/'; + } + $path_parts = parse_url( $path ); if ( false === $path_parts ) { return $memo; diff --git a/tests/phpunit/tests/rest-api.php b/tests/phpunit/tests/rest-api.php index 288c7140e8..4ec818013b 100644 --- a/tests/phpunit/tests/rest-api.php +++ b/tests/phpunit/tests/rest-api.php @@ -954,6 +954,31 @@ class Tests_REST_API extends WP_UnitTestCase { $GLOBALS['wp_rest_server'] = $rest_server; } + /** + * @ticket 51636 + */ + function test_rest_preload_api_request_removes_trailing_slashes() { + $rest_server = $GLOBALS['wp_rest_server']; + $GLOBALS['wp_rest_server'] = null; + + $preload_paths = array( + '/wp/v2/types//', + array( '/wp/v2/media///', 'OPTIONS' ), + '////', + ); + + $preload_data = array_reduce( + $preload_paths, + 'rest_preload_api_request', + array() + ); + + $this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS', '/' ) ); + $this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] ); + + $GLOBALS['wp_rest_server'] = $rest_server; + } + /** * @ticket 40614 */