Commit 132f18b365f for woocommerce
commit 132f18b365fadee503d7bae070656dec6b6cd8f4
Author: Albert Juhé Lluveras <contact@albertjuhe.com>
Date: Mon Sep 14 09:10:29 2026 +0200
Correctly hide patterns containing product-related blocks when products are password-protected (#68479)
* Correctly hide patterns containing product-related blocks when products are password-protected
* Add changelog
* Fix PHPStan
* Linting
* Add test
* Remove special check for Related Products pattern
diff --git a/plugins/woocommerce/changelog/fix-password-protected-products-patterns b/plugins/woocommerce/changelog/fix-password-protected-products-patterns
new file mode 100644
index 00000000000..d838a80ce36
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-password-protected-products-patterns
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Correctly hide patterns containing product-related blocks when products are password-protected
diff --git a/plugins/woocommerce/phpstan-baseline.neon b/plugins/woocommerce/phpstan-baseline.neon
index cba816698a6..5c0a47e7b25 100644
--- a/plugins/woocommerce/phpstan-baseline.neon
+++ b/plugins/woocommerce/phpstan-baseline.neon
@@ -53758,18 +53758,6 @@ parameters:
count: 1
path: src/Blocks/Utils/BlockTemplateUtils.php
- -
- message: '#^Parameter \#2 \$blocks of static method Automattic\\WooCommerce\\Blocks\\Utils\\BlockTemplateUtils\:\:has_block_including_patterns\(\) expects array\<Automattic\\WooCommerce\\Blocks\\Utils\\WP_Block\>, array\<int\|string, array\<string, array\|string\>\> given\.$#'
- identifier: argument.type
- count: 2
- path: src/Blocks/Utils/BlockTemplateUtils.php
-
- -
- message: '#^Parameter \$blocks of method Automattic\\WooCommerce\\Blocks\\Utils\\BlockTemplateUtils\:\:has_block_including_patterns\(\) has invalid type Automattic\\WooCommerce\\Blocks\\Utils\\WP_Block\.$#'
- identifier: class.notFound
- count: 1
- path: src/Blocks/Utils/BlockTemplateUtils.php
-
-
message: '#^Parameter \$template of method Automattic\\WooCommerce\\Blocks\\Utils\\BlockTemplateUtils\:\:update_template_data\(\) has invalid type Automattic\\WooCommerce\\Blocks\\Utils\\WP_Block_Template\.$#'
identifier: class.notFound
diff --git a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php
index c4e4d2f42ae..95fe802cb7b 100644
--- a/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php
+++ b/plugins/woocommerce/src/Blocks/Templates/SingleProductTemplate.php
@@ -243,9 +243,23 @@ class SingleProductTemplate extends AbstractTemplate {
return true;
}
- return 'core/pattern' === $block_name
- && isset( $block['attrs']['slug'] )
- && 'woocommerce-blocks/related-products' === $block['attrs']['slug'];
+ if (
+ 'core/pattern' !== $block_name ||
+ ! isset( $block['attrs']['slug'] )
+ ) {
+ return false;
+ }
+
+ $pattern = \WP_Block_Patterns_Registry::get_instance()->get_registered( $block['attrs']['slug'] );
+
+ if ( empty( $pattern['content'] ) ) {
+ return false;
+ }
+
+ return BlockTemplateUtils::has_block_including_patterns(
+ $single_product_template_blocks,
+ parse_blocks( $pattern['content'] )
+ );
}
/**
diff --git a/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php b/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
index 5e1c1059151..8308faffda3 100644
--- a/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
+++ b/plugins/woocommerce/src/Blocks/Utils/BlockTemplateUtils.php
@@ -623,8 +623,8 @@ class BlockTemplateUtils {
* Determines whether the provided $blocks contains any of the $block_names,
* or if they contain a pattern that contains any of the $block_names.
*
- * @param string[] $block_names Full block types to look for.
- * @param WP_Block[] $blocks Array of block objects.
+ * @param string[] $block_names Full block types to look for.
+ * @param array $blocks Array of blocks as returned by parse_blocks().
* @return bool Whether the content contains the specified block.
*/
public static function has_block_including_patterns( $block_names, $blocks ) {
diff --git a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
index f1bd225de10..e528734943e 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/Templates/SingleProductTemplateTests.php
@@ -390,4 +390,40 @@ class SingleProductTemplateTests extends WP_UnitTestCase {
TemplateContentUtils::strip_whitespace_and_password_form_ids( $expected_single_product_template )
);
}
+
+ /**
+ * @testdox Adds the password form when a pattern contains Single Product blocks.
+ */
+ public function test_replace_pattern_with_single_product_blocks_with_input_form() {
+ register_block_pattern(
+ 'test-single-product-pattern',
+ array(
+ 'title' => 'Test Single Product Pattern',
+ 'description' => 'Test Pattern Description',
+ 'content' => '<!-- wp:woocommerce/product-price /-->',
+ )
+ );
+
+ $default_single_product_template = '
+ <!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
+ <!-- wp:pattern {"slug":"test-single-product-pattern"} /-->
+ <!-- wp:template-part {"slug":"footer","theme":"twentytwentythree","tagName":"footer"} /-->';
+
+ $expected_single_product_template = sprintf(
+ '
+ <!-- wp:template-part {"slug":"header","theme":"twentytwentythree","tagName":"header"} /-->
+ <!-- wp:html -->%s<!-- /wp:html -->
+ <!-- wp:template-part {"slug":"footer","theme":"twentytwentythree","tagName":"footer"} /-->',
+ get_the_password_form()
+ );
+
+ $result = SingleProductTemplate::add_password_form(
+ $default_single_product_template
+ );
+
+ $this->assertEquals(
+ TemplateContentUtils::strip_whitespace_and_password_form_ids( $expected_single_product_template ),
+ TemplateContentUtils::strip_whitespace_and_password_form_ids( $result )
+ );
+ }
}