shopengine
Last commit date
assets
4 years ago
base
4 years ago
compatibility
4 years ago
core
4 years ago
languages
4 years ago
libs
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
398 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine; |
| 4 | |
| 5 | use ShopEngine\Compatibility\Conflicts\Manifest as Conflict_Manifest; |
| 6 | use ShopEngine\Core\Builders\Base; |
| 7 | use ShopEngine\Core\Query_Modifier; |
| 8 | use ShopEngine\Core\Template_Cpt; |
| 9 | use ShopEngine\Libs\License\License_Route; |
| 10 | use ShopEngine\Libs\Updater\Init as Updater; |
| 11 | use ShopEngine\Modules\Manifest as Module_Manifest; |
| 12 | use ShopEngine\Widgets\Manifest; |
| 13 | |
| 14 | defined('ABSPATH') || exit; |
| 15 | |
| 16 | /** |
| 17 | * Plugin final Class. |
| 18 | * Handles dynamically loading classes only when needed. Check Elementor Plugin, Woocomerce Plugin Loaded or Install. |
| 19 | * |
| 20 | * @since 1.0.0 |
| 21 | */ |
| 22 | final class Plugin { |
| 23 | |
| 24 | private static $instance; |
| 25 | |
| 26 | /** |
| 27 | * __construct function |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | public function __construct() { |
| 31 | // load autoload method |
| 32 | Autoloader::run(); |
| 33 | } |
| 34 | |
| 35 | |
| 36 | /** |
| 37 | * Public function init. |
| 38 | * call function for all |
| 39 | * |
| 40 | * @since 1.0.0 |
| 41 | */ |
| 42 | public function init() { |
| 43 | |
| 44 | $error = false; |
| 45 | |
| 46 | // check woocommerce plugin |
| 47 | if(!did_action('woocommerce_loaded')) { |
| 48 | add_action('admin_notices', [$this, 'missing_woocommerce']); |
| 49 | |
| 50 | $error = true; |
| 51 | } |
| 52 | |
| 53 | // Check if Elementor installed and activated. |
| 54 | if(!did_action('elementor/loaded')) { |
| 55 | add_action('admin_notices', [$this, 'missing_elementor']); |
| 56 | |
| 57 | $error = true; |
| 58 | } |
| 59 | |
| 60 | // Check for required Elementor version. |
| 61 | if(defined('ELEMENTOR_VERSION') && !version_compare(ELEMENTOR_VERSION, '3.0.0', '>=')) { |
| 62 | add_action('admin_notices', [$this, 'failed_elementor_version']); |
| 63 | |
| 64 | $error = true; |
| 65 | } |
| 66 | |
| 67 | if($error) { |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | |
| 73 | /** |
| 74 | * Routes initialization |
| 75 | * |
| 76 | */ |
| 77 | new License_Route(); |
| 78 | |
| 79 | /** |
| 80 | * Run pro plugin updater here.... |
| 81 | * |
| 82 | */ |
| 83 | add_action('admin_init', function () { |
| 84 | if(class_exists('ShopEngine_Pro')) { |
| 85 | new Updater(); |
| 86 | } |
| 87 | |
| 88 | }); |
| 89 | |
| 90 | global $pagenow; |
| 91 | |
| 92 | if(is_admin() && $pagenow == 'post.php' && $_REQUEST['action'] === 'edit') { |
| 93 | |
| 94 | $pid = $_REQUEST['post']; |
| 95 | $po = get_post($pid); |
| 96 | |
| 97 | if($po->post_type === Template_Cpt::TYPE) { |
| 98 | |
| 99 | $url = get_admin_url() . 'edit.php?post_type=' . Template_Cpt::TYPE . '#' . Template_Cpt::TYPE; |
| 100 | |
| 101 | wp_safe_redirect($url); |
| 102 | exit; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | add_action('wp_loaded', function () { |
| 107 | |
| 108 | if(isset($_REQUEST['preview']) && $_REQUEST['preview'] == 'true' && !empty($_REQUEST['preview_id'])) { |
| 109 | |
| 110 | $pid = (int)$_REQUEST['preview_id']; |
| 111 | |
| 112 | $po = get_post($pid); |
| 113 | |
| 114 | if($po->post_type === Template_Cpt::TYPE) { |
| 115 | |
| 116 | $template = \ShopEngine\Core\Builders\Templates::get_registered_template_data($pid); |
| 117 | |
| 118 | if(empty($template) || !isset($template['url'])) { |
| 119 | return; |
| 120 | } |
| 121 | |
| 122 | $param = [ |
| 123 | 'shopengine_template_id' => $pid, |
| 124 | 'preview_nonce' => wp_create_nonce('template_preview_' . $pid), |
| 125 | 'change_template' => '1', |
| 126 | ]; |
| 127 | |
| 128 | $url = \ShopEngine\Utils\Helper::add_to_url($template['url'], $param); |
| 129 | |
| 130 | wp_safe_redirect($url); |
| 131 | exit; |
| 132 | } |
| 133 | } |
| 134 | }); |
| 135 | |
| 136 | |
| 137 | // avoid themes for loading woocommerce functions |
| 138 | $avoid_themes = ['avada']; |
| 139 | |
| 140 | if( !in_array( strtolower(wp_get_theme()), $avoid_themes) ) { |
| 141 | /** |
| 142 | * Ensuring woocommerce functions are loaded before theme is modifying those |
| 143 | * |
| 144 | */ |
| 145 | require_once WC_ABSPATH . '/includes/wc-template-functions.php'; |
| 146 | } |
| 147 | |
| 148 | // Load custom elementor controls |
| 149 | new \ShopEngine\Core\Elementor_Controls\Init(); |
| 150 | |
| 151 | //Loading the scripts and styles |
| 152 | add_action('wp_enqueue_scripts', [$this, 'js_css_public']); |
| 153 | add_action('elementor/editor/after_enqueue_styles', [$this, 'js_css_elementor']); |
| 154 | |
| 155 | //woocommece theme support |
| 156 | if(!current_theme_supports('woocommerce')) { |
| 157 | add_theme_support('woocommerce'); |
| 158 | add_theme_support('wc-product-gallery-zoom'); |
| 159 | add_theme_support('wc-product-gallery-lightbox'); |
| 160 | add_theme_support('wc-product-gallery-slider'); |
| 161 | } |
| 162 | |
| 163 | |
| 164 | #Registering new post-type & etc |
| 165 | Base::instance()->init(); |
| 166 | |
| 167 | \ShopEngine\Core\Settings\Base::instance()->init(); |
| 168 | |
| 169 | (new Module_Manifest)->init(); |
| 170 | |
| 171 | // working get instance of elementor widget |
| 172 | (new Manifest)->init(); |
| 173 | |
| 174 | Query_Modifier::instance()->init(); |
| 175 | |
| 176 | (new Conflict_Manifest)->init(); |
| 177 | |
| 178 | // view count |
| 179 | add_action('get_header', [$this, 'shopengine_track_product_views']); |
| 180 | |
| 181 | // database migrations |
| 182 | // (new \ShopEngine\Compatibility\Migrations\Migration())->init(); |
| 183 | (new \ShopEngine\Compatibility\Migrations\Temp_Migration())->init(); |
| 184 | |
| 185 | add_filter('script_loader_tag', [$this, 'filter_load_type'], 99, 3); |
| 186 | } |
| 187 | |
| 188 | |
| 189 | // add async and defer attributes to enqueued scripts |
| 190 | public function filter_load_type($tag, $handle, $src) { |
| 191 | |
| 192 | if(strpos($handle, '-async') !== false) { |
| 193 | $tag = str_replace(' src', ' async="async" src', $tag); |
| 194 | } |
| 195 | |
| 196 | if(strpos($handle, '-defer') !== false) { |
| 197 | $tag = str_replace('<script ', '<script defer ', $tag); |
| 198 | } |
| 199 | |
| 200 | return $tag; |
| 201 | } |
| 202 | |
| 203 | /** |
| 204 | * Public function shopengine_track_product_views |
| 205 | * Adding Product Views Count Meta |
| 206 | */ |
| 207 | public function shopengine_track_product_views() { |
| 208 | |
| 209 | if(class_exists('WooCommerce') && !is_product()) { |
| 210 | return; |
| 211 | } |
| 212 | |
| 213 | $product_id = get_the_id(); |
| 214 | |
| 215 | $cookie_name = "shopengine_recent_viewed_product"; |
| 216 | |
| 217 | if(isset($_COOKIE[$cookie_name])) { |
| 218 | |
| 219 | $cookie_ids = $_COOKIE[$cookie_name]; |
| 220 | $product_ids = explode(',', $cookie_ids); |
| 221 | |
| 222 | if(!is_array($product_ids)) { |
| 223 | $product_ids = [$product_ids]; |
| 224 | } |
| 225 | |
| 226 | $product_ids = array_combine($product_ids, $product_ids); |
| 227 | unset($product_ids[$product_id]); |
| 228 | $product_ids[] = $product_id; |
| 229 | |
| 230 | $cookie_value = implode(',', $product_ids); |
| 231 | |
| 232 | } else { |
| 233 | $cookie_value = $product_id; |
| 234 | } |
| 235 | |
| 236 | setcookie($cookie_name, $cookie_value, strtotime('+30 days'), '/'); |
| 237 | |
| 238 | $count_key = 'shopengine_product_views_count'; |
| 239 | $count = get_post_meta($product_id, $count_key, true); |
| 240 | |
| 241 | if($count == '') { |
| 242 | $count = 1; |
| 243 | delete_post_meta($product_id, $count_key); |
| 244 | add_post_meta($product_id, $count_key, '1'); |
| 245 | } else { |
| 246 | $count++; |
| 247 | update_post_meta($product_id, $count_key, $count); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Public function js_css_public . |
| 253 | * Include public function |
| 254 | * |
| 255 | * @since 1.0.0 |
| 256 | */ |
| 257 | public function js_css_public() { |
| 258 | wp_enqueue_style('shopengine-icon', \ShopEngine::plugin_url() . 'assets/css/shopengine-icon.css', false, \ShopEngine::version()); |
| 259 | wp_enqueue_style('shopengine-simple-scrollbar-css', \ShopEngine::plugin_url() . 'assets/css/simple-scrollbar.css', false, \ShopEngine::version()); |
| 260 | wp_enqueue_style('shopengine-public-css', \ShopEngine::plugin_url() . 'assets/css/public-style.css', false, \ShopEngine::version()); |
| 261 | |
| 262 | // Modal Stylesheet |
| 263 | wp_register_style('shopengine-modal-styles', \ShopEngine::plugin_url() . 'assets/css/shopengine-modal.css', false, \ShopEngine::version()); |
| 264 | |
| 265 | // Modal Script |
| 266 | wp_register_script('shopengine-modal-script', \ShopEngine::plugin_url() . 'assets/js/shopengine-modal.js', ['jquery'], \ShopEngine::version(), true); |
| 267 | |
| 268 | wp_enqueue_script('shopengine-simple-scrollbar.js-js', \ShopEngine::plugin_url() . 'assets/js/simple-scrollbar.js', [], \ShopEngine::version(), true); |
| 269 | wp_enqueue_script('shopengine-filter-js', \ShopEngine::plugin_url() . 'assets/js/filter.js', [], \ShopEngine::version(), true); |
| 270 | wp_enqueue_script('shopengine-js', \ShopEngine::plugin_url() . 'assets/js/public.js', [], \ShopEngine::version(), true); |
| 271 | |
| 272 | |
| 273 | wp_localize_script('shopengine-js', 'shopEngineApiSettings', [ |
| 274 | 'resturl' => get_rest_url(), |
| 275 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 276 | ]); |
| 277 | |
| 278 | |
| 279 | /** |
| 280 | * Registering libs css/js |
| 281 | * |
| 282 | */ |
| 283 | |
| 284 | wp_register_style( |
| 285 | 'lib-sqv-css', |
| 286 | \ShopEngine::plugin_url() . '/assets/sqv/smart-quick-view.css', |
| 287 | [], |
| 288 | \ShopEngine::version() |
| 289 | ); |
| 290 | |
| 291 | wp_register_script( |
| 292 | 'lib-sqv-js', |
| 293 | \ShopEngine::plugin_url() . 'assets/sqv/smart-quick-view.js', |
| 294 | ['jquery', 'wc-single-product'], |
| 295 | \ShopEngine::version(), |
| 296 | true |
| 297 | ); |
| 298 | } |
| 299 | |
| 300 | public function js_css_elementor() { |
| 301 | wp_enqueue_style('shopnegine-panel-icon', \ShopEngine::plugin_url() . 'assets/css/shopengine-icon.css', false, \ShopEngine::version()); |
| 302 | |
| 303 | if('shopengine-template' === get_post_type()): |
| 304 | wp_enqueue_style('shopnegine-editor-css', \ShopEngine::plugin_url() . 'assets/css/editor.css', false, \ShopEngine::version()); |
| 305 | endif; |
| 306 | } |
| 307 | |
| 308 | |
| 309 | public function missing_woocommerce() { |
| 310 | |
| 311 | if(isset($_GET['activate'])) { |
| 312 | unset($_GET['activate']); |
| 313 | } |
| 314 | |
| 315 | if(file_exists(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php')) { |
| 316 | |
| 317 | $btn['label'] = esc_html__('Activate WooCommerce', 'shopengine'); |
| 318 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=all&paged=1', 'activate-plugin_woocommerce/woocommerce.php'); |
| 319 | |
| 320 | } else { |
| 321 | |
| 322 | $btn['label'] = esc_html__('Install WooCommerce', 'shopengine'); |
| 323 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=woocommerce'), 'install-plugin_woocommerce'); |
| 324 | } |
| 325 | |
| 326 | Utils\Notice::push( |
| 327 | [ |
| 328 | 'id' => 'missing-woo', |
| 329 | 'type' => 'error', |
| 330 | 'dismissible' => true, |
| 331 | 'btn' => $btn, |
| 332 | 'message' => sprintf(esc_html__('ShopEngine requires woocommerce Plugin, which is currently NOT RUNNING.', 'shopengine'), '4.1.0'), |
| 333 | ] |
| 334 | ); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | public function missing_elementor() { |
| 339 | |
| 340 | if(isset($_GET['activate'])) { |
| 341 | unset($_GET['activate']); |
| 342 | } |
| 343 | |
| 344 | if(file_exists(WP_PLUGIN_DIR . '/elementor/elementor.php')) { |
| 345 | |
| 346 | $btn['label'] = esc_html__('Activate Elementor', 'shopengine'); |
| 347 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=elementor/elementor.php&plugin_status=all&paged=1', 'activate-plugin_elementor/elementor.php'); |
| 348 | |
| 349 | } else { |
| 350 | |
| 351 | $btn['label'] = esc_html__('Install Elementor', 'shopengine'); |
| 352 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=elementor'), 'install-plugin_elementor'); |
| 353 | } |
| 354 | |
| 355 | Utils\Notice::push( |
| 356 | [ |
| 357 | 'id' => 'missing-elementor', |
| 358 | 'type' => 'error', |
| 359 | 'dismissible' => true, |
| 360 | 'btn' => $btn, |
| 361 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 362 | ] |
| 363 | ); |
| 364 | } |
| 365 | |
| 366 | |
| 367 | public function failed_elementor_version() { |
| 368 | |
| 369 | $btn['label'] = esc_html__('Update Elementor', 'shopengine'); |
| 370 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=elementor'), 'upgrade-plugin_elementor'); |
| 371 | |
| 372 | Utils\Notice::push( |
| 373 | [ |
| 374 | 'id' => 'unsupported-elementor-version', |
| 375 | 'type' => 'error', |
| 376 | 'dismissible' => true, |
| 377 | 'btn' => $btn, |
| 378 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 379 | ] |
| 380 | ); |
| 381 | } |
| 382 | |
| 383 | |
| 384 | public function flush_rewrites() { |
| 385 | $form_cpt = new Core\Builders\Cpt(); |
| 386 | $form_cpt->flush_rewrites(); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | public static function instance() { |
| 391 | if(!self::$instance) { |
| 392 | self::$instance = new self(); |
| 393 | } |
| 394 | |
| 395 | return self::$instance; |
| 396 | } |
| 397 | } |
| 398 |