From 4381994ea6ce17de1930be0dd5b998bb04b8eb6d Mon Sep 17 00:00:00 2001 From: Karol Manijak <20098064+kmanijak@users.noreply.github.com> Date: Wed, 26 Jul 2023 10:44:00 +0200 Subject: [PATCH 1/3] Check if WordPress version is higher than 6.2.2 to make Products block compatible with Gutenberg 16+ --- src/BlockTypes/ProductQuery.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 8916d615683..75ccf604266 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -91,6 +91,14 @@ protected function initialize() { protected function enqueue_data( array $attributes = [] ) { parent::enqueue_data( $attributes ); + if ( version_compare( $GLOBALS['wp_version'], '6.2.2', '>' ) ) { + $this->asset_data_registry->add( + 'post_template_has_support_for_grid_view', + true + ); + return; + } + $gutenberg_version = ''; if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) { From fee90dc2c830a68358182fd349a2d35935a961f8 Mon Sep 17 00:00:00 2001 From: Karol Manijak <20098064+kmanijak@users.noreply.github.com> Date: Wed, 26 Jul 2023 11:38:20 +0200 Subject: [PATCH 2/3] Extract the logic of checking the post template support for grid view toi separate function --- src/BlockTypes/ProductQuery.php | 41 ++++++++++++++++++++------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 75ccf604266..971d2eb36ae 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -82,26 +82,19 @@ protected function initialize() { } /** - * 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. + * Post Template support for grid view was introduced in Gutenberg 16 / WordPress 6.3 + * Fixed in: + * - https://github.com/woocommerce/woocommerce-blocks/pull/9916 + * - https://github.com/woocommerce/woocommerce-blocks/pull/10360 */ - protected function enqueue_data( array $attributes = [] ) { - parent::enqueue_data( $attributes ); - + private function check_if_post_template_has_support_for_grid_view() { if ( version_compare( $GLOBALS['wp_version'], '6.2.2', '>' ) ) { - $this->asset_data_registry->add( - 'post_template_has_support_for_grid_view', - true - ); - return; + return true; } - $gutenberg_version = ''; - if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) { + $gutenberg_version = ''; + if ( defined( 'GUTENBERG_VERSION' ) ) { $gutenberg_version = GUTENBERG_VERSION; } @@ -113,11 +106,27 @@ protected function enqueue_data( array $attributes = [] ) { ); $gutenberg_version = $gutenberg_data['Version']; } + return version_compare( $gutenberg_version, '16.0', '>=' ); } + return false; + } + + /** + * 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 ); + + $post_template_has_support_for_grid_view = $this->check_if_post_template_has_support_for_grid_view(); + $this->asset_data_registry->add( 'post_template_has_support_for_grid_view', - version_compare( $gutenberg_version, '16.0', '>=' ) + $post_template_has_support_for_grid_view ); } From cc4dbdd003ed5233f746477658e7b49e8f8985f0 Mon Sep 17 00:00:00 2001 From: Karol Manijak <20098064+kmanijak@users.noreply.github.com> Date: Wed, 26 Jul 2023 12:24:09 +0200 Subject: [PATCH 3/3] Change the versions comparison and improve description of custom version compare function --- src/BlockTypes/ProductQuery.php | 3 ++- src/Utils/Utils.php | 5 ++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/BlockTypes/ProductQuery.php b/src/BlockTypes/ProductQuery.php index 971d2eb36ae..17ac3f16dd7 100644 --- a/src/BlockTypes/ProductQuery.php +++ b/src/BlockTypes/ProductQuery.php @@ -2,6 +2,7 @@ namespace Automattic\WooCommerce\Blocks\BlockTypes; use WP_Query; +use Automattic\WooCommerce\Blocks\Utils\Utils; // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_tax_query // phpcs:disable WordPress.DB.SlowDBQuery.slow_db_query_meta_query @@ -88,7 +89,7 @@ protected function initialize() { * - https://github.com/woocommerce/woocommerce-blocks/pull/10360 */ private function check_if_post_template_has_support_for_grid_view() { - if ( version_compare( $GLOBALS['wp_version'], '6.2.2', '>' ) ) { + if ( Utils::wp_version_compare( '6.3', '>=' ) ) { return true; } diff --git a/src/Utils/Utils.php b/src/Utils/Utils.php index 72fab480581..25af8c9e828 100644 --- a/src/Utils/Utils.php +++ b/src/Utils/Utils.php @@ -7,7 +7,10 @@ class Utils { /** - * Compare the current WordPress version with a given version. + * Compare the current WordPress version with a given version. It's a wrapper around `version-compare` + * that additionally takes into account the suffix (like `-RC1`). + * For example: version 6.3 is considered lower than 6.3-RC2, so you can do + * wp_version_compare( '6.3', '>=' ) and that will return true for 6.3-RC2. * * @param string $version The version to compare against. * @param string|null $operator Optional. The comparison operator. Defaults to null.