Constants
3 weeks ago
Accordion.php
6 days ago
Alert.php
6 days ago
AttachmentCard.php
6 days ago
Avatar.php
6 days ago
Badge.php
6 days ago
BaseComponent.php
3 weeks ago
Button.php
6 days ago
ConfirmationModal.php
6 days ago
CourseFilter.php
6 days ago
DateFilter.php
6 days ago
DropdownFilter.php
6 days ago
EmptyState.php
6 days ago
FileUploader.php
6 days ago
InputField.php
6 days ago
Modal.php
6 days ago
Nav.php
6 days ago
Pagination.php
6 days ago
Popover.php
6 days ago
PreviewTrigger.php
6 days ago
Progress.php
6 days ago
SearchFilter.php
6 days ago
Sorting.php
6 days ago
StarRating.php
6 days ago
StarRatingInput.php
6 days ago
StatusSelect.php
6 days ago
SvgIcon.php
6 days ago
Table.php
6 days ago
Tabs.php
6 days ago
Tooltip.php
6 days ago
WPEditor.php
6 days ago
StarRating.php
221 lines
| 1 | <?php |
| 2 | /** |
| 3 | * StarRating Component Class. |
| 4 | * |
| 5 | * Renders a static star rating display. |
| 6 | * |
| 7 | * @package Tutor\Components |
| 8 | * @author Themeum |
| 9 | * @link https://themeum.com |
| 10 | * @since 4.0.0 |
| 11 | */ |
| 12 | |
| 13 | namespace Tutor\Components; |
| 14 | |
| 15 | defined( 'ABSPATH' ) || exit; |
| 16 | |
| 17 | use TUTOR\Icon; |
| 18 | |
| 19 | /** |
| 20 | * Class StarRating |
| 21 | * |
| 22 | * Example Usage: |
| 23 | * ```php |
| 24 | * // Display stars only (no numbers) |
| 25 | * StarRating::make() |
| 26 | * ->rating( 4.5 ) |
| 27 | * ->render(); |
| 28 | * |
| 29 | * // Stars with average numeric rating shown |
| 30 | * StarRating::make() |
| 31 | * ->rating( 4.5 ) |
| 32 | * ->show_average( true ) |
| 33 | * ->render(); |
| 34 | * |
| 35 | * // Stars with review count |
| 36 | * StarRating::make() |
| 37 | * ->rating( 3.8 ) |
| 38 | * ->count( 120 ) |
| 39 | * ->render(); |
| 40 | * |
| 41 | * // Stars with average and review count |
| 42 | * StarRating::make() |
| 43 | * ->rating( 4.2 ) |
| 44 | * ->show_average( true ) |
| 45 | * ->count( 58 ) |
| 46 | * ->render(); |
| 47 | * |
| 48 | * // Larger icon size |
| 49 | * StarRating::make() |
| 50 | * ->rating( 5.0 ) |
| 51 | * ->icon_size( 24 ) |
| 52 | * ->show_average( true ) |
| 53 | * ->render(); |
| 54 | * |
| 55 | * // Zero rating (no stars filled) |
| 56 | * StarRating::make() |
| 57 | * ->rating( 0 ) |
| 58 | * ->show_average( true ) |
| 59 | * ->count( 0 ) |
| 60 | * ->render(); |
| 61 | * |
| 62 | * // Retrieve HTML without echoing |
| 63 | * $html = StarRating::make()->rating( 4.5 )->show_average( true )->count( 30 )->get(); |
| 64 | * ``` |
| 65 | * |
| 66 | * @since 4.0.0 |
| 67 | */ |
| 68 | class StarRating extends BaseComponent { |
| 69 | |
| 70 | /** |
| 71 | * Rating value |
| 72 | * |
| 73 | * @since 4.0.0 |
| 74 | * |
| 75 | * @var float |
| 76 | */ |
| 77 | protected $rating = 0; |
| 78 | |
| 79 | /** |
| 80 | * Whether to show numerical rating |
| 81 | * |
| 82 | * @since 4.0.0 |
| 83 | * |
| 84 | * @var bool |
| 85 | */ |
| 86 | protected $show_average = false; |
| 87 | |
| 88 | /** |
| 89 | * Star icon size |
| 90 | * |
| 91 | * @since 4.0.0 |
| 92 | * |
| 93 | * @var int |
| 94 | */ |
| 95 | protected $icon_size = 16; |
| 96 | |
| 97 | /** |
| 98 | * Review count |
| 99 | * |
| 100 | * @since 4.0.0 |
| 101 | * |
| 102 | * @var int|null |
| 103 | */ |
| 104 | protected $count = null; |
| 105 | |
| 106 | /** |
| 107 | * Set rating value |
| 108 | * |
| 109 | * @since 4.0.0 |
| 110 | * |
| 111 | * @param float $rating rating. |
| 112 | * |
| 113 | * @return self |
| 114 | */ |
| 115 | public function rating( float $rating ): self { |
| 116 | $this->rating = $rating; |
| 117 | return $this; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Set whether to show average rating text |
| 122 | * |
| 123 | * @since 4.0.0 |
| 124 | * |
| 125 | * @param bool $show show average. |
| 126 | * |
| 127 | * @return self |
| 128 | */ |
| 129 | public function show_average( bool $show = true ): self { |
| 130 | $this->show_average = $show; |
| 131 | return $this; |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Set icon size |
| 136 | * |
| 137 | * @since 4.0.0 |
| 138 | * |
| 139 | * @param int $size size. |
| 140 | * |
| 141 | * @return self |
| 142 | */ |
| 143 | public function icon_size( int $size ): self { |
| 144 | $this->icon_size = $size; |
| 145 | return $this; |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * Set review count |
| 150 | * |
| 151 | * @since 4.0.0 |
| 152 | * |
| 153 | * @param int $count count. |
| 154 | * |
| 155 | * @return self |
| 156 | */ |
| 157 | public function count( int $count ): self { |
| 158 | $this->count = $count; |
| 159 | return $this; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get component content |
| 164 | * |
| 165 | * @since 4.0.0 |
| 166 | * |
| 167 | * @return string |
| 168 | */ |
| 169 | public function get(): string { |
| 170 | $rating = $this->rating; |
| 171 | $icon_size = $this->icon_size; |
| 172 | |
| 173 | $star_fill = SvgIcon::make()->name( Icon::STAR_FILL )->size( $icon_size )->ignore_kids()->get(); |
| 174 | $star_half = SvgIcon::make()->name( Icon::STAR_HALF )->size( $icon_size )->ignore_kids()->get(); |
| 175 | $star = SvgIcon::make()->name( Icon::STAR_LINE )->size( $icon_size )->ignore_kids()->get(); |
| 176 | |
| 177 | ob_start(); |
| 178 | ?> |
| 179 | <div class="tutor-ratings-stars tutor-flex tutor-items-center tutor-gap-1" data-rating-value="<?php echo esc_attr( $rating ); ?>"> |
| 180 | <div class="tutor-flex tutor-items-center tutor-gap-1"> |
| 181 | <?php for ( $i = 1; $i <= 5; $i++ ) : ?> |
| 182 | <span class="tutor-icon-exception4 tutor-flex-center"> |
| 183 | <?php |
| 184 | if ( (int) $rating >= $i ) { |
| 185 | echo $star_fill; // phpcs:ignore -- get_svg_icon returns escaped html |
| 186 | } elseif ( ( $rating - $i ) >= -0.5 ) { |
| 187 | echo $star_half; // phpcs:ignore -- get_svg_icon returns escaped html |
| 188 | } else { |
| 189 | echo $star; // phpcs:ignore -- get_svg_icon returns escaped html |
| 190 | } |
| 191 | ?> |
| 192 | </span> |
| 193 | <?php endfor; ?> |
| 194 | </div> |
| 195 | |
| 196 | <?php if ( $this->show_average || $this->count ) : ?> |
| 197 | <div class="tutor-ratings-meta tutor-flex tutor-items-center tutor-gap-1 tutor-ml-1"> |
| 198 | <?php if ( $this->show_average ) : ?> |
| 199 | <span class="tutor-small tutor-font-medium tutor-text-primary"> |
| 200 | <?php echo esc_html( number_format( $rating, 1 ) ); ?> |
| 201 | </span> |
| 202 | <?php endif; ?> |
| 203 | |
| 204 | <?php if ( $this->count ) : ?> |
| 205 | <span class="tutor-small tutor-text-subdued"> |
| 206 | <?php |
| 207 | /* translators: %s: review count */ |
| 208 | echo esc_html( sprintf( _n( '(%s Review)', '(%s Reviews)', $this->count, 'tutor' ), number_format_i18n( $this->count ) ) ); |
| 209 | ?> |
| 210 | </span> |
| 211 | <?php endif; ?> |
| 212 | </div> |
| 213 | <?php endif; ?> |
| 214 | </div> |
| 215 | <?php |
| 216 | |
| 217 | $this->component_string = ob_get_clean(); |
| 218 | return $this->component_string; |
| 219 | } |
| 220 | } |
| 221 |