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.6.2
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 ) ) );
$filter_options = array_map(
function ( $rating ) use ( $selected_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['rating'],
);
return array(
'label' => $this->render_rating_label( (int) $rating['rating'] ),
'ariaLabel' => $aria_label,
'value' => (string) $rating['rating'],
'selected' => in_array( $rating['rating'], $selected_rating, true ),
'count' => $rating['count'],
'type' => 'rating',
);
},
$rating_counts_with_min
);
$filter_context = array(
'items' => $filter_options,
'showCounts' => $attributes['showCounts'] ?? false,
'groupLabel' => __( 'Rating', 'woocommerce' ),
);
$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',
),
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( 'filterData' => $filter_context ) ) )->render();
return $carry;
},
''
)
);
}