From 32ccf017de9b68b4cba1229ee33902dd39808b1b Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Fri, 24 Feb 2017 20:56:00 +0000 Subject: [PATCH] REST API: Correctly parse body parameters for DELETE requests. DELETE was inadvertently omitted from the list of non-POST HTTP methods that should be able to accept body parameters. Parameters passed to DELETE requests as JSON are already parsed correctly; this commit fixes application/x-www-form-urlencoded parameters as well. Props mnelson4. Merges [40105] to the 4.7 branch. Fixes #39933. git-svn-id: https://develop.svn.wordpress.org/branches/4.7@40113 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-request.php | 2 +- tests/phpunit/tests/rest-api/rest-request.php | 21 +++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php index 4dd0dc2090..74a81c92e8 100644 --- a/src/wp-includes/rest-api/class-wp-rest-request.php +++ b/src/wp-includes/rest-api/class-wp-rest-request.php @@ -364,7 +364,7 @@ class WP_REST_Request implements ArrayAccess { $this->parse_body_params(); } - $accepts_body_data = array( 'POST', 'PUT', 'PATCH' ); + $accepts_body_data = array( 'POST', 'PUT', 'PATCH', 'DELETE' ); if ( in_array( $this->method, $accepts_body_data ) ) { $order[] = 'POST'; } diff --git a/tests/phpunit/tests/rest-api/rest-request.php b/tests/phpunit/tests/rest-api/rest-request.php index 7953f65bad..ffd4ea29e7 100644 --- a/tests/phpunit/tests/rest-api/rest-request.php +++ b/tests/phpunit/tests/rest-api/rest-request.php @@ -203,11 +203,22 @@ class Tests_REST_Request extends WP_UnitTestCase { $this->assertEmpty( $this->request->get_param( 'has_json_params' ) ); } + public function non_post_http_methods_with_request_body_provider() { + return array( + array( 'PUT' ), + array( 'PATCH' ), + array( 'DELETE' ), + ); + } + /** - * PUT requests don't get $_POST automatically parsed, so ensure that - * WP_REST_Request does it for us. + * Tests that methods supporting request bodies have access to the + * request's body. For POST this is straightforward via `$_POST`; for + * other methods `WP_REST_Request` needs to parse the body for us. + * + * @dataProvider non_post_http_methods_with_request_body_provider */ - public function test_parameters_for_put() { + public function test_non_post_body_parameters( $request_method ) { $data = array( 'foo' => 'bar', 'alot' => array( @@ -219,11 +230,9 @@ class Tests_REST_Request extends WP_UnitTestCase { 'stuff', ), ); - - $this->request->set_method( 'PUT' ); + $this->request->set_method( $request_method ); $this->request->set_body_params( array() ); $this->request->set_body( http_build_query( $data ) ); - foreach ( $data as $key => $expected_value ) { $this->assertEquals( $expected_value, $this->request->get_param( $key ) ); }