Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::renderprotectedWC 1.0

Render the block.

Method of the class: ProductFilterAttribute{}

No Hooks.

Returns

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 10.9.4

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'] );

	if ( ! $product_attribute ) {
		return '';
	}

	$attribute_counts = $this->get_attribute_counts( $block, $product_attribute->slug, $block_attributes['queryType'] );
	$hide_empty       = $block_attributes['hideEmpty'] ?? true;
	$orderby          = $block_attributes['sortOrder'] ? explode( '-', $block_attributes['sortOrder'] )[0] : 'name';
	$order            = $block_attributes['sortOrder'] ? strtoupper( explode( '-', $block_attributes['sortOrder'] )[1] ) : 'DESC';

	$args = array(
		'taxonomy' => $product_attribute->slug,
		'orderby'  => $orderby,
		'order'    => $order,
	);

	if ( $hide_empty ) {
		$args['include'] = array_keys( $attribute_counts );
	} else {
		$args['hide_empty'] = false;
	}

	$attribute_terms = get_terms( $args );

	if ( is_wp_error( $attribute_terms ) ) {
		$attribute_terms = array();
	}

	$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 ] ) && is_string( $filter_params[ $filter_param_key ] ) ) {
		$selected_terms = array_filter( explode( ',', $filter_params[ $filter_param_key ] ) );
	}

	$filter_context = array(
		'items'          => array(),
		'selectionMode'  => $block_attributes['selectType'] ?? 'multiple',
		'storeNamespace' => 'woocommerce/product-filters',
		'groupLabel'     => $product_attribute->name,
	);

	if ( ! empty( $attribute_counts ) ) {
		$show_counts         = $block_attributes['showCounts'] ?? false;
		$is_visual_attribute = VisualAttributeTermMeta::is_visual_attribute_taxonomy( $product_attribute->slug );
		$visual_values       = array();

		if ( $is_visual_attribute ) {
			$visual_values = VisualAttributeTermMeta::get_term_visuals( wp_list_pluck( $attribute_terms, 'term_id' ) );
		}

		$attribute_options = array_map(
			function ( $term ) use ( $block_attributes, $attribute_counts, $selected_terms, $product_attribute, $show_counts, $is_visual_attribute, $visual_values ) {
				$term          = (array) $term;
				$term['count'] = $attribute_counts[ $term['term_id'] ] ?? 0;

				$type = 'attribute/' . str_replace( 'pa_', '', $product_attribute->slug );
				$item = array(
					'id'                 => $type . '-' . $term['slug'],
					'label'              => $term['name'],
					'ariaLabel'          => $term['name'],
					'value'              => $term['slug'],
					'selected'           => in_array( $term['slug'], $selected_terms, true ),
					'type'               => $type,
					'attributeQueryType' => $block_attributes['queryType'],
				);

				if ( $show_counts ) {
					$item['count'] = $term['count'];
				}

				if ( $is_visual_attribute ) {
					$item['visual'] = $visual_values[ $term['term_id'] ] ?? VisualAttributeTermMeta::get_empty_visual();
				}

				return $item;
			},
			$attribute_terms
		);

		$filter_context['items'] = array_values( $attribute_options );
	}//end if

	$wrapper_attributes = array(
		'data-wp-interactive' => 'woocommerce/product-filters',
		'data-wp-key'         => wp_unique_prefixed_id( $this->get_full_block_name() ),
		'data-wp-context'     => wp_json_encode(
			array(
				'activeLabelTemplate' => "$product_attribute->name: {{label}}",
				'filterType'          => 'attribute/' . str_replace( 'pa_', '', $product_attribute->slug ),
				'items'               => $filter_context['items'],
			),
			JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
		),
	);

	if ( empty( $filter_context['items'] ) ) {
		$wrapper_attributes['hidden'] = true;
		$wrapper_attributes['class']  = 'wc-block-product-filter--hidden';
	}

	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( 'woocommerce/selectableItems' => $filter_context ) ) )->render();
				return $carry;
			},
			''
		)
	);
}