From 83bed48693c3dff6e22cf9e509e86ecc44955503 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 20 Jun 2023 14:05:57 +0200 Subject: [PATCH 1/2] Revert "fix products block layout on gutenberg 16 (#9886)" This reverts commit f1e5dd75bf30233d03214fdc99d8823fd5fc4df4. --- assets/js/blocks/product-query/constants.ts | 4 ---- .../blocks/product-query/variations/related-products.tsx | 8 +------- docs/internal-developers/testing/smoke-testing.md | 2 +- patterns/featured-products-5-cols.php | 2 +- patterns/product-collections-featured-collection.php | 2 +- patterns/product-collections-newest-arrivals.php | 2 +- patterns/product-featured-2-columns.php | 2 +- templates/templates/blockified/archive-product.html | 2 +- .../templates/blockified/product-search-results.html | 2 +- templates/templates/blockified/single-product.html | 2 +- .../templates/blockified/taxonomy-product_attribute.html | 4 ++-- templates/templates/blockified/taxonomy-product_cat.html | 4 ++-- templates/templates/blockified/taxonomy-product_tag.html | 4 ++-- 13 files changed, 15 insertions(+), 25 deletions(-) diff --git a/assets/js/blocks/product-query/constants.ts b/assets/js/blocks/product-query/constants.ts index f4ef7d34a82..61e0b9771cc 100644 --- a/assets/js/blocks/product-query/constants.ts +++ b/assets/js/blocks/product-query/constants.ts @@ -74,10 +74,6 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ 'core/post-template', { __woocommerceNamespace: PRODUCT_TEMPLATE_ID, - layout: { - type: 'grid', - columnsCount: 3, - }, /** * This class is used to add default styles for inner blocks. */ diff --git a/assets/js/blocks/product-query/variations/related-products.tsx b/assets/js/blocks/product-query/variations/related-products.tsx index 6ff35170b37..e13a243f48b 100644 --- a/assets/js/blocks/product-query/variations/related-products.tsx +++ b/assets/js/blocks/product-query/variations/related-products.tsx @@ -53,13 +53,7 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ ], [ 'core/post-template', - { - __woocommerceNamespace: PRODUCT_TEMPLATE_ID, - layout: { - type: 'grid', - columnsCount: 3, - }, - }, + { __woocommerceNamespace: PRODUCT_TEMPLATE_ID }, [ [ 'woocommerce/product-image', diff --git a/docs/internal-developers/testing/smoke-testing.md b/docs/internal-developers/testing/smoke-testing.md index 7905ffd7277..251f4d00216 100644 --- a/docs/internal-developers/testing/smoke-testing.md +++ b/docs/internal-developers/testing/smoke-testing.md @@ -174,7 +174,7 @@ Note: some blocks might fail to render because they are based on products having
-
+
diff --git a/patterns/featured-products-5-cols.php b/patterns/featured-products-5-cols.php index dae4ff3743e..db992b2450b 100644 --- a/patterns/featured-products-5-cols.php +++ b/patterns/featured-products-5-cols.php @@ -14,7 +14,7 @@
- +
diff --git a/patterns/product-collections-featured-collection.php b/patterns/product-collections-featured-collection.php index fbb70e5b508..04291c3456d 100644 --- a/patterns/product-collections-featured-collection.php +++ b/patterns/product-collections-featured-collection.php @@ -23,7 +23,7 @@
- + diff --git a/patterns/product-collections-newest-arrivals.php b/patterns/product-collections-newest-arrivals.php index 117e78d8d8f..c372bdcc66e 100644 --- a/patterns/product-collections-newest-arrivals.php +++ b/patterns/product-collections-newest-arrivals.php @@ -31,7 +31,7 @@
- +
diff --git a/patterns/product-featured-2-columns.php b/patterns/product-featured-2-columns.php index 6ce0f3f557e..327a432eb21 100644 --- a/patterns/product-featured-2-columns.php +++ b/patterns/product-featured-2-columns.php @@ -12,7 +12,7 @@
- + diff --git a/templates/templates/blockified/archive-product.html b/templates/templates/blockified/archive-product.html index 69f533bffd2..81687d48b5d 100644 --- a/templates/templates/blockified/archive-product.html +++ b/templates/templates/blockified/archive-product.html @@ -18,7 +18,7 @@
- + diff --git a/templates/templates/blockified/product-search-results.html b/templates/templates/blockified/product-search-results.html index 17b39c4bb44..109fb56f4d5 100644 --- a/templates/templates/blockified/product-search-results.html +++ b/templates/templates/blockified/product-search-results.html @@ -16,7 +16,7 @@
- + diff --git a/templates/templates/blockified/single-product.html b/templates/templates/blockified/single-product.html index 501777643c1..0fecbef4636 100644 --- a/templates/templates/blockified/single-product.html +++ b/templates/templates/blockified/single-product.html @@ -53,7 +53,7 @@

Related products

- + diff --git a/templates/templates/blockified/taxonomy-product_attribute.html b/templates/templates/blockified/taxonomy-product_attribute.html index 3bf25ca4931..62d797de82c 100644 --- a/templates/templates/blockified/taxonomy-product_attribute.html +++ b/templates/templates/blockified/taxonomy-product_attribute.html @@ -18,7 +18,7 @@
- + @@ -46,4 +46,4 @@
- + \ No newline at end of file diff --git a/templates/templates/blockified/taxonomy-product_cat.html b/templates/templates/blockified/taxonomy-product_cat.html index 3bf25ca4931..62d797de82c 100644 --- a/templates/templates/blockified/taxonomy-product_cat.html +++ b/templates/templates/blockified/taxonomy-product_cat.html @@ -18,7 +18,7 @@
- + @@ -46,4 +46,4 @@
- + \ No newline at end of file diff --git a/templates/templates/blockified/taxonomy-product_tag.html b/templates/templates/blockified/taxonomy-product_tag.html index 9c6699850d0..1de3af7a0ed 100644 --- a/templates/templates/blockified/taxonomy-product_tag.html +++ b/templates/templates/blockified/taxonomy-product_tag.html @@ -18,7 +18,7 @@
- + @@ -43,4 +43,4 @@
-
+
\ No newline at end of file From 7dba73e4101db89a11929090897be0da31f9b269 Mon Sep 17 00:00:00 2001 From: Luigi Date: Tue, 20 Jun 2023 14:32:31 +0200 Subject: [PATCH 2/2] add post_template_has_support_for_grid_view setting --- assets/js/blocks/product-query/constants.ts | 14 ++++++-- .../variations/related-products.tsx | 15 ++++++++- src/BlockTypes/ProductQuery.php | 32 +++++++++++++++++++ 3 files changed, 58 insertions(+), 3 deletions(-) diff --git a/assets/js/blocks/product-query/constants.ts b/assets/js/blocks/product-query/constants.ts index 61e0b9771cc..ec91fd2f4f1 100644 --- a/assets/js/blocks/product-query/constants.ts +++ b/assets/js/blocks/product-query/constants.ts @@ -1,10 +1,10 @@ /** * External dependencies */ -import { getSetting } from '@woocommerce/settings'; +import { getSetting, getSettingWithCoercion } from '@woocommerce/settings'; import { objectOmit } from '@woocommerce/utils'; import type { InnerBlockTemplate } from '@wordpress/blocks'; - +import { isBoolean } from '@woocommerce/types'; /** * Internal dependencies */ @@ -69,6 +69,13 @@ export const QUERY_DEFAULT_ATTRIBUTES: QueryBlockAttributes = { }, }; +// This is necessary to fix https://github.com/woocommerce/woocommerce-blocks/issues/9884. +const postTemplateHasSupportForGridView = getSettingWithCoercion( + 'post_template_has_support_for_grid_view', + false, + isBoolean +); + export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ [ 'core/post-template', @@ -78,6 +85,9 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ * This class is used to add default styles for inner blocks. */ className: 'products-block-post-template', + ...( postTemplateHasSupportForGridView && { + layout: { type: 'grid', columnCount: 3 }, + } ), }, [ [ diff --git a/assets/js/blocks/product-query/variations/related-products.tsx b/assets/js/blocks/product-query/variations/related-products.tsx index e13a243f48b..7fa859db0a9 100644 --- a/assets/js/blocks/product-query/variations/related-products.tsx +++ b/assets/js/blocks/product-query/variations/related-products.tsx @@ -6,6 +6,8 @@ import { Icon } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; import { stacks } from '@woocommerce/icons'; import { registerBlockSingleProductTemplate } from '@woocommerce/atomic-utils'; +import { getSettingWithCoercion } from '@woocommerce/settings'; +import { isBoolean } from '@woocommerce/types'; /** * Internal dependencies @@ -43,6 +45,12 @@ export const BLOCK_ATTRIBUTES = { }, }; +const postTemplateHasSupportForGridView = getSettingWithCoercion( + 'post_template_has_support_for_grid_view', + false, + isBoolean +); + export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ [ 'core/heading', @@ -53,7 +61,12 @@ export const INNER_BLOCKS_TEMPLATE: InnerBlockTemplate[] = [ ], [ 'core/post-template', - { __woocommerceNamespace: PRODUCT_TEMPLATE_ID }, + { + __woocommerceNamespace: PRODUCT_TEMPLATE_ID, + ...( postTemplateHasSupportForGridView && { + layout: { type: 'grid', columnCount: 3 }, + } ), + }, [ [ 'woocommerce/product-image', diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 2edd0a5a956..380f9a34224 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -75,6 +75,38 @@ protected function initialize() { add_filter( 'rest_product_collection_params', array( $this, 'extend_rest_query_allowed_params' ), 10, 1 ); } + /** + * Extra data passed through from server to client for block. + * + * @param array $attributes Any attributes that currently are available from the block. + * Note, this will be empty in the editor context when the block is + * not in the post content on editor load. + */ + protected function enqueue_data( array $attributes = [] ) { + parent::enqueue_data( $attributes ); + + $gutenberg_version = ''; + + if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) { + if ( defined( 'GUTENBERG_VERSION' ) ) { + $gutenberg_version = GUTENBERG_VERSION; + } + + if ( ! $gutenberg_version ) { + $gutenberg_data = get_file_data( + WP_PLUGIN_DIR . '/gutenberg/gutenberg.php', + array( 'Version' => 'Version' ) + ); + $gutenberg_version = $gutenberg_data['Version']; + } + } + + $this->asset_data_registry->add( + 'post_template_has_support_for_grid_view', + version_compare( $gutenberg_version, '16.0', '>=' ) + ); + } + /** * Check if a given block *