Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::render()protectedWC 1.0

Render the block.

Method of the class: ProductFilterAttribute{}

No Hooks.

Return

String. Rendered block type output.

Usage

// protected - for code of main (parent) or child class
$result = $this->render( $block_attributes, $content, $block );
$block_attributes(array) (required)
Block attributes.
$content(string) (required)
Block content.
$block(WP_Block) (required)
Block instance.

ProductFilterAttribute::render() code WC 9.7.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;
				return array(
					'label'     => $block_attributes['showCounts'] ? sprintf( '%1$s (%2$d)', $term['name'], $term['count'] ) : $term['name'],
					'ariaLabel' => $block_attributes['showCounts'] ? sprintf( '%1$s (%2$d)', $term['name'], $term['count'] ) : $term['name'],
					'value'     => $term['slug'],
					'selected'  => in_array( $term['slug'], $selected_terms, true ),
					'type'      => 'attribute',
					'data'      => $term,
				);
			},
			$attribute_terms
		);

		$filter_context = array(
			'items'  => $attribute_options,
			'parent' => $this->get_full_block_name(),
		);
	}

	$context = array(
		'attributeSlug'       => str_replace( 'pa_', '', $product_attribute->slug ),
		'queryType'           => $block_attributes['queryType'],
		'selectType'          => 'multiple',
		'hasFilterOptions'    => ! empty( $filter_context ),
		/* translators: {{label}} is the product attribute filter item label. */
		'activeLabelTemplate' => "{$product_attribute->name}: {{label}}",
	);

	$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',
	);

	if ( empty( $filter_context ) ) {
		$wrapper_attributes['hidden'] = true;
	}

	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;
			},
			''
		)
	);
}