shopengine
Last commit date
assets
4 years ago
base
4 years ago
compatibility
4 years ago
core
4 years ago
languages
4 years ago
modules
4 years ago
traits
5 years ago
utils
4 years ago
widgets
4 years ago
autoloader.php
5 years ago
plugin.php
4 years ago
readme.txt
4 years ago
shopengine.php
4 years ago
plugin.php
255 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine; |
| 4 | |
| 5 | use ShopEngine\Core\Builders\Base; |
| 6 | use ShopEngine\Core\Query_Modifier; |
| 7 | use ShopEngine\Modules\Manifest as Module_Manifest; |
| 8 | use ShopEngine\Compatibility\Conflicts\Manifest as Conflict_Manifest; |
| 9 | use ShopEngine\Widgets\Manifest; |
| 10 | |
| 11 | defined('ABSPATH') || exit; |
| 12 | |
| 13 | /** |
| 14 | * Plugin final Class. |
| 15 | * Handles dynamically loading classes only when needed. Check Elementor Plugin, Woocomerce Plugin Loaded or Install. |
| 16 | * |
| 17 | * @since 1.0.0 |
| 18 | */ |
| 19 | final class Plugin { |
| 20 | |
| 21 | private static $instance; |
| 22 | |
| 23 | /** |
| 24 | * __construct function |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | // load autoload method |
| 29 | Autoloader::run(); |
| 30 | } |
| 31 | |
| 32 | |
| 33 | /** |
| 34 | * Public function init. |
| 35 | * call function for all |
| 36 | * |
| 37 | * @since 1.0.0 |
| 38 | */ |
| 39 | public function init() { |
| 40 | |
| 41 | $error = false; |
| 42 | |
| 43 | // check woocommerce plugin |
| 44 | if(!did_action('woocommerce_loaded')) { |
| 45 | add_action('admin_notices', array($this, 'missing_woocommerce')); |
| 46 | |
| 47 | $error = true; |
| 48 | } |
| 49 | |
| 50 | // Check if Elementor installed and activated. |
| 51 | if(!did_action('elementor/loaded')) { |
| 52 | add_action('admin_notices', array($this, 'missing_elementor')); |
| 53 | |
| 54 | $error = true; |
| 55 | } |
| 56 | |
| 57 | // Check for required Elementor version. |
| 58 | if(defined('ELEMENTOR_VERSION') && !version_compare(ELEMENTOR_VERSION, '3.0.0', '>=')) { |
| 59 | add_action('admin_notices', array($this, 'failed_elementor_version')); |
| 60 | |
| 61 | $error = true; |
| 62 | } |
| 63 | |
| 64 | if($error) { |
| 65 | |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | |
| 70 | /** |
| 71 | * Ensuring woocommerce functions are loaded before theme is modifying those |
| 72 | * |
| 73 | */ |
| 74 | require_once WC_ABSPATH . '/includes/wc-template-functions.php'; |
| 75 | |
| 76 | |
| 77 | // Load custom elementor controls |
| 78 | new \ShopEngine\Core\Elementor_Controls\Init(); |
| 79 | |
| 80 | //Loading the scripts and styles |
| 81 | add_action('wp_enqueue_scripts', [$this, 'js_css_public']); |
| 82 | add_action( 'elementor/editor/after_enqueue_styles', [$this, 'js_css_elementor'] ); |
| 83 | |
| 84 | //woocommece theme support |
| 85 | if( !current_theme_supports('woocommerce') ) { |
| 86 | add_theme_support( 'woocommerce' ); |
| 87 | add_theme_support( 'wc-product-gallery-zoom' ); |
| 88 | add_theme_support( 'wc-product-gallery-lightbox' ); |
| 89 | add_theme_support( 'wc-product-gallery-slider' ); |
| 90 | } |
| 91 | |
| 92 | |
| 93 | #Registering new post-type & etc |
| 94 | Base::instance()->init(); |
| 95 | |
| 96 | \ShopEngine\Core\Settings\Base::instance()->init(); |
| 97 | |
| 98 | (new Module_Manifest)->init(); |
| 99 | |
| 100 | // working get instance of elementor widget |
| 101 | (new Manifest)->init(); |
| 102 | |
| 103 | Query_Modifier::instance()->init(); |
| 104 | |
| 105 | (new Conflict_Manifest)->init(); |
| 106 | |
| 107 | // view count |
| 108 | add_action( 'wp_head', [$this, 'shopengine_track_product_views']); |
| 109 | |
| 110 | // database migrations |
| 111 | // (new \ShopEngine\Compatibility\Migrations\Migration())->init(); |
| 112 | } |
| 113 | |
| 114 | |
| 115 | /** |
| 116 | * Public function shopengine_track_product_views |
| 117 | * Adding Product Views Count Meta |
| 118 | */ |
| 119 | public function shopengine_track_product_views($product_id){ |
| 120 | if ( class_exists('WooCommerce') && !is_product() ) return; |
| 121 | $product_id = get_the_id(); |
| 122 | |
| 123 | $count_key = 'shopengine_product_views_count'; |
| 124 | $count = get_post_meta($product_id, $count_key, true); |
| 125 | if($count==''){ |
| 126 | $count = 1; |
| 127 | delete_post_meta($product_id, $count_key); |
| 128 | add_post_meta($product_id, $count_key, '1'); |
| 129 | }else{ |
| 130 | $count++; |
| 131 | update_post_meta($product_id, $count_key, $count); |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Public function js_css_public . |
| 137 | * Include public function |
| 138 | * |
| 139 | * @since 1.0.0 |
| 140 | */ |
| 141 | public function js_css_public() { |
| 142 | wp_enqueue_style('shopengine-icon', \ShopEngine::plugin_url() . 'assets/css/shopengine-icon.css', false, \ShopEngine::version()); |
| 143 | wp_enqueue_style('shopengine-simple-scrollbar-css', \ShopEngine::plugin_url() . 'assets/css/simple-scrollbar.css', false, \ShopEngine::version()); |
| 144 | wp_enqueue_style('shopengine-public-css', \ShopEngine::plugin_url() . 'assets/css/public-style.css', false, \ShopEngine::version()); |
| 145 | wp_enqueue_script('shopengine-simple-scrollbar.js-js', \ShopEngine::plugin_url() . 'assets/js/simple-scrollbar.js', array(), \ShopEngine::version(), true); |
| 146 | wp_enqueue_script('shopengine-filter-js', \ShopEngine::plugin_url() . 'assets/js/filter.js', array(), \ShopEngine::version(), true); |
| 147 | wp_enqueue_script('shopengine-js', \ShopEngine::plugin_url() . 'assets/js/public.js', array(), \ShopEngine::version(), true); |
| 148 | |
| 149 | wp_localize_script('shopengine-js', 'shopEngineApiSettings', [ |
| 150 | 'resturl' => get_rest_url(), |
| 151 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 152 | ]); |
| 153 | } |
| 154 | |
| 155 | public function js_css_elementor() { |
| 156 | wp_enqueue_style('shopnegine-panel-icon', \ShopEngine::plugin_url() . 'assets/css/shopengine-icon.css', false, \ShopEngine::version()); |
| 157 | |
| 158 | if ( 'shopengine-template' === get_post_type() ): |
| 159 | wp_enqueue_style('shopnegine-editor-css', \ShopEngine::plugin_url() . 'assets/css/editor.css', false, \ShopEngine::version()); |
| 160 | endif; |
| 161 | } |
| 162 | |
| 163 | |
| 164 | public function missing_woocommerce() { |
| 165 | |
| 166 | if(isset($_GET['activate'])) { |
| 167 | unset($_GET['activate']); |
| 168 | } |
| 169 | |
| 170 | if(file_exists(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php')) { |
| 171 | |
| 172 | $btn['label'] = esc_html__('Activate Woocommerce', 'shopengine'); |
| 173 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=all&paged=1', 'activate-plugin_woocommerce/woocommerce.php'); |
| 174 | |
| 175 | } else { |
| 176 | |
| 177 | $btn['label'] = esc_html__('Install Woocommerce', 'shopengine'); |
| 178 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=woocommerce'), 'install-plugin_woocommerce'); |
| 179 | } |
| 180 | |
| 181 | Utils\Notice::push( |
| 182 | [ |
| 183 | 'id' => 'missing-woo', |
| 184 | 'type' => 'error', |
| 185 | 'dismissible' => true, |
| 186 | 'btn' => $btn, |
| 187 | 'message' => sprintf(esc_html__('ShopEngine requires woocommerce Plugin, which is currently NOT RUNNING.', 'shopengine'), '4.1.0'), |
| 188 | ] |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | |
| 193 | public function missing_elementor() { |
| 194 | |
| 195 | if(isset($_GET['activate'])) { |
| 196 | unset($_GET['activate']); |
| 197 | } |
| 198 | |
| 199 | if(file_exists(WP_PLUGIN_DIR . '/elementor/elementor.php')) { |
| 200 | |
| 201 | $btn['label'] = esc_html__('Activate Elementor', 'shopengine'); |
| 202 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=elementor/elementor.php&plugin_status=all&paged=1', 'activate-plugin_elementor/elementor.php'); |
| 203 | |
| 204 | } else { |
| 205 | |
| 206 | $btn['label'] = esc_html__('Install Elementor', 'shopengine'); |
| 207 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=elementor'), 'install-plugin_elementor'); |
| 208 | } |
| 209 | |
| 210 | Utils\Notice::push( |
| 211 | [ |
| 212 | 'id' => 'missing-elementor', |
| 213 | 'type' => 'error', |
| 214 | 'dismissible' => true, |
| 215 | 'btn' => $btn, |
| 216 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 217 | ] |
| 218 | ); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | public function failed_elementor_version() { |
| 223 | |
| 224 | $btn['label'] = esc_html__('Update Elementor', 'shopengine'); |
| 225 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=elementor'), 'upgrade-plugin_elementor'); |
| 226 | |
| 227 | Utils\Notice::push( |
| 228 | [ |
| 229 | 'id' => 'unsupported-elementor-version', |
| 230 | 'type' => 'error', |
| 231 | 'dismissible' => true, |
| 232 | 'btn' => $btn, |
| 233 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 234 | ] |
| 235 | ); |
| 236 | } |
| 237 | |
| 238 | |
| 239 | |
| 240 | public function flush_rewrites() { |
| 241 | $form_cpt = new Core\Builders\Cpt(); |
| 242 | $form_cpt->flush_rewrites(); |
| 243 | } |
| 244 | |
| 245 | |
| 246 | public static function instance() { |
| 247 | if(!self::$instance) { |
| 248 | self::$instance = new self(); |
| 249 | } |
| 250 | |
| 251 | return self::$instance; |
| 252 | } |
| 253 | |
| 254 | } |
| 255 |