Files
cannaiq/wordpress-plugin/dutchie-analytics/includes/Shortcodes.php
2025-11-28 19:45:44 -07:00

350 lines
14 KiB
PHP

<?php
namespace DutchieAnalytics;
class Shortcodes {
public function __construct() {
add_shortcode('dutchie_products', [$this, 'products_grid']);
add_shortcode('dutchie_carousel', [$this, 'products_carousel']);
add_shortcode('dutchie_stores', [$this, 'stores_list']);
add_shortcode('dutchie_brands', [$this, 'brands_list']);
add_shortcode('dutchie_specials', [$this, 'specials']);
}
/**
* Products Grid Shortcode
* [dutchie_products store_id="1" limit="12" category_id="5" fields="name,price,brand"]
*/
public function products_grid($atts) {
$atts = shortcode_atts([
'store_id' => '',
'category_id' => '',
'limit' => 12,
'in_stock' => 'true',
'search' => '',
'columns' => 4,
'fields' => '',
'style' => 'modern',
], $atts);
if (empty($atts['store_id'])) {
return '<p>' . __('Please specify a store_id', 'dutchie-analytics') . '</p>';
}
$api = new API_Client();
$args = [
'store_id' => $atts['store_id'],
'limit' => (int) $atts['limit'],
];
if (!empty($atts['category_id'])) {
$args['category_id'] = $atts['category_id'];
}
if (!empty($atts['search'])) {
$args['search'] = $atts['search'];
}
if ($atts['in_stock'] === 'true') {
$args['in_stock'] = true;
}
if (!empty($atts['fields'])) {
$args['fields'] = $atts['fields'];
}
$data = $api->get_products($args);
$products = $data['products'] ?? [];
if (empty($products)) {
return '<p>' . __('No products found', 'dutchie-analytics') . '</p>';
}
ob_start();
?>
<div class="dutchie-products-grid" data-columns="<?php echo esc_attr($atts['columns']); ?>">
<?php foreach ($products as $product): ?>
<div class="dutchie-product-card dutchie-product-card-<?php echo esc_attr($atts['style']); ?>">
<?php if (!empty($product['image_url_full'])): ?>
<div class="dutchie-product-image">
<img src="<?php echo esc_url($product['image_url_full']); ?>" alt="<?php echo esc_attr($product['name']); ?>" loading="lazy">
<?php if ($product['in_stock']): ?>
<span class="dutchie-stock-badge in-stock"><?php _e('In Stock', 'dutchie-analytics'); ?></span>
<?php else: ?>
<span class="dutchie-stock-badge out-of-stock"><?php _e('Out of Stock', 'dutchie-analytics'); ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="dutchie-product-content">
<h3 class="dutchie-product-name"><?php echo esc_html($product['name']); ?></h3>
<?php if (!empty($product['brand'])): ?>
<p class="dutchie-product-brand"><?php echo esc_html($product['brand']); ?></p>
<?php endif; ?>
<?php if (!empty($product['description'])): ?>
<p class="dutchie-product-description"><?php echo esc_html(wp_trim_words($product['description'], 20)); ?></p>
<?php endif; ?>
<div class="dutchie-product-meta">
<?php if (!empty($product['price'])): ?>
<span class="dutchie-product-price">$<?php echo number_format($product['price'], 2); ?></span>
<?php endif; ?>
<?php if (!empty($product['thc_percentage'])): ?>
<span class="dutchie-thc-badge"><?php echo esc_html($product['thc_percentage']); ?>% THC</span>
<?php endif; ?>
<?php if (!empty($product['cbd_percentage'])): ?>
<span class="dutchie-cbd-badge"><?php echo esc_html($product['cbd_percentage']); ?>% CBD</span>
<?php endif; ?>
</div>
<?php if (!empty($product['weight'])): ?>
<p class="dutchie-product-weight"><?php echo esc_html($product['weight']); ?></p>
<?php endif; ?>
</div>
</div>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Products Carousel Shortcode
* [dutchie_carousel store_id="1" limit="20"]
*/
public function products_carousel($atts) {
$atts = shortcode_atts([
'store_id' => '',
'category_id' => '',
'limit' => 20,
'in_stock' => 'true',
'slides_per_view' => 4,
'autoplay' => 'true',
'style' => 'modern',
], $atts);
if (empty($atts['store_id'])) {
return '<p>' . __('Please specify a store_id', 'dutchie-analytics') . '</p>';
}
$api = new API_Client();
$args = [
'store_id' => $atts['store_id'],
'limit' => (int) $atts['limit'],
];
if (!empty($atts['category_id'])) {
$args['category_id'] = $atts['category_id'];
}
if ($atts['in_stock'] === 'true') {
$args['in_stock'] = true;
}
$data = $api->get_products($args);
$products = $data['products'] ?? [];
if (empty($products)) {
return '<p>' . __('No products found', 'dutchie-analytics') . '</p>';
}
$carousel_id = 'dutchie-carousel-' . uniqid();
ob_start();
?>
<div class="dutchie-products-carousel-wrapper">
<div class="swiper <?php echo esc_attr($carousel_id); ?>">
<div class="swiper-wrapper">
<?php foreach ($products as $product): ?>
<div class="swiper-slide">
<div class="dutchie-product-card dutchie-product-card-<?php echo esc_attr($atts['style']); ?>">
<?php if (!empty($product['image_url_full'])): ?>
<div class="dutchie-product-image">
<img src="<?php echo esc_url($product['image_url_full']); ?>" alt="<?php echo esc_attr($product['name']); ?>" loading="lazy">
<?php if ($product['in_stock']): ?>
<span class="dutchie-stock-badge in-stock"><?php _e('In Stock', 'dutchie-analytics'); ?></span>
<?php else: ?>
<span class="dutchie-stock-badge out-of-stock"><?php _e('Out of Stock', 'dutchie-analytics'); ?></span>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="dutchie-product-content">
<h3 class="dutchie-product-name"><?php echo esc_html($product['name']); ?></h3>
<?php if (!empty($product['brand'])): ?>
<p class="dutchie-product-brand"><?php echo esc_html($product['brand']); ?></p>
<?php endif; ?>
<div class="dutchie-product-meta">
<?php if (!empty($product['price'])): ?>
<span class="dutchie-product-price">$<?php echo number_format($product['price'], 2); ?></span>
<?php endif; ?>
<?php if (!empty($product['thc_percentage'])): ?>
<span class="dutchie-thc-badge"><?php echo esc_html($product['thc_percentage']); ?>% THC</span>
<?php endif; ?>
</div>
</div>
</div>
</div>
<?php endforeach; ?>
</div>
<div class="swiper-button-prev"></div>
<div class="swiper-button-next"></div>
<div class="swiper-pagination"></div>
</div>
</div>
<script>
jQuery(document).ready(function($) {
new Swiper('.<?php echo esc_js($carousel_id); ?>', {
slidesPerView: 1,
spaceBetween: 24,
loop: true,
<?php if ($atts['autoplay'] === 'true'): ?>
autoplay: {
delay: 3000,
disableOnInteraction: false,
},
<?php endif; ?>
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
breakpoints: {
640: { slidesPerView: 2 },
768: { slidesPerView: 3 },
1024: { slidesPerView: <?php echo (int) $atts['slides_per_view']; ?> },
}
});
});
</script>
<?php
return ob_get_clean();
}
/**
* Stores List Shortcode
* [dutchie_stores]
*/
public function stores_list($atts) {
$api = new API_Client();
$data = $api->get_stores();
$stores = $data['stores'] ?? [];
if (empty($stores)) {
return '<p>' . __('No stores found', 'dutchie-analytics') . '</p>';
}
ob_start();
?>
<div class="dutchie-stores-grid">
<?php foreach ($stores as $store): ?>
<div class="dutchie-store-card">
<?php if (!empty($store['logo_url'])): ?>
<img src="<?php echo esc_url($store['logo_url']); ?>" alt="<?php echo esc_attr($store['name']); ?>" class="dutchie-store-logo">
<?php endif; ?>
<h3><?php echo esc_html($store['name']); ?></h3>
<p class="dutchie-store-stats">
<?php printf(__('%d Products', 'dutchie-analytics'), $store['product_count']); ?>
<?php printf(__('%d Categories', 'dutchie-analytics'), $store['category_count']); ?>
</p>
<?php if ($store['dutchie_url']): ?>
<a href="<?php echo esc_url($store['dutchie_url']); ?>" target="_blank" class="dutchie-store-link">
<?php _e('View Store', 'dutchie-analytics'); ?>
</a>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Brands List Shortcode
* [dutchie_brands store_id="1"]
*/
public function brands_list($atts) {
$atts = shortcode_atts(['store_id' => ''], $atts);
if (empty($atts['store_id'])) {
return '<p>' . __('Please specify a store_id', 'dutchie-analytics') . '</p>';
}
$api = new API_Client();
$data = $api->get_store_brands($atts['store_id']);
$brands = $data['brands'] ?? [];
if (empty($brands)) {
return '<p>' . __('No brands found', 'dutchie-analytics') . '</p>';
}
ob_start();
?>
<div class="dutchie-brands-grid">
<?php foreach ($brands as $brand): ?>
<div class="dutchie-brand-badge"><?php echo esc_html($brand); ?></div>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
/**
* Specials Shortcode
* [dutchie_specials store_id="1" date="2025-01-15"]
*/
public function specials($atts) {
$atts = shortcode_atts([
'store_id' => '',
'date' => '',
], $atts);
if (empty($atts['store_id'])) {
return '<p>' . __('Please specify a store_id', 'dutchie-analytics') . '</p>';
}
$api = new API_Client();
$data = $api->get_store_specials($atts['store_id'], $atts['date'] ?: null);
$specials = $data['specials'] ?? [];
if (empty($specials)) {
return '<p>' . __('No specials for this date', 'dutchie-analytics') . '</p>';
}
ob_start();
?>
<div class="dutchie-specials-grid">
<?php foreach ($specials as $special): ?>
<div class="dutchie-special-card">
<?php if (!empty($special['product_image'])): ?>
<img src="<?php echo esc_url($special['product_image']); ?>" alt="<?php echo esc_attr($special['name']); ?>">
<?php endif; ?>
<h3><?php echo esc_html($special['name']); ?></h3>
<?php if (!empty($special['description'])): ?>
<p><?php echo esc_html($special['description']); ?></p>
<?php endif; ?>
<?php if (!empty($special['discount_percentage'])): ?>
<span class="dutchie-discount-badge"><?php echo esc_html($special['discount_percentage']); ?>% OFF</span>
<?php endif; ?>
</div>
<?php endforeach; ?>
</div>
<?php
return ob_get_clean();
}
}