Automattic\WooCommerce\Blocks\BlockTypes
ProductFilterRating::render
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() ProductFilterRating::render code WC 10.9.4
protected function render( $attributes, $content, $block ) {
// don't render if its admin, or ajax in progress.
if ( is_admin() || wp_doing_ajax() ) {
return '';
}
$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 & $rating['rating'] < 6;
}
);
$filter_params = $block->context['filterParams'] ?? array();
$rating_query = $filter_params[ self::RATING_FILTER_QUERY_VAR ] ?? '';
$selected_rating = array_filter( array_map( 'absint', explode( ',', $rating_query ) ) );
$show_counts = $attributes['showCounts'] ?? false;
$filter_options = array_map(
function ( $rating ) use ( $selected_rating, $show_counts ) {
$rating_value = (int) $rating['rating'];
$aria_label = sprintf(
/* translators: %1$d is referring to rating value. Example: Rated 4 out of 5. */
__( 'Rated %1$d out of 5', 'woocommerce' ),
$rating_value,
);
$item = array(
'id' => 'rating-' . $rating_value,
'label' => '',
'ariaLabel' => $aria_label,
'value' => (string) $rating_value,
'selected' => in_array( $rating_value, $selected_rating, true ),
'type' => 'rating',
);
if ( $show_counts ) {
$item['count'] = $rating['count'];
}
return $item;
},
$rating_counts_with_min
);
$filter_context = array(
'items' => array_values( $filter_options ),
'selectionMode' => 'multiple',
'storeNamespace' => 'woocommerce/product-filters',
'groupLabel' => __( 'Rating', 'woocommerce' ),
'filterType' => 'rating',
);
$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(
/* translators: {{label}} is the rating filter item label. */
'activeLabelTemplate' => __( 'Rating: {{label}}', 'woocommerce' ),
'filterType' => 'rating',
'items' => array_values( $filter_options ),
),
JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP
),
);
if ( empty( $filter_options ) ) {
$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;
},
''
)
);
}