Block Hooks: Inject hooked blocks into modified templates and parts.

Using the new technique introduced in [57157] of using a `metadata.ignoredHookedBlocks` attribute in the anchor block to store information about whether or not a hooked block should be considered for injection, extend said injection to encompass ''modified'' templates and parts.

Fixes #59646.
Props gziolo, matveb.

git-svn-id: https://develop.svn.wordpress.org/trunk@57594 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
bernhard-reiter
2024-02-12 13:13:38 +00:00
parent 13a042f8b7
commit cb522a3b25
3 changed files with 67 additions and 2 deletions

View File

@@ -901,6 +901,14 @@ function _build_block_template_result_from_post( $post ) {
}
}
$hooked_blocks = get_hooked_blocks();
if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
$before_block_visitor = make_before_block_visitor( $hooked_blocks, $template );
$after_block_visitor = make_after_block_visitor( $hooked_blocks, $template );
$blocks = parse_blocks( $template->content );
$template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor );
}
return $template;
}

View File

@@ -39,7 +39,7 @@ abstract class WP_Block_Templates_UnitTestCase extends WP_UnitTestCase {
'post_type' => 'wp_template',
'post_name' => 'my_template',
'post_title' => 'My Template',
'post_content' => 'Content',
'post_content' => '<!-- wp:heading {"level":1} --><h1>Template</h1><!-- /wp:heading -->',
'post_excerpt' => 'Description of my template',
'tax_input' => array(
'wp_theme' => array(
@@ -57,7 +57,7 @@ abstract class WP_Block_Templates_UnitTestCase extends WP_UnitTestCase {
'post_type' => 'wp_template_part',
'post_name' => 'my_template_part',
'post_title' => 'My Template Part',
'post_content' => 'Content',
'post_content' => '<!-- wp:heading {"level":2} --><h2>Template Part</h2><!-- /wp:heading -->',
'post_excerpt' => 'Description of my template part',
'tax_input' => array(
'wp_theme' => array(

View File

@@ -8,6 +8,21 @@ require_once __DIR__ . '/base.php';
*/
class Tests_Block_Templates_BuildBlockTemplateResultFromPost extends WP_Block_Templates_UnitTestCase {
/**
* Tear down each test method.
*
* @since 6.5.0
*/
public function tear_down() {
$registry = WP_Block_Type_Registry::get_instance();
if ( $registry->is_registered( 'tests/my-block' ) ) {
$registry->unregister( 'tests/my-block' );
}
parent::tear_down();
}
/**
* @ticket 54335
*/
@@ -49,4 +64,46 @@ class Tests_Block_Templates_BuildBlockTemplateResultFromPost extends WP_Block_Te
$this->assertSame( WP_TEMPLATE_PART_AREA_HEADER, $template_part->area );
$this->assertSame( self::$template_part_post->post_modified, $template_part->modified, 'Template part result properties match' );
}
/**
* @ticket 59646
*/
public function test_should_inject_hooked_block_into_template() {
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/heading' => 'before',
),
)
);
$template = _build_block_template_result_from_post(
self::$template_post,
'wp_template'
);
$this->assertStringStartsWith( '<!-- wp:tests/my-block /-->', $template->content );
$this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template->content );
}
/**
* @ticket 59646
*/
public function test_should_inject_hooked_block_into_template_part() {
register_block_type(
'tests/my-block',
array(
'block_hooks' => array(
'core/heading' => 'after',
),
)
);
$template_part = _build_block_template_result_from_post(
self::$template_part_post,
'wp_template_part'
);
$this->assertStringEndsWith( '<!-- wp:tests/my-block /-->', $template_part->content );
$this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content );
}
}