Completed
Push — master ( 07eadf...5397e4 )
by Mike
20:38 queued 11s
created

WooCommerce::is_rest_api_request()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13

Duplication

Lines 13
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 13
loc 13
ccs 0
cts 6
cp 0
crap 6
rs 9.8333
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce setup
4
 *
5
 * @package WooCommerce
6
 * @since   3.2.0
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Main WooCommerce Class.
13
 *
14
 * @class WooCommerce
15
 */
16
final class WooCommerce {
17
18
	/**
19
	 * WooCommerce version.
20
	 *
21
	 * @var string
22
	 */
23
	public $version = '3.6.0';
24
25
	/**
26
	 * The single instance of the class.
27
	 *
28
	 * @var WooCommerce
29
	 * @since 2.1
30
	 */
31
	protected static $_instance = null;
32
33
	/**
34
	 * Session instance.
35
	 *
36
	 * @var WC_Session|WC_Session_Handler
37
	 */
38
	public $session = null;
39
40
	/**
41
	 * Query instance.
42
	 *
43
	 * @var WC_Query
44
	 */
45
	public $query = null;
46
47
	/**
48
	 * Product factory instance.
49
	 *
50
	 * @var WC_Product_Factory
51
	 */
52
	public $product_factory = null;
53
54
	/**
55
	 * Countries instance.
56
	 *
57
	 * @var WC_Countries
58
	 */
59
	public $countries = null;
60
61
	/**
62
	 * Integrations instance.
63
	 *
64
	 * @var WC_Integrations
65
	 */
66
	public $integrations = null;
67
68
	/**
69
	 * Cart instance.
70
	 *
71
	 * @var WC_Cart
72
	 */
73
	public $cart = null;
74
75
	/**
76
	 * Customer instance.
77
	 *
78
	 * @var WC_Customer
79
	 */
80
	public $customer = null;
81
82
	/**
83
	 * Order factory instance.
84
	 *
85
	 * @var WC_Order_Factory
86
	 */
87
	public $order_factory = null;
88
89
	/**
90
	 * Structured data instance.
91
	 *
92
	 * @var WC_Structured_Data
93
	 */
94
	public $structured_data = null;
95
96
	/**
97
	 * Array of deprecated hook handlers.
98
	 *
99
	 * @var array of WC_Deprecated_Hooks
100
	 */
101
	public $deprecated_hook_handlers = array();
102
103
	/**
104
	 * Main WooCommerce Instance.
105
	 *
106
	 * Ensures only one instance of WooCommerce is loaded or can be loaded.
107
	 *
108
	 * @since 2.1
109
	 * @static
110
	 * @see WC()
111
	 * @return WooCommerce - Main instance.
112
	 */
113 777
	public static function instance() {
114 777
		if ( is_null( self::$_instance ) ) {
115
			self::$_instance = new self();
116
		}
117 777
		return self::$_instance;
118
	}
119
120
	/**
121
	 * Cloning is forbidden.
122
	 *
123
	 * @since 2.1
124
	 */
125
	public function __clone() {
126
		wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'woocommerce' ), '2.1' );
127
	}
128
129
	/**
130
	 * Unserializing instances of this class is forbidden.
131
	 *
132
	 * @since 2.1
133
	 */
134
	public function __wakeup() {
135
		wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '2.1' );
136
	}
137
138
	/**
139
	 * Auto-load in-accessible properties on demand.
140
	 *
141
	 * @param mixed $key Key name.
142
	 * @return mixed
143
	 */
144 547
	public function __get( $key ) {
145 547
		if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) {
146 547
			return $this->$key();
147
		}
148
	}
149
150
	/**
151
	 * WooCommerce Constructor.
152
	 */
153
	public function __construct() {
154
		$this->define_constants();
155
		$this->includes();
156
		$this->init_hooks();
157
	}
158
159
	/**
160
	 * When WP has loaded all plugins, trigger the `woocommerce_loaded` hook.
161
	 *
162
	 * This ensures `woocommerce_loaded` is called only after all other plugins
163
	 * are loaded, to avoid issues caused by plugin directory naming changing
164
	 * the load order. See #21524 for details.
165
	 *
166
	 * @since 3.6.0
167
	 */
168
	public function on_plugins_loaded() {
169
		do_action( 'woocommerce_loaded' );
170
	}
171
172
	/**
173
	 * Hook into actions and filters.
174
	 *
175
	 * @since 2.3
176
	 */
177
	private function init_hooks() {
178
		register_activation_hook( WC_PLUGIN_FILE, array( 'WC_Install', 'install' ) );
179
		register_shutdown_function( array( $this, 'log_errors' ) );
180
181
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 );
182
		add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
