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

Commit a2682b4

Browse files
Fix an endless loop when using product grid blocks inside product descriptions (#6471)
* Fix an endless loop when using product grid blocks inside product descriptions * Add array_key_exists protection * Typo in comment * 👌IMPROVE: Improve comments - #6416 Co-authored-by: Panos (Panagiotis) Synetos <[email protected]>
1 parent 046ee6b commit a2682b4

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Utils/BlocksWpQuery.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* BlocksWpQuery query.
88
*
9-
* Wrapper for WP Query with additonal helper methods.
9+
* Wrapper for WP Query with additional helper methods.
1010
* Allows query args to be set and parsed without doing running it, so that a cache can be used.
1111
*
1212
* @deprecated 2.5.0
@@ -24,6 +24,25 @@ class BlocksWpQuery extends WP_Query {
2424
public function __construct( $query = '' ) {
2525
if ( ! empty( $query ) ) {
2626
$this->init();
27+
28+
// Remove current product from query to avoid infinite loops and out of memory issues.
29+
// That might happen if the store is using Gutenberg for product descriptions.
30+
// See https://github.com/woocommerce/woocommerce-blocks/issues/6416.
31+
global $post;
32+
if ( $post && array_key_exists( 'post__in', $query ) && is_array( $query['post__in'] ) ) {
33+
$global_post_id = $post->ID;
34+
$filtered_post__in = array_filter(
35+
$query['post__in'],
36+
static function ( $post_id ) use ( $global_post_id ) {
37+
if ( $global_post_id === $post_id ) {
38+
return false;
39+
}
40+
return true;
41+
}
42+
);
43+
$query['post__in'] = $filtered_post__in;
44+
}
45+
2746
$this->query = wp_parse_args( $query );
2847
$this->query_vars = $this->query;
2948
$this->parse_query_vars();

0 commit comments

Comments
 (0)