Automattic\WooCommerce\Blocks\BlockTypes
FeaturedItem::replace_post_for_featured_item_inner_block
Replace the global post for the Featured Item inner blocks and reset it after.
This is needed because some of the inner blocks may use the global post instead of fetching the product through the context, so even if the context is passed to the inner block, it will still use the global post.
Method of the class: FeaturedItem{}
No Hooks.
Returns
null. Nothing (null).
Usage
// protected - for code of main (parent) or child class $result = $this->replace_post_for_featured_item_inner_block( $block, $context );
- $block(array) (required)
- Block attributes.
- $context(array) (required) (passed by reference — &)
- Block context.
FeaturedItem::replace_post_for_featured_item_inner_block() FeaturedItem::replace post for featured item inner block code WC 10.8.1
protected function replace_post_for_featured_item_inner_block( $block, &$context ) {
if ( $this->featured_item_inner_blocks_names ) {
$block_name = end( $this->featured_item_inner_blocks_names );
if ( $block_name === $block['blockName'] ) {
array_pop( $this->featured_item_inner_blocks_names );
// Handle core blocks that need global post manipulation.
if ( 'core/post-excerpt' === $block_name || 'core/post-title' === $block_name ) {
global $post;
$post = get_post( $this->featured_item_id ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
if ( $post instanceof \WP_Post ) {
setup_postdata( $post );
}
}
$context['postId'] = $this->featured_item_id;
$context['postType'] = 'product';
$this->current_item = wc_get_product( $this->featured_item_id );
}
}
}