assets
3 years ago
loop-product-support
3 years ago
admin-product.php
3 years ago
attribute-hooks.php
3 years ago
frontend.php
3 years ago
helper.php
3 years ago
swatches.php
3 years ago
swatches.php
147 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine\Modules\Swatches; |
| 4 | |
| 5 | use ShopEngine; |
| 6 | use ShopEngine\Modules\Swatches\Loop_Product_Support\Shopengine_Swatches; |
| 7 | use ShopEngine\Traits\Singleton; |
| 8 | |
| 9 | defined('ABSPATH') || exit; |
| 10 | |
| 11 | |
| 12 | class Swatches |
| 13 | { |
| 14 | const PA_COLOR = 'shopengine_color'; |
| 15 | const PA_IMAGE = 'shopengine_image'; |
| 16 | const PA_LABEL = 'shopengine_label'; |
| 17 | |
| 18 | private $attribute_types = []; |
| 19 | |
| 20 | use Singleton; |
| 21 | |
| 22 | |
| 23 | public static function get_module_uri() { |
| 24 | |
| 25 | return plugin_dir_url(__FILE__); |
| 26 | } |
| 27 | |
| 28 | |
| 29 | public static function get_module_dir() { |
| 30 | |
| 31 | return plugin_dir_path(__FILE__); |
| 32 | } |
| 33 | |
| 34 | |
| 35 | public static function asset_source($type = 'css', $directory = null) { |
| 36 | |
| 37 | return self::get_module_uri() . 'assets/' . $type . '/' . $directory; |
| 38 | } |
| 39 | |
| 40 | |
| 41 | public function init() { |
| 42 | |
| 43 | $this->set_attribute_types(self::PA_COLOR, esc_html__('Shopengine Color', 'shopengine')); |
| 44 | $this->set_attribute_types(self::PA_IMAGE, esc_html__('Shopengine Image', 'shopengine')); |
| 45 | $this->set_attribute_types(self::PA_LABEL, esc_html__('Shopengine Label', 'shopengine')); |
| 46 | |
| 47 | |
| 48 | //Add option to attribute....................................................... |
| 49 | add_filter('product_attributes_type_selector', [$this, 'push_attribute_types']); |
| 50 | |
| 51 | if(is_admin()) { |
| 52 | |
| 53 | add_action('admin_init', [$this, 'init_hooks']); |
| 54 | add_action('admin_print_scripts', [$this, 'enqueue']); |
| 55 | add_action('admin_init', [$this, 'includes_product']); |
| 56 | } |
| 57 | |
| 58 | |
| 59 | if(!is_admin() || (defined('DOING_AJAX') && DOING_AJAX)) { |
| 60 | |
| 61 | add_action('init', [$this, 'init_frontend_hook']); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * add swatches to product loop |
| 66 | */ |
| 67 | Shopengine_Swatches::getInstance(); |
| 68 | } |
| 69 | |
| 70 | |
| 71 | public function push_attribute_types($types) { |
| 72 | |
| 73 | $types = array_merge($types, $this->attribute_types); |
| 74 | |
| 75 | return $types; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | private function set_attribute_types($key, $title) { |
| 80 | |
| 81 | $this->attribute_types[$key] = $title; |
| 82 | |
| 83 | return $this; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | public function includes_product() { |
| 88 | |
| 89 | Admin_Product::instance()->init(); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | public function init_hooks() { |
| 94 | |
| 95 | Attribute_Hooks::instance()->init(); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | public function init_frontend_hook() { |
| 100 | |
| 101 | Frontend::instance()->init(); |
| 102 | } |
| 103 | |
| 104 | |
| 105 | public function enqueue() { |
| 106 | |
| 107 | $screen = get_current_screen(); |
| 108 | if(empty($screen)) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | if(strpos($screen->id, 'edit-pa_') === false && strpos($screen->id, 'product') === false) { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | wp_enqueue_media(); |
| 117 | wp_enqueue_style('shopengine-css-admin', Swatches::asset_source('css', 'admin.css'), ['wp-color-picker'], ShopEngine::version()); |
| 118 | wp_enqueue_script('shopengine-js-admin', Swatches::asset_source('js', 'admin.js'), ['jquery', 'wp-color-picker', 'wp-util'], ShopEngine::version(), true); |
| 119 | |
| 120 | wp_localize_script( |
| 121 | 'shopengine-js-admin', |
| 122 | 'swatch_conf', |
| 123 | [ |
| 124 | 'i18n' => [ |
| 125 | 'title' => esc_html__('Choose an image', 'shopengine'), |
| 126 | 'button' => esc_html__('Use image', 'shopengine'), |
| 127 | ], |
| 128 | 'dummy' => Helper::get_dummy(), |
| 129 | ] |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | public function get_available_types() { |
| 135 | |
| 136 | return $this->attribute_types; |
| 137 | } |
| 138 | |
| 139 | |
| 140 | public static function is_module_active() { |
| 141 | |
| 142 | // todo - implement the logic later........... |
| 143 | |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 |