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

Commit

Permalink
Single Product Template - Compatibility Layer: don't skip custom HTML (
Browse files Browse the repository at this point in the history
  • Loading branch information
gigitux committed Apr 18, 2023
1 parent 1edccf3 commit 5d08a97
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
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, '' );
}
}

0 comments on commit 5d08a97

Please sign in to comment.