diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 47e6619fe2b86..6902b36773245 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 @@ public function set_props( $args ) { $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 bb0ed75c83c93..d27f68255eb51 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..408927197a883 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..43450ec463a18 100644 --- a/tests/phpunit/tests/blocks/wpBlock.php +++ b/tests/phpunit/tests/blocks/wpBlock.php @@ -82,8 +82,14 @@ 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( - $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 a6d83be5655ca..28eb8f1edfc63 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -84,6 +84,46 @@ 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->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 15738397ac263..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,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(), $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 @@ 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->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'] );