Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle trailing slashes in rest_preload_api_request #6927

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Next Next commit
Commit progress.
  • Loading branch information
anton-vlasenko authored and Anton Vlasenko committed Jun 26, 2024
commit 2641716a68258ad727bf109a3c6cc8fa07e88c0c
5 changes: 5 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2916,6 +2916,11 @@ function rest_preload_api_request( $memo, $path ) {
return $memo;
}

// Remove trailing slashes from the "path" part of the URL.
if ( ! empty( $path_parts['path'] ) ) {
$path_parts['path'] = untrailingslashit( $path_parts['path'] );;
}

$request = new WP_REST_Request( $method, $path_parts['path'] );
if ( ! empty( $path_parts['query'] ) ) {
parse_str( $path_parts['query'], $query_params );
Expand Down
18 changes: 17 additions & 1 deletion tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,11 @@ public function test_rest_preload_api_request_removes_trailing_slashes() {
'/wp/v2/types//',
array( '/wp/v2/media///', 'OPTIONS' ),
'////',
'/wp/v2/types//?////',
'/wp/v2/types//?fields////',
'/wp/v2/types//?fields=////',
'/wp/v2/types//?_fields=foo,bar////',
'/wp/v2/types////?_fields=foo,bar&limit=1000////',
);

$preload_data = array_reduce(
Expand All @@ -978,7 +983,18 @@ public function test_rest_preload_api_request_removes_trailing_slashes() {
array()
);

$this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS', '/' ) );
$expected_urls = array(
'/wp/v2/types',
'OPTIONS',
'/',
'/wp/v2/types/?////',
'/wp/v2/types/?fields////',
'/wp/v2/types/?fields=////',
'/wp/v2/types/?_fields=foo,bar////',
'/wp/v2/types/?_fields=foo,bar&limit=1000////',
);

$this->assertSame( array_keys( $preload_data ), $expected_urls );
$this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );

$GLOBALS['wp_rest_server'] = $rest_server;
Expand Down