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

Commit

Permalink
Single Product Block > Ensure the Product Summary and Title are alway…
Browse files Browse the repository at this point in the history
…s rendered with the correct context independent of their position (#9968)

* Ensure the Product Summary and Product Title blocks are always rendered independent of their position in the editor.

* Update docblock with note informing where the global post variable is restored for the post title and excerpt blocks.
  • Loading branch information
nefeline authored Jun 23, 2023
1 parent 5148543 commit bf63c69
Showing 1 changed file with 35 additions and 10 deletions.
45 changes: 35 additions & 10 deletions src/BlockTypes/SingleProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,32 @@ class SingleProduct extends AbstractBlock {
*/
protected function initialize() {
parent::initialize();
add_filter( 'render_block_context', array( $this, 'update_context' ), 10, 3 );
add_filter( 'render_block_context', [ $this, 'update_context' ], 10, 3 );
add_filter( 'render_block_core/post-excerpt', [ $this, 'restore_global_post' ], 10, 3 );
add_filter( 'render_block_core/post-title', [ $this, 'restore_global_post' ], 10, 3 );
}

/**
* Restore the global post variable right before generating the render output for the post title and/or post excerpt blocks.
*
* This is required due to the changes made via the replace_post_for_single_product_inner_block method.
* It is a temporary fix to ensure these blocks work as expected until Gutenberg versions 15.2 and 15.6 are part of the core of WordPress.
*
* @see https://github.com/WordPress/gutenberg/pull/48001
* @see https://github.com/WordPress/gutenberg/pull/49495
*
* @param string $block_content The block content.
* @param array $parsed_block The full block, including name and attributes.
* @param \WP_Block $block_instance The block instance.
*
* @return mixed
*/
public function restore_global_post( $block_content, $parsed_block, $block_instance ) {
if ( isset( $block_instance->context['singleProduct'] ) && $block_instance->context['singleProduct'] ) {
wp_reset_postdata();
}

return $block_content;
}


Expand Down Expand Up @@ -125,28 +150,28 @@ protected function replace_post_for_single_product_inner_block( $block, &$contex
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
* until Gutenberg versions 15.2 and 15.6 are included in the core of WordPress.
*
* Important: the original post data is restored in the restore_global_post method.
*
* @see https://github.com/WordPress/gutenberg/pull/48001
* @see https://github.com/WordPress/gutenberg/pull/49495
*/
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 );
$global_post_variable_changed = setup_postdata( $post );
$post = get_post( $this->product_id );

if ( $post instanceof \WP_Post ) {
setup_postdata( $post );
}
}
$context['postId'] = $this->product_id;
}

if ( ! $this->single_product_inner_blocks_names && $global_post_variable_changed ) {
wp_reset_postdata();
$global_post_variable_changed = false;
$context['postId'] = $this->product_id;
$context['singleProduct'] = true;
}
}
}
Expand Down

0 comments on commit bf63c69

Please sign in to comment.