Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::render()protectedWC 1.0

Render the block.

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

Хуков нет.

Возвращает

Строку. Rendered block type output.

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

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

Код ProductFilterAttribute::render() WC 9.5.1

protected function render( $block_attributes, $content, $block ) {
	if ( empty( $block_attributes['attributeId'] ) ) {
		$default_product_attribute       = $this->get_default_product_attribute();
		$block_attributes['attributeId'] = $default_product_attribute->attribute_id;
	}

	// don't render if its admin, or ajax in progress.
	if ( is_admin() || wp_doing_ajax() || empty( $block_attributes['attributeId'] ) ) {
		return '';
	}

	$product_attribute = wc_get_attribute( $block_attributes['attributeId'] );
	$attribute_counts  = $this->get_attribute_counts( $block, $product_attribute->slug, $block_attributes['queryType'] );
	$hide_empty        = $block_attributes['hideEmpty'] ?? true;

	if ( $hide_empty ) {
		$attribute_terms = get_terms(
			array(
				'taxonomy' => $product_attribute->slug,
				'include'  => array_keys( $attribute_counts ),
			)
		);
	} else {
		$attribute_terms = get_terms(
			array(
				'taxonomy'   => $product_attribute->slug,
				'hide_empty' => false,
			)
		);
	}

	$filter_param_key = 'filter_' . str_replace( 'pa_', '', $product_attribute->slug );
	$filter_params    = $block->context['filterParams'] ?? array();
	$selected_terms   = array();

	if ( $filter_params && ! empty( $filter_params[ $filter_param_key ] ) ) {
		$selected_terms = array_filter( explode( ',', $filter_params[ $filter_param_key ] ) );
	}

	$filter_context = array();

	if ( ! empty( $attribute_counts ) ) {
		$attribute_options = array_map(
			function ( $term ) use ( $block_attributes, $attribute_counts, $selected_terms ) {
				$term             = (array) $term;
				$term['count']    = $attribute_counts[ $term['term_id'] ] ?? 0;
				$term['selected'] = in_array( $term['slug'], $selected_terms, true );
				return array(
					'label'    => $block_attributes['showCounts'] ? sprintf( '%1$s (%2$d)', $term['name'], $term['count'] ) : $term['name'],
					'value'    => $term['slug'],
					'selected' => $term['selected'],
					'rawData'  => $term,
				);
			},
			$attribute_terms
		);

		$filter_context = array(
			'items'   => $attribute_options,
			'actions' => array(
				'toggleFilter' => "{$this->get_full_block_name()}::actions.toggleFilter",
			),
		);
	}

	$context = array(
		'attributeSlug'      => str_replace( 'pa_', '', $product_attribute->slug ),
		'queryType'          => $block_attributes['queryType'],
		'selectType'         => 'multiple',
		'hasSelectedFilters' => count( $selected_terms ) > 0,
		'hasFilterOptions'   => ! empty( $filter_context ),
	);

	$wrapper_attributes = array(
		'data-wc-interactive'  => wp_json_encode( array( 'namespace' => $this->get_full_block_name() ), JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ),
		'data-wc-key'          => 'product-filter-attribute-' . md5( wp_json_encode( $block_attributes ) ),
		'data-wc-context'      => wp_json_encode( $context, JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP ),
		'data-wc-bind--hidden' => '!context.hasFilterOptions',
	);

	return sprintf(
		'<div %1$s>%2$s</div>',
		get_block_wrapper_attributes( $wrapper_attributes ),
		array_reduce(
			$block->parsed_block['innerBlocks'],
			function ( $carry, $parsed_block ) use ( $filter_context ) {
				$carry .= ( new \WP_Block( $parsed_block, array( 'filterData' => $filter_context ) ) )->render();
				return $carry;
			},
			''
		)
	);
}