Single Product Block > Only reset the post data if `setup_postdata` was invoked. (https://github.com/woocommerce/woocommerce-blocks/pull/9474)

* Only call wp_reset_postdata() if the global post variable was changed.

* Make global_post_variable_changed static.

* rely on the output of setup_postdata for verifying if the global variable was successfully changed.

* ditch the static var and defaulting to false instead.

* Rely on static as the post reset only happens when the loop of inner blocks ends and not right away.

* Update the  variable when the postdata is reset.
This commit is contained in:
Patricia Hillebrandt 2023-05-18 15:35:46 +02:00 committed by GitHub
parent 0bf8048bd1
commit 61fbf572cb
1 changed files with 6 additions and 3 deletions

View File

@ -125,6 +125,8 @@ class SingleProduct extends AbstractBlock {
if ( $this->single_product_inner_blocks_names ) {
$block_name = array_pop( $this->single_product_inner_blocks_names );
static $global_post_variable_changed;
if ( $block_name === $block['blockName'] ) {
/**
* This is a temporary fix to ensure the Post Title and Excerpt blocks work as expected
@ -136,14 +138,15 @@ class SingleProduct extends AbstractBlock {
if ( 'core/post-excerpt' === $block_name || 'core/post-title' === $block_name ) {
global $post;
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = get_post( $this->product_id );
setup_postdata( $post );
$post = get_post( $this->product_id );
$global_post_variable_changed = setup_postdata( $post );
}
$context['postId'] = $this->product_id;
}
if ( ! $this->single_product_inner_blocks_names ) {
if ( ! $this->single_product_inner_blocks_names && $global_post_variable_changed ) {
wp_reset_postdata();
$global_post_variable_changed = false;
}
}
}