183
		add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
184
		add_action( 'init', array( $this, 'init' ), 0 );
185
		add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
186
		add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
187
		add_action( 'init', array( $this, 'wpdb_table_fix' ), 0 );
188
		add_action( 'init', array( $this, 'add_image_sizes' ) );
189
		add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
190
	}
191
192
	/**
193
	 * Ensures fatal errors are logged so they can be picked up in the status report.
194
	 *
195
	 * @since 3.2.0
196
	 */
197
	public function log_errors() {
198
		$error = error_get_last();
199
		if ( in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
200
			$logger = wc_get_logger();
201
			$logger->critical(
202
				/* translators: 1: error message 2: file name and path 3: line number */
203
				sprintf( __( '%1$s in %2$s on line %3$s', 'woocommerce' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
204
				array(
205
					'source' => 'fatal-errors',
206
				)
207
			);
208
			do_action( 'woocommerce_shutdown_error', $error );
209
		}
210
	}
211
212
	/**
213
	 * Define WC Constants.
214
	 */
215
	private function define_constants() {
216
		$upload_dir = wp_upload_dir( null, false );
217
218
		$this->define( 'WC_ABSPATH', dirname( WC_PLUGIN_FILE ) . '/' );
219
		$this->define( 'WC_PLUGIN_BASENAME', plugin_basename( WC_PLUGIN_FILE ) );
220
		$this->define( 'WC_VERSION', $this->version );
221
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
222
		$this->define( 'WC_ROUNDING_PRECISION', 6 );
223
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
224
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
225
		$this->define( 'WC_DELIMITER', '|' );
226
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
227
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
228
		$this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
229
	}
230
231
	/**
232
	 * Define constant if not already set.
233
	 *
234
	 * @param string      $name  Constant name.
235
	 * @param string|bool $value Constant value.
236
	 */
237
	private function define( $name, $value ) {
238
		if ( ! defined( $name ) ) {
239
			define( $name, $value );
240
		}
241
	}
242
243
	/**
244
	 * Returns true if the request is a non-legacy REST API request.
245
	 *
246
	 * Legacy REST requests should still run some extra code for backwards compatibility.
247
	 *
248
	 * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061.
249
	 *
250
	 * @return bool
251
	 */
252 View Code Duplication
	public function is_rest_api_request() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
253
		if ( empty( $_SERVER['REQUEST_URI'] ) ) {
254
			return false;
255
		}
256
257
		// REST API prefix.
258
		$rest_prefix = trailingslashit( rest_get_url_prefix() );
259
260
		// Check if this is a WC endpoint.
261
		$is_woocommerce_endpoint = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix . 'wc/' ) ); // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
0 ignored issues
show
introduced by
Detected usage of a non-sanitized input variable: $_SERVER
Loading history...
262
263
		return apply_filters( 'woocommerce_is_rest_api_request', $is_woocommerce_endpoint );
264
	}
265
266
	/**
267
	 * What type of request is this?
268
	 *
269
	 * @param  string $type admin, ajax, cron or frontend.
270
	 * @return bool
271
	 */
272
	private function is_request( $type ) {
273
		switch ( $type ) {
274
			case 'admin':
275
				return is_admin();
276
			case 'ajax':
277
				return defined( 'DOING_AJAX' );
278
			case 'cron':
279
				return defined( 'DOING_CRON' );
280
			case 'frontend':
281
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request();
282
		}
283
	}
284
285
	/**
286
	 * Include required core files used in admin and on the frontend.
287
	 */
