Skip to content

Commit

Permalink
Block Editor: Expose api_version in the block type and the REST endpo…
Browse files Browse the repository at this point in the history
…int.

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
  • Loading branch information
youknowriad committed Oct 20, 2020
1 parent af76703 commit 123a965
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down
8 changes: 8 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class WP_Block_Type {

/**
* Block API version.
*
* @since 5.6.0
* @var int
*/
public $api_version = 1;

/**
* Block type key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ public function prepare_item_for_response( $block_type, $request ) {

$schema = $this->get_item_schema();
$extra_fields = array(
'api_version',
'name',
'title',
'description',
Expand Down Expand Up @@ -365,6 +366,13 @@ public function get_item_schema() {
'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',
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/blocks/fixtures/block.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"apiVersion": 2,
"name": "my-plugin/notice",
"title": "Notice",
"category": "common",
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ function test_block_registers_with_metadata_fixture() {
);

$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 );
Expand Down
4 changes: 3 additions & 1 deletion tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,8 @@ public function test_get_item_schema() {
$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 );
Expand Down Expand Up @@ -431,6 +432,7 @@ protected function check_block_type_object( $block_type, $data, $links ) {
$this->assertSame( $data['is_dynamic'], $block_type->is_dynamic() );

$extra_fields = array(
'api_version',
'name',
'category',
'editor_script',
Expand Down

0 comments on commit 123a965

Please sign in to comment.