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: fix compatibility layer logic when the block…
Browse files Browse the repository at this point in the history
…s aren't wrapped in a group block
  • Loading branch information
gigitux committed Jun 23, 2023
1 parent bf63c69 commit bdcea75
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 18 deletions.
52 changes: 34 additions & 18 deletions src/Templates/SingleProductTemplateCompatibility.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,16 @@ protected function set_hook_data() {
* @return string
*/
public static function add_compatibility_layer( $template_content ) {
$parsed_blocks = parse_blocks( $template_content );

if ( ! self::has_single_product_template_blocks( $parsed_blocks ) ) {
$template = self::inject_custom_attributes_to_first_and_last_block_single_product_template( $parsed_blocks );
return self::serialize_blocks_after_parsed( $template );
}

$wrapped_blocks = self::wrap_single_product_template( $template_content );
$template = self::inject_custom_attributes_to_first_and_last_block_single_product_template( $wrapped_blocks );

return array_reduce(
$template,
function( $carry, $item ) {
if ( is_array( $item ) ) {
return $carry . serialize_blocks( $item );
}
return $carry . serialize_block( $item );
},
''
);
return self::serialize_blocks_after_parsed( $template );

}

Expand All @@ -286,15 +283,13 @@ private static function wrap_single_product_template( $template_content ) {
$parsed_blocks = parse_blocks( $template_content );
$grouped_blocks = self::group_blocks( $parsed_blocks );

$single_product_template_blocks = array( 'woocommerce/product-image-gallery', 'woocommerce/product-details', 'woocommerce/add-to-cart-form', 'woocommerce/product-meta', 'woocommerce/product-price', 'woocommerce/breadcrumbs' );

$wrapped_blocks = array_map(
function( $blocks ) use ( $single_product_template_blocks ) {
function( $blocks ) {
if ( 'core/template-part' === $blocks[0]['blockName'] ) {
return $blocks;
}

$has_single_product_template_blocks = self::has_single_product_template_blocks( $blocks, $single_product_template_blocks );
$has_single_product_template_blocks = self::has_single_product_template_blocks( $blocks );

if ( $has_single_product_template_blocks ) {
$wrapped_block = self::create_wrap_block_group( $blocks );
Expand All @@ -320,7 +315,8 @@ function( $carry, $item ) {

$index = $carry['index'];
$carry['index'] = $carry['index'] + 1;
$block = $item[0];
// If the block is a child of a group block, we need to get the first block of the group.
$block = isset( $item[0] ) ? $item[0] : $item;

if ( 'core/template-part' === $block['blockName'] || self::is_custom_html( $block ) ) {
$carry['template'][] = $block;
Expand Down Expand Up @@ -396,10 +392,11 @@ private static function create_wrap_block_group( $blocks ) {
* woocommerce/product-gallery-image, woocommerce/product-details, woocommerce/add-to-cart-form]
*
* @param array $parsed_blocks Array of parsed block objects.
* @param array $single_product_template_blocks Array of single product template blocks.
* @return bool True if the template has a single product template block, false otherwise.
*/
private static function has_single_product_template_blocks( $parsed_blocks, $single_product_template_blocks ) {
private static function has_single_product_template_blocks( $parsed_blocks ) {
$single_product_template_blocks = array( 'woocommerce/product-image-gallery', 'woocommerce/product-details', 'woocommerce/add-to-cart-form', 'woocommerce/product-meta', 'woocommerce/product-price', 'woocommerce/breadcrumbs' );

$found = false;

foreach ( $parsed_blocks as $block ) {
Expand Down Expand Up @@ -478,4 +475,23 @@ private function inject_hooks_after_the_wrapper( $block_content, $hooks ) {
private static function is_custom_html( $block ) {
return empty( $block['blockName'] ) && ! empty( $block['innerHTML'] );
}

/**
* Serialize template.
*
* @param array $parsed_blocks Parsed blocks.
* @return string
*/
private static function serialize_blocks_after_parsed( $parsed_blocks ) {
return array_reduce(
$parsed_blocks,
function( $carry, $item ) {
if ( is_array( $item ) ) {
return $carry . serialize_blocks( $item );
}
return $carry . serialize_block( $item );
},
''
);
}
}
39 changes: 39 additions & 0 deletions tests/php/Templates/SingleProductTemplateCompatibilityTests.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,45 @@ public function test_add_compatibility_layer_if_contains_single_product_blocks_a
$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, '' );
}

/**
* @group failing
* Test that the Single Product Template doesn't remove any blocks if those aren't grouped in a a core/group block
*/
public function test_add_compatibility_layer_if_contains_blocks_not_related_to_the_single_product_template_and_not_grouped() {
$default_single_product_template = '
<!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
<!-- wp:paragraph -->
<p>hello</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>hello1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>hello2</p>
<!-- /wp:paragraph -->
<!-- wp:template-part {"slug":"footer","theme":"twentytwentythree","tagName":"footer"} /-->';

$expected_single_product_template = '
<!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
<!-- wp:paragraph {"__wooCommerceIsFirstBlock":true} -->
<p>hello</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph -->
<p>hello1</p>
<!-- /wp:paragraph -->
<!-- wp:paragraph {"__wooCommerceIsLastBlock":true} -->
<p>hello2</p>
<!-- /wp:paragraph -->
<!-- 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 bdcea75

Please sign in to comment.