From 123a965c70acee04b1cb7ab58599fb6284999835 Mon Sep 17 00:00:00 2001 From: Riad Benguella Date: Tue, 20 Oct 2020 07:52:06 +0000 Subject: [PATCH] Block Editor: Expose api_version in the block type and the REST endpoint. The new block editor included in 5.6 introduces an api_version property that indicates which block API version the block is using. This commits makes this property available on the block type and the endpoint. Props TimothyBlynJacobs, gziolo. Fixes #51529. git-svn-id: https://develop.svn.wordpress.org/trunk@49224 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/blocks.php | 1 + src/wp-includes/class-wp-block-type.php | 8 ++++++++ .../endpoints/class-wp-rest-block-types-controller.php | 8 ++++++++ tests/phpunit/tests/blocks/fixtures/block.json | 1 + tests/phpunit/tests/blocks/register.php | 1 + .../phpunit/tests/rest-api/rest-block-type-controller.php | 4 +++- 6 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 128e38a7e0..f6e023c67f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -207,6 +207,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) { 'supports' => 'supports', 'styles' => 'styles', 'example' => 'example', + 'apiVersion' => 'api_version', ); foreach ( $property_mappings as $key => $mapped_key ) { diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 79b0e3e6d1..7687d2b931 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -16,6 +16,14 @@ */ class WP_Block_Type { + /** + * Block API version. + * + * @since 5.6.0 + * @var int + */ + public $api_version = 1; + /** * Block type key. * diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 5ae139991d..f34ed6ace9 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -256,6 +256,7 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { $schema = $this->get_item_schema(); $extra_fields = array( + 'api_version', 'name', 'title', 'description', @@ -365,6 +366,13 @@ class WP_REST_Block_Types_Controller extends WP_REST_Controller { 'title' => 'block-type', 'type' => 'object', 'properties' => array( + 'api_version' => array( + 'description' => __( 'Version of block API.' ), + 'type' => 'integer', + 'default' => 1, + 'context' => array( 'embed', 'view', 'edit' ), + 'readonly' => true, + ), 'title' => array( 'description' => __( 'Title of block type.' ), 'type' => 'string', diff --git a/tests/phpunit/tests/blocks/fixtures/block.json b/tests/phpunit/tests/blocks/fixtures/block.json index be4205ce76..4cd2b4cd69 100644 --- a/tests/phpunit/tests/blocks/fixtures/block.json +++ b/tests/phpunit/tests/blocks/fixtures/block.json @@ -1,4 +1,5 @@ { + "apiVersion": 2, "name": "my-plugin/notice", "title": "Notice", "category": "common", diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index 6a872c0455..5fb6e9e062 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -285,6 +285,7 @@ class WP_Test_Block_Register extends WP_UnitTestCase { ); $this->assertInstanceOf( 'WP_Block_Type', $result ); + $this->assertSame( 2, $result->api_version ); $this->assertSame( 'my-plugin/notice', $result->name ); $this->assertSame( 'Notice', $result->title ); $this->assertSame( 'common', $result->category ); diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 272709e008..06db6d9675 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -312,7 +312,8 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); $properties = $data['schema']['properties']; - $this->assertCount( 19, $properties ); + $this->assertCount( 20, $properties ); + $this->assertArrayHasKey( 'api_version', $properties ); $this->assertArrayHasKey( 'title', $properties ); $this->assertArrayHasKey( 'icon', $properties ); $this->assertArrayHasKey( 'description', $properties ); @@ -431,6 +432,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $this->assertSame( $data['is_dynamic'], $block_type->is_dynamic() ); $extra_fields = array( + 'api_version', 'name', 'category', 'editor_script',