From 7f7ce1603f4393d0a53de58dfb0f5e9eb3a35795 Mon Sep 17 00:00:00 2001 From: Luigi Date: Mon, 17 Apr 2023 23:43:19 +0200 Subject: [PATCH] Single Product Template - Compatibility Layer: don't skip custom HTML --- .../SingleProductTemplateCompatibility.php | 18 +++++++-- ...ingleProductTemplateCompatibilityTests.php | 37 +++++++++++++++++++ 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Templates/SingleProductTemplateCompatibility.php b/src/Templates/SingleProductTemplateCompatibility.php index bb74c62bb89..8b2a21d13a6 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,9 @@ function( $carry, $block ) { $carry[] = array( $block ); return $carry; } - if ( empty( $block['blockName'] ) ) { - return $carry; - } + // if ( empty( $block['blockName'] && ! self::is_custom_html( $block ) ) ) { + // 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 +468,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, '' ); + } }