Automattic\WooCommerce\Blocks\BlockTypes

FeaturedItem::replace_post_for_featured_item_inner_blockprotectedWC 1.0

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.

Метод класса: FeaturedItem{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// protected - в коде основоного (родительского) или дочернего класса
$result = $this->replace_post_for_featured_item_inner_block( $block, $context );
$block(массив) (обязательный)
Block attributes.
$context(массив) (обязательный) (передается по ссылке — &)
Block context.

Код FeaturedItem::replace_post_for_featured_item_inner_block() WC 11.0.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 );
		}
	}
}