PluginProbe
Elementor Website Builder – more than just a page builder / 3.18.2
Elementor Website Builder – more than just a page builder v3.18.2
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.18.2, at includes/widgets/rating.php

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