Skip to content
This repository has been archived by the owner on Feb 23, 2024. It is now read-only.

Check if WordPress version is higher than 6.2.2 to make Products block compatible with Gutenberg 16+ #10360

Merged
merged 3 commits into from
Jul 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -82,18 +83,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 );

$gutenberg_version = '';
private function check_if_post_template_has_support_for_grid_view() {
if ( Utils::wp_version_compare( '6.3', '>=' ) ) {
return true;
}

if ( is_plugin_active( 'gutenberg/gutenberg.php' ) ) {
$gutenberg_version = '';

if ( defined( 'GUTENBERG_VERSION' ) ) {
$gutenberg_version = GUTENBERG_VERSION;
}
Expand All @@ -105,11 +107,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
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/Utils/Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down