288
	public function includes() {
289
		/**
290
		 * Class autoloader.
291
		 */
292
		include_once WC_ABSPATH . 'includes/class-wc-autoloader.php';
293
294
		/**
295
		 * Interfaces.
296
		 */
297
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-abstract-order-data-store-interface.php';
298
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-coupon-data-store-interface.php';
299
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-data-store-interface.php';
300
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-data-store-interface.php';
301
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-log-data-store-interface.php';
302
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-object-data-store-interface.php';
303
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-data-store-interface.php';
304
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-data-store-interface.php';
305
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-product-data-store-interface.php';
306
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-type-data-store-interface.php';
307
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-refund-data-store-interface.php';
308
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-payment-token-data-store-interface.php';
309
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-data-store-interface.php';
310
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-variable-data-store-interface.php';
311
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-shipping-zone-data-store-interface.php';
312
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-logger-interface.php';
313
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-log-handler-interface.php';
314
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-webhooks-data-store-interface.php';
315
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-queue-interface.php';
316
317
		/**
318
		 * Abstract classes.
319
		 */
320
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php';
321
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-object-query.php';
322
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php';
323
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php';
324
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php';
325
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php';
326
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php';
327
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php';
328
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php';
329
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-log-handler.php';
330
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-deprecated-hooks.php';
331
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-session.php';
332
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-privacy.php';
333
334
		/**
335
		 * Core classes.
336
		 */
337
		include_once WC_ABSPATH . 'includes/wc-core-functions.php';
338
		include_once WC_ABSPATH . 'includes/class-wc-datetime.php';
339
		include_once WC_ABSPATH . 'includes/class-wc-post-types.php';
340
		include_once WC_ABSPATH . 'includes/class-wc-install.php';
341
		include_once WC_ABSPATH . 'includes/class-wc-geolocation.php';
342
		include_once WC_ABSPATH . 'includes/class-wc-download-handler.php';
343
		include_once WC_ABSPATH . 'includes/class-wc-comments.php';
344
		include_once WC_ABSPATH . 'includes/class-wc-post-data.php';
345
		include_once WC_ABSPATH . 'includes/class-wc-ajax.php';
346
		include_once WC_ABSPATH . 'includes/class-wc-emails.php';
347
		include_once WC_ABSPATH . 'includes/class-wc-data-exception.php';
348
		include_once WC_ABSPATH . 'includes/class-wc-query.php';
349
		include_once WC_ABSPATH . 'includes/class-wc-meta-data.php';
350
		include_once WC_ABSPATH . 'includes/class-wc-order-factory.php';
351
		include_once WC_ABSPATH . 'includes/class-wc-order-query.php';
352
		include_once WC_ABSPATH . 'includes/class-wc-product-factory.php';
353
		include_once WC_ABSPATH . 'includes/class-wc-product-query.php';
354
		include_once WC_ABSPATH . 'includes/class-wc-payment-tokens.php';
355
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zone.php';
356
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php';
357
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php';
358
		include_once WC_ABSPATH . 'includes/class-wc-countries.php';
359
		include_once WC_ABSPATH . 'includes/class-wc-integrations.php';
360
		include_once WC_ABSPATH . 'includes/class-wc-cache-helper.php';
361
		include_once WC_ABSPATH . 'includes/class-wc-https.php';
362
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php';
363
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php';
364
		include_once WC_ABSPATH . 'includes/class-wc-background-emailer.php';
365
		include_once WC_ABSPATH . 'includes/class-wc-discounts.php';
366
		include_once WC_ABSPATH . 'includes/class-wc-cart-totals.php';
367
		include_once WC_ABSPATH . 'includes/customizer/class-wc-shop-customizer.php';
368
		include_once WC_ABSPATH . 'includes/class-wc-regenerate-images.php';
369
		include_once WC_ABSPATH . 'includes/class-wc-privacy.php';
370
		include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
371
		include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
372
		include_once WC_ABSPATH . 'includes/class-wc-logger.php';
373
		include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
374
		include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
375
376
		/**
377
		 * Data stores - used to store and retrieve CRUD object data from the database.
378
		 */
379
		include_once WC_ABSPATH . 'includes/class-wc-data-store.php';
380
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-data-store-wp.php';
381
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php';
382
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-data-store-cpt.php';
383
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php';
384
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php';
385
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php';
386
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php';
387
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php';
388
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php';
389
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php';
390
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-data-store.php';
391
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php';
392
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-tax-data-store.php';
393
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-payment-token-data-store.php';
394
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php';
395
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php';
396
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-data-store.php';
397
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-log-data-store.php';
398
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-shipping-zone-data-store.php';
399
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-data-store-cpt.php';
400
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-data-store-cpt.php';
401
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-refund-data-store-cpt.php';
402
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-webhook-data-store.php';
403
404
		/**
405
		 * REST API.
406
		 */
407
		include_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-api.php';
408
		include_once WC_ABSPATH . 'includes/class-wc-api.php';
409
		include_once WC_ABSPATH . 'includes/class-wc-auth.php';
410
		include_once WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php';
411
412
		/**
413
		 * Libraries
414
		 */
415
		include_once WC_ABSPATH . 'includes/libraries/action-scheduler/action-scheduler.php';
416
417
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
418
			include_once WC_ABSPATH . 'includes/class-wc-cli.php';
419
		}
420
421
		if ( $this->is_request( 'admin' ) ) {
422
			include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
423
		}
424
425
		if ( $this->is_request( 'frontend' ) ) {
426
			$this->frontend_includes();
427
		}
428
429
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
430
			include_once WC_ABSPATH . 'includes/class-wc-tracker.php';
431
		}
432
433
		$this->theme_support_includes();
434
		$this->query = new WC_Query();
