shopengine
Last commit date
assets
2 years ago
base
3 years ago
compatibility
3 years ago
core
2 years ago
languages
2 years ago
libs
2 years ago
modules
2 years ago
traits
3 years ago
utils
2 years ago
widgets
2 years ago
woocommerce
2 years ago
autoloader.php
4 years ago
phpcs.xml
3 years ago
plugin.php
2 years ago
readme.txt
2 years ago
shopengine.php
2 years ago
plugin.php
454 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ShopEngine; |
| 4 | |
| 5 | defined('ABSPATH') || exit; |
| 6 | |
| 7 | use ShopEngine\Compatibility\Conflicts\Manifest as Conflict_Manifest; |
| 8 | use ShopEngine\Core\Builders\Base; |
| 9 | use ShopEngine\Compatibility\Migrations\LangMigration; |
| 10 | use ShopEngine\Core\Query_Modifier; |
| 11 | use ShopEngine\Core\Wc_Customizer\Register_Settings; |
| 12 | use ShopEngine\Core\Template_Cpt; |
| 13 | use ShopEngine\Libs\License\License_Route; |
| 14 | use ShopEngine\Libs\Rating\Rating; |
| 15 | use ShopEngine\Libs\Updater\Init as Updater; |
| 16 | use ShopEngine\Modules\Manifest as Module_Manifest; |
| 17 | use ShopEngine\Widgets\Manifest; |
| 18 | |
| 19 | |
| 20 | /** |
| 21 | * Plugin final Class. |
| 22 | * Handles dynamically loading classes only when needed. Check Elementor Plugin, Woocomerce Plugin Loaded or Install. |
| 23 | * |
| 24 | * @since 1.0.0 |
| 25 | */ |
| 26 | final class Plugin { |
| 27 | |
| 28 | private static $instance; |
| 29 | |
| 30 | /** |
| 31 | * __construct function |
| 32 | * @since 1.0.0 |
| 33 | */ |
| 34 | public function __construct() { |
| 35 | // load autoload method |
| 36 | Autoloader::run(); |
| 37 | add_action( 'wp_ajax_shopengine_admin_action', [\ShopEngine\Utils\Util::class, 'shopengine_admin_action'] ); |
| 38 | } |
| 39 | |
| 40 | public function utils_url() |
| 41 | { |
| 42 | return $this->plugin_url() . 'utils/'; |
| 43 | } |
| 44 | /** |
| 45 | * Public function init. |
| 46 | * call function for all |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function init() { |
| 51 | |
| 52 | $error = false; |
| 53 | |
| 54 | // check woocommerce plugin |
| 55 | if(!did_action('woocommerce_loaded')) { |
| 56 | add_action('admin_notices', [$this, 'missing_woocommerce']); |
| 57 | |
| 58 | $error = true; |
| 59 | } |
| 60 | |
| 61 | $check_elementor_version = false; |
| 62 | |
| 63 | // Check if Elementor installed and activated. |
| 64 | if(!did_action('elementor/loaded')) { |
| 65 | |
| 66 | if(!did_action('shopengine-gutenberg-addon/before_loaded')) { |
| 67 | |
| 68 | add_action('admin_notices', [$this, 'missing_elementor']); |
| 69 | |
| 70 | $error = true; |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | // Check for required Elementor version. |
| 75 | if(did_action('elementor/loaded') && defined('ELEMENTOR_VERSION') && !version_compare(ELEMENTOR_VERSION, '3.0.0', '>=')) { |
| 76 | |
| 77 | add_action('admin_notices', [$this, 'failed_elementor_version']); |
| 78 | |
| 79 | $error = true; |
| 80 | } |
| 81 | |
| 82 | if($error) { |
| 83 | return; |
| 84 | } |
| 85 | |
| 86 | add_filter("plugin_action_links_shopengine/shopengine.php", function ($links) { |
| 87 | $free = esc_html__("Go To Shopengine","shopengine"); |
| 88 | $pro = esc_html__("Go To ShopenginePro","shopengine"); |
| 89 | |
| 90 | $custom_links[] = '<a title="' . $free . '" href="'.admin_url('edit.php?post_type=shopengine-template#getting-started').'" target="_blank">' . esc_html__('Settings', 'shopengine') . '</a>'; |
| 91 | |
| 92 | foreach ($custom_links as $custom_link): |
| 93 | array_unshift($links, $custom_link); |
| 94 | endforeach; |
| 95 | |
| 96 | if (!is_plugin_active('shopengine-pro/shopengine-pro.php')) { |
| 97 | $links[] = '<a title="' . $pro . '" href="https://wpmet.com/plugin/shopengine/pricing/" style="color:#FCB214;font-weight:700" target="_blank" rel="noopener">' . esc_html__('Go Pro', 'shopengine') . '</a>'; |
| 98 | } |
| 99 | return $links; |
| 100 | }); |
| 101 | |
| 102 | |
| 103 | /** |
| 104 | * Routes initialization |
| 105 | * |
| 106 | */ |
| 107 | new License_Route(); |
| 108 | |
| 109 | /** |
| 110 | * Run pro plugin updater here.... |
| 111 | * |
| 112 | */ |
| 113 | add_action('admin_init', function () { |
| 114 | if(class_exists('ShopEngine_Pro')) { |
| 115 | new Updater(); |
| 116 | } |
| 117 | new \ShopEngine\Compatibility\Migrations\Direct_Checkout; |
| 118 | }); |
| 119 | |
| 120 | |
| 121 | add_action('wp_loaded', function () { |
| 122 | /** |
| 123 | * migrate data |
| 124 | */ |
| 125 | LangMigration::instance()->init(); |
| 126 | }); |
| 127 | |
| 128 | // avoid themes for loading woocommerce functions |
| 129 | $avoid_themes = ['avada', 'avada child', 'woodmart', 'woodmart child']; |
| 130 | if(!in_array(strtolower(wp_get_theme()), $avoid_themes)) { |
| 131 | /** |
| 132 | * Ensuring woocommerce functions are loaded before theme is modifying those |
| 133 | * |
| 134 | */ |
| 135 | require_once WC_ABSPATH . '/includes/wc-template-functions.php'; |
| 136 | } |
| 137 | |
| 138 | |
| 139 | if(did_action('elementor/loaded')) { |
| 140 | // Load custom elementor controls |
| 141 | new \ShopEngine\Core\Elementor_Controls\Init(); |
| 142 | |
| 143 | //Loading the scripts and styles |
| 144 | add_action('elementor/editor/after_enqueue_styles', [$this, 'js_css_elementor']); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | //Loading public scripts and styles |
| 149 | add_action('wp_enqueue_scripts', [$this, 'js_css_public']); |
| 150 | |
| 151 | //woocommece theme support |
| 152 | if(!current_theme_supports('woocommerce')) { |
| 153 | add_theme_support('woocommerce'); |
| 154 | add_theme_support('wc-product-gallery-zoom'); |
| 155 | add_theme_support('wc-product-gallery-lightbox'); |
| 156 | add_theme_support('wc-product-gallery-slider'); |
| 157 | } |
| 158 | |
| 159 | |
| 160 | $filter_string = ''; // elementskit,metform-pro |
| 161 | $filter_string .= ((!in_array('elementskit/elementskit.php', apply_filters('active_plugins', get_option('active_plugins')))) ? '' : ',elementskit'); |
| 162 | $filter_string .= (!class_exists('\MetForm\Plugin') ? '' : ',metform'); |
| 163 | $filter_string .= (!class_exists('\MetForm_Pro\Plugin') ? '' : ',metform-pro'); |
| 164 | |
| 165 | #Registering new post-type & etc |
| 166 | Base::instance()->init(); |
| 167 | |
| 168 | |
| 169 | Rating::instance('shopengine') |
| 170 | ->set_plugin( 'ShopEngine', 'https://wpmet.com/wordpress.org/rating/shopengine' ) |
| 171 | ->set_plugin_logo( 'https://ps.w.org/shopengine/assets/icon-256x256.gif?rev=2505061', 'width:150px !important' ) |
| 172 | ->set_priority( 10 ) |
| 173 | ->set_first_appear_day( 7 ) |
| 174 | ->set_condition( true ) |
| 175 | ->call(); |
| 176 | |
| 177 | if ( is_admin() && \ShopEngine\Utils\Util::get_settings( 'shopengine_user_consent_for_banner', 'true' ) == 'true' ) { |
| 178 | |
| 179 | /** |
| 180 | * Show WPMET stories widget in dashboard |
| 181 | */ |
| 182 | |
| 183 | \Wpmet\Libs\Stories::instance('shopengine') |
| 184 | |
| 185 | ->set_filter($filter_string) |
| 186 | ->set_plugin('ShopEngine', 'https://wpmet.com/plugin/shopengine/') |
| 187 | ->set_api_url('https://api.wpmet.com/public/stories/') |
| 188 | ->call(); |
| 189 | |
| 190 | // banner |
| 191 | \Wpmet\Libs\Banner::instance('shopengine') |
| 192 | ->set_filter(ltrim($filter_string, ',')) |
| 193 | ->set_api_url('https://api.wpmet.com/public/jhanda') |
| 194 | ->set_plugin_screens('edit-shopengine-template') |
| 195 | ->set_plugin_screens('edit-shopengine-template') |
| 196 | ->call(); |
| 197 | } |
| 198 | |
| 199 | \ShopEngine\Core\MultiLanguage\Language::instance()->init(); |
| 200 | |
| 201 | \ShopEngine\Core\Settings\Base::instance()->init(); |
| 202 | |
| 203 | |
| 204 | new Libs\Select_Api\Base(); |
| 205 | |
| 206 | (new Module_Manifest())->init(); |
| 207 | |
| 208 | // working get instance of elementor widget |
| 209 | (new Manifest())->init(); |
| 210 | |
| 211 | Query_Modifier::instance()->init(); |
| 212 | |
| 213 | (new Conflict_Manifest())->init(); |
| 214 | |
| 215 | // view count |
| 216 | add_action('get_header', [$this, 'shopengine_track_product_views']); |
| 217 | |
| 218 | // database migrations |
| 219 | // (new \ShopEngine\Compatibility\Migrations\Migration())->init(); |
| 220 | // (new \ShopEngine\Compatibility\Migrations\Temp_Migration())->init(); |
| 221 | |
| 222 | |
| 223 | // call service providers |
| 224 | |
| 225 | $service_providers = include \ShopEngine::plugin_dir().'core/service-provider-manager.php'; |
| 226 | $method = 'init'; |
| 227 | foreach( $service_providers as $service_provider ){ |
| 228 | |
| 229 | if(class_exists($service_provider) && method_exists($service_provider, $method)) { |
| 230 | $instance = new $service_provider(); |
| 231 | $instance->$method(); |
| 232 | } |
| 233 | |
| 234 | } |
| 235 | |
| 236 | add_filter('script_loader_tag', [$this, 'filter_load_type'], 99, 3); |
| 237 | |
| 238 | //it will register an option in customizer for woocommerce products catelog. It's related with our Arcive Products widget. |
| 239 | Register_Settings::instance()->init(); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | // add async and defer attributes to enqueued scripts |
| 244 | public function filter_load_type($tag, $handle, $src) { |
| 245 | |
| 246 | if(strpos($handle, '-async') !== false) { |
| 247 | $tag = str_replace(' src', ' async="async" src', $tag); |
| 248 | } |
| 249 | |
| 250 | if(strpos($handle, '-defer') !== false) { |
| 251 | $tag = str_replace('<script ', '<script defer ', $tag); |
| 252 | } |
| 253 | |
| 254 | return $tag; |
| 255 | } |
| 256 | |
| 257 | /** |
| 258 | * Public function shopengine_track_product_views |
| 259 | * Adding Product Views Count Meta |
| 260 | */ |
| 261 | public function shopengine_track_product_views() { |
| 262 | |
| 263 | if(class_exists('WooCommerce') && !is_product()) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | $product_id = get_the_id(); |
| 268 | |
| 269 | $cookie_name = "shopengine_recent_viewed_product"; |
| 270 | |
| 271 | if(isset($_COOKIE[$cookie_name])) { |
| 272 | |
| 273 | $cookie_ids = sanitize_text_field(wp_unslash($_COOKIE[$cookie_name])); |
| 274 | $product_ids = explode(',', $cookie_ids); |
| 275 | |
| 276 | if(!is_array($product_ids)) { |
| 277 | $product_ids = [$product_ids]; |
| 278 | } |
| 279 | |
| 280 | $product_ids = array_combine($product_ids, $product_ids); |
| 281 | unset($product_ids[$product_id]); |
| 282 | $product_ids[] = $product_id; |
| 283 | |
| 284 | $cookie_value = implode(',', $product_ids); |
| 285 | |
| 286 | } else { |
| 287 | $cookie_value = $product_id; |
| 288 | } |
| 289 | |
| 290 | setcookie($cookie_name, $cookie_value, strtotime('+30 days'), '/' ); |
| 291 | |
| 292 | $count_key = 'shopengine_product_views_count'; |
| 293 | $count = get_post_meta($product_id, $count_key, true); |
| 294 | |
| 295 | if($count == '') { |
| 296 | $count = 1; |
| 297 | delete_post_meta($product_id, $count_key); |
| 298 | add_post_meta($product_id, $count_key, '1'); |
| 299 | } else { |
| 300 | $count++; |
| 301 | update_post_meta($product_id, $count_key, $count); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Public function js_css_public . |
| 307 | * Include public function |
| 308 | * |
| 309 | * @since 1.0.0 |
| 310 | */ |
| 311 | public function js_css_public() { |
| 312 | wp_register_style('shopengine-public', \ShopEngine::plugin_url() . 'assets/css/shopengine-public.css', false, \ShopEngine::version()); |
| 313 | |
| 314 | // Modal Stylesheet |
| 315 | wp_register_style('shopengine-modal-styles', \ShopEngine::plugin_url() . 'assets/css/shopengine-modal.css', false, \ShopEngine::version()); |
| 316 | |
| 317 | // Modal Script |
| 318 | wp_register_script('shopengine-modal-script', \ShopEngine::plugin_url() . 'assets/js/shopengine-modal.js', ['jquery'], \ShopEngine::version(), true); |
| 319 | |
| 320 | wp_enqueue_script('shopengine-simple-scrollbar.js-js', \ShopEngine::plugin_url() . 'assets/js/simple-scrollbar.js', [], \ShopEngine::version(), true); |
| 321 | wp_enqueue_script('shopengine-filter-js', \ShopEngine::plugin_url() . 'assets/js/filter.js', [], \ShopEngine::version(), true); |
| 322 | wp_enqueue_script('shopengine-js', \ShopEngine::plugin_url() . 'assets/js/public.js', [], \ShopEngine::version(), true); |
| 323 | |
| 324 | |
| 325 | wp_localize_script('shopengine-js', 'shopEngineApiSettings', [ |
| 326 | 'resturl' => get_rest_url(), |
| 327 | 'rest_nonce' => wp_create_nonce('wp_rest'), |
| 328 | ]); |
| 329 | |
| 330 | |
| 331 | /** |
| 332 | * Registering libs css/js |
| 333 | * |
| 334 | */ |
| 335 | |
| 336 | wp_register_style( |
| 337 | 'lib-sqv-css', |
| 338 | \ShopEngine::plugin_url() . '/assets/sqv/smart-quick-view.css', |
| 339 | [], |
| 340 | \ShopEngine::version() |
| 341 | ); |
| 342 | |
| 343 | wp_register_script( |
| 344 | 'lib-sqv-js', |
| 345 | \ShopEngine::plugin_url() . 'assets/sqv/smart-quick-view.js', |
| 346 | ['jquery', 'wc-single-product'], |
| 347 | \ShopEngine::version(), |
| 348 | true |
| 349 | ); |
| 350 | } |
| 351 | |
| 352 | public function js_css_elementor() { |
| 353 | wp_enqueue_style('shopnegine-panel-icon', \ShopEngine::plugin_url() . 'assets/css/shopengine-icon.css', false, \ShopEngine::version()); |
| 354 | |
| 355 | if('shopengine-template' === get_post_type()): |
| 356 | wp_enqueue_style('shopnegine-editor-css', \ShopEngine::plugin_url() . 'assets/css/editor.css', false, \ShopEngine::version()); |
| 357 | endif; |
| 358 | } |
| 359 | |
| 360 | |
| 361 | public function missing_woocommerce() { |
| 362 | |
| 363 | if(!current_user_can('manage_options')) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | if(file_exists(WP_PLUGIN_DIR . '/woocommerce/woocommerce.php')) { |
| 368 | |
| 369 | $btn['label'] = esc_html__('Activate WooCommerce', 'shopengine'); |
| 370 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=woocommerce/woocommerce.php&plugin_status=all&paged=1', 'activate-plugin_woocommerce/woocommerce.php'); |
| 371 | |
| 372 | } else { |
| 373 | |
| 374 | $btn['label'] = esc_html__('Install WooCommerce', 'shopengine'); |
| 375 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=woocommerce'), 'install-plugin_woocommerce'); |
| 376 | } |
| 377 | |
| 378 | Utils\Notice::push( |
| 379 | [ |
| 380 | 'id' => 'missing-woo', |
| 381 | 'type' => 'error', |
| 382 | 'dismissible' => true, |
| 383 | 'btn' => $btn, |
| 384 | 'message' => sprintf(esc_html__('ShopEngine requires woocommerce Plugin, which is currently NOT RUNNING.', 'shopengine'), '4.1.0'), |
| 385 | ] |
| 386 | ); |
| 387 | } |
| 388 | |
| 389 | |
| 390 | public function missing_elementor() { |
| 391 | |
| 392 | if(!current_user_can('manage_options')) { |
| 393 | return; |
| 394 | } |
| 395 | |
| 396 | if(file_exists(WP_PLUGIN_DIR . '/elementor/elementor.php')) { |
| 397 | |
| 398 | $btn['label'] = esc_html__('Activate Elementor', 'shopengine'); |
| 399 | $btn['url'] = wp_nonce_url('plugins.php?action=activate&plugin=elementor/elementor.php&plugin_status=all&paged=1', 'activate-plugin_elementor/elementor.php'); |
| 400 | |
| 401 | } else { |
| 402 | |
| 403 | $btn['label'] = esc_html__('Install Elementor', 'shopengine'); |
| 404 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=install-plugin&plugin=elementor'), 'install-plugin_elementor'); |
| 405 | } |
| 406 | |
| 407 | Utils\Notice::push( |
| 408 | [ |
| 409 | 'id' => 'missing-elementor', |
| 410 | 'type' => 'error', |
| 411 | 'dismissible' => true, |
| 412 | 'btn' => $btn, |
| 413 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 414 | ] |
| 415 | ); |
| 416 | } |
| 417 | |
| 418 | |
| 419 | public function failed_elementor_version() { |
| 420 | |
| 421 | if(!current_user_can('manage_options')) { |
| 422 | return; |
| 423 | } |
| 424 | |
| 425 | $btn['label'] = esc_html__('Update Elementor', 'shopengine'); |
| 426 | $btn['url'] = wp_nonce_url(self_admin_url('update.php?action=upgrade-plugin&plugin=elementor'), 'upgrade-plugin_elementor'); |
| 427 | |
| 428 | Utils\Notice::push( |
| 429 | [ |
| 430 | 'id' => 'unsupported-elementor-version', |
| 431 | 'type' => 'error', |
| 432 | 'dismissible' => true, |
| 433 | 'btn' => $btn, |
| 434 | 'message' => sprintf(esc_html__('ShopEngine requires Elementor version %1$s+, which is currently NOT RUNNING.', 'shopengine'), '3.0.0'), |
| 435 | ] |
| 436 | ); |
| 437 | } |
| 438 | |
| 439 | |
| 440 | public function flush_rewrites() { |
| 441 | $form_cpt = new Core\Builders\Cpt(); |
| 442 | $form_cpt->flush_rewrites(); |
| 443 | } |
| 444 | |
| 445 | |
| 446 | public static function instance() { |
| 447 | if(!self::$instance) { |
| 448 | self::$instance = new self(); |
| 449 | } |
| 450 | |
| 451 | return self::$instance; |
| 452 | } |
| 453 | } |
| 454 |