Block Types: Add metadata global attribute.

Add a new global attribute (i.e. an attribute that can be added to all and any blocks) called `metadata`. This is required for use cases such as allowing the user to assign custom names to blocks, or for making Block Hooks work with user-modified templates/parts/patterns (#59646).

Props Mamaduka, gziolo, get_dave.
Fixes #59797.

git-svn-id: https://develop.svn.wordpress.org/trunk@57068 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Bernie Reiter 2023-11-06 14:45:28 +00:00
parent 69e85003d5
commit 2a93cafb52
6 changed files with 28 additions and 10 deletions

View File

@ -242,11 +242,13 @@ class WP_Block_Type {
/**
* Attributes supported by every block.
*
* @since 6.0.0
* @since 6.0.0 Added `lock`.
* @since 6.5.0 Added `metadata`.
* @var array
*/
const GLOBAL_ATTRIBUTES = array(
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
);
/**

View File

@ -935,7 +935,8 @@ class Tests_Admin_IncludesPost extends WP_UnitTestCase {
'description' => '',
'icon' => 'text',
'attributes' => array(
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
'usesContext' => array(),
'blockHooks' => array( 'core/post-content' => 'before' ),

View File

@ -726,6 +726,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
* @ticket 50263
* @ticket 50328
* @ticket 57585
* @ticket 59797
*/
public function test_block_registers_with_metadata_fixture() {
$result = register_block_type_from_metadata(
@ -744,10 +745,11 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
$this->assertSameSets( array( 'alert', 'message' ), $result->keywords );
$this->assertSame(
array(
'message' => array(
'message' => array(
'type' => 'string',
),
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
$result->attributes
);

View File

@ -59,6 +59,7 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
/**
* @ticket 49927
* @ticket 59797
*/
public function test_constructor_assigns_block_type_from_registry() {
$block_type_settings = array(
@ -83,6 +84,7 @@ class Tests_Blocks_wpBlock extends WP_UnitTestCase {
'default' => 10,
),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
$block->block_type->attributes
);

View File

@ -80,6 +80,7 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase {
/*
* @ticket 55567
* @ticket 59797
* @covers WP_Block_Type::set_props
*/
public function test_core_attributes() {
@ -87,7 +88,8 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase {
$this->assertSameSetsWithIndex(
array(
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
$block_type->attributes
);
@ -95,6 +97,7 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase {
/*
* @ticket 55567
* @ticket 59797
* @covers WP_Block_Type::set_props
*/
public function test_core_attributes_matches_custom() {
@ -102,9 +105,12 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase {
'core/fake',
array(
'attributes' => array(
'lock' => array(
'lock' => array(
'type' => 'string',
),
'metadata' => array(
'type' => 'number',
),
),
)
);
@ -112,7 +118,8 @@ class Tests_Blocks_wpBlockType extends WP_UnitTestCase {
// Backward compatibility: Don't override attributes with the same name.
$this->assertSameSetsWithIndex(
array(
'lock' => array( 'type' => 'string' ),
'lock' => array( 'type' => 'string' ),
'metadata' => array( 'type' => 'number' ),
),
$block_type->attributes
);

View File

@ -198,6 +198,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
* @ticket 47620
* @ticket 57585
* @ticket 59346
* @ticket 59797
*/
public function test_get_item_invalid() {
$block_type = 'fake/invalid';
@ -242,7 +243,8 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
$this->assertNull( $data['textdomain'] );
$this->assertSameSetsWithIndex(
array(
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
$data['attributes']
);
@ -272,6 +274,7 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
* @ticket 47620
* @ticket 57585
* @ticket 59346
* @ticket 59797
*/
public function test_get_item_defaults() {
$block_type = 'fake/false';
@ -316,7 +319,8 @@ class REST_Block_Type_Controller_Test extends WP_Test_REST_Controller_Testcase {
$this->assertNull( $data['textdomain'] );
$this->assertSameSetsWithIndex(
array(
'lock' => array( 'type' => 'object' ),
'lock' => array( 'type' => 'object' ),
'metadata' => array( 'type' => 'object' ),
),
$data['attributes']
);