From b472ad3d750d5d82f12e1a61344146f16a10ecac Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 16:27:37 +0400 Subject: [PATCH 1/9] Register 'lock' attribute for every block on the server --- .env | 4 +-- src/wp-includes/class-wp-block-type.php | 22 +++++++++++++++ tests/phpunit/tests/blocks/wpBlockType.php | 32 ++++++++++++++++++++++ 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 9affb93f5321e..05cf96ffd2812 100644 --- a/.env +++ b/.env @@ -28,7 +28,7 @@ LOCAL_PHP_MEMCACHED=false # # Supported values are `mysql` and `mariadb`. ## -LOCAL_DB_TYPE=mysql +LOCAL_DB_TYPE=mariadb ## # The database version to use. @@ -38,7 +38,7 @@ LOCAL_DB_TYPE=mysql # When using `mysql`, see https://hub.docker.com/_/mysql/ for valid versions. # When using `mariadb`, see https://hub.docker.com/_/mariadb for valid versions. ## -LOCAL_DB_VERSION=5.7 +LOCAL_DB_VERSION=10 # The debug settings to add to `wp-config.php`. LOCAL_WP_DEBUG=true diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 47e6619fe2b86..782813ddb05c3 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 CORE_ATTRIBUTES = array( + 'lock' => array( 'type' => 'object' ), + ); + /** * Constructor. * @@ -368,6 +378,18 @@ public function set_props( $args ) { foreach ( $args as $property_name => $property_value ) { $this->$property_name = $property_value; } + + // Setup attributes if needed. + if ( ! $this->attributes ) { + $this->attributes = array(); + } + + // Register core attributes. + foreach ( static::CORE_ATTRIBUTES as $attr_key => $attr_schema ) { + if ( ! array_key_exists( $attr_key, $this->attributes ) ) { + $this->attributes[ $attr_key ] = $attr_schema; + } + } } /** diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index a6d83be5655ca..1c3866f5b640c 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -84,6 +84,38 @@ public function test_set_props() { $this->assertSame( $args['foo'], $block_type->foo ); } + public function test_core_attributes() { + $block_type = new WP_Block_Type( 'core/fake', array() ); + + $this->assertEquals( + array( + 'lock' => array( 'type' => 'object' ), + ), + $block_type->attributes + ); + } + + 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->assertEquals( + array( + 'lock' => array( 'type' => 'string' ), + ), + $block_type->attributes + ); + } + /** * @ticket 45097 */ From 3eb1b1dd05245e7ebdca749bc5c96bbb96efb1d3 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 16:31:16 +0400 Subject: [PATCH 2/9] Remove testing leftovers --- .env | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.env b/.env index 05cf96ffd2812..9affb93f5321e 100644 --- a/.env +++ b/.env @@ -28,7 +28,7 @@ LOCAL_PHP_MEMCACHED=false # # Supported values are `mysql` and `mariadb`. ## -LOCAL_DB_TYPE=mariadb +LOCAL_DB_TYPE=mysql ## # The database version to use. @@ -38,7 +38,7 @@ LOCAL_DB_TYPE=mariadb # When using `mysql`, see https://hub.docker.com/_/mysql/ for valid versions. # When using `mariadb`, see https://hub.docker.com/_/mariadb for valid versions. ## -LOCAL_DB_VERSION=10 +LOCAL_DB_VERSION=5.7 # The debug settings to add to `wp-config.php`. LOCAL_WP_DEBUG=true From 09818fde16671fdbde651077efe0a4aa5d9a830d Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 17:04:22 +0400 Subject: [PATCH 3/9] Try fixing unit tests --- src/wp-includes/class-wp-block-type.php | 2 +- tests/phpunit/tests/admin/includesPost.php | 3 +++ tests/phpunit/tests/blocks/register.php | 1 + tests/phpunit/tests/blocks/wpBlock.php | 8 +++++++- .../phpunit/tests/rest-api/rest-block-type-controller.php | 8 ++++++-- 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 782813ddb05c3..713b55bb8af02 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -380,7 +380,7 @@ public function set_props( $args ) { } // Setup attributes if needed. - if ( ! $this->attributes ) { + if ( ! is_array( $this->attributes ) ) { $this->attributes = array(); } diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index bb0ed75c83c93..a831802649fbb 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -844,6 +844,9 @@ public function test_get_block_editor_server_block_settings() { '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 c4eda0876be32..bf46ebee1f6e6 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -392,6 +392,7 @@ public function test_block_registers_with_metadata_fixture() { '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 d329a65273362..2b21532a6394f 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -83,7 +83,13 @@ public function test_constructor_assigns_block_type_from_registry() { $this->assertInstanceOf( WP_Block_Type::class, $block->block_type ); $this->assertSame( - $block_type_settings['attributes'], + array( + 'defaulted' => array( + 'type' => 'number', + 'default' => 10, + ), + 'lock' => array( 'type' => 'object' ), + ), $block->block_type->attributes ); } 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 15738397ac263..1783ab1b91796 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,9 @@ public function test_get_item_invalid() { $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); $this->assertSameSets( array(), $data['provides_context'] ); - $this->assertSameSets( array(), $data['attributes'] ); + $this->assertSameSets( 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 +301,9 @@ public function test_get_item_defaults() { $this->assertNull( $data['view_script'] ); $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); - $this->assertSameSets( array(), $data['attributes'] ); + $this->assertSameSets( array( + 'lock' => array( 'type' => 'object' ) + ), $data['attributes'] ); $this->assertSameSets( array(), $data['provides_context'] ); $this->assertSameSets( array(), $data['uses_context'] ); $this->assertSameSets( array(), $data['keywords'] ); From 6b8ff08822606a3bd9219c0c978117c81b431ac8 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Thu, 21 Apr 2022 17:10:39 +0400 Subject: [PATCH 4/9] Fix lint error --- tests/phpunit/tests/admin/includesPost.php | 2 +- tests/phpunit/tests/blocks/register.php | 2 +- tests/phpunit/tests/blocks/wpBlock.php | 2 +- .../rest-api/rest-block-type-controller.php | 18 ++++++++++++------ 4 files changed, 15 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/tests/admin/includesPost.php b/tests/phpunit/tests/admin/includesPost.php index a831802649fbb..d27f68255eb51 100644 --- a/tests/phpunit/tests/admin/includesPost.php +++ b/tests/phpunit/tests/admin/includesPost.php @@ -844,7 +844,7 @@ public function test_get_block_editor_server_block_settings() { 'title' => '', 'description' => '', 'icon' => 'text', - 'attributes' => array( + 'attributes' => array( 'lock' => array( 'type' => 'object' ), ), 'usesContext' => array(), diff --git a/tests/phpunit/tests/blocks/register.php b/tests/phpunit/tests/blocks/register.php index bf46ebee1f6e6..408927197a883 100644 --- a/tests/phpunit/tests/blocks/register.php +++ b/tests/phpunit/tests/blocks/register.php @@ -392,7 +392,7 @@ public function test_block_registers_with_metadata_fixture() { 'source' => 'html', 'selector' => '.message', ), - 'lock' => array( 'type' => 'object' ), + 'lock' => array( 'type' => 'object' ), ), $result->attributes ); diff --git a/tests/phpunit/tests/blocks/wpBlock.php b/tests/phpunit/tests/blocks/wpBlock.php index 2b21532a6394f..a8e9bb1cd8da2 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -88,7 +88,7 @@ public function test_constructor_assigns_block_type_from_registry() { 'type' => 'number', 'default' => 10, ), - 'lock' => array( 'type' => 'object' ), + 'lock' => array( 'type' => 'object' ), ), $block->block_type->attributes ); 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 1783ab1b91796..0717feae8fd77 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -243,9 +243,12 @@ public function test_get_item_invalid() { $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); $this->assertSameSets( array(), $data['provides_context'] ); - $this->assertSameSets( array( - 'lock' => array( 'type' => 'object' ) - ), $data['attributes'] ); + $this->assertSameSets( + 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'] ); @@ -301,9 +304,12 @@ public function test_get_item_defaults() { $this->assertNull( $data['view_script'] ); $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); - $this->assertSameSets( array( - 'lock' => array( 'type' => 'object' ) - ), $data['attributes'] ); + $this->assertSameSets( + array( + 'lock' => array( 'type' => 'object' ), + ), + $data['attributes'] + ); $this->assertSameSets( array(), $data['provides_context'] ); $this->assertSameSets( array(), $data['uses_context'] ); $this->assertSameSets( array(), $data['keywords'] ); From 33343ade3aca68ed616b5d9e90d4c30407d32f29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Mon, 25 Apr 2022 18:16:40 +0200 Subject: [PATCH 5/9] Apply suggestions from code review Co-authored-by: Peter Wilson <519727+peterwilsoncc@users.noreply.github.com> --- tests/phpunit/tests/blocks/wpBlock.php | 2 +- tests/phpunit/tests/blocks/wpBlockType.php | 8 ++++++-- .../phpunit/tests/rest-api/rest-block-type-controller.php | 4 ++-- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlock.php b/tests/phpunit/tests/blocks/wpBlock.php index a8e9bb1cd8da2..43450ec463a18 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -82,7 +82,7 @@ public function test_constructor_assigns_block_type_from_registry() { $block = new WP_Block( $parsed_block, $context, $this->registry ); $this->assertInstanceOf( WP_Block_Type::class, $block->block_type ); - $this->assertSame( + $this->assertSameSetsWithIndex( array( 'defaulted' => array( 'type' => 'number', diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index 1c3866f5b640c..6ef792bfb33b4 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -84,10 +84,14 @@ public function test_set_props() { $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->assertEquals( + $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'object' ), ), @@ -108,7 +112,7 @@ public function test_core_attributes_matches_custom() { ); // Backward compatibility: Don't override attributes with the same name. - $this->assertEquals( + $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'string' ), ), 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 0717feae8fd77..03b77d624c5d6 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,7 @@ public function test_get_item_invalid() { $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); $this->assertSameSets( array(), $data['provides_context'] ); - $this->assertSameSets( + $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'object' ), ), @@ -304,7 +304,7 @@ public function test_get_item_defaults() { $this->assertNull( $data['view_script'] ); $this->assertNull( $data['editor_style'] ); $this->assertNull( $data['style'] ); - $this->assertSameSets( + $this->assertSameSetsWithIndex( array( 'lock' => array( 'type' => 'object' ), ), From 32dba4036f4075c0f7a9d3b14a8d57431d762c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Greg=20Zi=C3=B3=C5=82kowski?= Date: Mon, 25 Apr 2022 18:17:45 +0200 Subject: [PATCH 6/9] Update tests/phpunit/tests/blocks/wpBlockType.php --- tests/phpunit/tests/blocks/wpBlockType.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index 6ef792bfb33b4..28eb8f1edfc63 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -99,6 +99,10 @@ public function test_core_attributes() { ); } + /* + * @ticket 55567 + * @covers WP_Block_Type::set_props + */ public function test_core_attributes_matches_custom() { $block_type = new WP_Block_Type( 'core/fake', From b5df5801a900f182db1f68ec65c67033e5567ad4 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 26 Apr 2022 11:14:11 +0400 Subject: [PATCH 7/9] Address feedback --- src/wp-includes/class-wp-block-type.php | 26 ++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 713b55bb8af02..52e9203131bc1 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -210,7 +210,7 @@ class WP_Block_Type { * @since 6.0.0 * @var array */ - const CORE_ATTRIBUTES = array( + const GLOBAL_ATTRIBUTES = array( 'lock' => array( 'type' => 'object' ), ); @@ -365,6 +365,18 @@ public function set_props( $args ) { $args['name'] = $this->name; + // Setup attributes if needed. + if ( ! is_array( $this->attributes ) ) { + $this->attributes = array(); + } + + // Register core attributes. + foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) { + if ( ! array_key_exists( $attr_key, $this->attributes ) ) { + $this->attributes[ $attr_key ] = $attr_schema; + } + } + /** * Filters the arguments for registering a block type. * @@ -378,18 +390,6 @@ public function set_props( $args ) { foreach ( $args as $property_name => $property_value ) { $this->$property_name = $property_value; } - - // Setup attributes if needed. - if ( ! is_array( $this->attributes ) ) { - $this->attributes = array(); - } - - // Register core attributes. - foreach ( static::CORE_ATTRIBUTES as $attr_key => $attr_schema ) { - if ( ! array_key_exists( $attr_key, $this->attributes ) ) { - $this->attributes[ $attr_key ] = $attr_schema; - } - } } /** From ae19fc5fc8b6d278d538e3fdd5b7d01160f976ea Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 26 Apr 2022 11:48:09 +0400 Subject: [PATCH 8/9] Use args --- src/wp-includes/class-wp-block-type.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 52e9203131bc1..eb4816b34ed18 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -366,14 +366,14 @@ public function set_props( $args ) { $args['name'] = $this->name; // Setup attributes if needed. - if ( ! is_array( $this->attributes ) ) { - $this->attributes = array(); + if ( ! isset( $args['attributes'] ) ) { + $args['attributes'] = array(); } // Register core attributes. foreach ( static::GLOBAL_ATTRIBUTES as $attr_key => $attr_schema ) { - if ( ! array_key_exists( $attr_key, $this->attributes ) ) { - $this->attributes[ $attr_key ] = $attr_schema; + if ( ! array_key_exists( $attr_key, $args['attributes'] ) ) { + $args['attributes'][ $attr_key ] = $attr_schema; } } From 6f260613b377875796566cb511264c2e334c72b0 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 26 Apr 2022 12:18:02 +0400 Subject: [PATCH 9/9] Fix tests --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index eb4816b34ed18..6902b36773245 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -366,7 +366,7 @@ public function set_props( $args ) { $args['name'] = $this->name; // Setup attributes if needed. - if ( ! isset( $args['attributes'] ) ) { + if ( ! isset( $args['attributes'] ) || ! is_array( $args['attributes'] ) ) { $args['attributes'] = array(); }