Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Single Product Template - Compatibility Layer: don't skip block without name #9075

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions src/Templates/SingleProductTemplateCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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'] );
}
}
37 changes: 37 additions & 0 deletions tests/php/Templates/SingleProductTemplateCompatibilityTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '
<!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
<span>Custom HTML</span>
<!-- wp:group {"layout":{"inherit":true,"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:woocommerce/product-image-gallery /-->
</div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","theme":"twentytwentythree","tagName":"footer"} /-->';

$expected_single_product_template = '
<!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
<span>Custom HTML</span>
<!-- wp:group {"className":"woocommerce product", "__wooCommerceIsFirstBlock":true,"__wooCommerceIsLastBlock":true} -->
<div class="wp-block-group woocommerce product">
<!-- wp:group {"layout":{"inherit":true,"type":"constrained"}} -->
<div class="wp-block-group">
<!-- wp:woocommerce/product-image-gallery /-->
</div>
<!-- /wp:group -->
</div>
<!-- /wp:group -->
<!-- wp:template-part {"slug":"footer","theme":"twentytwentythree","tagName":"footer"} /-->';

$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, '' );
}
}