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

Commit

Permalink
Check if WordPress version is higher than 6.2.2 to make Products blo…
Browse files Browse the repository at this point in the history
…ck compatible with Gutenberg 16+ (#10360)

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

* Extract the logic of checking the post template support for grid view toi separate function

* Change the versions comparison and improve description of custom version compare function
  • Loading branch information
kmanijak authored Jul 26, 2023
1 parent 5233bb0 commit beab040
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
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

0 comments on commit beab040

Please sign in to comment.