Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Refactor code based on the review feedback
Props to @ockham.
  • Loading branch information
gziolo committed Sep 18, 2023
commit 2089f546e1783b20dc54b92e705425870f0fc99d
14 changes: 0 additions & 14 deletions src/wp-includes/class-wp-block-type.php
Original file line number Diff line number Diff line change
Expand Up @@ -522,20 +522,6 @@ public function set_props( $args ) {
$args = apply_filters( 'register_block_type_args', $args, $this->name );

foreach ( $args as $property_name => $property_value ) {
// Avoid infinite recursion in block hooks (hooking to itself).
if ( 'block_hooks' === $property_name ) {
if ( ! is_array( $property_value ) ) {
continue;
}
if ( array_key_exists( $this->name, $property_value ) ) {
_doing_it_wrong(
__METHOD__,
__( 'Cannot hook block to itself.' ),
'6.4.0'
);
unset( $property_value[ $this->name ] );
}
}
$this->$property_name = $property_value;
}
}
Expand Down
8 changes: 8 additions & 0 deletions tests/phpunit/data/blocks/hooked-block-error/block.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "tests/hooked-block-error",
"description": "A block that throws an error because it tries to hook a block to itself.",
"blockHooks": {
"tests/hooked-block-error": "before",
"tests/other-block": "after"
}
}
15 changes: 4 additions & 11 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -1080,22 +1080,15 @@ public function test_register_block_style_name_without_spaces() {
*
* @covers ::register_block_type
*
* @expectedIncorrectUsage WP_Block_Type::set_props
* @expectedIncorrectUsage register_block_type_from_metadata
*/
public function test_register_block_hooks_targeting_itself() {
$block_name = 'tests/block-name';
$block_type = register_block_type(
$block_name,
array(
'block_hooks' => array(
$block_name => 'first',
'tests/other-block' => 'last',
),
)
DIR_TESTDATA . '/blocks/hooked-block-error'
);

$this->assertSameSets(
array( 'tests/other-block' => 'last' ),
$this->assertSame(
array( 'tests/other-block' => 'after' ),
$block_type->block_hooks
);
}
Expand Down