Make WordPress Core

Ticket #45753: 45753.2.diff

File 45753.2.diff, 2.7 KB (added by killua99, 6 years ago)

WIP: Patch with simple Unit Test. It doesn't test against a login user with capabilities edit_posts.

  • src/wp-includes/rest-api.php

    diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php
    index a2e3363951..a4373a2d82 100644
    a b function rest_send_cors_headers( $value ) { 
    602602 * @since 4.4.0
    603603 *
    604604 * @param mixed           $response Current response, either response or `null` to indicate pass-through.
    605  * @param WP_REST_Server  $handler  ResponseHandler instance (usually WP_REST_Server).
    606  * @param WP_REST_Request $request  The request that was used to make current response.
     605 * @param WP_REST_Server  $handler ResponseHandler instance (usually WP_REST_Server).
     606 * @param WP_REST_Request $request The request that was used to make current response.
     607 *
    607608 * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through.
    608609 */
    609610function rest_handle_options_request( $response, $handler, $request ) {
    function rest_handle_options_request( $response, $handler, $request ) { 
    615616        $data     = array();
    616617
    617618        foreach ( $handler->get_routes() as $route => $endpoints ) {
    618                 $match = preg_match( '@^' . $route . '$@i', $request->get_route() );
     619                $match = preg_match( '@^' . $route . '$@i', $request->get_route(), $matches );
    619620
    620621                if ( ! $match ) {
    621622                        continue;
    622623                }
    623624
     625                $args = array();
     626                foreach ( $matches as $param => $value ) {
     627                        if ( ! is_int( $param ) ) {
     628                                $args[ $param ] = $value;
     629                        }
     630                }
     631
     632                foreach ( $endpoints as $endpoint ) {
     633                        // Remove the redundant preg_match argument.
     634                        unset( $args[0] );
     635
     636                        $request->set_url_params( $args );
     637                        $request->set_attributes( $endpoint );
     638                }
     639
    624640                $data = $handler->get_data_for_route( $route, $endpoints, 'help' );
    625641                $response->set_matched_route( $route );
    626642                break;
    627643        }
    628644
     645        $response = rest_send_allow_header( $response, $handler, $request );
     646
    629647        $response->set_data( $data );
     648
    630649        return $response;
    631650}
    632651
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git a/tests/phpunit/tests/rest-api/rest-posts-controller.php b/tests/phpunit/tests/rest-api/rest-posts-controller.php
    index ee01eb717e..f718c0d9e3 100644
    a b class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    189189                $this->assertEquals( array( 'context', 'id', 'password' ), $keys );
    190190        }
    191191
     192        public function test_allow_method_options() {
     193                $request  = new WP_REST_Request( 'OPTIONS', sprintf( '/wp/v2/posts/%d', self::$post_id ) );
     194                $response = rest_get_server()->dispatch( $request );
     195                $headers  = $response->get_headers();
     196
     197                $this->assertNotEmpty( $headers['Allow'] );
     198        }
     199
    192200        public function test_get_items() {
    193201                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
    194202                $response = rest_get_server()->dispatch( $request );