Skip to content

Commit 82ed784

Browse files
Show a more accurate star rating. (#670)
Co-authored-by: Kelly Dwan <[email protected]>
1 parent 718e2ce commit 82ed784

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

mu-plugins/blocks/ratings-stars/render.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929
<?php else : ?>
3030
<div class="wporg-ratings-stars__icons">
3131
<?php
32-
33-
$display_rating = round( $data['rating'] / 10 ) * 0.5;
32+
$rounded_rating = round( $data['rating'] / 10 ) * 0.5;
3433
for ( $i = 0; $i < 5; $i++ ) {
35-
if ( $i + 1 <= $display_rating ) {
34+
if ( $i + 1 <= $rounded_rating ) {
3635
echo '<svg class="is-star-filled" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M11.776 4.454a.25.25 0 01.448 0l2.069 4.192a.25.25 0 00.188.137l4.626.672a.25.25 0 01.139.426l-3.348 3.263a.25.25 0 00-.072.222l.79 4.607a.25.25 0 01-.362.263l-4.138-2.175a.25.25 0 00-.232 0l-4.138 2.175a.25.25 0 01-.363-.263l.79-4.607a.25.25 0 00-.071-.222L4.754 9.881a.25.25 0 01.139-.426l4.626-.672a.25.25 0 00.188-.137l2.069-4.192z"></path></svg>';
37-
} else if ( $i + 0.5 === $display_rating ) {
36+
} else if ( $i + 0.5 <= $rounded_rating ) {
3837
echo '<svg class="is-star-half" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M9.518 8.783a.25.25 0 00.188-.137l2.069-4.192a.25.25 0 01.448 0l2.07 4.192a.25.25 0 00.187.137l4.626.672a.25.25 0 01.139.427l-3.347 3.262a.25.25 0 00-.072.222l.79 4.607a.25.25 0 01-.363.264l-4.137-2.176a.25.25 0 00-.233 0l-4.138 2.175a.25.25 0 01-.362-.263l.79-4.607a.25.25 0 00-.072-.222L4.753 9.882a.25.25 0 01.14-.427l4.625-.672zM12 14.533c.28 0 .559.067.814.2l1.895.997-.362-2.11a1.75 1.75 0 01.504-1.55l1.533-1.495-2.12-.308a1.75 1.75 0 01-1.317-.957L12 7.39v7.143z"></path></svg>';
3938
} else {
4039
echo '<svg class="is-star-empty" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path fill-rule="evenodd" d="M9.706 8.646a.25.25 0 01-.188.137l-4.626.672a.25.25 0 00-.139.427l3.348 3.262a.25.25 0 01.072.222l-.79 4.607a.25.25 0 00.362.264l4.138-2.176a.25.25 0 01.233 0l4.137 2.175a.25.25 0 00.363-.263l-.79-4.607a.25.25 0 01.072-.222l3.347-3.262a.25.25 0 00-.139-.427l-4.626-.672a.25.25 0 01-.188-.137l-2.069-4.192a.25.25 0 00-.448 0L9.706 8.646zM12 7.39l-.948 1.921a1.75 1.75 0 01-1.317.957l-2.12.308 1.534 1.495c.412.402.6.982.503 1.55l-.362 2.11 1.896-.997a1.75 1.75 0 011.629 0l1.895.997-.362-2.11a1.75 1.75 0 01.504-1.55l1.533-1.495-2.12-.308a1.75 1.75 0 01-1.317-.957L12 7.39z" clip-rule="evenodd"></path></svg>';
@@ -45,10 +44,15 @@
4544

4645
<div class="wporg-ratings-stars__label">
4746
<?php
47+
$display_rating = $data['rating'] / 20;
48+
49+
// Remove the trailing .0 if it's a whole number
50+
$display_rating_str = rtrim( number_format( $display_rating, 1 ), '.0' );
51+
4852
printf(
4953
// translators: %s is the current rating value.
5054
esc_html__( '%s out of 5 stars.', 'wporg' ),
51-
'<span>' . $display_rating . '</span>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
55+
'<span>' . esc_html( $display_rating_str ) . '</span>'
5256
);
5357
?>
5458
</div>

0 commit comments

Comments
 (0)