Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterRating::render()protectedWC 1.0

Include and render the block.

Method of the class: ProductFilterRating{}

No Hooks.

Returns

String. Rendered block type output.

Usage

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

ProductFilterRating::render() code WC 9.8.5

protected function render( $attributes, $content, $block ) {
	// don't render if its admin, or ajax in progress.
	if ( is_admin() || wp_doing_ajax() ) {
		return '';
	}

	wp_enqueue_script_module( $this->get_full_block_name() );

	$min_rating    = $attributes['minRating'] ?? 0;
	$rating_counts = $this->get_rating_counts( $block );
	// User selected minimum rating to display.
	$rating_counts_with_min = array_filter(
		$rating_counts,
		function ( $rating ) use ( $min_rating ) {
			return $rating['rating'] >= $min_rating;
		}
	);
	$filter_params          = $block->context['filterParams'] ?? array();
	$rating_query           = $filter_params[ self::RATING_FILTER_QUERY_VAR ] ?? '';
	$selected_rating        = array_filter( explode( ',', $rating_query ) );

	$filter_options = array_map(
		function ( $rating ) use ( $selected_rating, $attributes ) {
			$value       = (string) $rating['rating'];
			$count_label = $attributes['showCounts'] ? "({$rating['count']})" : '';

			$aria_label = sprintf(
				/* translators: %s is referring to rating value. Example: Rated 4 out of 5. */
				__( 'Rated %s out of 5', 'woocommerce' ),
				$value,
			);

			return array(
				'id'        => 'rating-' . $value,
				'selected'  => in_array( $value, $selected_rating, true ),
				'label'     => $this->render_rating_label( (int) $value, $count_label ),
				'ariaLabel' => $aria_label,
				'value'     => $value,
				'type'      => 'rating',
				'data'      => $rating,
			);
		},
		$rating_counts_with_min
	);

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

	$wrapper_attributes = array(
		'data-wp-interactive'  => $this->get_full_block_name(),
		'data-wp-context'      => wp_json_encode(
			array(
				'hasFilterOptions'    => ! empty( $filter_options ),
				/* translators: {{labe}} is the rating filter item label. */
				'activeLabelTemplate' => __( 'Rating: {{label}}', 'woocommerce' ),
			),
			JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
		),
		'data-wp-bind--hidden' => '!context.hasFilterOptions',
	);

	if ( empty( $filter_options ) ) {
		$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;
			},
			''
		)
	);
}