Skip to content

Commit 27c566f

Browse files
committed
REST API: Remove trailing slashes when preloading requests and there is a query string.
Follow-up to [51648], see #51636. Props antonvlasenko, swissspidy, spacedmonkey. Fixes #57048. git-svn-id: https://develop.svn.wordpress.org/trunk@59457 602fd350-edb4-49c9-b593-d223f7449a82
1 parent e80aa00 commit 27c566f

File tree

2 files changed

+40
-15
lines changed

2 files changed

+40
-15
lines changed

src/wp-includes/rest-api.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2935,6 +2935,7 @@ function rest_preload_api_request( $memo, $path ) {
29352935
}
29362936
}
29372937

2938+
// Remove trailing slashes at the end of the REST API path (query part).
29382939
$path = untrailingslashit( $path );
29392940
if ( empty( $path ) ) {
29402941
$path = '/';
@@ -2945,6 +2946,14 @@ function rest_preload_api_request( $memo, $path ) {
29452946
return $memo;
29462947
}
29472948

2949+
if ( isset( $path_parts['path'] ) && '/' !== $path_parts['path'] ) {
2950+
// Remove trailing slashes from the "path" part of the REST API path.
2951+
$path_parts['path'] = untrailingslashit( $path_parts['path'] );
2952+
$path = str_contains( $path, '?' ) ?
2953+
$path_parts['path'] . '?' . ( $path_parts['query'] ?? '' ) :
2954+
$path_parts['path'];
2955+
}
2956+
29482957
$request = new WP_REST_Request( $method, $path_parts['path'] );
29492958
if ( ! empty( $path_parts['query'] ) ) {
29502959
parse_str( $path_parts['query'], $query_params );

tests/phpunit/tests/rest-api.php

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -960,30 +960,46 @@ public function test_rest_preload_api_request_with_method() {
960960
}
961961

962962
/**
963+
* @dataProvider data_rest_preload_api_request_removes_trailing_slashes
964+
*
963965
* @ticket 51636
966+
* @ticket 57048
967+
*
968+
* @param string $preload_path The path to preload.
969+
* @param array|string $expected_preload_path Expected path after preloading.
964970
*/
965-
public function test_rest_preload_api_request_removes_trailing_slashes() {
971+
public function test_rest_preload_api_request_removes_trailing_slashes( $preload_path, $expected_preload_path ) {
966972
$rest_server = $GLOBALS['wp_rest_server'];
967973
$GLOBALS['wp_rest_server'] = null;
968974

969-
$preload_paths = array(
970-
'/wp/v2/types//',
971-
array( '/wp/v2/media///', 'OPTIONS' ),
972-
'////',
973-
);
974-
975-
$preload_data = array_reduce(
976-
$preload_paths,
977-
'rest_preload_api_request',
978-
array()
979-
);
980-
981-
$this->assertSame( array_keys( $preload_data ), array( '/wp/v2/types', 'OPTIONS', '/' ) );
982-
$this->assertArrayHasKey( '/wp/v2/media', $preload_data['OPTIONS'] );
975+
$actual_preload_path = rest_preload_api_request( array(), $preload_path );
976+
if ( '' !== $preload_path ) {
977+
$actual_preload_path = key( $actual_preload_path );
978+
}
979+
$this->assertSame( $expected_preload_path, $actual_preload_path );
983980

984981
$GLOBALS['wp_rest_server'] = $rest_server;
985982
}
986983

984+
/**
985+
* Data provider.
986+
*
987+
* @return array
988+
*/
989+
public static function data_rest_preload_api_request_removes_trailing_slashes() {
990+
return array(
991+
'no query part' => array( '/wp/v2/types//', '/wp/v2/types' ),
992+
'no query part, more slashes' => array( '/wp/v2/media///', '/wp/v2/media' ),
993+
'only slashes' => array( '////', '/' ),
994+
'empty path' => array( '', array() ),
995+
'no query parameters' => array( '/wp/v2/types//?////', '/wp/v2/types?' ),
996+
'no query parameters, with slashes' => array( '/wp/v2/types//?fields////', '/wp/v2/types?fields' ),
997+
'query parameters with no values' => array( '/wp/v2/types//?fields=////', '/wp/v2/types?fields=' ),
998+
'single query parameter' => array( '/wp/v2/types//?_fields=foo,bar////', '/wp/v2/types?_fields=foo,bar' ),
999+
'multiple query parameters' => array( '/wp/v2/types////?_fields=foo,bar&limit=1000////', '/wp/v2/types?_fields=foo,bar&limit=1000' ),
1000+
);
1001+
}
1002+
9871003
/**
9881004
* @ticket 40614
9891005
*/

0 commit comments

Comments
 (0)