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
12 changes: 12 additions & 0 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2906,6 +2906,7 @@ function rest_preload_api_request( $memo, $path ) {
}
}

// Remove trailing slashes at the end of the REST API path (query part).
$path = untrailingslashit( $path );
if ( empty( $path ) ) {
$path = '/';
Expand All @@ -2916,6 +2917,17 @@ function rest_preload_api_request( $memo, $path ) {
return $memo;
}

if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) {
$original_path = $path_parts['path'];
// Remove trailing slashes from the "path" part of the REST API path.
$path_parts['path'] = untrailingslashit( $path_parts['path'] );

if ( str_starts_with( $path, $original_path ) ) {
// Replace the original path with the trimmed path.
$path = $path_parts['path'] . substr( $path, strlen( $original_path ) );
}
}

$request = new WP_REST_Request( $method, $path_parts['path'] );
if ( ! empty( $path_parts['query'] ) ) {
parse_str( $path_parts['query'], $query_params );
Expand Down
46 changes: 31 additions & 15 deletions tests/phpunit/tests/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -960,30 +960,46 @@ public function test_rest_preload_api_request_with_method() {
}

/**
* @dataProvider data_rest_preload_api_request_removes_trailing_slashes
*
* @ticket 51636
* @ticket 57048
*
* @param string $preload_path The path to preload.
* @param array|string $expected_preload_path Expected path after preloading.
*/
public function test_rest_preload_api_request_removes_trailing_slashes() {
public function test_rest_preload_api_request_removes_trailing_slashes( $preload_path, $expected_preload_path ) {
$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'] );
$actual_preload_path = rest_preload_api_request( array(), $preload_path );
if ( '' !== $preload_path ) {
$actual_preload_path = key( $actual_preload_path );
}
$this->assertSame( $expected_preload_path, $actual_preload_path );

$GLOBALS['wp_rest_server'] = $rest_server;
}

/**
* Data provider.
*
* @return array
*/
public function data_rest_preload_api_request_removes_trailing_slashes() {
return array(
'no query part' => array( '/wp/v2/types//', '/wp/v2/types' ),
'no query part, more slashes' => array( '/wp/v2/media///', '/wp/v2/media' ),
'only slashes' => array( '////', '/' ),
'empty path' => array( '', array() ),
'no query parameters' => array( '/wp/v2/types//?////', '/wp/v2/types?' ),
'no query parameters, with slashes' => array( '/wp/v2/types//?fields////', '/wp/v2/types?fields' ),
'query parameters with no values' => array( '/wp/v2/types//?fields=////', '/wp/v2/types?fields=' ),
'single query parameter' => array( '/wp/v2/types//?_fields=foo,bar////', '/wp/v2/types?_fields=foo,bar' ),
'multiple query parameters' => array( '/wp/v2/types////?_fields=foo,bar&limit=1000////', '/wp/v2/types?_fields=foo,bar&limit=1000' ),
);
}

/**
* @ticket 40614
*/
Expand Down
Loading