Make WordPress Core

Changeset 46099


Ignore:
Timestamp:
09/12/2019 09:46:41 PM (6 years ago)
Author:
kadamwhite
Message:

REST API: Accept string path in rest_ensure_request.

Update rest_ensure_request() to accept a string path, permitting a string path to be passed to rest_do_request() as is indicated (previously inaccurately) in that method's PHPDoc.

Props TimothyBlynJacobs, kadamwhite.
Fixes #40614.

Location:
trunk
Files:
2 edited

Legend:

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

    r45932 r46099  
    483483 *
    484484 * @since 4.4.0
    485  *
    486  * @param array|WP_REST_Request $request Request to check.
     485 * @since 5.3.0 Accept string argument for the request path.
     486 *
     487 * @param array|string|WP_REST_Request $request Request to check.
    487488 * @return WP_REST_Request REST request instance.
    488489 */
     
    490491    if ( $request instanceof WP_REST_Request ) {
    491492        return $request;
     493    }
     494
     495    if ( is_string( $request ) ) {
     496        return new WP_REST_Request( 'GET', $request );
    492497    }
    493498
  • trunk/tests/phpunit/tests/rest-api.php

    r45607 r46099  
    745745        $GLOBALS['wp_rest_server'] = $rest_server;
    746746    }
     747
     748    /**
     749     * @ticket 40614
     750     */
     751    function test_rest_ensure_response_accepts_path_string() {
     752        $request = rest_ensure_request( '/wp/v2/posts' );
     753        $this->assertInstanceOf( 'WP_REST_Request', $request );
     754        $this->assertEquals( '/wp/v2/posts', $request->get_route() );
     755        $this->assertEquals( 'GET', $request->get_method() );
     756    }
    747757}
Note: See TracChangeset for help on using the changeset viewer.