Files
cannaiq/wordpress-plugin/widgets/strain-badge.php
Kelly c33ed1cae9
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
feat: CannaiQ Menus WordPress Plugin v2.0.0 - Modular Component Library
New modular component widgets:
- Discount Ribbon (ribbon/pill/text styles)
- Strain Badge (Sativa/Indica/Hybrid colored pills)
- THC/CBD Meter (progress bars or badges)
- Effects Display (styled chips with icons)
- Price Block (original + sale price)
- Cart Button (styled CTA linking to menu)
- Stock Indicator (in/out of stock badges)
- Product Image + Badges (image with overlays)

New card template:
- Premium Product Card (ready-to-use template)

Extended dynamic tags (30+ total):
- Discount %, Strain Badge, THC/CBD Badge
- Effects Chips, Terpenes, Price Display
- Menu URL, Stock Status, and more

New files:
- assets/css/components.css
- includes/effects-icons.php (SVG icons)
- 10 new widget files
- dynamic-tags-extended.php

Branding updated to "CannaiQ" throughout.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2025-12-17 00:21:40 -07:00

251 lines
7.4 KiB
PHP

<?php
/**
* CannaIQ Strain Badge Widget
*
* Displays strain type (Sativa/Indica/Hybrid) as a colored badge.
*
* @package CannaIQ_Menus
* @since 2.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
class CannaIQ_Strain_Badge_Widget extends \Elementor\Widget_Base {
public function get_name() {
return 'cannaiq_strain_badge';
}
public function get_title() {
return __('Strain Badge', 'cannaiq-menus');
}
public function get_icon() {
return 'eicon-tags';
}
public function get_categories() {
return ['cannaiq'];
}
public function get_keywords() {
return ['strain', 'sativa', 'indica', 'hybrid', 'badge', 'cannaiq'];
}
protected function register_controls() {
$this->start_controls_section(
'content_section',
[
'label' => __('Content', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
]
);
$this->add_control(
'source',
[
'label' => __('Strain Source', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'auto',
'options' => [
'auto' => __('Auto (from product)', 'cannaiq-menus'),
'custom' => __('Custom value', 'cannaiq-menus'),
],
]
);
$this->add_control(
'custom_strain',
[
'label' => __('Strain Type', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'hybrid',
'options' => [
'sativa' => __('Sativa', 'cannaiq-menus'),
'indica' => __('Indica', 'cannaiq-menus'),
'hybrid' => __('Hybrid', 'cannaiq-menus'),
],
'condition' => [
'source' => 'custom',
],
]
);
$this->add_control(
'format',
[
'label' => __('Display Format', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'pill',
'options' => [
'pill' => __('Pill', 'cannaiq-menus'),
'text' => __('Text Only', 'cannaiq-menus'),
],
]
);
$this->add_control(
'text_format',
[
'label' => __('Text Format', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'uppercase',
'options' => [
'uppercase' => __('UPPERCASE', 'cannaiq-menus'),
'capitalize' => __('Capitalize', 'cannaiq-menus'),
'lowercase' => __('lowercase', 'cannaiq-menus'),
],
]
);
$this->add_control(
'show_icon',
[
'label' => __('Show Icon', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SWITCHER,
'label_on' => __('Yes', 'cannaiq-menus'),
'label_off' => __('No', 'cannaiq-menus'),
'return_value' => 'yes',
'default' => '',
]
);
$this->end_controls_section();
// Style Section
$this->start_controls_section(
'style_section',
[
'label' => __('Style', 'cannaiq-menus'),
'tab' => \Elementor\Controls_Manager::TAB_STYLE,
]
);
$this->add_control(
'size',
[
'label' => __('Size', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::SELECT,
'default' => 'medium',
'options' => [
'small' => __('Small', 'cannaiq-menus'),
'medium' => __('Medium', 'cannaiq-menus'),
'large' => __('Large', 'cannaiq-menus'),
],
]
);
$this->add_control(
'sativa_color',
[
'label' => __('Sativa Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#22c55e',
]
);
$this->add_control(
'indica_color',
[
'label' => __('Indica Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#8b5cf6',
]
);
$this->add_control(
'hybrid_color',
[
'label' => __('Hybrid Color', 'cannaiq-menus'),
'type' => \Elementor\Controls_Manager::COLOR,
'default' => '#f97316',
]
);
$this->add_group_control(
\Elementor\Group_Control_Typography::get_type(),
[
'name' => 'typography',
'selector' => '{{WRAPPER}} .cannaiq-strain-badge',
]
);
$this->end_controls_section();
}
protected function render() {
$settings = $this->get_settings_for_display();
// Get strain type
$strain = '';
if ($settings['source'] === 'custom') {
$strain = $settings['custom_strain'];
} else {
global $cannaiq_current_product;
if (isset($cannaiq_current_product)) {
$strain = strtolower($cannaiq_current_product['strainType'] ?? $cannaiq_current_product['strain_type'] ?? '');
}
}
if (empty($strain)) {
return;
}
// Normalize strain type
$strain_key = strtolower($strain);
if (!in_array($strain_key, ['sativa', 'indica', 'hybrid'])) {
return;
}
// Format text
switch ($settings['text_format']) {
case 'uppercase':
$text = strtoupper($strain);
break;
case 'lowercase':
$text = strtolower($strain);
break;
default:
$text = ucfirst($strain);
}
// Get color based on strain type
$color = $settings[$strain_key . '_color'];
// Build classes
$classes = [
'cannaiq-strain-badge',
'cannaiq-strain-badge--' . $settings['format'],
'cannaiq-strain-badge--' . $strain_key,
];
if ($settings['size'] !== 'medium') {
$classes[] = 'cannaiq-strain-badge--' . $settings['size'];
}
// Build style
$style = '';
if ($settings['format'] === 'pill') {
$style = sprintf('background-color: %s; color: white;', esc_attr($color));
} else {
$style = sprintf('color: %s;', esc_attr($color));
}
// Icon SVG (leaf icon)
$icon = '';
if ($settings['show_icon'] === 'yes') {
$icon = '<svg width="12" height="12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M11 20A7 7 0 0 1 9.8 6.1C15.5 5 17 4.48 19 2c1 2 2 4.18 2 8 0 5.5-4.78 10-10 10Z"/><path d="M2 21c0-3 1.85-5.36 5.08-6C9.5 14.52 12 13 13 12"/></svg>';
}
printf(
'<span class="%s" style="%s">%s%s</span>',
esc_attr(implode(' ', $classes)),
esc_attr($style),
$icon,
esc_html($text)
);
}
}