435
		$this->api   = new WC_API();
436
	}
437
438
	/**
439
	 * Include classes for theme support.
440
	 *
441
	 * @since 3.3.0
442
	 */
443
	private function theme_support_includes() {
444
		if ( wc_is_active_theme( array( 'twentynineteen', 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' ) ) ) {
445
			switch ( get_template() ) {
446
				case 'twentyten':
447
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-ten.php';
448
					break;
449
				case 'twentyeleven':
450
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-eleven.php';
451
					break;
452
				case 'twentytwelve':
453
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twelve.php';
454
					break;
455
				case 'twentythirteen':
456
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-thirteen.php';
457
					break;
458
				case 'twentyfourteen':
459
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fourteen.php';
460
					break;
461
				case 'twentyfifteen':
462
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fifteen.php';
463
					break;
464
				case 'twentysixteen':
465
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-sixteen.php';
466
					break;
467
				case 'twentyseventeen':
468
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-seventeen.php';
469
					break;
470
				case 'twentynineteen':
471
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-nineteen.php';
472
					break;
473
			}
474
		}
475
	}
476
477
	/**
478
	 * Include required frontend files.
479
	 */
480
	public function frontend_includes() {
481
		include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
482
		include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
483
		include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
484
		include_once WC_ABSPATH . 'includes/class-wc-template-loader.php';
485
		include_once WC_ABSPATH . 'includes/class-wc-frontend-scripts.php';
486
		include_once WC_ABSPATH . 'includes/class-wc-form-handler.php';
487
		include_once WC_ABSPATH . 'includes/class-wc-cart.php';
488
		include_once WC_ABSPATH . 'includes/class-wc-tax.php';
489
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zones.php';
490
		include_once WC_ABSPATH . 'includes/class-wc-customer.php';
491
		include_once WC_ABSPATH . 'includes/class-wc-embed.php';
492
		include_once WC_ABSPATH . 'includes/class-wc-session-handler.php';
493
	}
494
495
	/**
496
	 * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
497
	 */
498
	public function include_template_functions() {
499
		include_once WC_ABSPATH . 'includes/wc-template-functions.php';
500
	}
501
502
	/**
503
	 * Init WooCommerce when WordPress Initialises.
504
	 */
505
	public function init() {
506
		// Before init action.
507
		do_action( 'before_woocommerce_init' );
508
509
		// Set up localisation.
510
		$this->load_plugin_textdomain();
511
512
		// Load class instances.
513
		$this->product_factory                     = new WC_Product_Factory();
514
		$this->order_factory                       = new WC_Order_Factory();
515 1
		$this->countries                           = new WC_Countries();
516 1
		$this->integrations                        = new WC_Integrations();
517 1
		$this->structured_data                     = new WC_Structured_Data();
518
		$this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks();
519 1
		$this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks();
520 1
521 1
		// Classes/actions loaded for the frontend and for ajax requests.
522
		if ( $this->is_request( 'frontend' ) ) {
523
			// Session class, handles session data for users - can be overwritten if custom handler is needed.
524
			$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
525
			$this->session = new $session_class();
526
			$this->session->init();
527
528
			$this->customer = new WC_Customer( get_current_user_id(), true );
529
			// Cart needs the customer info.
530
			$this->cart = new WC_Cart();
531
532
			// Customer should be saved during shutdown.
533
			add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
534
		}
535
536
		$this->load_webhooks();
537
538
		// Init action.
539
		do_action( 'woocommerce_init' );
540
	}
541
542
	/**
543
	 * Load Localisation files.
544
	 *
545
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
546
	 *
547
	 * Locales found in:
548
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
549
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
550
	 */
551
	public function load_plugin_textdomain() {
552
		$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
553
		$locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
554
555
		unload_textdomain( 'woocommerce' );
556
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
557
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
558
	}
559
560
	/**
561
	 * Ensure theme and server variable compatibility and setup image sizes.
562
	 */
563
	public function setup_environment() {
564
		/**
565
		 * WC_TEMPLATE_PATH constant.
566
		 *
567
		 * @deprecated 2.2 Use WC()->template_path() instead.
568
		 */
569
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
570
571
		$this->add_thumbnail_support();
572
	}
573
574
	/**
575
	 * Ensure post thumbnail support is turned on.
576
	 */
577
	private function add_thumbnail_support() {
578 24
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
579 24
			add_theme_support( 'post-thumbnails' );
580
		}
581
		add_post_type_support( 'product', 'thumbnail' );
582
	}
583
584
	/**
585
	 * Add WC Image sizes to WP.
586
	 *
587 38
	 * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
588 38
	 * and defining an array of args. If these are not defined, we will use defaults. This is
589
	 * handled in wc_get_image_size function.
590
	 *
591
	 * 3.3 sizes:
592
	 *
593
	 * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
594
	 * woocommerce_single - Used on single product pages for the main image.
595
	 *
596 23
	 * @since 2.3
597 23
	 */
598
	public function add_image_sizes() {
599
		$thumbnail         = wc_get_image_size( 'thumbnail' );
600
		$single            = wc_get_image_size( 'single' );
601
		$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
602
603
		add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
604
		add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
605
		add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
606
607
		/**
608
		 * Legacy image sizes.
609
		 *
610
		 * @deprecated These sizes will be removed in 4.0.
611
		 */
612
		add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
613
		add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
614
		add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
615
	}
616
617
	/**
618
	 * Get the plugin url.
619
	 *
620
	 * @return string
621
	 */
622
	public function plugin_url() {
623
		return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
624
	}
625
626
	/**
627
	 * Get the plugin path.
628
	 *
629
	 * @return string
630
	 */
631
	public function plugin_path() {
632
		return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
633
	}
634
635
	/**
636
	 * Get the template path.
637
	 *
638
	 * @return string
639
	 */
640
	public function template_path() {
641
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
642
	}
643
644
	/**
645
	 * Get Ajax URL.
646
	 *
647
	 * @return string
648
	 */
649
	public function ajax_url() {
650
		return admin_url( 'admin-ajax.php', 'relative' );
651
	}
652
653
	/**
654
	 * Return the WC API URL for a given request.
655
	 *
656
	 * @param string    $request Requested endpoint.
657
	 * @param bool|null $ssl     If should use SSL, null if should auto detect. Default: null.
658
	 * @return string
659
	 */
660
	public function api_request_url( $request, $ssl = null ) {
661
		if ( is_null( $ssl ) ) {
662
			$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
663
		} elseif ( $ssl ) {
664
			$scheme = 'https';
665
		} else {
666
			$scheme = 'http';
667
		}
668
669
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
670
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
671
		} elseif ( get_option( 'permalink_structure' ) ) {
672
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
673
		} else {
674
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
675
		}
676
677
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
678
	}
