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 ) { |
| 602 | 602 | * @since 4.4.0 |
| 603 | 603 | * |
| 604 | 604 | * @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 | * |
| 607 | 608 | * @return WP_REST_Response Modified response, either response or `null` to indicate pass-through. |
| 608 | 609 | */ |
| 609 | 610 | function rest_handle_options_request( $response, $handler, $request ) { |
| … |
… |
function rest_handle_options_request( $response, $handler, $request ) { |
| 615 | 616 | $data = array(); |
| 616 | 617 | |
| 617 | 618 | 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 ); |
| 619 | 620 | |
| 620 | 621 | if ( ! $match ) { |
| 621 | 622 | continue; |
| 622 | 623 | } |
| 623 | 624 | |
| | 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 | |
| 624 | 640 | $data = $handler->get_data_for_route( $route, $endpoints, 'help' ); |
| 625 | 641 | $response->set_matched_route( $route ); |
| 626 | 642 | break; |
| 627 | 643 | } |
| 628 | 644 | |
| | 645 | $response = rest_send_allow_header( $response, $handler, $request ); |
| | 646 | |
| 629 | 647 | $response->set_data( $data ); |
| | 648 | |
| 630 | 649 | return $response; |
| 631 | 650 | } |
| 632 | 651 | |
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 |
| 189 | 189 | $this->assertEquals( array( 'context', 'id', 'password' ), $keys ); |
| 190 | 190 | } |
| 191 | 191 | |
| | 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 | |
| 192 | 200 | public function test_get_items() { |
| 193 | 201 | $request = new WP_REST_Request( 'GET', '/wp/v2/posts' ); |
| 194 | 202 | $response = rest_get_server()->dispatch( $request ); |