| 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 'eicon-star'; |
| 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 |
'default' => '({count} reviews)', |
| 77 |
] |
| 78 |
); |
| 79 |
|
| 80 |
$this->add_control( |
| 81 |
'text_template', |
| 82 |
[ |
| 83 |
'label' => sprintf(__('Text template %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 84 |
'type' => Controls_Manager::TEXT, |
| 85 |
'default' => '{rating} / {max} ({count})', |
| 86 |
'description' => esc_html__('Placeholders: {rating}, {max}, {count}', 'king-addons'), |
| 87 |
'condition' => [ |
| 88 |
'layout' => 'text', |
| 89 |
], |
| 90 |
] |
| 91 |
); |
| 92 |
|
| 93 |
$this->add_control( |
| 94 |
'link_to_reviews', |
| 95 |
[ |
| 96 |
'label' => sprintf(__('Link to reviews anchor %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 97 |
'type' => Controls_Manager::SWITCHER, |
| 98 |
'return_value' => 'yes', |
| 99 |
] |
| 100 |
); |
| 101 |
|
| 102 |
$this->add_control( |
| 103 |
'reviews_anchor', |
| 104 |
[ |
| 105 |
'label' => esc_html__('Reviews anchor (Pro)', 'king-addons'), |
| 106 |
'type' => Controls_Manager::TEXT, |
| 107 |
'default' => '#reviews', |
| 108 |
] |
| 109 |
); |
| 110 |
|
| 111 |
$this->end_controls_section(); |
| 112 |
|
| 113 |
$this->start_controls_section( |
| 114 |
'section_style', |
| 115 |
[ |
| 116 |
'label' => esc_html__('Style', 'king-addons'), |
| 117 |
'tab' => Controls_Manager::TAB_STYLE, |
| 118 |
] |
| 119 |
); |
| 120 |
|
| 121 |
$this->add_control( |
| 122 |
'star_size', |
| 123 |
[ |
| 124 |
'label' => esc_html__('Star Size', 'king-addons'), |
| 125 |
'type' => Controls_Manager::SLIDER, |
| 126 |
'range' => [ |
| 127 |
'px' => ['min' => 10, 'max' => 48], |
| 128 |
], |
| 129 |
'selectors' => [ |
| 130 |
'{{WRAPPER}} .ka-woo-product-rating .star-rating' => 'font-size: {{SIZE}}{{UNIT}};', |
| 131 |
], |
| 132 |
] |
| 133 |
); |
| 134 |
|
| 135 |
$this->add_control( |
| 136 |
'star_color', |
| 137 |
[ |
| 138 |
'label' => esc_html__('Filled Color', 'king-addons'), |
| 139 |
'type' => Controls_Manager::COLOR, |
| 140 |
'selectors' => [ |
| 141 |
'{{WRAPPER}} .ka-woo-product-rating .star-rating span:before' => 'color: {{VALUE}};', |
| 142 |
], |
| 143 |
] |
| 144 |
); |
| 145 |
|
| 146 |
$this->add_control( |
| 147 |
'star_empty_color', |
| 148 |
[ |
| 149 |
'label' => esc_html__('Empty Color', 'king-addons'), |
| 150 |
'type' => Controls_Manager::COLOR, |
| 151 |
'selectors' => [ |
| 152 |
'{{WRAPPER}} .ka-woo-product-rating .star-rating:before' => 'color: {{VALUE}};', |
| 153 |
], |
| 154 |
] |
| 155 |
); |
| 156 |
|
| 157 |
$this->add_group_control( |
| 158 |
Group_Control_Typography::get_type(), |
| 159 |
[ |
| 160 |
'name' => 'count_typography', |
| 161 |
'selector' => '{{WRAPPER}} .ka-woo-product-rating__count', |
| 162 |
'label' => esc_html__('Count Typography', 'king-addons'), |
| 163 |
] |
| 164 |
); |
| 165 |
|
| 166 |
$this->add_control( |
| 167 |
'count_color', |
| 168 |
[ |
| 169 |
'label' => esc_html__('Count Color', 'king-addons'), |
| 170 |
'type' => Controls_Manager::COLOR, |
| 171 |
'selectors' => [ |
| 172 |
'{{WRAPPER}} .ka-woo-product-rating__count' => 'color: {{VALUE}};', |
| 173 |
], |
| 174 |
] |
| 175 |
); |
| 176 |
|
| 177 |
$this->add_responsive_control( |
| 178 |
'gap', |
| 179 |
[ |
| 180 |
'label' => esc_html__('Gap', 'king-addons'), |
| 181 |
'type' => Controls_Manager::SLIDER, |
| 182 |
'range' => [ |
| 183 |
'px' => ['min' => 0, 'max' => 30], |
| 184 |
], |
| 185 |
'selectors' => [ |
| 186 |
'{{WRAPPER}} .ka-woo-product-rating' => 'gap: {{SIZE}}{{UNIT}};', |
| 187 |
], |
| 188 |
] |
| 189 |
); |
| 190 |
|
| 191 |
$this->add_responsive_control( |
| 192 |
'margin', |
| 193 |
[ |
| 194 |
'label' => esc_html__('Margin', 'king-addons'), |
| 195 |
'type' => Controls_Manager::DIMENSIONS, |
| 196 |
'size_units' => ['px', 'em', '%'], |
| 197 |
'selectors' => [ |
| 198 |
'{{WRAPPER}} .ka-woo-product-rating' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};', |
| 199 |
], |
| 200 |
] |
| 201 |
); |
| 202 |
|
| 203 |
$this->end_controls_section(); |
| 204 |
} |
| 205 |
|
| 206 |
protected function render(): void |
| 207 |
{ |
| 208 |
$product = $this->get_product(); |
| 209 |
if (!$product) { |
| 210 |
$this->render_missing_product_notice(); |
| 211 |
return; |
| 212 |
} |
| 213 |
|
| 214 |
if (!wc_review_ratings_enabled()) { |
| 215 |
return; |
| 216 |
} |
| 217 |
|
| 218 |
$settings = $this->get_settings_for_display(); |
| 219 |
$layout = $settings['layout'] ?? 'stars_count'; |
| 220 |
$can_pro = king_addons_can_use_pro(); |
| 221 |
|
| 222 |
if ('text' === $layout && !$can_pro) { |
| 223 |
$layout = 'stars_count'; |
| 224 |
} |
| 225 |
|
| 226 |
$rating = (float) $product->get_average_rating(); |
| 227 |
$count = (int) $product->get_rating_count(); |
| 228 |
|
| 229 |
if ($count === 0) { |
| 230 |
return; |
| 231 |
} |
| 232 |
|
| 233 |
$this->add_render_attribute('wrapper', 'class', 'ka-woo-product-rating'); |
| 234 |
|
| 235 |
$link_open = ''; |
| 236 |
$link_close = ''; |
| 237 |
if (!empty($settings['link_to_reviews']) && $can_pro) { |
| 238 |
$anchor = $settings['reviews_anchor'] ?: '#reviews'; |
| 239 |
$link_open = '<a class="ka-woo-product-rating__link" href="' . esc_url($anchor) . '">'; |
| 240 |
$link_close = '</a>'; |
| 241 |
} |
| 242 |
|
| 243 |
echo '<div ' . $this->get_render_attribute_string('wrapper') . '>'; |
| 244 |
|
| 245 |
if ('text' === $layout) { |
| 246 |
$tpl = $settings['text_template'] ?? '{rating} / {max} ({count})'; |
| 247 |
$text = str_replace( |
| 248 |
['{rating}', '{max}', '{count}'], |
| 249 |
[number_format($rating, 1), '5', (string) $count], |
| 250 |
$tpl |
| 251 |
); |
| 252 |
echo $link_open . '<span class="ka-woo-product-rating__text">' . esc_html($text) . '</span>' . $link_close; |
| 253 |
} else { |
| 254 |
// stars |
| 255 |
$html = wc_get_rating_html($rating, $count); |
| 256 |
echo $link_open . $html . $link_close; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 257 |
|
| 258 |
if ('stars_count' === $layout) { |
| 259 |
$count_text = $settings['count_text'] ?: '({count} reviews)'; |
| 260 |
$count_text = str_replace('{count}', (string) $count, $count_text); |
| 261 |
echo '<span class="ka-woo-product-rating__count">' . esc_html($count_text) . '</span>'; |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
echo '</div>'; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|