Automattic\WooCommerce\Blocks\Templates

SingleProductTemplateCompatibility::inject_custom_attributes_to_first_and_last_block_single_product_template()private staticWC 1.0

Add custom attributes to the first group block and last group block that wrap Single Product Template blocks.

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

Хуков нет.

Возвращает

Массив.

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

$result = SingleProductTemplateCompatibility::inject_custom_attributes_to_first_and_last_block_single_product_template( $wrapped_blocks );
$wrapped_blocks(массив) (обязательный)
Wrapped blocks.

Код SingleProductTemplateCompatibility::inject_custom_attributes_to_first_and_last_block_single_product_template() WC 9.3.3

private static function inject_custom_attributes_to_first_and_last_block_single_product_template( $wrapped_blocks ) {
	$template_with_custom_attributes = array_reduce(
		$wrapped_blocks,
		function ( $carry, $item ) {

			$index          = $carry['index'];
			$carry['index'] = $carry['index'] + 1;
			// If the block is a child of a group block, we need to get the first block of the group.
			$block = isset( $item[0] ) ? $item[0] : $item;

			if ( 'core/template-part' === $block['blockName'] || self::is_custom_html( $block ) ) {
				$carry['template'][] = $block;
				return $carry;
			}

			if ( '' === $carry['first_block']['index'] ) {
				$block['attrs'][ self::IS_FIRST_BLOCK ] = true;
				$carry['first_block']['index']          = $index;
			}

			if ( '' !== $carry['last_block']['index'] ) {
				$index_element                         = $carry['last_block']['index'];
				$carry['last_block']['index']          = $index;
				$block['attrs'][ self::IS_LAST_BLOCK ] = true;
				unset( $carry['template'][ $index_element ]['attrs'][ self::IS_LAST_BLOCK ] );

				$carry['template'][] = $block;

				return $carry;
			}

			$block['attrs'][ self::IS_LAST_BLOCK ] = true;
			$carry['last_block']['index']          = $index;

			$carry['template'][] = $block;

			return $carry;
		},
		array(
			'template'    => array(),
			'first_block' => array(
				'index' => '',
			),
			'last_block'  => array(
				'index' => '',
			),
			'index'       => 0,
		)
	);

	return array( $template_with_custom_attributes['template'] );
}