PluginProbe
Elementor Website Builder – more than just a page builder / 3.25.8
Elementor Website Builder – more than just a page builder v3.25.8
4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 4.0.7 All 451 releases
elementor / includes / widgets / rating.php

rating.php in Elementor Website Builder – more than just a page builder 3.25.8, at includes/widgets/rating.php

320 lines 7.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Icons_Manager;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor (new) rating widget.
12 *
13 * @since 3.17.0
14 */
15 class Widget_Rating extends Widget_Base {
16
17 public function get_name() {
18 return 'rating';
19 }
20
21 public function get_title() {
22 return esc_html__( 'Rating', 'elementor' );
23 }
24
25 public function get_icon() {
26 return 'eicon-rating';
27 }
28
29 public function get_keywords() {
30 return [ 'star', 'rating', 'review', 'score', 'scale' ];
31 }
32
33 protected function is_dynamic_content(): bool {
34 return false;
35 }
36
37 public function get_style_depends(): array {
38 return [ 'widget-rating' ];
39 }
40
41 /**
42 * @return void
43 */
44 private function add_style_tab() {
45 $this->start_controls_section(
46 'section_icon_style',
47 [
48 'label' => esc_html__( 'Icon', 'elementor' ),
49 'tab' => Controls_Manager::TAB_STYLE,
50 ]
51 );
52
53 $this->add_responsive_control(
54 'icon_size',
55 [
56 'label' => esc_html__( 'Size', 'elementor' ),
57 'type' => Controls_Manager::SLIDER,
58 'size_units' => [ 'px', 'em', 'rem', 'vw', 'custom' ],
59 'range' => [
60 'px' => [
61 'max' => 100,
62 ],
63 'em' => [
64 'min' => 0,
65 'max' => 10,
66 ],
67 'rem' => [
68 'min' => 0,
69 'max' => 10,
70 ],
71 ],
72 'selectors' => [
73 '{{WRAPPER}}' => '--e-rating-icon-font-size: {{SIZE}}{{UNIT}}',
74 ],
75 ]
76 );
77
78 $this->add_responsive_control(
79 'icon_gap',
80 [
81 'label' => esc_html__( 'Spacing', 'elementor' ),
82 'type' => Controls_Manager::SLIDER,
83 'size_units' => [ 'px', 'em', 'rem', 'vw', 'custom' ],
84 'range' => [
85 'px' => [
86 'max' => 100,
87 ],
88 'em' => [
89 'min' => 0,
90 'max' => 10,
91 ],
92 'rem' => [
93 'min' => 0,
94 'max' => 10,
95 ],
96 ],
97 'selectors' => [
98 '{{WRAPPER}}' => '--e-rating-gap: {{SIZE}}{{UNIT}}',
99 ],
100 ]
101 );
102
103 $this->add_control(
104 'icon_color',
105 [
106 'label' => esc_html__( 'Color', 'elementor' ),
107 'type' => Controls_Manager::COLOR,
108 'selectors' => [
109 '{{WRAPPER}}' => '--e-rating-icon-marked-color: {{VALUE}}',
110 ],
111 'separator' => 'before',
112 ]
113 );
114
115 $this->add_control(
116 'icon_unmarked_color',
117 [
118 'label' => esc_html__( 'Unmarked Color', 'elementor' ),
119 'type' => Controls_Manager::COLOR,
120 'selectors' => [
121 '{{WRAPPER}}' => '--e-rating-icon-color: {{VALUE}}',
122 ],
123 ]
124 );
125
126 $this->end_controls_section();
127 }
128
129 protected function register_controls() {
130 $start_logical = is_rtl() ? 'end' : 'start';
131 $end_logical = is_rtl() ? 'start' : 'end';
132
133 $this->start_controls_section(
134 'section_rating',
135 [
136 'label' => esc_html__( 'Rating', 'elementor' ),
137 ]
138 );
139
140 $this->add_control(
141 'rating_scale',
142 [
143 'label' => esc_html__( 'Rating Scale', 'elementor' ),
144 'type' => Controls_Manager::SLIDER,
145 'range' => [
146 'px' => [
147 'min' => 1,
148 'max' => 10,
149 ],
150 ],
151 'default' => [
152 'size' => 5,
153 ],
154 ]
155 );
156
157 $this->add_control(
158 'rating_value',
159 [
160 'label' => esc_html__( 'Rating', 'elementor' ),
161 'type' => Controls_Manager::NUMBER,
162 'min' => 0,
163 'step' => 0.5,
164 'dynamic' => [
165 'active' => true,
166 ],
167 ]
168 );
169
170 $this->add_control(
171 'rating_icon',
172 [
173 'label' => esc_html__( 'Icon', 'elementor' ),
174 'type' => Controls_Manager::ICONS,
175 'fa4compatibility' => 'icon',
176 'skin' => 'inline',
177 'label_block' => false,
178 'skin_settings' => [
179 'inline' => [
180 'icon' => [
181 'icon' => 'eicon-star',
182 ],
183 ],
184 ],
185 'default' => [
186 'value' => 'eicon-star',
187 'library' => 'eicons',
188 ],
189 'separator' => 'before',
190 'exclude_inline_options' => [ 'none' ],
191 ]
192 );
193
194 $this->add_responsive_control( 'icon_alignment', [
195 'label' => esc_html__( 'Alignment', 'elementor' ),
196 'type' => Controls_Manager::CHOOSE,
197 'options' => [
198 'start' => [
199 'title' => esc_html__( 'Start', 'elementor' ),
200 'icon' => "eicon-align-$start_logical-h",
201 ],
202 'center' => [
203 'title' => esc_html__( 'Center', 'elementor' ),
204 'icon' => 'eicon-align-center-h',
205 ],
206 'end' => [
207 'title' => esc_html__( 'End', 'elementor' ),
208 'icon' => "eicon-align-$end_logical-h",
209 ],
210 ],
211 'selectors_dictionary' => [
212 'start' => '--e-rating-justify-content: flex-start;',
213 'center' => '--e-rating-justify-content: center;',
214 'end' => '--e-rating-justify-content: flex-end;',
215 ],
216 'selectors' => [
217 '{{WRAPPER}}' => '{{VALUE}}',
218 ],
219 'separator' => 'before',
220 ] );
221
222 $this->end_controls_section();
223
224 $this->add_style_tab();
225 }
226
227 protected function get_rating_value(): float {
228 $initial_value = $this->get_rating_scale();
229 $rating_value = $this->get_settings_for_display( 'rating_value' );
230
231 if ( '' === $rating_value ) {
232 $rating_value = $initial_value;
233 }
234
235 $rating_value = floatval( $rating_value );
236
237 return round( $rating_value, 2 );
238 }
239
240 protected function get_rating_scale(): int {
241 return intval( $this->get_settings_for_display( 'rating_scale' )['size'] );
242 }
243
244 protected function get_icon_marked_width( $icon_index ): string {
245 $rating_value = $this->get_rating_value();
246
247 $width = '0%';
248
249 if ( $rating_value >= $icon_index ) {
250 $width = '100%';
251 } elseif ( intval( ceil( $rating_value ) ) === $icon_index ) {
252 $width = ( $rating_value - ( $icon_index - 1 ) ) * 100 . '%';
253 }
254
255 return $width;
256 }
257
258 protected function get_icon_markup(): string {
259 $icon = $this->get_settings_for_display( 'rating_icon' );
260 $rating_scale = $this->get_rating_scale();
261
262 ob_start();
263
264 for ( $index = 1; $index <= $rating_scale; $index++ ) {
265 $this->add_render_attribute( 'icon_marked_' . $index, [
266 'class' => 'e-icon-wrapper e-icon-marked',
267 ] );
268
269 $icon_marked_width = $this->get_icon_marked_width( $index );
270
271 if ( '100%' !== $icon_marked_width ) {
272 $this->add_render_attribute( 'icon_marked_' . $index, [
273 'style' => '--e-rating-icon-marked-width: ' . $icon_marked_width . ';',
274 ] );
275 }
276 ?>
277 <div class="e-icon">
278 <div <?php $this->print_render_attribute_string( 'icon_marked_' . $index ); ?>>
279 <?php echo Icons_Manager::try_get_icon_html( $icon, [ 'aria-hidden' => 'true' ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
280 </div>
281 <div class="e-icon-wrapper e-icon-unmarked">
282 <?php echo Icons_Manager::try_get_icon_html( $icon, [ 'aria-hidden' => 'true' ] ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
283 </div>
284 </div>
285 <?php
286 }
287
288 return ob_get_clean();
289 }
290
291 protected function render() {
292 $this->add_render_attribute( 'widget', [
293 'class' => 'e-rating',
294 'itemtype' => 'https://schema.org/Rating',
295 'itemscope' => '',
296 'itemprop' => 'reviewRating',
297 ] );
298
299 $this->add_render_attribute( 'widget_wrapper', [
300 'class' => 'e-rating-wrapper',
301 'itemprop' => 'ratingValue',
302 'content' => $this->get_rating_value(),
303 'role' => 'img',
304 'aria-label' => sprintf( esc_html__( 'Rated %1$s out of %2$s', 'elementor' ),
305 $this->get_rating_value(),
306 $this->get_rating_scale()
307 ),
308 ] );
309 ?>
310 <div <?php $this->print_render_attribute_string( 'widget' ); ?>>
311 <meta itemprop="worstRating" content="0">
312 <meta itemprop="bestRating" content="<?php echo $this->get_rating_scale(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>">
313 <div <?php $this->print_render_attribute_string( 'widget_wrapper' ); ?>>
314 <?php echo $this->get_icon_markup(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>
315 </div>
316 </div>
317 <?php
318 }
319 }
320