mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
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 https://github.com/WordPress/wordpress-develop/pull/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
This commit is contained in:
parent
4ee594761f
commit
6340624328
@ -739,6 +739,27 @@ function get_dynamic_block_names() {
|
||||
return $dynamic_block_names;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves block types (and positions) hooked into the given block.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @param string $name Block type name including namespace.
|
||||
* @return array Associative array of `$block_type_name => $position` pairs.
|
||||
*/
|
||||
function get_hooked_blocks( $name ) {
|
||||
$block_types = WP_Block_Type_Registry::get_instance()->get_all_registered();
|
||||
$hooked_blocks = array();
|
||||
foreach ( $block_types as $block_type ) {
|
||||
foreach ( $block_type->block_hooks as $anchor_block_type => $relative_position ) {
|
||||
if ( $anchor_block_type === $name ) {
|
||||
$hooked_blocks[ $block_type->name ] = $relative_position;
|
||||
}
|
||||
}
|
||||
}
|
||||
return $hooked_blocks;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an array of attributes, returns a string in the serialized attributes
|
||||
* format prepared for post content.
|
||||
|
||||
100
tests/phpunit/tests/blocks/blockHooks.php
Normal file
100
tests/phpunit/tests/blocks/blockHooks.php
Normal file
@ -0,0 +1,100 @@
|
||||
<?php
|
||||
/**
|
||||
* Tests for block hooks feature functions.
|
||||
*
|
||||
* @package WordPress
|
||||
* @subpackage Blocks
|
||||
*
|
||||
* @since 6.4.0
|
||||
*
|
||||
* @group blocks
|
||||
*/
|
||||
class Tests_Blocks_BlockHooks extends WP_UnitTestCase {
|
||||
|
||||
/**
|
||||
* Tear down after each test.
|
||||
*
|
||||
* @since 6.4.0
|
||||
*/
|
||||
public function tear_down() {
|
||||
$registry = WP_Block_Type_Registry::get_instance();
|
||||
|
||||
foreach ( array( 'tests/my-block', 'tests/my-container-block' ) as $block_name ) {
|
||||
if ( $registry->is_registered( $block_name ) ) {
|
||||
$registry->unregister( $block_name );
|
||||
}
|
||||
}
|
||||
|
||||
parent::tear_down();
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59383
|
||||
*
|
||||
* @covers ::get_hooked_blocks
|
||||
*/
|
||||
public function test_get_hooked_blocks_no_match_found() {
|
||||
$result = get_hooked_blocks( 'tests/no-hooked-blocks' );
|
||||
|
||||
$this->assertSame( array(), $result );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 59383
|
||||
*
|
||||
* @covers ::get_hooked_blocks
|
||||
*/
|
||||
public function test_get_hooked_blocks_matches_found() {
|
||||
register_block_type(
|
||||
'tests/injected-one',
|
||||
array(
|
||||
'block_hooks' => array(
|
||||
'tests/hooked-at-before' => 'before',
|
||||
'tests/hooked-at-after' => 'after',
|
||||
),
|
||||
)
|
||||
);
|
||||
register_block_type(
|
||||
'tests/injected-two',
|
||||
array(
|
||||
'block_hooks' => array(
|
||||
'tests/hooked-at-before' => 'before',
|
||||
'tests/hooked-at-after' => 'after',
|
||||
'tests/hooked-at-first-child' => 'first_child',
|
||||
'tests/hooked-at-last-child' => 'last_child',
|
||||
),
|
||||
)
|
||||
);
|
||||
|
||||
$this->assertSame(
|
||||
array(
|
||||
'tests/injected-one' => 'before',
|
||||
'tests/injected-two' => 'before',
|
||||
),
|
||||
get_hooked_blocks( 'tests/hooked-at-before' ),
|
||||
'block hooked at the before position'
|
||||
);
|
||||
$this->assertSame(
|
||||
array(
|
||||
'tests/injected-one' => 'after',
|
||||
'tests/injected-two' => 'after',
|
||||
),
|
||||
get_hooked_blocks( 'tests/hooked-at-after' ),
|
||||
'block hooked at the after position'
|
||||
);
|
||||
$this->assertSame(
|
||||
array(
|
||||
'tests/injected-two' => 'first_child',
|
||||
),
|
||||
get_hooked_blocks( 'tests/hooked-at-first-child' ),
|
||||
'block hooked at the first child position'
|
||||
);
|
||||
$this->assertSame(
|
||||
array(
|
||||
'tests/injected-two' => 'last_child',
|
||||
),
|
||||
get_hooked_blocks( 'tests/hooked-at-last-child' ),
|
||||
'block hooked at the last child position'
|
||||
);
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user