From 3b78c439ca8db7f6f8c89416dbc1d56c71d6a825 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Nov 2023 17:46:23 +0100 Subject: [PATCH 1/3] Inject hooked blocks into modified templates and parts --- src/wp-includes/block-template-utils.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 3f912ad0611b6..9237d1339d02d 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -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; } From bcf555f226792e8820d435039c4d3e7f75e086d9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 7 Dec 2023 16:22:15 +0100 Subject: [PATCH 2/3] Add test coverage for _build_block_template_result_from_post --- tests/phpunit/tests/block-templates/base.php | 4 +- .../buildBlockTemplateResultFromPost.php | 57 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-templates/base.php b/tests/phpunit/tests/block-templates/base.php index f6a5a9f4dfcec..c3fabee272849 100644 --- a/tests/phpunit/tests/block-templates/base.php +++ b/tests/phpunit/tests/block-templates/base.php @@ -39,7 +39,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => 'Content', + 'post_content' => '

Template

' , 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -57,7 +57,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', - 'post_content' => 'Content', + 'post_content' => '

Template Part

' , 'post_excerpt' => 'Description of my template part', 'tax_input' => array( 'wp_theme' => array( diff --git a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php index 371c50a183533..0d469abe88106 100644 --- a/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php +++ b/tests/phpunit/tests/block-templates/buildBlockTemplateResultFromPost.php @@ -8,6 +8,21 @@ */ 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 @@ public function test_should_build_template_part() { $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( '', $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( '', $template_part->content ); + $this->assertStringContainsString( '"metadata":{"ignoredHookedBlocks":["tests/my-block"]}', $template_part->content ); + } } From 3ae395064be47525c68366cb995e58e3dca877f9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 12 Feb 2024 10:43:06 +0100 Subject: [PATCH 3/3] Coding Standards --- tests/phpunit/tests/block-templates/base.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/block-templates/base.php b/tests/phpunit/tests/block-templates/base.php index c3fabee272849..4add267507505 100644 --- a/tests/phpunit/tests/block-templates/base.php +++ b/tests/phpunit/tests/block-templates/base.php @@ -39,7 +39,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template', 'post_name' => 'my_template', 'post_title' => 'My Template', - 'post_content' => '

Template

' , + 'post_content' => '

Template

', 'post_excerpt' => 'Description of my template', 'tax_input' => array( 'wp_theme' => array( @@ -57,7 +57,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'post_type' => 'wp_template_part', 'post_name' => 'my_template_part', 'post_title' => 'My Template Part', - 'post_content' => '

Template Part

' , + 'post_content' => '

Template Part

', 'post_excerpt' => 'Description of my template part', 'tax_input' => array( 'wp_theme' => array(