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

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