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

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