-
Notifications
You must be signed in to change notification settings - Fork 219
Fix Product Query block hijacking all Query blocks queries #6952
Fix Product Query block hijacking all Query blocks queries #6952
Conversation
The Product Query block was adding a filter to Gutenberg's core Query Loop block. When this filter was added once, it would from then on apply to all other Query Loop blocks (or variations thereof) on a page. This change makes sure that: 1. No filters are added in the first place if our custom `__woocommerceVariationProps` are not set. 2. The filter only gets run once.
The release ZIP for this PR is accessible via:
|
Size Change: 0 B Total Size: 868 kB ℹ️ View Unchanged
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Good catch! For now, I think that we can adopt this approach 👍 What do you think if we add a comment? Maybe add a link to this PR, it is a good idea too!
…ub.com:woocommerce/woocommerce-gutenberg-products-block into fix/6950-product-query-hijacks-all-query-loops
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested on my end too, and confirming it fixes the issue. 👌
I agree with Luigi that we can go ahead with this solution for now. We can always improve it in the future if we have any other ideas.
One thing we could try is to store $parsed_block
into a class property, this way we don't need to pass it to the filter function every time. Something along these lines:
public function update_query( $pre_render, $parsed_block ) {
if ( 'core/query' !== $parsed_block['blockName'] || ! isset( $parsed_block['attrs']['__woocommerceVariationProps'] ) ) {
return;
}
$this->parsed_block = $parsed_block;
add_filter(
'gutenberg_build_query_vars_from_query_block',
array( $this, 'get_query_vars' ),
10,
1
);
}
public function get_query_vars( $query ) {
remove_filter(
'gutenberg_build_query_vars_from_query_block',
array( $this, 'get_query_vars' )
);
return $this->get_query_by_attributes( $query, $this->parsed_block );
}
It has the benefit that we can remove the filter, with the expense that we need to add a property to the class. So leaving the final decision to you.
…ub.com:woocommerce/woocommerce-gutenberg-products-block into fix/6950-product-query-hijacks-all-query-loops
Thank you @Aljullu!! I've tested your implementation and it's great. You solved my conundrum! This is a much cleaner solution and I don't have the reservations I had before for this fix now! This made me a happy dev :D |
…ce#6952) * Fix Product Query block hijacking all Query blocks queries The Product Query block was adding a filter to Gutenberg's core Query Loop block. When this filter was added once, it would from then on apply to all other Query Loop blocks (or variations thereof) on a page. This change makes sure that: 1. No filters are added in the first place if our custom `__woocommerceVariationProps` are not set. 2. The filter only gets run once.
The Product Query block was adding a filter to Gutenberg's core Query Loop block. When this filter was added once, it would from then on apply to all other Query Loop blocks (or variations thereof) on a page.
This change makes sure that:
__woocommerceVariationProps
are not set.
Fixes #6950
Testing
Automated Tests
User Facing Testing
WooCommerce Visibility
Performance Impact
Filters are still created, hooked and ran. They are not removed even when they have ran their course. Tradeoff explained above.
Changelog