From 30aed1d54bcf866e1965e87b6a6adf942ef5e31b Mon Sep 17 00:00:00 2001 From: Saad Tarhi Date: Tue, 28 Mar 2023 12:48:37 +0000 Subject: [PATCH] Fix duplicated Shipping method Block on the front-end (https://github.com/woocommerce/woocommerce-blocks/pull/8861) * Fix duplicate "Shipping method" Block on the front-end - Issue: When local pickup is enabled and the shipping method's description is edited, the "Shipping method" is duplicated on the front-end - Solution: The previous regex didn't match some special characters such as ".", "!", etc. Because of that, it considers the Local Pickup toggle template to be missing and duplicate it. * Fix the regex looking for block templates The previous regex didn't account for some special characters that the user can use for a block's title or description, such as ".", "!", etc. As a result, the Block's template will be considered missing. --------- Co-authored-by: Niels Lange --- plugins/woocommerce-blocks/src/BlockTypes/Cart.php | 6 +++--- plugins/woocommerce-blocks/src/BlockTypes/Checkout.php | 8 ++++---- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php index 1888272f32b..d9baf83f9f5 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Cart.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Cart.php @@ -98,7 +98,7 @@ class Cart extends AbstractBlock { * The blocks used for testing should be always available in the block (not removable by the user). */ - $regex_for_filled_cart_block = '//mi'; + $regex_for_filled_cart_block = '/]*?>/mi'; // Filled Cart block was added in i2, so we search for it to see if we have a Cart i1 template. $has_i1_template = ! preg_match( $regex_for_filled_cart_block, $content ); @@ -140,8 +140,8 @@ class Cart extends AbstractBlock {
'; // Order summary subtotal block was added in i3, so we search for it to see if we have a Cart i2 template. - $regex_for_order_summary_subtotal = '//mi'; - $regex_for_order_summary = '//mi'; + $regex_for_order_summary_subtotal = '/]*?>/mi'; + $regex_for_order_summary = '/]*?>/mi'; $has_i2_template = ! preg_match( $regex_for_order_summary_subtotal, $content ); if ( $has_i2_template ) { diff --git a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php index 75b15c45abc..107d66b93cc 100644 --- a/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php +++ b/plugins/woocommerce-blocks/src/BlockTypes/Checkout.php @@ -143,8 +143,8 @@ class Checkout extends AbstractBlock {
'; // Order summary subtotal block was added in i3, so we search for it to see if we have a Checkout i2 template. - $regex_for_order_summary_subtotal = '//mi'; - $regex_for_order_summary = '//mi'; + $regex_for_order_summary_subtotal = '/]*?>/mi'; + $regex_for_order_summary = '/]*?>/mi'; $has_i2_template = ! preg_match( $regex_for_order_summary_subtotal, $content ); if ( $has_i2_template ) { @@ -155,11 +155,11 @@ class Checkout extends AbstractBlock { * Add the Local Pickup toggle to checkouts missing this forced template. */ $local_pickup_inner_blocks = '
' . PHP_EOL . PHP_EOL . '
' . PHP_EOL . PHP_EOL . '$0'; - $has_local_pickup_regex = '//mi'; + $has_local_pickup_regex = '/]*?>/mi'; $has_local_pickup = preg_match( $has_local_pickup_regex, $content ); if ( ! $has_local_pickup ) { - $shipping_address_block_regex = '/<\/div>/mi'; + $shipping_address_block_regex = '/]*?><\/div>/mi'; $content = preg_replace( $shipping_address_block_regex, $local_pickup_inner_blocks, $content ); }