| 1 |
<?php |
| 2 |
/** |
| 3 |
* Woo Product Meta 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 meta info (categories, tags, optional SKU/brand). |
| 19 |
*/ |
| 20 |
class Woo_Product_Meta extends Abstract_Single_Widget |
| 21 |
{ |
| 22 |
public function get_name(): string |
| 23 |
{ |
| 24 |
return 'woo_product_meta'; |
| 25 |
} |
| 26 |
|
| 27 |
public function get_title(): string |
| 28 |
{ |
| 29 |
return esc_html__('Product Meta', 'king-addons'); |
| 30 |
} |
| 31 |
|
| 32 |
public function get_icon(): string |
| 33 |
{ |
| 34 |
return 'king-addons-icon king-addons-woo-product-meta'; |
| 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-meta-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 |
'show_categories', |
| 59 |
[ |
| 60 |
'label' => esc_html__('Show categories', 'king-addons'), |
| 61 |
'type' => Controls_Manager::SWITCHER, |
| 62 |
'return_value' => 'yes', |
| 63 |
'default' => 'yes', |
| 64 |
] |
| 65 |
); |
| 66 |
|
| 67 |
$this->add_control( |
| 68 |
'show_tags', |
| 69 |
[ |
| 70 |
'label' => esc_html__('Show tags', 'king-addons'), |
| 71 |
'type' => Controls_Manager::SWITCHER, |
| 72 |
'return_value' => 'yes', |
| 73 |
'default' => 'yes', |
| 74 |
] |
| 75 |
); |
| 76 |
|
| 77 |
$this->add_control( |
| 78 |
'show_sku', |
| 79 |
[ |
| 80 |
'label' => sprintf(__('Show SKU %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 81 |
'type' => Controls_Manager::SWITCHER, |
| 82 |
'return_value' => 'yes', |
| 83 |
] |
| 84 |
); |
| 85 |
|
| 86 |
$this->add_control( |
| 87 |
'show_brand', |
| 88 |
[ |
| 89 |
'label' => sprintf(__('Show brand taxonomy %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 90 |
'type' => Controls_Manager::SWITCHER, |
| 91 |
'return_value' => 'yes', |
| 92 |
] |
| 93 |
); |
| 94 |
|
| 95 |
$this->add_control( |
| 96 |
'brand_taxonomy', |
| 97 |
[ |
| 98 |
'label' => esc_html__('Brand taxonomy (Pro)', 'king-addons'), |
| 99 |
'type' => Controls_Manager::TEXT, |
| 100 |
'default' => 'product_brand', |
| 101 |
'condition' => [ |
| 102 |
'show_brand' => 'yes', |
| 103 |
], |
| 104 |
] |
| 105 |
); |
| 106 |
|
| 107 |
$this->add_control( |
| 108 |
'brand_fallback_taxonomy', |
| 109 |
[ |
| 110 |
'label' => esc_html__('Brand fallback taxonomy (Pro)', 'king-addons'), |
| 111 |
'type' => Controls_Manager::TEXT, |
| 112 |
'default' => 'pa_brand', |
| 113 |
'condition' => [ |
| 114 |
'show_brand' => 'yes', |
| 115 |
], |
| 116 |
] |
| 117 |
); |
| 118 |
|
| 119 |
$this->add_control( |
| 120 |
'layout', |
| 121 |
[ |
| 122 |
'label' => sprintf(__('Layout %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 123 |
'type' => Controls_Manager::SELECT, |
| 124 |
'options' => [ |
| 125 |
'inline' => esc_html__('Inline', 'king-addons'), |
| 126 |
'stacked' => esc_html__('Stacked (Pro)', 'king-addons'), |
| 127 |
'inline_no_label' => esc_html__('Inline, no labels (Pro)', 'king-addons'), |
| 128 |
], |
| 129 |
'default' => 'inline', |
| 130 |
] |
| 131 |
); |
| 132 |
|
| 133 |
$this->add_control( |
| 134 |
'format_preset', |
| 135 |
[ |
| 136 |
'label' => sprintf(__('Format preset %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 137 |
'type' => Controls_Manager::SELECT, |
| 138 |
'options' => [ |
| 139 |
'' => esc_html__('Default', 'king-addons'), |
| 140 |
'pipe' => esc_html__('Pipe inline', 'king-addons'), |
| 141 |
'bullets' => esc_html__('Bullets', 'king-addons'), |
| 142 |
'badges' => esc_html__('Badges', 'king-addons'), |
| 143 |
], |
| 144 |
'default' => '', |
| 145 |
] |
| 146 |
); |
| 147 |
|
| 148 |
$this->add_control( |
| 149 |
'separator', |
| 150 |
[ |
| 151 |
'label' => esc_html__('Separator', 'king-addons'), |
| 152 |
'type' => Controls_Manager::TEXT, |
| 153 |
'dynamic' => ['active' => true], |
| 154 |
'default' => ', ', |
| 155 |
] |
| 156 |
); |
| 157 |
|
| 158 |
$this->add_control( |
| 159 |
'label_categories', |
| 160 |
[ |
| 161 |
'label' => esc_html__('Label: Categories', 'king-addons'), |
| 162 |
'type' => Controls_Manager::TEXT, |
| 163 |
'dynamic' => ['active' => true], |
| 164 |
'default' => esc_html__('Category:', 'king-addons'), |
| 165 |
] |
| 166 |
); |
| 167 |
|
| 168 |
$this->add_control( |
| 169 |
'label_tags', |
| 170 |
[ |
| 171 |
'label' => esc_html__('Label: Tags', 'king-addons'), |
| 172 |
'type' => Controls_Manager::TEXT, |
| 173 |
'dynamic' => ['active' => true], |
| 174 |
'default' => esc_html__('Tags:', 'king-addons'), |
| 175 |
] |
| 176 |
); |
| 177 |
|
| 178 |
$this->add_control( |
| 179 |
'label_brand', |
| 180 |
[ |
| 181 |
'label' => esc_html__('Label: Brand (Pro)', 'king-addons'), |
| 182 |
'type' => Controls_Manager::TEXT, |
| 183 |
'dynamic' => ['active' => true], |
| 184 |
'default' => esc_html__('Brand:', 'king-addons'), |
| 185 |
] |
| 186 |
); |
| 187 |
|
| 188 |
$this->add_control( |
| 189 |
'label_sku', |
| 190 |
[ |
| 191 |
'label' => esc_html__('Label: SKU (Pro)', 'king-addons'), |
| 192 |
'type' => Controls_Manager::TEXT, |
| 193 |
'dynamic' => ['active' => true], |
| 194 |
'default' => esc_html__('SKU:', 'king-addons'), |
| 195 |
] |
| 196 |
); |
| 197 |
|
| 198 |
$this->add_control( |
| 199 |
'label_transform', |
| 200 |
[ |
| 201 |
'label' => sprintf(__('Label transform %s', 'king-addons'), '<i class="eicon-pro-icon"></i>'), |
| 202 |
'type' => Controls_Manager::SELECT, |
| 203 |
'options' => [ |
| 204 |
'' => esc_html__('None', 'king-addons'), |
| 205 |
'uppercase' => esc_html__('Uppercase', 'king-addons'), |
| 206 |
'caps' => esc_html__('Capitalize', 'king-addons'), |
| 207 |
], |
| 208 |
'default' => '', |
| 209 |
] |
| 210 |
); |
| 211 |
|
| 212 |
$this->end_controls_section(); |
| 213 |
|
| 214 |
$this->start_controls_section( |
| 215 |
'section_style', |
| 216 |
[ |
| 217 |
'label' => esc_html__('Style', 'king-addons'), |
| 218 |
'tab' => Controls_Manager::TAB_STYLE, |
| 219 |
] |
| 220 |
); |
| 221 |
|
| 222 |
$this->add_group_control( |
| 223 |
Group_Control_Typography::get_type(), |
| 224 |
[ |
| 225 |
'name' => 'label_typo', |
| 226 |
'selector' => '{{WRAPPER}} .ka-woo-product-meta__label', |
| 227 |
'label' => esc_html__('Label Typography', 'king-addons'), |
| 228 |
] |
| 229 |
); |
| 230 |
|
| 231 |
$this->add_control( |
| 232 |
'label_color', |
| 233 |
[ |
| 234 |
'label' => esc_html__('Label Color', 'king-addons'), |
| 235 |
'type' => Controls_Manager::COLOR, |
| 236 |
'selectors' => [ |
| 237 |
'{{WRAPPER}} .ka-woo-product-meta__label' => 'color: {{VALUE}};', |
| 238 |
], |
| 239 |
] |
| 240 |
); |
| 241 |
|
| 242 |
$this->add_group_control( |
| 243 |
Group_Control_Typography::get_type(), |
| 244 |
[ |
| 245 |
'name' => 'value_typo', |
| 246 |
'selector' => '{{WRAPPER}} .ka-woo-product-meta__value', |
| 247 |
'label' => esc_html__('Value Typography', 'king-addons'), |
| 248 |
] |
| 249 |
); |
| 250 |
|
| 251 |
$this->add_control( |
| 252 |
'value_color', |
| 253 |
[ |
| 254 |
'label' => esc_html__('Value Color', 'king-addons'), |
| 255 |
'type' => Controls_Manager::COLOR, |
| 256 |
'selectors' => [ |
| 257 |
'{{WRAPPER}} .ka-woo-product-meta__value' => 'color: {{VALUE}};', |
| 258 |
'{{WRAPPER}} .ka-woo-product-meta__value a' => 'color: {{VALUE}};', |
| 259 |
], |
| 260 |
] |
| 261 |
); |
| 262 |
|
| 263 |
$this->add_responsive_control( |
| 264 |
'item_gap', |
| 265 |
[ |
| 266 |
'label' => esc_html__('Items gap', 'king-addons'), |
| 267 |
'type' => Controls_Manager::SLIDER, |
| 268 |
'range' => [ |
| 269 |
'px' => ['min' => 0, 'max' => 30], |
| 270 |
], |
| 271 |
'selectors' => [ |
| 272 |
'{{WRAPPER}} .ka-woo-product-meta__item' => 'margin-bottom: {{SIZE}}{{UNIT}};', |
| 273 |
], |
| 274 |
] |
| 275 |
); |
| 276 |
|
| 277 |
$this->end_controls_section(); |
| 278 |
} |
| 279 |
|
| 280 |
protected function render(): void |
| 281 |
{ |
| 282 |
if (!class_exists('WooCommerce') || !function_exists('wc_get_product_category_list') || !function_exists('wc_get_product_tag_list')) { |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
$product = $this->get_product(); |
| 287 |
if (!$product) { |
| 288 |
$this->render_missing_product_notice(); |
| 289 |
return; |
| 290 |
} |
| 291 |
|
| 292 |
$settings = $this->get_settings_for_display(); |
| 293 |
$can_pro = king_addons_can_use_pro(); |
| 294 |
|
| 295 |
$items = []; |
| 296 |
$sep = sanitize_text_field((string) ($settings['separator'] ?? ', ')); |
| 297 |
|
| 298 |
if (!empty($settings['show_categories'])) { |
| 299 |
$cats = wc_get_product_category_list($product->get_id(), $sep); |
| 300 |
if ($cats) { |
| 301 |
$items[] = [ |
| 302 |
'label' => $settings['label_categories'] ?: __('Category:', 'king-addons'), |
| 303 |
'value' => $cats, |
| 304 |
]; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
if (!empty($settings['show_tags'])) { |
| 309 |
$tags = wc_get_product_tag_list($product->get_id(), $sep); |
| 310 |
if ($tags) { |
| 311 |
$items[] = [ |
| 312 |
'label' => $settings['label_tags'] ?: __('Tags:', 'king-addons'), |
| 313 |
'value' => $tags, |
| 314 |
]; |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
if (!empty($settings['show_sku']) && $can_pro) { |
| 319 |
$sku = $product->get_sku(); |
| 320 |
if ($sku) { |
| 321 |
$items[] = [ |
| 322 |
'label' => $settings['label_sku'] ?: __('SKU:', 'king-addons'), |
| 323 |
'value' => esc_html($sku), |
| 324 |
]; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
if (!empty($settings['show_brand']) && $can_pro) { |
| 329 |
$taxes = []; |
| 330 |
if (!empty($settings['brand_taxonomy'])) { |
| 331 |
$taxes[] = sanitize_key($settings['brand_taxonomy']); |
| 332 |
} |
| 333 |
if (!empty($settings['brand_fallback_taxonomy'])) { |
| 334 |
$taxes[] = sanitize_key($settings['brand_fallback_taxonomy']); |
| 335 |
} |
| 336 |
$taxes = array_unique(array_filter($taxes)); |
| 337 |
foreach ($taxes as $tax) { |
| 338 |
$terms = get_the_terms($product->get_id(), $tax); |
| 339 |
if (!empty($terms) && !is_wp_error($terms)) { |
| 340 |
$links = []; |
| 341 |
foreach ($terms as $term) { |
| 342 |
$links[] = '<a href="' . esc_url(get_term_link($term)) . '">' . esc_html($term->name) . '</a>'; |
| 343 |
} |
| 344 |
$items[] = [ |
| 345 |
'label' => $settings['label_brand'] ?: __('Brand:', 'king-addons'), |
| 346 |
'value' => implode($sep, $links), |
| 347 |
]; |
| 348 |
break; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
if (empty($items)) { |
| 354 |
// Nothing to list is normal for a product with no terms; say so in |
| 355 |
// the editor instead of drawing an empty box. |
| 356 |
if (\Elementor\Plugin::$instance->editor->is_edit_mode()) { |
| 357 |
echo '<div class="king-addons-woo-builder-notice">' |
| 358 |
. esc_html__('This product has no categories, tags, SKU or brand to show.', 'king-addons') |
| 359 |
. '</div>'; |
| 360 |
} |
| 361 |
return; |
| 362 |
} |
| 363 |
|
| 364 |
// The value goes straight into a class name, so an unknown one has to |
| 365 |
// fall back rather than pass through. |
| 366 |
$layout = (string) ($settings['layout'] ?? 'inline'); |
| 367 |
if (!in_array($layout, ['inline', 'stacked', 'inline_no_label'], true)) { |
| 368 |
$layout = 'inline'; |
| 369 |
} |
| 370 |
if (in_array($layout, ['stacked', 'inline_no_label'], true) && !$can_pro) { |
| 371 |
$layout = 'inline'; |
| 372 |
} |
| 373 |
|
| 374 |
$format_preset = $settings['format_preset'] ?? ''; |
| 375 |
if (!empty($format_preset) && !$can_pro) { |
| 376 |
$format_preset = ''; |
| 377 |
} |
| 378 |
|
| 379 |
$wrapper_classes = ['ka-woo-product-meta', 'ka-woo-product-meta--' . $layout]; |
| 380 |
if (!empty($format_preset)) { |
| 381 |
$wrapper_classes[] = 'ka-woo-product-meta--format-' . sanitize_html_class($format_preset); |
| 382 |
} |
| 383 |
if (!empty($settings['label_transform']) && $can_pro) { |
| 384 |
$wrapper_classes[] = 'ka-woo-product-meta--label-' . sanitize_html_class($settings['label_transform']); |
| 385 |
} |
| 386 |
|
| 387 |
echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '">'; |
| 388 |
foreach ($items as $item) { |
| 389 |
echo '<div class="ka-woo-product-meta__item">'; |
| 390 |
if ('inline_no_label' !== $layout) { |
| 391 |
echo '<span class="ka-woo-product-meta__label">' . esc_html($item['label']) . '</span> '; |
| 392 |
} |
| 393 |
echo '<span class="ka-woo-product-meta__value">' . wp_kses_post($item['value']) . '</span>'; |
| 394 |
echo '</div>'; |
| 395 |
} |
| 396 |
echo '</div>'; |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|