Constants
19 hours ago
Accordion.php
19 hours ago
Alert.php
19 hours ago
AttachmentCard.php
19 hours ago
Avatar.php
19 hours ago
Badge.php
19 hours ago
BaseComponent.php
19 hours ago
Button.php
19 hours ago
ConfirmationModal.php
19 hours ago
CourseFilter.php
19 hours ago
DateFilter.php
19 hours ago
DropdownFilter.php
19 hours ago
EmptyState.php
19 hours ago
FileUploader.php
19 hours ago
InputField.php
19 hours ago
Modal.php
19 hours ago
Nav.php
19 hours ago
Pagination.php
19 hours ago
Popover.php
19 hours ago
PreviewTrigger.php
19 hours ago
Progress.php
19 hours ago
SearchFilter.php
19 hours ago
Sorting.php
19 hours ago
StarRating.php
19 hours ago
StarRatingInput.php
19 hours ago
StatusSelect.php
19 hours ago
SvgIcon.php
19 hours ago
Table.php
19 hours ago
Tabs.php
19 hours ago
Tooltip.php
19 hours ago
WPEditor.php
19 hours ago
StarRating.php
185 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 | * ``` |
| 24 | * StarRating::make() |
| 25 | * ->rating(4.5) |
| 26 | * ->show_average(true) |
| 27 | * ->render(); |
| 28 | * ``` |
| 29 | * |
| 30 | * @since 4.0.0 |
| 31 | */ |
| 32 | class StarRating extends BaseComponent { |
| 33 | |
| 34 | /** |
| 35 | * Rating value |
| 36 | * |
| 37 | * @since 4.0.0 |
| 38 | * |
| 39 | * @var float |
| 40 | */ |
| 41 | protected $rating = 0; |
| 42 | |
| 43 | /** |
| 44 | * Whether to show numerical rating |
| 45 | * |
| 46 | * @since 4.0.0 |
| 47 | * |
| 48 | * @var bool |
| 49 | */ |
| 50 | protected $show_average = false; |
| 51 | |
| 52 | /** |
| 53 | * Star icon size |
| 54 | * |
| 55 | * @since 4.0.0 |
| 56 | * |
| 57 | * @var int |
| 58 | */ |
| 59 | protected $icon_size = 16; |
| 60 | |
| 61 | /** |
| 62 | * Review count |
| 63 | * |
| 64 | * @since 4.0.0 |
| 65 | * |
| 66 | * @var int|null |
| 67 | */ |
| 68 | protected $count = null; |
| 69 | |
| 70 | /** |
| 71 | * Set rating value |
| 72 | * |
| 73 | * @since 4.0.0 |
| 74 | * |
| 75 | * @param float $rating rating. |
| 76 | * |
| 77 | * @return self |
| 78 | */ |
| 79 | public function rating( float $rating ): self { |
| 80 | $this->rating = $rating; |
| 81 | return $this; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Set whether to show average rating text |
| 86 | * |
| 87 | * @since 4.0.0 |
| 88 | * |
| 89 | * @param bool $show show average. |
| 90 | * |
| 91 | * @return self |
| 92 | */ |
| 93 | public function show_average( bool $show = true ): self { |
| 94 | $this->show_average = $show; |
| 95 | return $this; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Set icon size |
| 100 | * |
| 101 | * @since 4.0.0 |
| 102 | * |
| 103 | * @param int $size size. |
| 104 | * |
| 105 | * @return self |
| 106 | */ |
| 107 | public function icon_size( int $size ): self { |
| 108 | $this->icon_size = $size; |
| 109 | return $this; |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Set review count |
| 114 | * |
| 115 | * @since 4.0.0 |
| 116 | * |
| 117 | * @param int $count count. |
| 118 | * |
| 119 | * @return self |
| 120 | */ |
| 121 | public function count( int $count ): self { |
| 122 | $this->count = $count; |
| 123 | return $this; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Get component content |
| 128 | * |
| 129 | * @since 4.0.0 |
| 130 | * |
| 131 | * @return string |
| 132 | */ |
| 133 | public function get(): string { |
| 134 | $rating = $this->rating; |
| 135 | $icon_size = $this->icon_size; |
| 136 | |
| 137 | $star_fill = SvgIcon::make()->name( Icon::STAR_FILL )->size( $icon_size )->ignore_kids()->get(); |
| 138 | $star_half = SvgIcon::make()->name( Icon::STAR_HALF )->size( $icon_size )->ignore_kids()->get(); |
| 139 | $star = SvgIcon::make()->name( Icon::STAR_LINE )->size( $icon_size )->ignore_kids()->get(); |
| 140 | |
| 141 | ob_start(); |
| 142 | ?> |
| 143 | <div class="tutor-ratings-stars tutor-flex tutor-items-center tutor-gap-1" data-rating-value="<?php echo esc_attr( $rating ); ?>"> |
| 144 | <div class="tutor-flex tutor-items-center tutor-gap-1"> |
| 145 | <?php for ( $i = 1; $i <= 5; $i++ ) : ?> |
| 146 | <span class="tutor-icon-exception4 tutor-flex-center"> |
| 147 | <?php |
| 148 | if ( (int) $rating >= $i ) { |
| 149 | echo $star_fill; // phpcs:ignore -- get_svg_icon returns escaped html |
| 150 | } elseif ( ( $rating - $i ) >= -0.5 ) { |
| 151 | echo $star_half; // phpcs:ignore -- get_svg_icon returns escaped html |
| 152 | } else { |
| 153 | echo $star; // phpcs:ignore -- get_svg_icon returns escaped html |
| 154 | } |
| 155 | ?> |
| 156 | </span> |
| 157 | <?php endfor; ?> |
| 158 | </div> |
| 159 | |
| 160 | <?php if ( $this->show_average || $this->count ) : ?> |
| 161 | <div class="tutor-ratings-meta tutor-flex tutor-items-center tutor-gap-1 tutor-ml-1"> |
| 162 | <?php if ( $this->show_average ) : ?> |
| 163 | <span class="tutor-small tutor-font-medium tutor-text-primary"> |
| 164 | <?php echo esc_html( number_format( $rating, 1 ) ); ?> |
| 165 | </span> |
| 166 | <?php endif; ?> |
| 167 | |
| 168 | <?php if ( $this->count ) : ?> |
| 169 | <span class="tutor-small tutor-text-subdued"> |
| 170 | <?php |
| 171 | /* translators: %s: review count */ |
| 172 | echo esc_html( sprintf( _n( '(%s Review)', '(%s Reviews)', $this->count, 'tutor' ), number_format_i18n( $this->count ) ) ); |
| 173 | ?> |
| 174 | </span> |
| 175 | <?php endif; ?> |
| 176 | </div> |
| 177 | <?php endif; ?> |
| 178 | </div> |
| 179 | <?php |
| 180 | |
| 181 | $this->component_string = ob_get_clean(); |
| 182 | return $this->component_string; |
| 183 | } |
| 184 | } |
| 185 |