PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 1.9.1
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v1.9.1
2.3.0 2.2.0 2.1.1 2.1.0 2.0.0 1.10.0 1.9.1 1.9.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.4.0 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 All 59 releases
storeengine / storeengine.php

storeengine.php in StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More 1.9.1, at storeengine.php

377 lines 11.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Plugin Name: StoreEngine
4 * Plugin URI: https://storeengine.pro
5 * Description: Powerful WordPress eCommerce Plugin for Payments, Memberships, Affiliates, Sales & More
6 * Version: 1.9.1
7 * Author: Kodezen
8 * Author URI: http://kodezen.com
9 * License: GPL-3.0+
10 * License URI: http://www.gnu.org/licenses/gpl-3.0.txt
11 * Text Domain: storeengine
12 * Domain Path: /i18n/languages/
13 *
14 * Requires PHP: 7.4
15 * Requires at least: 6.5
16 * Tested up to: 6.9
17 *
18 * @package StoreEngine
19 */
20
21 use StoreEngine\ActionQueue;
22 use StoreEngine\Admin\Settings;
23 use StoreEngine\Classes\Cart;
24 use StoreEngine\Classes\Customer;
25 use StoreEngine\Classes\DownloadHandler;
26 use StoreEngine\Classes\Exceptions\StoreEngineException;
27 use StoreEngine\Classes\Logger;
28 use StoreEngine\Classes\Tax;
29 use StoreEngine\Classes\UrlPresigner;
30
31 if ( ! defined( 'ABSPATH' ) ) {
32 exit;
33 }
34
35 final class StoreEngine {
36
37 protected static ?StoreEngine $instance = null;
38
39 public ?Customer $customer = null;
40 public ?Cart $cart = null;
41
42 private function __construct() {
43 $this->define_constants();
44 $this->load_dependency();
45
46 register_activation_hook( __FILE__, [ $this, 'activate' ] );
47 register_deactivation_hook( __FILE__, [ $this, 'deactivate' ] );
48 register_shutdown_function( [ $this, 'log_errors' ] );
49
50 Settings::load_settings();
51 StoreEngine\Installer::init();
52
53 add_action( 'activated_plugin', array( $this, 'activated_redirect' ), 10, 2 );
54 add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ) );
55 add_action( 'storeengine_loaded', array( $this, 'init_plugin' ) );
56 add_action( 'init', array( $this, 'load_textdomain' ) );
57 add_action( 'init', array( $this, 'dispatch_post_hooks' ) );
58 }
59
60 public static function init(): StoreEngine {
61
62 if ( null === self::$instance ) {
63 self::$instance = new self();
64 }
65
66 return self::$instance;
67 }
68
69 public function define_constants() {
70 /**
71 * Defines CONSTANTS for Whole plugins.
72 */
73 define( 'STOREENGINE_VERSION', '1.9.1' );
74 define( 'STOREENGINE_DB_VERSION', '1.0' );
75 define( 'STOREENGINE_PLUGIN_FILE', __FILE__ );
76 define( 'STOREENGINE_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
77 define( 'STOREENGINE_PLUGIN_SLUG', 'storeengine' );
78 define( 'STOREENGINE_SETTINGS_NAME', 'storeengine_settings' );
79 define( 'STOREENGINE_ADDONS_SETTINGS_NAME', 'storeengine_addons' );
80 define( 'STOREENGINE_PAYMENTS_SETTINGS_NAME', 'storeengine_payments_settings' );
81 define( 'STOREENGINE_PLUGIN_ROOT_URI', plugins_url( '/', __FILE__ ) );
82 /** @define "STOREENGINE_ROOT_DIR_PATH" "./" */
83 define( 'STOREENGINE_ROOT_DIR_PATH', plugin_dir_path( __FILE__ ) );
84 /** @define "STOREENGINE_INCLUDES_DIR_PATH" "./includes/" */
85 define( 'STOREENGINE_INCLUDES_DIR_PATH', STOREENGINE_ROOT_DIR_PATH . 'includes/' );
86 define( 'STOREENGINE_ASSETS_DIR_PATH', STOREENGINE_ROOT_DIR_PATH . 'assets/' );
87 define( 'STOREENGINE_ASSETS_URI', STOREENGINE_PLUGIN_ROOT_URI . 'assets/' );
88 define( 'STOREENGINE_ADDONS_DIR_PATH', STOREENGINE_ROOT_DIR_PATH . 'addons/' );
89 define( 'STOREENGINE_LIBRARY_PATH', STOREENGINE_ROOT_DIR_PATH . 'libraries/' );
90 define( 'STOREENGINE_TEMPLATE_DEBUG_MODE', false );
91 /** @define "STOREENGINE_TEMPLATE_PATH" "./templates/" */
92 define( 'STOREENGINE_TEMPLATE_PATH', STOREENGINE_ROOT_DIR_PATH . 'templates/' );
93 define( 'STOREENGINE_BLOCK_TEMPLATES_DIR_PATH', STOREENGINE_ROOT_DIR_PATH . 'templates/block-templates/' );
94
95 $upload_dir = wp_get_upload_dir();
96 /** @define "STOREENGINE_SECURE_UPLOADS_DIR" "./../../uploads/storeengine_uploads" */
97 define( 'STOREENGINE_SECURE_UPLOADS_DIR', $upload_dir['basedir'] . '/storeengine_uploads' );
98 define( 'STOREENGINE_LOGS_DIR', $upload_dir['basedir'] . '/storeengine-logs' );
99 }
100
101 public function log_errors() {
102 $error = error_get_last();
103 $allowed_errors = [ E_ERROR, E_PARSE, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR ];
104
105 // Catch only severe errors that halt execution.
106 if ( $error !== null && in_array( $error['type'], $allowed_errors, true ) ) {
107
108 // Map fatal error types to readable names.
109 $error_names = [
110 E_ERROR => 'Fatal Error',
111 E_PARSE => 'Parse Error',
112 E_CORE_ERROR => 'Core Error',
113 E_COMPILE_ERROR => 'Compile Error',
114 E_USER_ERROR => 'User Error',
115 ];
116 $error_type = $error_names[ $error['type'] ] ?? 'Fatal Error';
117 $error_copy = $error;
118 $message = $error_copy['message'];
119 unset( $error_copy['message'] );
120
121 $context = [ 'error' => $error_copy ];
122
123 if ( false !== strpos( $message, 'Stack trace:' ) ) {
124 $segments = explode( 'Stack trace:', $message );
125 $message = str_replace( PHP_EOL, ' ', trim( $segments[0] ) );
126 $backtrace = array_map( 'trim', explode( PHP_EOL, $segments[1] ) );
127
128 $context['backtrace'] = $backtrace;
129 } else {
130 $context['backtrace'] = true;
131 }
132
133 // Format a descriptive title, e.g., Critical Crash: PHP Fatal Error.
134 $title = sprintf( 'Critical Crash: PHP %s', $error_type );
135
136 $content = [
137 'message' => $message,
138 'file' => $error['file'],
139 'line' => $error['line'],
140 'context' => $context,
141 ];
142
143 Logger::log( $title, $content, Logger::CRITICAL, 'system' );
144
145 /**
146 * Action triggered when there are errors during shutdown.
147 *
148 * @since 1.8.0
149 */
150 do_action( 'storeengine_shutdown_error', $error );
151 }
152 }
153
154 /**
155 * When WP has loaded all plugins, trigger the `storeengine_loaded` hook.
156 *
157 * This ensures `storeengine_loaded` is called only after all other plugins
158 * are loaded, to avoid issues caused by plugin directory naming changing
159 */
160 public function on_plugins_loaded() {
161 /**
162 * When WP has loaded all plugins, trigger the storeengine_loaded hook.
163 */
164 do_action( 'storeengine_loaded' );
165 }
166
167 /**
168 * Initialize the plugin
169 *
170 * @return void
171 */
172 public function init_plugin() {
173 //$non_existent_variable->method();
174 /**
175 * Before the plugin initialization.
176 */
177 do_action( 'storeengine/before_init' );
178 StoreEngine\Utils\Caching::init();
179 $this->load_addons();
180 StoreEngine\Payment_Gateways::init();
181 $this->dispatch_hooks();
182 /**
183 * Initialize the plugin.
184 */
185 do_action( 'storeengine_init' );
186 }
187
188 public function dispatch_hooks() {
189 DownloadHandler::init();
190 UrlPresigner::init();
191 StoreEngine\Cli::init();
192 StoreEngine\Hooks::init();
193 StoreEngine\Database::init();
194 StoreEngine\PermalinkRewrite::init();
195 StoreEngine\Shipping\Shipping::init();
196 StoreEngine\API::init();
197 StoreEngine\Ajax::init();
198 StoreEngine\Assets::init();
199 StoreEngine\Shortcode::init();
200 StoreEngine\Integrations::init();
201 StoreEngine\Migration::init();
202 StoreEngine\Miscellaneous::init();
203 StoreEngine\Schedule::init();
204 Tax::init();
205
206 if ( is_admin() ) {
207 StoreEngine\Admin::init();
208 }
209
210 StoreEngine\Frontend::init();
211 }
212
213 public function dispatch_post_hooks() {
214 StoreEngine\Post::init();
215 }
216
217 public function load_addons() {
218 StoreEngine\Addons::init();
219 }
220
221 public function load_textdomain() {
222 $locale = determine_locale();
223
224 /**
225 * Filter to adjust the plugin locale to use for translations.
226 */
227 $locale = apply_filters( 'plugin_locale', $locale, 'storeengine' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
228
229 unload_textdomain( 'storeengine', true );
230 load_textdomain( 'storeengine', WP_LANG_DIR . '/storeengine/storeengine-' . $locale . '.mo' );
231 load_plugin_textdomain( // phpcs:ignore PluginCheck.CodeAnalysis.DiscouragedFunctions.load_plugin_textdomainFound
232 'storeengine',
233 false,
234 plugin_basename( dirname( STOREENGINE_PLUGIN_FILE ) ) . '/i18n/languages'
235 );
236 }
237
238 /**
239 * Load global settings.
240 *
241 * @deprecated
242 * @see \StoreEngine\Admin\Settings::load_settings()
243 */
244 public function set_global_settings() {
245 $GLOBALS['storeengine_settings'] = apply_filters( 'storeengine_settings', json_decode( get_option( STOREENGINE_SETTINGS_NAME, '{}' ) ) );
246 $GLOBALS['storeengine_addons'] = json_decode( get_option( STOREENGINE_ADDONS_SETTINGS_NAME, '{}' ) );
247 }
248
249 public function load_dependency() {
250 // Internal Autoload
251 require_once STOREENGINE_INCLUDES_DIR_PATH . 'autoload.php';
252
253 // Autoload prefixed packages.
254 if ( file_exists( STOREENGINE_ROOT_DIR_PATH . 'vendor/prefixed/autoload.php' ) ) {
255 require_once STOREENGINE_ROOT_DIR_PATH . 'vendor/prefixed/autoload.php';
256 }
257
258 // Autoload other packages.
259 if ( file_exists( STOREENGINE_ROOT_DIR_PATH . 'vendor/autoload.php' ) ) {
260 require_once STOREENGINE_ROOT_DIR_PATH . 'vendor/autoload.php';
261 }
262
263 // Frontend Template and Hooks
264 require_once STOREENGINE_INCLUDES_DIR_PATH . 'frontend/functions.php';
265 require_once STOREENGINE_INCLUDES_DIR_PATH . 'frontend/hooks.php';
266 }
267
268 /**
269 * Initialize and load the cart functionality.
270 */
271 public function load_cart() {
272 if ( ! did_action( 'storeengine/before_init' ) || doing_action( 'storeengine/before_init' ) ) {
273 /* translators: 1: initialize_session 2: storeengine/before_init */
274 _doing_it_wrong( __FUNCTION__, sprintf( esc_html__( '%1$s should not be called before the %2$s action.', 'storeengine' ), 'storeengine_load_cart', 'storeengine/before_init' ), '1.0.0' );
275
276 return;
277 }
278
279 $this->initialize_cart();
280 }
281
282 /**
283 * Initialize the customer and cart objects and setup customer saving on shutdown.
284 *
285 * Note, storeengine()->customer is session based. Changes to customer data via this property are not persisted to the database automatically.
286 *
287 * @return void
288 */
289 public function initialize_cart() {
290 if ( did_action( 'storeengine/cart/initialized' ) && $this->customer && $this->cart ) {
291 return;
292 }
293
294 if ( ! $this->customer instanceof Customer ) {
295 $this->customer = new Customer( get_current_user_id(), true );
296 }
297
298 if ( ! $this->cart instanceof Cart ) {
299 $this->cart = Cart::init();
300 }
301
302 /**
303 * Fires after cart initialization finished.
304 */
305 do_action( 'storeengine/cart/initialized' );
306 }
307
308 /**
309 * @return ?Cart
310 */
311 public function get_cart(): ?Cart {
312 return $this->cart;
313 }
314
315 /**
316 * @return ?Customer
317 */
318 public function get_customer(): ?Customer {
319 return $this->customer;
320 }
321
322 /**
323 * Get queue instance.
324 *
325 * @return ActionQueue
326 * @throws StoreEngineException
327 */
328 public function queue(): ActionQueue {
329 return ActionQueue::get_instance();
330 }
331
332 public function activate() {
333 StoreEngine\Installer::install();
334 }
335
336 public function deactivate() {
337 StoreEngine\Installer::uninstall();
338 }
339
340 public function activated_redirect( $plugin, $network_wide = null ) {
341 if ( $network_wide || class_exists( \WP_CLI::class, false ) || STOREENGINE_PLUGIN_BASENAME !== $plugin ) {
342 return;
343 }
344
345 if ( 'yes' === get_option( 'storeengine_has_redirect_to_setup_wizard', 'no' ) ) {
346 return;
347 }
348
349 update_option( 'storeengine_has_redirect_to_setup_wizard', 'yes', false );
350 wp_safe_redirect( admin_url( 'admin.php?page=storeengine-setup' ) );
351 exit;
352 }
353 }
354
355 /**
356 * Initializes the main plugin
357 *
358 * @return StoreEngine
359 */
360 function storeengine(): StoreEngine {
361 return StoreEngine::init();
362 }
363
364 /**
365 * Initializes the main plugin
366 *
367 * @return StoreEngine
368 * @deprecated in favor of storeengine()
369 * @see storeengine()
370 */
371 function storeengine_start(): StoreEngine {
372 return storeengine();
373 }
374
375 // Plugin Start
376 storeengine();
377