Automattic\WooCommerce\Blocks\BlockTypes

ProductFilterRating::render()protectedWC 1.0

Include and render the block.

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

Хуков нет.

Возвращает

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

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

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

Код ProductFilterRating::render() WC 9.7.1

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

	$rating_counts   = $this->get_rating_counts( $block );
	$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
	);

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

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