Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterAttribute::renderprotectedWC 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 10.3.6

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

	$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(
		'showCounts' => $block_attributes['showCounts'] ?? false,
		'items'      => array(),
		'groupLabel' => $product_attribute->name,
	);

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

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

		$filter_context['items'] = $attribute_options;
	}

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