PluginProbe
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder / 51.1.86
King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder v51.1.86
51.1.86 51.1.84 51.1.85 51.1.83 51.1.82 51.1.81 51.1.79 51.1.78 51.1.77 51.1.76 51.1.74 51.1.75 51.1.65 51.1.64 51.1.63 trunk 51.1.14 51.1.2 51.1.35 51.1.36 51.1.37 51.1.38 51.1.39 51.1.44 51.1.45 All 40 releases
king-addons / includes / widgets / Woo_Product_Rating / Woo_Product_Rating.php

Woo_Product_Rating.php in King Addons for Elementor – 100+ Elementor Widgets, 4 000+ Elementor Templates, WooCommerce Builder, Mega Menu, Popup Builder 51.1.86, at includes/widgets/Woo_Product_Rating/Woo_Product_Rating.php

291 lines 9.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Woo Product Rating widget.
4 *
5 * @package King_Addons
6 */
7
8 namespace King_Addons;
9
10 use Elementor\Controls_Manager;
11 use Elementor\Group_Control_Typography;
12
13 if (!defined('ABSPATH')) {
14 exit;
15 }
16
17 /**
18 * Displays product rating and count.
19 */
20 class Woo_Product_Rating extends Abstract_Single_Widget
21 {
22 public function get_name(): string
23 {
24 return 'woo_product_rating';
25 }
26
27 public function get_title(): string
28 {
29 return esc_html__('Product Rating', 'king-addons');
30 }
31
32 public function get_icon(): string
33 {
34 return 'king-addons-icon king-addons-woo-product-rating';
35 }
36
37 public function get_categories(): array
38 {
39 return ['king-addons-woo-builder'];
40 }
41
42 public function get_style_depends(): array
43 {
44 return [KING_ADDONS_ASSETS_UNIQUE_KEY . '-woo-product-rating-style'];
45 }
46
47 protected function register_controls(): void
48 {
49 $this->start_controls_section(
50 'section_content',
51 [
52 'label' => esc_html__('Content', 'king-addons'),
53 'tab' => Controls_Manager::TAB_CONTENT,
54 ]
55 );
56
57 $this->add_control(
58 'layout',
59 [
60 'label' => esc_html__('Layout', 'king-addons'),
61 'type' => Controls_Manager::SELECT,
62 'options' => [
63 'stars' => esc_html__('Stars', 'king-addons'),
64 'stars_count' => esc_html__('Stars + count', 'king-addons'),
65 'text' => sprintf(__('Text only %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
66 ],
67 'default' => 'stars_count',
68 ]
69 );
70
71 $this->add_control(
72 'count_text',
73 [
74 'label' => esc_html__('Count text', 'king-addons'),
75 'type' => Controls_Manager::TEXT,
76 'dynamic' => ['active' => true],
77 'default' => '({count} reviews)',
78 ]
79 );
80
81 $this->add_control(
82 'text_template',
83 [
84 'label' => sprintf(__('Text template %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
85 'type' => Controls_Manager::TEXT,
86 'default' => '{rating} / {max} ({count})',
87 'description' => esc_html__('Placeholders: {rating}, {max}, {count}', 'king-addons'),
88 'condition' => [
89 'layout' => 'text',
90 ],
91 ]
92 );
93
94 $this->add_control(
95 'link_to_reviews',
96 [
97 'label' => sprintf(__('Link to reviews anchor %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'),
98 'type' => Controls_Manager::SWITCHER,
99 'return_value' => 'yes',
100 ]
101 );
102
103 $this->add_control(
104 'reviews_anchor',
105 [
106 'label' => esc_html__('Reviews anchor (Pro)', 'king-addons'),
107 'type' => Controls_Manager::TEXT,
108 'default' => '#reviews',
109 ]
110 );
111
112 $this->end_controls_section();
113
114 $this->start_controls_section(
115 'section_style',
116 [
117 'label' => esc_html__('Style', 'king-addons'),
118 'tab' => Controls_Manager::TAB_STYLE,
119 ]
120 );
121
122 $this->add_control(
123 'star_size',
124 [
125 'label' => esc_html__('Star Size', 'king-addons'),
126 'type' => Controls_Manager::SLIDER,
127 'range' => [
128 'px' => ['min' => 10, 'max' => 48],
129 ],
130 'selectors' => [
131 '{{WRAPPER}} .ka-woo-product-rating .star-rating' => 'font-size: {{SIZE}}{{UNIT}};',
132 ],
133 ]
134 );
135
136 $this->add_control(
137 'star_color',
138 [
139 'label' => esc_html__('Filled Color', 'king-addons'),
140 'type' => Controls_Manager::COLOR,
141 'selectors' => [
142 '{{WRAPPER}} .ka-woo-product-rating .star-rating span:before' => 'color: {{VALUE}};',
143 ],
144 ]
145 );
146
147 $this->add_control(
148 'star_empty_color',
149 [
150 'label' => esc_html__('Empty Color', 'king-addons'),
151 'type' => Controls_Manager::COLOR,
152 'selectors' => [
153 '{{WRAPPER}} .ka-woo-product-rating .star-rating:before' => 'color: {{VALUE}};',
154 ],
155 ]
156 );
157
158 $this->add_group_control(
159 Group_Control_Typography::get_type(),
160 [
161 'name' => 'count_typography',
162 'selector' => '{{WRAPPER}} .ka-woo-product-rating__count',
163 'label' => esc_html__('Count Typography', 'king-addons'),
164 ]
165 );
166
167 $this->add_control(
168 'count_color',
169 [
170 'label' => esc_html__('Count Color', 'king-addons'),
171 'type' => Controls_Manager::COLOR,
172 'selectors' => [
173 '{{WRAPPER}} .ka-woo-product-rating__count' => 'color: {{VALUE}};',
174 ],
175 ]
176 );
177
178 $this->add_responsive_control(
179 'gap',
180 [
181 'label' => esc_html__('Gap', 'king-addons'),
182 'type' => Controls_Manager::SLIDER,
183 'range' => [
184 'px' => ['min' => 0, 'max' => 30],
185 ],
186 'selectors' => [
187 '{{WRAPPER}} .ka-woo-product-rating' => 'gap: {{SIZE}}{{UNIT}};',
188 ],
189 ]
190 );
191
192 $this->add_responsive_control(
193 'margin',
194 [
195 'label' => esc_html__('Margin', 'king-addons'),
196 'type' => Controls_Manager::DIMENSIONS,
197 'size_units' => ['px', 'em', '%'],
198 'selectors' => [
199 '{{WRAPPER}} .ka-woo-product-rating' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
200 ],
201 ]
202 );
203
204 $this->end_controls_section();
205 }
206
207 protected function render(): void
208 {
209 $product = $this->get_product();
210 if (!$product) {
211 $this->render_missing_product_notice();
212 return;
213 }
214
215 if (!wc_review_ratings_enabled()) {
216 return;
217 }
218
219 $settings = $this->get_settings_for_display();
220 $layout = $settings['layout'] ?? 'stars_count';
221 $can_pro = king_addons_can_use_pro();
222
223 if ('text' === $layout && !$can_pro) {
224 $layout = 'stars_count';
225 }
226
227 $rating = (float) $product->get_average_rating();
228 $count = (int) $product->get_rating_count();
229 $reviews = (int) $product->get_review_count();
230
231 // Woo can report reviews without a rating_count (comments that never
232 // stored a star value). Hiding the widget then looks like a fault
233 // next to a Reviews tab that already shows those comments.
234 if ($count === 0) {
235 $count = $reviews;
236 }
237
238 if ($count === 0 && $rating <= 0) {
239 // No reviews yet is a normal state, but a blank widget in the
240 // editor reads as a fault.
241 if (\Elementor\Plugin::$instance->editor->is_edit_mode()) {
242 echo '<div class="king-addons-woo-builder-notice">'
243 . esc_html__('The rating appears once the product has reviews.', 'king-addons')
244 . '</div>';
245 }
246 return;
247 }
248
249 $this->add_render_attribute('wrapper', 'class', 'ka-woo-product-rating');
250
251 $link_open = '';
252 $link_close = '';
253 if (!empty($settings['link_to_reviews']) && $can_pro) {
254 $anchor = $settings['reviews_anchor'] ?: '#reviews';
255 $link_open = '<a class="ka-woo-product-rating__link" href="' . esc_url($anchor) . '">';
256 $link_close = '</a>';
257 }
258
259 echo '<div ' . $this->get_render_attribute_string('wrapper') . '>';
260
261 if ('text' === $layout) {
262 $tpl = $settings['text_template'] ?? '{rating} / {max} ({count})';
263 $text = str_replace(
264 ['{rating}', '{max}', '{count}'],
265 [number_format($rating, 1), '5', (string) $count],
266 $tpl
267 );
268 echo $link_open . '<span class="ka-woo-product-rating__text">' . esc_html($text) . '</span>' . $link_close;
269 } else {
270 // stars
271 $html = wc_get_rating_html($rating, $count);
272 echo $link_open . $html . $link_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
273
274 if ('stars_count' === $layout) {
275 $count_text = $settings['count_text'] ?: '({count} reviews)';
276 $count_text = str_replace('{count}', (string) $count, $count_text);
277 echo '<span class="ka-woo-product-rating__count">' . esc_html($count_text) . '</span>';
278 }
279 }
280
281 echo '</div>';
282 }
283 }
284
285
286
287
288
289
290
291