| 1 |
<?php |
| 2 |
|
| 3 |
namespace UltimateStoreKit\Modules\QrCode\Widgets; |
| 4 |
|
| 5 |
use Elementor\Controls_Manager; |
| 6 |
use Elementor\Group_Control_Border; |
| 7 |
use Elementor\Group_Control_Box_Shadow; |
| 8 |
use Elementor\Group_Control_Typography; |
| 9 |
use Elementor\Group_Control_Background; |
| 10 |
use UltimateStoreKit\Base\Module_Base; |
| 11 |
|
| 12 |
|
| 13 |
if (!defined('ABSPATH')) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
// Exit if accessed directly |
| 18 |
|
| 19 |
class QR_Code extends Module_Base { |
| 20 |
|
| 21 |
public function get_name() { |
| 22 |
return 'usk-qr-code'; |
| 23 |
} |
| 24 |
|
| 25 |
public function get_title() { |
| 26 |
return esc_html__('QR Code', 'ultimate-store-kit'); |
| 27 |
} |
| 28 |
|
| 29 |
public function get_icon() { |
| 30 |
return 'usk-widget-icon usk-icon-qr-code usk-new'; |
| 31 |
} |
| 32 |
|
| 33 |
public function get_categories() { |
| 34 |
return ['ultimate-store-kit']; |
| 35 |
} |
| 36 |
|
| 37 |
public function get_keywords() { |
| 38 |
return ['product', 'qr', 'code', 'bar', 'scan', 'qr code']; |
| 39 |
} |
| 40 |
|
| 41 |
// public function get_script_depends() { |
| 42 |
// return ['usk-checkout-coupon-form']; |
| 43 |
// } |
| 44 |
|
| 45 |
public function get_style_depends() { |
| 46 |
if ($this->usk_is_edit_mode()) { |
| 47 |
return ['usk-all-styles']; |
| 48 |
} else { |
| 49 |
return ['usk-font', 'usk-qr-code']; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// public function get_custom_help_url() { |
| 54 |
// return 'https://youtu.be/3VkvuskVaNAM'; |
| 55 |
// } |
| 56 |
public function has_widget_inner_wrapper(): bool { |
| 57 |
return ! \Elementor\Plugin::$instance->experiments->is_feature_active( 'e_optimized_markup' ); |
| 58 |
} |
| 59 |
protected function is_dynamic_content(): bool { |
| 60 |
return false; |
| 61 |
} |
| 62 |
protected function register_controls() { |
| 63 |
$this->start_controls_section( |
| 64 |
'section_content_qrcode', |
| 65 |
[ |
| 66 |
'label' => __('QR Code', 'ultimate-store-kit'), |
| 67 |
'tab' => Controls_Manager::TAB_CONTENT, |
| 68 |
] |
| 69 |
); |
| 70 |
|
| 71 |
$this->add_control( |
| 72 |
'qr_code_dimesion_size', |
| 73 |
[ |
| 74 |
'label' => __('Dimension Size', 'ultimate-store-kit'), |
| 75 |
'type' => Controls_Manager::NUMBER, |
| 76 |
'min' => 50, |
| 77 |
'max' => 800, |
| 78 |
'step' => 1, |
| 79 |
'default' => 150, |
| 80 |
] |
| 81 |
); |
| 82 |
|
| 83 |
$this->add_control( |
| 84 |
'qr_code_cart_url', |
| 85 |
[ |
| 86 |
'label' => __('Enable Add to Cart URL', 'ultimate-store-kit'), |
| 87 |
'type' => Controls_Manager::SWITCHER, |
| 88 |
'return_value' => 'yes', |
| 89 |
'default' => 'no', |
| 90 |
'separator' => 'before', |
| 91 |
] |
| 92 |
); |
| 93 |
$this->add_control( |
| 94 |
'qr_code_product_quantity', |
| 95 |
[ |
| 96 |
'label' => __('Quantity', 'ultimate-store-kit'), |
| 97 |
'type' => Controls_Manager::NUMBER, |
| 98 |
'min' => 1, |
| 99 |
'max' => 1000, |
| 100 |
'step' => 1, |
| 101 |
'default' => 1, |
| 102 |
'dynamic' => ['active' => true], |
| 103 |
'condition' => [ |
| 104 |
'qr_code_cart_url' => 'yes' |
| 105 |
] |
| 106 |
] |
| 107 |
); |
| 108 |
|
| 109 |
$this->add_control( |
| 110 |
'qr_code_alignemnt', |
| 111 |
[ |
| 112 |
'label' => __('Alignement', 'ultimate-store-kit'), |
| 113 |
'type' => Controls_Manager::CHOOSE, |
| 114 |
'options' => [ |
| 115 |
'left' => [ |
| 116 |
'title' => __('Left', 'ultimate-store-kit'), |
| 117 |
'icon' => 'eicon-h-align-left', |
| 118 |
], |
| 119 |
'center' => [ |
| 120 |
'title' => __('Center', 'ultimate-store-kit'), |
| 121 |
'icon' => 'eicon-h-align-center', |
| 122 |
], |
| 123 |
'right' => [ |
| 124 |
'title' => __('Right', 'ultimate-store-kit'), |
| 125 |
'icon' => 'eicon-h-align-right', |
| 126 |
], |
| 127 |
], |
| 128 |
'selectors' => [ |
| 129 |
'{{WRAPPER}} .usk-qrcode' => 'text-align: {{VALUE}};', |
| 130 |
], |
| 131 |
'separator' => 'before', |
| 132 |
] |
| 133 |
); |
| 134 |
|
| 135 |
$this->end_controls_section(); |
| 136 |
} |
| 137 |
|
| 138 |
protected function get_last_order_id() { |
| 139 |
global $wpdb; |
| 140 |
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Editor-preview helper; picks the newest product once per preview render. |
| 141 |
$results = $wpdb->get_col( |
| 142 |
"SELECT MAX(ID) FROM {$wpdb->posts} WHERE post_type = 'product' AND post_status = 'publish'" |
| 143 |
); |
| 144 |
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching |
| 145 |
return reset($results); |
| 146 |
} |
| 147 |
|
| 148 |
protected function render() { |
| 149 |
$settings = $this->get_settings_for_display(); |
| 150 |
$product_id = ultimate_store_kit_is_preview() ? $this->get_last_order_id() : $product_id = get_the_ID(); |
| 151 |
$quantity = (!empty($settings['qr_code_product_quantity']) ? $settings['qr_code_product_quantity'] : 1); |
| 152 |
if ('yes' === $settings['qr_code_cart_url']) { |
| 153 |
$product_url = get_the_permalink($product_id) . sprintf('?add-to-cart=%s&quantity=%s', $product_id, $quantity); |
| 154 |
} else { |
| 155 |
$product_url = get_the_permalink($product_id); |
| 156 |
} |
| 157 |
$size = (!empty($settings['qr_code_dimesion_size']) ? $settings['qr_code_dimesion_size'] : 150); |
| 158 |
$dimension = $size . 'x' . $size; |
| 159 |
$image_src = sprintf('//api.qrserver.com/v1/create-qr-code/?size=%s&ecc=L&qzone=1&data=%s', $dimension, urlencode($product_url)); |
| 160 |
printf('<div class="usk-qrcode"><img src="%1$s" alt="%2$s"></div>', esc_url($image_src), esc_attr(get_the_title($product_id))); |
| 161 |
} |
| 162 |
} |
| 163 |
|