679
680 1
	/**
681 1
	 * Load & enqueue active webhooks.
682
	 *
683
	 * @since 2.2
684
	 */
685
	private function load_webhooks() {
686
687
		if ( ! is_blog_installed() ) {
688
			return;
689 104
		}
690 104
691
		/**
692
		 * Hook: woocommerce_load_webhooks_limit.
693
		 *
694
		 * @since 3.6.0
695
		 * @param int $limit Used to limit how many webhooks are loaded. Default: no limit.
696
		 */
697
		$limit = apply_filters( 'woocommerce_load_webhooks_limit', null );
698 547
699 547
		wc_load_webhooks( 'active', $limit );
700
	}
701
702
	/**
703
	 * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names.
704
	 */
705
	public function wpdb_table_fix() {
706
		global $wpdb;
707 1
		$wpdb->payment_tokenmeta = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
708 1
		$wpdb->order_itemmeta    = $wpdb->prefix . 'woocommerce_order_itemmeta';
709
		$wpdb->tables[]          = 'woocommerce_payment_tokenmeta';
710
		$wpdb->tables[]          = 'woocommerce_order_itemmeta';
711
712
		if ( get_option( 'db_version' ) < 34370 ) {
713
			$wpdb->woocommerce_termmeta = $wpdb->prefix . 'woocommerce_termmeta';
714
			$wpdb->tables[]             = 'woocommerce_termmeta';
715
		}
716
	}
717
718
	/**
719
	 * Get queue instance.
720
	 *
721
	 * @return WC_Queue_Interface
722
	 */
723
	public function queue() {
724
		return WC_Queue::instance();
725
	}
726
727
	/**
728
	 * Get Checkout Class.
729
	 *
730
	 * @return WC_Checkout
731
	 */
732
	public function checkout() {
733
		return WC_Checkout::instance();
734
	}
735
736
	/**
737
	 * Get gateways class.
738
	 *
739
	 * @return WC_Payment_Gateways
740
	 */
741
	public function payment_gateways() {
742
		return WC_Payment_Gateways::instance();
743
	}
744
745
	/**
746
	 * Get shipping class.
747
	 *
748
	 * @return WC_Shipping
749
	 */
750
	public function shipping() {
751
		return WC_Shipping::instance();
752
	}
753
754
	/**
755
	 * Email Class.
756
	 *
757
	 * @return WC_Emails
758
	 */
759
	public function mailer() {
760
		return WC_Emails::instance();
761
	}
762
}
763