From c88553d07658ed9c2158892f5712196b53d5b193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=83=C2=B3=C3=85=E2=80=9Akowski?= Date: Tue, 26 Apr 2022 09:48:20 +0000 Subject: [PATCH] Editor: Register 'lock' attribute for every block on the server Backports changes from https://github.com/WordPress/gutenberg/pull/40468. The lock attribute needs to be supported by every block, but currently, it is only done on the client site. As a result, it was causing block rendered API requests to fail when blocks are locked. Props mamaduka, peterwilsoncc. See #55567. git-svn-id: https://develop.svn.wordpress.org/trunk@53268 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-block-type.php | 22 ++++++++++ tests/phpunit/tests/admin/includesPost.php | 3 ++ tests/phpunit/tests/blocks/register.php | 1 + tests/phpunit/tests/blocks/wpBlock.php | 10 ++++- tests/phpunit/tests/blocks/wpBlockType.php | 40 +++++++++++++++++++ .../rest-api/rest-block-type-controller.php | 14 ++++++- 6 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 47e6619fe2..6902b36773 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -204,6 +204,16 @@ class WP_Block_Type { */ public $style = null; + /** + * Attributes supported by every block. + * + * @since 6.0.0 + * @var array + */ + const GLOBAL_ATTRIBUTES = array( + 'lock' => array( 'type' => 'object' ), + ); + /** * Constructor. * @@ -355,6 +365,18 @@ class WP_Block_Type { $args['name'] = $this->name; + // Setup attributes if needed. + if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { + $args['attributes'] = array(); + } + + // Register core attributes. + foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) { + if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) { + $args['attributes'][ $attr_key ] = $attr_schema; + } + } + /** * Filters the arguments for registering a block type. * diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index bb0ed75c83..d27f68255e 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -844,6 +844,9 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase { 'title' => '', 'description' => '', 'icon' => 'text', + 'attributes' => array( + 'lock' => array( 'type' => 'object' ), + ), 'usesContext' => array(), 'category' => 'common', 'styles' => array(), diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index c4eda0876b..408927197a 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -392,6 +392,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase { 'source' => 'html', 'selector' => '.message', ), + 'lock' => array( 'type' => 'object' ), ), $result->attributes ); diff --git a/tests/phpunit/tests/blocks/wpBlock.php b/tests/phpunit/tests/blocks/wpBlock.php index d329a65273..43450ec463 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -82,8 +82,14 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase { $block = new WP_Block( $parsed_block, $context, $this->registry ); $this->assertInstanceOf( WP_Block_Type::class, $block->block_type ); - $this->assertSame( - $block_type_settings['attributes'], + $this->assertSameSetsWithIndex( + array( + 'defaulted' => array( + 'type' => 'number', + 'default' => 10, + ), + 'lock' => array( 'type' => 'object' ), + ), $block->block_type->attributes ); } diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index a6d83be565..28eb8f1edf 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -84,6 +84,46 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase { $this->assertSame( $args['foo'], $block_type->foo ); } + /* + * @ticket 55567 + * @covers WP_Block_Type::set_props + */ + public function test_core_attributes() { + $block_type = new WP_Block_Type( 'core/fake', array() ); + + $this->assertSameSetsWithIndex( + array( + 'lock' => array( 'type' => 'object' ), + ), + $block_type->attributes + ); + } + + /* + * @ticket 55567 + * @covers WP_Block_Type::set_props + */ + public function test_core_attributes_matches_custom() { + $block_type = new WP_Block_Type( + 'core/fake', + array( + 'attributes' => array( + 'lock' => array( + 'type' => 'string', + ), + ), + ) + ); + + // Backward compatibility: Don't override attributes with the same name. + $this->assertSameSetsWithIndex( + array( + 'lock' => array( 'type' => 'string' ), + ), + $block_type->attributes + ); + } + /** * @ticket 45097 */ 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 15738397ac..03b77d624c 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -243,7 +243,12 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); $this->assertSameSets( array(), $data['provides_context'] ); - $this->assertSameSets( array(), $data['attributes'] ); + $this->assertSameSetsWithIndex( + array( + 'lock' => array( 'type' => 'object' ), + ), + $data['attributes'] + ); $this->assertSameSets( array( 'invalid_uses_context' ), $data['uses_context'] ); $this->assertSameSets( array( 'invalid_keywords' ), $data['keywords'] ); $this->assertSameSets( array( 'invalid_parent' ), $data['parent'] ); @@ -299,7 +304,12 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase { $this->assertNull( $data['view_script'] ); $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); - $this->assertSameSets( array(), $data['attributes'] ); + $this->assertSameSetsWithIndex( + array( + 'lock' => array( 'type' => 'object' ), + ), + $data['attributes'] + ); $this->assertSameSets( array(), $data['provides_context'] ); $this->assertSameSets( array(), $data['uses_context'] ); $this->assertSameSets( array(), $data['keywords'] );