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.2.3 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.3 3.6.4 3.7.0 3.7.1 3.7.2 3.7.3 3.7.4 3.8.0 3.8.1 3.8.2 3.8.3 3.9.0 3.9.1 3.9.10 3.9.2 3.9.3 3.9.4 3.9.5 3.9.6 3.9.7 3.9.8 3.9.9
tutor / components / StarRating.php
tutor / components Last commit date
Constants 1 month ago Accordion.php 1 month ago Alert.php 1 month ago AttachmentCard.php 1 month ago Avatar.php 1 month ago Badge.php 1 month ago BaseComponent.php 1 month ago Button.php 1 month ago ConfirmationModal.php 1 month ago CourseFilter.php 1 month ago DateFilter.php 1 month ago DropdownFilter.php 1 month ago EmptyState.php 1 month ago FileUploader.php 1 month ago InputField.php 1 month ago Modal.php 1 month ago Nav.php 1 month ago Pagination.php 1 month ago Popover.php 1 month ago PreviewTrigger.php 1 month ago Progress.php 1 month ago SearchFilter.php 1 month ago Sorting.php 1 month ago StarRating.php 1 month ago StarRatingInput.php 1 month ago StatusSelect.php 1 month ago SvgIcon.php 1 month ago Table.php 1 month ago Tabs.php 1 month ago Tooltip.php 1 month ago WPEditor.php 1 month 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