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
This commit is contained in:
Greg Ziółkowski 2022-04-26 09:48:20 +00:00
parent e53a3ea0c0
commit c88553d076
6 changed files with 86 additions and 4 deletions

View File

@ -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.
*

View File

@ -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(),

View File

@ -392,6 +392,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
'source' => 'html',
'selector' => '.message',
),
'lock' => array( 'type' => 'object' ),
),
$result->attributes
);

View File

@ -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
);
}

View File

@ -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
*/

View File

@ -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'] );