Skip to content

Commit

Permalink
Editor: Register 'lock' attribute for every block on the server
Browse files Browse the repository at this point in the history
Backports changes from WordPress/gutenberg#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
  • Loading branch information
gziolo committed Apr 26, 2022
1 parent e53a3ea commit c88553d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 4 deletions.
22 changes: 22 additions & 0 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
3 changes: 3 additions & 0 deletions tests/phpunit/tests/admin/includesPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ public function test_block_registers_with_metadata_fixture() {
'source' => 'html',
'selector' => '.message',
),
'lock' => array( 'type' => 'object' ),
),
$result->attributes
);
Expand Down
10 changes: 8 additions & 2 deletions tests/phpunit/tests/blocks/wpBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
Expand Down
40 changes: 40 additions & 0 deletions tests/phpunit/tests/blocks/wpBlockType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
14 changes: 12 additions & 2 deletions tests/phpunit/tests/rest-api/rest-block-type-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] );
Expand Down Expand Up @@ -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'] );
Expand Down

0 comments on commit c88553d

Please sign in to comment.