diff --git a/src/Templates/SingleProductTemplateCompatibility.php b/src/Templates/SingleProductTemplateCompatibility.php index bb74c62bb89..5c2e9e4acdf 100644 --- a/src/Templates/SingleProductTemplateCompatibility.php +++ b/src/Templates/SingleProductTemplateCompatibility.php @@ -321,7 +321,7 @@ function( $carry, $item ) { $carry['index'] = $carry['index'] + 1; $block = $item[0]; - if ( 'core/template-part' === $block['blockName'] ) { + if ( 'core/template-part' === $block['blockName'] || self::is_custom_html( $block ) ) { $carry['template'][] = $block; return $carry; } @@ -433,9 +433,6 @@ function( $carry, $block ) { $carry[] = array( $block ); return $carry; } - if ( empty( $block['blockName'] ) ) { - return $carry; - } $last_element_index = count( $carry ) - 1; if ( isset( $carry[ $last_element_index ][0]['blockName'] ) && 'core/template-part' !== $carry[ $last_element_index ][0]['blockName'] ) { $carry[ $last_element_index ][] = $block; @@ -468,6 +465,16 @@ private function inject_hooks_after_the_wrapper( $block_content, $hooks ) { $closing_tag_position + 1, 0 ); + } + + /** + * Plain custom HTML block is parsed as block with an empty blockName with a filled innerHTML. + * + * @param array $block Parse block. + * @return bool + */ + private static function is_custom_html( $block ) { + return empty( $block['blockName'] ) && ! empty( $block['innerHTML'] ); } } diff --git a/tests/php/Templates/SingleProductTemplateCompatibilityTests.php b/tests/php/Templates/SingleProductTemplateCompatibilityTests.php index fd87ba6c5f1..77ac60e13e0 100644 --- a/tests/php/Templates/SingleProductTemplateCompatibilityTests.php +++ b/tests/php/Templates/SingleProductTemplateCompatibilityTests.php @@ -327,4 +327,41 @@ public function test_add_compatibility_layer_with_multiple_blocks_related_to_the $this->assertEquals( $result_without_withespace, $expected_single_product_template_without_whitespace, '' ); } + + /** + * Test that the Single Product Template is wrapped in a div with the correct class if it contains a block related to the Single Product Template and custom HTML isn't removed. + */ + public function test_add_compatibility_layer_if_contains_single_product_blocks_and_custom_HTML_not_removed() { + + $default_single_product_template = ' + + Custom HTML + +
+ +
+ + '; + + $expected_single_product_template = ' + + Custom HTML + +
+ +
+ +
+ +
+ + '; + + $result = SingleProductTemplateCompatibility::add_compatibility_layer( $default_single_product_template ); + + $result_without_withespace = preg_replace( '/\s+/', '', $result ); + $expected_single_product_template_without_whitespace = preg_replace( '/\s+/', '', $expected_single_product_template ); + + $this->assertEquals( $result_without_withespace, $expected_single_product_template_without_whitespace, '' ); + } }