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
Changes from 2 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
39 changes: 28 additions & 11 deletions src/BlockTypes/ProductQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +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.
*/
protected function enqueue_data( array $attributes = [] ) {
parent::enqueue_data( $attributes );

$gutenberg_version = '';
* 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
*/
private function check_if_post_template_has_support_for_grid_view() {
if ( version_compare( $GLOBALS['wp_version'], '6.2.2', '>' ) ) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Should we put both logic in a dedicated function and add a comment? (it would be great just the link to this PR)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: Should we put both logic in a dedicated function and add a comment? (it would be great just the link to this PR)

Good idea! I extracted the logic to separate function, do you mind taking a look one more time?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed that we should fix the check for the WP version :D

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure that this check is correct? Should be

Suggested change
if ( version_compare( $GLOBALS['wp_version'], '6.2.2', '>' ) ) {
if ( version_compare( $GLOBALS['wp_version'], '6.3.0', '>=' ) ) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version_compare consider versions with suffix as lower, for example, 6.3.0 is higher than 6.3.0-RC2. That means the fix won't be applied to the RC version.

But you're right, I cannot compare to 6.2.2, because there may be another 6.2.x release and there will be incorrect behaviour.

I'll try to use this custom function wp_version_compare which I belive was written for exactly this case or I'll think of something custom.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

version_compare consider versions with suffix as lower, for example, 6.3.0 is higher than 6.3.0-RC2. That means the fix won't be applied to the RC version.

TIL

version_compare consider versions with suffix as lower, for example, 6.3.0 is higher than 6.3.0-RC2. That means the fix won't be applied to the RC version.

Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, code updated! And I updated testing steps to cover cases with higher and lower than WP 6.3 + Gutenberg 16 enabled and disabled.

return true;
}

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

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

Expand Down