Automattic\WooCommerce\Blocks\BlockTypes

ProductTemplate::render()protectedWC 1.0

Render the block.

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

Хуков нет.

Возвращает

Строку. | void Rendered block output.

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

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

Код ProductTemplate::render() WC 9.8.4

protected function render( $attributes, $content, $block ) {
	$query = ProductCollectionUtils::prepare_and_execute_query( $block );

	if ( ! $query->have_posts() ) {
		return '';
	}

	if ( $this->block_core_post_template_uses_featured_image( $block->inner_blocks ) ) {
		update_post_thumbnail_cache( $query );
	}

	$classnames = '';
	if ( isset( $block->context['displayLayout'] ) && isset( $block->context['query'] ) ) {
		$classnames = 'is-product-collection-layout-' . $block->context['displayLayout']['type'] . ' ';

		if ( isset( $block->context['displayLayout']['type'] ) && 'flex' === $block->context['displayLayout']['type'] ) {
			if ( isset( $block->context['displayLayout']['shrinkColumns'] ) && $block->context['displayLayout']['shrinkColumns'] ) {
				$classnames = "wc-block-product-template__responsive columns-{$block->context['displayLayout']['columns']}";
			} else {
				$classnames = "is-flex-container columns-{$block->context['displayLayout']['columns']}";
			}
		}
	}

	if ( isset( $attributes['style']['elements']['link']['color']['text'] ) ) {
		$classnames .= ' has-link-color';
	}

	$classnames .= ' wc-block-product-template';

	$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => trim( $classnames ) ) );

	$content = '';
	while ( $query->have_posts() ) {
		$query->the_post();

		// Get an instance of the current Post Template block.
		$block_instance = $block->parsed_block;
		$product_id     = get_the_ID();

		// Set the block name to one that does not correspond to an existing registered block.
		// This ensures that for the inner instances of the Post Template block, we do not render any block supports.
		$block_instance['blockName'] = 'core/null';

		// Relay the block context to the inner blocks.
		$available_context = array_merge(
			(array) $block->context,
			array(
				'postType' => get_post_type(),
				'postId'   => $product_id,
			)
		);

		// Render the inner blocks of the Post Template block with `dynamic` set to `false` to prevent calling
		// `render_callback` and ensure that no wrapper markup is included.
		$block_content = (
			new WP_Block(
				$block_instance,
				$available_context
			)
		)->render( array( 'dynamic' => false ) );

		$context = array(
			'productId' => $product_id,
		);

		$li_directives = '
			data-wp-interactive="woocommerce/product-collection"
			data-wp-context=\'' . wp_json_encode( $context, JSON_NUMERIC_CHECK | JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ) . '\'
			data-wp-key="product-item-' . $product_id . '"
		';

		// Wrap the render inner blocks in a `li` element with the appropriate post classes.
		$post_classes = implode( ' ', get_post_class( 'wc-block-product' ) );
		$content     .= strtr(
			'<li class="{classes}"
				{li_directives}
			>
				{content}
			</li>',
			array(
				'{classes}'       => esc_attr( $post_classes ),
				'{li_directives}' => $li_directives,
				'{content}'       => $block_content,
			)
		);
	}

	/*
	* Use this function to restore the context of the template tags
	* from a secondary query loop back to the main query loop.
	* Since we use two custom loops, it's safest to always restore.
	*/
	wp_reset_postdata();

	return sprintf(
		'<ul %1$s>%2$s</ul>',
		$wrapper_attributes,
		$content
	);
}