From 5460e0df1e9a8fabecb44cf162478c70efa44ce1 Mon Sep 17 00:00:00 2001 From: Timothy Jacobs Date: Sun, 3 May 2020 22:56:01 +0000 Subject: [PATCH] REST API: Accept POST requests in the block renderer endpoint. Rendering a block is idempotent, so a GET is the most natural request method. However, the maximum length of URLs prevented blocks with large attributes from being rendered. Props ryankienstra. Fixes #49680. git-svn-id: https://develop.svn.wordpress.org/trunk@47756 602fd350-edb4-49c9-b593-d223f7449a82 --- ...lass-wp-rest-block-renderer-controller.php | 2 +- .../rest-block-renderer-controller.php | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 2 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 94d349a1ba..3bdcae5435 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 @@ class WP_REST_Block_Renderer_Controller extends WP_REST_Controller { ), ), 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( 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 cf41f079a0..88b1b69bde 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,25 @@ class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca $this->assertEquals( $expected_title, $data['rendered'] ); } + /** + * 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 ); + $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_header( 'content-type', 'application/json' ); + $request->set_body( wp_json_encode( compact( '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 +522,7 @@ class REST_Block_Renderer_Controller_Test extends WP_Test_REST_Controller_Testca $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'] )