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