diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index f71ae26aab220..a4e54432d2aa2 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -547,18 +547,15 @@ function _build_block_template_result_from_file( $template_file, $template_type $template->area = $template_file['area']; } - $before_block_visitor = ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ? '_inject_theme_attribute_in_template_part_block' : null; + $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; $after_block_visitor = null; $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 ); } - if ( null !== $before_block_visitor || null !== $after_block_visitor ) { - $blocks = parse_blocks( $template_content ); - $template_content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - } - $template->content = $template_content; + $blocks = parse_blocks( $template_content ); + $template->content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); return $template; } diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3a2e943ad5cfc..ecb4fa5b3dfb4 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -779,9 +779,7 @@ function make_before_block_visitor( $hooked_blocks, $context ) { * @return string The serialized markup for the given block, with the markup for any hooked blocks prepended to it. */ return function ( &$block, $parent_block = null, $prev = null ) use ( $hooked_blocks, $context ) { - if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { - _inject_theme_attribute_in_template_part_block( $block ); - } + _inject_theme_attribute_in_template_part_block( $block ); $markup = ''; diff --git a/src/wp-includes/class-wp-block-patterns-registry.php b/src/wp-includes/class-wp-block-patterns-registry.php index 1c528abb3f92e..a11bac06bef02 100644 --- a/src/wp-includes/class-wp-block-patterns-registry.php +++ b/src/wp-includes/class-wp-block-patterns-registry.php @@ -165,16 +165,14 @@ public function unregister( $pattern_name ) { private function prepare_content( $pattern, $hooked_blocks ) { $content = $pattern['content']; - $before_block_visitor = ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ? '_inject_theme_attribute_in_template_part_block' : null; + $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; $after_block_visitor = null; if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { $before_block_visitor = make_before_block_visitor( $hooked_blocks, $pattern ); $after_block_visitor = make_after_block_visitor( $hooked_blocks, $pattern ); } - if ( null !== $before_block_visitor || null !== $after_block_visitor ) { - $blocks = parse_blocks( $content ); - $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); - } + $blocks = parse_blocks( $content ); + $content = traverse_and_serialize_blocks( $blocks, $before_block_visitor, $after_block_visitor ); return $content; } diff --git a/tests/phpunit/tests/block-template-utils.php b/tests/phpunit/tests/block-template-utils.php index 814e1ee8511ed..b06e931529f1e 100644 --- a/tests/phpunit/tests/block-template-utils.php +++ b/tests/phpunit/tests/block-template-utils.php @@ -161,6 +161,64 @@ public function test_build_block_template_result_from_file() { $this->assertEmpty( $template_part->modified ); } + /** + * @ticket 59325 + * + * @covers ::_build_block_template_result_from_file + * + * @dataProvider data_build_block_template_result_from_file_injects_theme_attribute + * + * @param string $filename The template's filename. + * @param string $expected The expected block markup. + */ + public function test_build_block_template_result_from_file_injects_theme_attribute( $filename, $expected ) { + $template = _build_block_template_result_from_file( + array( + 'slug' => 'single', + 'path' => DIR_TESTDATA . "/templates/$filename", + ), + 'wp_template' + ); + $this->assertSame( $expected, $template->content ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_build_block_template_result_from_file_injects_theme_attribute() { + $theme = 'block-theme'; + return array( + 'a template with a template part block' => array( + 'filename' => 'template-with-template-part.html', + 'expected' => sprintf( + '', + $theme + ), + ), + 'a template with a template part block nested inside another block' => array( + 'filename' => 'template-with-nested-template-part.html', + 'expected' => sprintf( + ' + +', + $theme + ), + ), + 'a template with a template part block with an existing theme attribute' => array( + 'filename' => 'template-with-template-part-with-existing-theme-attribute.html', + 'expected' => '', + ), + 'a template with no template part block' => array( + 'filename' => 'template.html', + 'expected' => ' +
Just a paragraph
+', + ), + ); + } + /** * @ticket 59338 * diff --git a/tests/phpunit/tests/blocks/renderBlockCoreTemplatePart.php b/tests/phpunit/tests/blocks/renderBlockCoreTemplatePart.php deleted file mode 100644 index b85c4c821d6f6..0000000000000 --- a/tests/phpunit/tests/blocks/renderBlockCoreTemplatePart.php +++ /dev/null @@ -1,121 +0,0 @@ -original_block_to_render = WP_Block_Supports::$block_to_render; - } - - public function tear_down() { - WP_Block_Supports::$block_to_render = $this->original_block_to_render; - $this->original_block_to_render = null; - - if ( $this->switch_to_default_theme_at_teardown ) { - $this->switch_to_default_theme_at_teardown = false; - switch_theme( WP_DEFAULT_THEME ); - } - - parent::tear_down(); - } - - /** - * Tests that the core template part block assumes the current theme if no theme attribute provided. - * - * @ticket 59583 - */ - public function test_render_block_core_template_part_without_theme_attribute() { - $this->maybe_switch_theme( 'block-theme' ); - - WP_Block_Supports::$block_to_render = array( 'blockName' => 'core/template-part' ); - - $content = render_block_core_template_part( array( 'slug' => 'small-header' ) ); - - $expected = 'Small Header Template Part
' . "\n"; - $expected .= 'Small Header Template Part
' . "\n"; - $expected .= '