From 46c0e501989f9e7f2d51a0bc3374930157d9752f Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Sun, 22 Mar 2020 16:53:31 -0600 Subject: [PATCH 1/5] Also allow POST requests in WP_REST_Block_Renderer_Controller In order to allow components to have a large attributes object, ideally it could POST attributes instead of making a GET request for them. So add support for a POST request in this endpoint that uses. --- ...class-wp-rest-block-renderer-controller.php | 5 +++-- .../rest-block-renderer-controller.php | 18 +++++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php index 94d349a1bae30..bc1dbd3e8325b 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php @@ -52,7 +52,7 @@ public function register_routes() { ), ), array( - 'methods' => WP_REST_Server::READABLE, + 'methods' => array( WP_REST_Server::READABLE, WP_REST_Server::CREATABLE ), 'callback' => array( $this, 'get_item' ), 'permission_callback' => array( $this, 'get_item_permissions_check' ), 'args' => array( @@ -148,7 +148,8 @@ public function get_item( $request ) { ); } - $attributes = $request->get_param( 'attributes' ); + // In a POST request, the attributes appear as JSON in the request body. + $attributes = WP_REST_Server::CREATABLE === $request->get_method() ? json_decode( $request->get_body(), true ) : $request->get_param( 'attributes' ); // Create an array representation simulating the output of parse_blocks. $block = array( diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php index cf41f079a03b7..0974063739bda 100644 --- a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php @@ -455,6 +455,22 @@ public function test_get_item_with_post_context() { $this->assertEquals( $expected_title, $data['rendered'] ); } + /** + * Test a POST request, with the attributes in the body. + */ + public function test_get_item_post_request() { + wp_set_current_user( self::$user_id ); + $string_attribute = 'Lorem ipsum dolor'; + $attributes = array( 'some_string' => $string_attribute ); + $request = new WP_REST_Request( 'POST', self::$rest_api_route . self::$block_name ); + $request->set_param( 'context', 'edit' ); + $request->set_body( wp_json_encode( $attributes ) ); + $response = rest_get_server()->dispatch( $request ); + + $this->assertEquals( 200, $response->get_status() ); + $this->assertContains( $string_attribute, $response->get_data()['rendered'] ); + } + /** * Test getting item with invalid post ID. * @@ -503,7 +519,7 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertEqualSets( array( 'GET' ), $data['endpoints'][0]['methods'] ); + $this->assertEqualSets( array( 'GET', 'POST' ), $data['endpoints'][0]['methods'] ); $this->assertEqualSets( array( 'name', 'context', 'attributes', 'post_id' ), array_keys( $data['endpoints'][0]['args'] ) From 11b44b35dc9c931ed83a3014e4d82dbb8265ec0b Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Sun, 22 Mar 2020 17:55:28 -0600 Subject: [PATCH 2/5] Add a ticket tag to the DocBlock of the new test Following the convention of other test methods. --- tests/phpunit/tests/rest-api/rest-block-renderer-controller.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php index 0974063739bda..546489a41ff4b 100644 --- a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php @@ -457,6 +457,8 @@ public function test_get_item_with_post_context() { /** * Test a POST request, with the attributes in the body. + * + * @ticket 49680 */ public function test_get_item_post_request() { wp_set_current_user( self::$user_id ); From fad2b162985a82fe85dc9d5a2e0d30a008e0aac0 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Mon, 27 Apr 2020 19:22:53 -0500 Subject: [PATCH 3/5] Allow passing attributes in POST body via [ 'attributes' => [] ]. Instead of having attributes be the entire body, they are now simply a property of the body. --- .../endpoints/class-wp-rest-block-renderer-controller.php | 3 +-- .../phpunit/tests/rest-api/rest-block-renderer-controller.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php index bc1dbd3e8325b..3bdcae54352c2 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-renderer-controller.php @@ -148,8 +148,7 @@ public function get_item( $request ) { ); } - // In a POST request, the attributes appear as JSON in the request body. - $attributes = WP_REST_Server::CREATABLE === $request->get_method() ? json_decode( $request->get_body(), true ) : $request->get_param( 'attributes' ); + $attributes = $request->get_param( 'attributes' ); // Create an array representation simulating the output of parse_blocks. $block = array( diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php index 546489a41ff4b..eca4347ac9b5c 100644 --- a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php @@ -466,7 +466,7 @@ public function test_get_item_post_request() { $attributes = array( 'some_string' => $string_attribute ); $request = new WP_REST_Request( 'POST', self::$rest_api_route . self::$block_name ); $request->set_param( 'context', 'edit' ); - $request->set_body( wp_json_encode( $attributes ) ); + $request->set_body( wp_json_encode( compact( 'attributes' ) ) ); $response = rest_get_server()->dispatch( $request ); $this->assertEquals( 200, $response->get_status() ); From eb8b2d65aae7cdce2bad352d0fd6dd00fe6acb81 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Mon, 27 Apr 2020 22:22:45 -0500 Subject: [PATCH 4/5] First attempt at fixing text The test didn't fail locally, so this might not work. --- tests/phpunit/tests/rest-api/rest-block-renderer-controller.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php index eca4347ac9b5c..269e2e78b6e16 100644 --- a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php @@ -465,6 +465,7 @@ public function test_get_item_post_request() { $string_attribute = 'Lorem ipsum dolor'; $attributes = array( 'some_string' => $string_attribute ); $request = new WP_REST_Request( 'POST', self::$rest_api_route . self::$block_name ); + $request->set_header( 'content-type', 'application/json' ); $request->set_param( 'context', 'edit' ); $request->set_body( wp_json_encode( compact( 'attributes' ) ) ); $response = rest_get_server()->dispatch( $request ); From c1c293a27739ee537f998480d70e63fb8ac0b532 Mon Sep 17 00:00:00 2001 From: Ryan Kienstra Date: Mon, 27 Apr 2020 22:35:39 -0500 Subject: [PATCH 5/5] Move the add_header() call, in an effort to fix failed test --- tests/phpunit/tests/rest-api/rest-block-renderer-controller.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php index 269e2e78b6e16..88b1b69bde3cc 100644 --- a/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-renderer-controller.php @@ -465,8 +465,8 @@ public function test_get_item_post_request() { $string_attribute = 'Lorem ipsum dolor'; $attributes = array( 'some_string' => $string_attribute ); $request = new WP_REST_Request( 'POST', self::$rest_api_route . self::$block_name ); - $request->set_header( 'content-type', 'application/json' ); $request->set_param( 'context', 'edit' ); + $request->set_header( 'content-type', 'application/json' ); $request->set_body( wp_json_encode( compact( 'attributes' ) ) ); $response = rest_get_server()->dispatch( $request );