Automattic\WooCommerce\Blocks\Templates

ArchiveProductTemplatesCompatibility::inject_hooks()publicWC 1.0

Inject hooks to rendered content of corresponding blocks.

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

Хуков нет.

Возвращает

Строку.

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

$ArchiveProductTemplatesCompatibility = new ArchiveProductTemplatesCompatibility();
$ArchiveProductTemplatesCompatibility->inject_hooks( $block_content, $block );
$block_content(разное) (обязательный)
The rendered block content.
$block(разное) (обязательный)
The parsed block data.

Код ArchiveProductTemplatesCompatibility::inject_hooks() WC 7.7.2

public function inject_hooks( $block_content, $block ) {
	if ( ! $this->is_archive_template() ) {
		return $block_content;
	}
	/**
	 * If the block is not inherited, we don't need to inject hooks.
	 */
	if ( empty( $block['attrs']['isInherited'] ) ) {
		return $block_content;
	}

	$block_name = $block['blockName'];

	/**
	 * The core/post-template has two different block names:
	 * - core/post-template when the wrapper is rendered.
	 * - core/null when the loop item is rendered.
	 */
	if (
		'core/null' === $block_name &&
		isset( $block['attrs']['__woocommerceNamespace'] ) &&
		'woocommerce/product-query/product-template' === $block['attrs']['__woocommerceNamespace']
	) {
		$block_name = self::LOOP_ITEM_ID;
	}

	$supported_blocks = array_map(
		function( $hook ) {
			return $hook['block_name'];
		},
		array_values( $this->hook_data )
	);

	if ( ! in_array( $block_name, $supported_blocks, true ) ) {
		return $block_content;
	}

	/**
	 * `core/query-no-result` is a special case because it can return two
	 * different content depending on the context. We need to check if the
	 * block content is empty to determine if we need to inject hooks.
	 */
	if (
		'core/query-no-results' === $block_name &&
		empty( trim( $block_content ) )
	) {
		return $block_content;
	}

	$block_hooks = array_filter(
		$this->hook_data,
		function( $hook ) use ( $block_name ) {
			return $hook['block_name'] === $block_name;
		}
	);

	return sprintf(
		'%1$s%2$s%3$s',
		$this->get_hooks_buffer( $block_hooks, 'before' ),
		$block_content,
		$this->get_hooks_buffer( $block_hooks, 'after' )
	);
}