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

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