Make WordPress Core

Changeset 59457


Ignore:
Timestamp:
11/25/2024 10:08:05 AM (6 days ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api.php

    r59456 r59457  
    29362936    }
    29372937
     2938    // Remove trailing slashes at the end of the REST API path (query part).
    29382939    $path = untrailingslashit( $path );
    29392940    if ( empty( $path ) ) {
     
    29442945    if ( false === $path_parts ) {
    29452946        return $memo;
     2947    }
     2948
     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'];
    29462955    }
    29472956
  • trunk/tests/phpunit/tests/rest-api.php

    r57133 r59457  
    961961
    962962    /**
     963     * @dataProvider data_rest_preload_api_request_removes_trailing_slashes
     964     *
    963965     * @ticket 51636
    964      */
    965     public function test_rest_preload_api_request_removes_trailing_slashes() {
     966     * @ticket 57048
     967     *
     968     * @param string       $preload_path          The path to preload.
     969     * @param array|string $expected_preload_path Expected path after preloading.
     970     */
     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;
     982    }
     983
     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        );
    9851001    }
    9861002
Note: See TracChangeset for help on using the changeset viewer.