PluginProbe
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More / 1.10.0
StoreEngine — Complete eCommerce Solution with Memberships, Licensing, Affiliates & More v1.10.0
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.10.0, at storeengine.php

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