Skip to content

Commit 6340624

Browse files
committed
Blocks: Introduce helper function to retrieve hooked blocks
In order to implement Block Hooks (see #59313), we added block_hooks field to the WP_Block_Type class, as well as to block registration related functions. In this follow-up, new helper function gets introduced that is going to compute the list of hooked blocks by other registered blocks for a given block type. Extracted from WordPress#5158 and covered with unit tests. Props ockham. Fixes #59383. git-svn-id: https://develop.svn.wordpress.org/trunk@56610 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 4ee5947 commit 6340624

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

src/wp-includes/blocks.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -739,6 +739,27 @@ function get_dynamic_block_names() {
739739
return $dynamic_block_names;
740740
}
741741

742+
/**
743+
* Retrieves block types (and positions) hooked into the given block.
744+
*
745+
* @since 6.4.0
746+
*
747+
* @param string $name Block type name including namespace.
748+
* @return array Associative array of `$block_type_name => $position` pairs.
749+
*/
750+
function get_hooked_blocks( $name ) {
751+
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
752+
$hooked_blocks = array();
753+
foreach ( $block_types as $block_type ) {
754+
foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
755+
if ( $anchor_block_type === $name ) {
756+
$hooked_blocks[ $block_type->name ] = $relative_position;
757+
}
758+
}
759+
}
760+
return $hooked_blocks;
761+
}
762+
742763
/**
743764
* Given an array of attributes, returns a string in the serialized attributes
744765
* format prepared for post content.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
/**
3+
* Tests for block hooks feature functions.
4+
*
5+
* @package WordPress
6+
* @subpackage Blocks
7+
*
8+
* @since 6.4.0
9+
*
10+
* @group blocks
11+
*/
12+
class Tests_Blocks_BlockHooks extends WP_UnitTestCase {
13+
14+
/**
15+
* Tear down after each test.
16+
*
17+
* @since 6.4.0
18+
*/
19+
public function tear_down() {
20+
$registry = WP_Block_Type_Registry::get_instance();
21+
22+
foreach ( array( 'tests/my-block', 'tests/my-container-block' ) as $block_name ) {
23+
if ( $registry->is_registered( $block_name ) ) {
24+
$registry->unregister( $block_name );
25+
}
26+
}
27+
28+
parent::tear_down();
29+
}
30+
31+
/**
32+
* @ticket 59383
33+
*
34+
* @covers ::get_hooked_blocks
35+
*/
36+
public function test_get_hooked_blocks_no_match_found() {
37+
$result = get_hooked_blocks( 'tests/no-hooked-blocks' );
38+
39+
$this->assertSame( array(), $result );
40+
}
41+
42+
/**
43+
* @ticket 59383
44+
*
45+
* @covers ::get_hooked_blocks
46+
*/
47+
public function test_get_hooked_blocks_matches_found() {
48+
register_block_type(
49+
'tests/injected-one',
50+
array(
51+
'block_hooks' => array(
52+
'tests/hooked-at-before' => 'before',
53+
'tests/hooked-at-after' => 'after',
54+
),
55+
)
56+
);
57+
register_block_type(
58+
'tests/injected-two',
59+
array(
60+
'block_hooks' => array(
61+
'tests/hooked-at-before' => 'before',
62+
'tests/hooked-at-after' => 'after',
63+
'tests/hooked-at-first-child' => 'first_child',
64+
'tests/hooked-at-last-child' => 'last_child',
65+
),
66+
)
67+
);
68+
69+
$this->assertSame(
70+
array(
71+
'tests/injected-one' => 'before',
72+
'tests/injected-two' => 'before',
73+
),
74+
get_hooked_blocks( 'tests/hooked-at-before' ),
75+
'block hooked at the before position'
76+
);
77+
$this->assertSame(
78+
array(
79+
'tests/injected-one' => 'after',
80+
'tests/injected-two' => 'after',
81+
),
82+
get_hooked_blocks( 'tests/hooked-at-after' ),
83+
'block hooked at the after position'
84+
);
85+
$this->assertSame(
86+
array(
87+
'tests/injected-two' => 'first_child',
88+
),
89+
get_hooked_blocks( 'tests/hooked-at-first-child' ),
90+
'block hooked at the first child position'
91+
);
92+
$this->assertSame(
93+
array(
94+
'tests/injected-two' => 'last_child',
95+
),
96+
get_hooked_blocks( 'tests/hooked-at-last-child' ),
97+
'block hooked at the last child position'
98+
);
99+
}
100+
}

0 commit comments

Comments
 (0)