Completed
Push — master ( 15aa29...17da96 )
by Claudio
18:39 queued 11s
created

WooCommerce::initialize_cart()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 5

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 0
dl 0
loc 11
ccs 6
cts 6
cp 1
crap 5
rs 9.6111
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.7.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 827
	public static function instance() {
114 827
		if ( is_null( self::$_instance ) ) {
115
			self::$_instance = new self();
116
		}
117 827
		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 98
	public function __get( $key ) {
145 98
		if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) {
146 98
			return $this->$key();
147
		}
148
	}
149
150
	/**
151
	 * WooCommerce Constructor.
152
	 */
153
	public function __construct() {
154
		$this->define_constants();
155
		$this->define_tables();
156
		$this->includes();
157
		$this->init_hooks();
158
	}
159
160
	/**
161
	 * When WP has loaded all plugins, trigger the `woocommerce_loaded` hook.
162
	 *
163
	 * This ensures `woocommerce_loaded` is called only after all other plugins
164
	 * are loaded, to avoid issues caused by plugin directory naming changing
165
	 * the load order. See #21524 for details.
166
	 *
167
	 * @since 3.6.0
168
	 */
169
	public function on_plugins_loaded() {
170
		do_action( 'woocommerce_loaded' );
171
	}
172
173
	/**
174
	 * Hook into actions and filters.
175
	 *
176
	 * @since 2.3
177
	 */
178
	private function init_hooks() {
179
		register_activation_hook( WC_PLUGIN_FILE, array( 'WC_Install', 'install' ) );
180
		register_shutdown_function( array( $this, 'log_errors' ) );
181
182
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 );
183
		add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
184
		add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
185
		add_action( 'init', array( $this, 'init' ), 0 );
186
		add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
187
		add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
188
		add_action( 'init', array( $this, 'add_image_sizes' ) );
189
		add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
190
		add_action( 'activated_plugin', array( $this, 'activated_plugin' ) );
191
		add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) );
192
	}
193
194
	/**
195
	 * Ensures fatal errors are logged so they can be picked up in the status report.
196
	 *
197
	 * @since 3.2.0
198
	 */
199
	public function log_errors() {
200
		$error = error_get_last();
201
		if ( in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
202
			$logger = wc_get_logger();
203
			$logger->critical(
204
				/* translators: 1: error message 2: file name and path 3: line number */
205
				sprintf( __( '%1$s in %2$s on line %3$s', 'woocommerce' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
206
				array(
207
					'source' => 'fatal-errors',
208
				)
209
			);
210
			do_action( 'woocommerce_shutdown_error', $error );
211
		}
212
	}
213
214
	/**
215
	 * Define WC Constants.
216
	 */
217
	private function define_constants() {
218
		$upload_dir = wp_upload_dir( null, false );
219
220
		$this->define( 'WC_ABSPATH', dirname( WC_PLUGIN_FILE ) . '/' );
221
		$this->define( 'WC_PLUGIN_BASENAME', plugin_basename( WC_PLUGIN_FILE ) );
222
		$this->define( 'WC_VERSION', $this->version );
223
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
224
		$this->define( 'WC_ROUNDING_PRECISION', 6 );
225
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
226
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
227
		$this->define( 'WC_DELIMITER', '|' );
228
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
229
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
230
		$this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
231
	}
232
233
	/**
234
	 * Register custom tables within $wpdb object.
235
	 */
236
	private function define_tables() {
237
		global $wpdb;
238
239
		// List of tables without prefixes.
240
		$tables = array(
241
			'payment_tokenmeta'      => 'woocommerce_payment_tokenmeta',
242
			'order_itemmeta'         => 'woocommerce_order_itemmeta',
243
			'wc_product_meta_lookup' => 'wc_product_meta_lookup',
244
		);
245
246
		foreach ( $tables as $name => $table ) {
247
			$wpdb->$name    = $wpdb->prefix . $table;
248
			$wpdb->tables[] = $table;
249
		}
250
	}
251
252
	/**
253
	 * Define constant if not already set.
254
	 *
255
	 * @param string      $name  Constant name.
256
	 * @param string|bool $value Constant value.
257
	 */
258
	private function define( $name, $value ) {
259
		if ( ! defined( $name ) ) {
260
			define( $name, $value );
261
		}
262
	}
263
264
	/**
265
	 * Returns true if the request is a non-legacy REST API request.
266
	 *
267
	 * Legacy REST requests should still run some extra code for backwards compatibility.
268
	 *
269
	 * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061.
270
	 *
271
	 * @return bool
272
	 */
273 19
	public function is_rest_api_request() {
274 19
		if ( empty( $_SERVER['REQUEST_URI'] ) ) {
275 19
			return false;
276
		}
277
278
		$rest_prefix         = trailingslashit( rest_get_url_prefix() );
279
		$is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ); // 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...
280
281
		return apply_filters( 'woocommerce_is_rest_api_request', $is_rest_api_request );
282
	}
283
284
	/**
285
	 * What type of request is this?
286
	 *
287
	 * @param  string $type admin, ajax, cron or frontend.
288
	 * @return bool
289
	 */
290
	private function is_request( $type ) {
291
		switch ( $type ) {
292
			case 'admin':
293
				return is_admin();
294
			case 'ajax':
295
				return defined( 'DOING_AJAX' );
296
			case 'cron':
297
				return defined( 'DOING_CRON' );
298
			case 'frontend':
299
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request();
300
		}
301
	}
302
303
	/**
304
	 * Include required core files used in admin and on the frontend.
305
	 */
306
	public function includes() {
307
		/**
308
		 * Class autoloader.
309
		 */
310
		include_once WC_ABSPATH . 'includes/class-wc-autoloader.php';
311
312
		/**
313
		 * Interfaces.
314
		 */
315
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-abstract-order-data-store-interface.php';
316
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-coupon-data-store-interface.php';
317
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-data-store-interface.php';
318
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-data-store-interface.php';
319
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-log-data-store-interface.php';
320
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-object-data-store-interface.php';
321
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-data-store-interface.php';
322
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-data-store-interface.php';
323
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-product-data-store-interface.php';
324
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-type-data-store-interface.php';
325
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-refund-data-store-interface.php';
326
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-payment-token-data-store-interface.php';
327
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-data-store-interface.php';
328
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-variable-data-store-interface.php';
329
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-shipping-zone-data-store-interface.php';
330
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-logger-interface.php';
331
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-log-handler-interface.php';
332
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-webhooks-data-store-interface.php';
333
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-queue-interface.php';
334
335
		/**
336
		 * Abstract classes.
337
		 */
338
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php';
339
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-object-query.php';
340
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php';
341
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php';
342
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php';
343
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php';
344
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php';
345
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php';
346
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php';
347
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-log-handler.php';
348
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-deprecated-hooks.php';
349
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-session.php';
350
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-privacy.php';
351
352
		/**
353
		 * Core classes.
354
		 */
355
		include_once WC_ABSPATH . 'includes/wc-core-functions.php';
356
		include_once WC_ABSPATH . 'includes/class-wc-datetime.php';
357
		include_once WC_ABSPATH . 'includes/class-wc-post-types.php';
358
		include_once WC_ABSPATH . 'includes/class-wc-install.php';
359
		include_once WC_ABSPATH . 'includes/class-wc-geolocation.php';
360
		include_once WC_ABSPATH . 'includes/class-wc-download-handler.php';
361
		include_once WC_ABSPATH . 'includes/class-wc-comments.php';
362
		include_once WC_ABSPATH . 'includes/class-wc-post-data.php';
363
		include_once WC_ABSPATH . 'includes/class-wc-ajax.php';
364
		include_once WC_ABSPATH . 'includes/class-wc-emails.php';
365
		include_once WC_ABSPATH . 'includes/class-wc-data-exception.php';
366
		include_once WC_ABSPATH . 'includes/class-wc-query.php';
367
		include_once WC_ABSPATH . 'includes/class-wc-meta-data.php';
368
		include_once WC_ABSPATH . 'includes/class-wc-order-factory.php';
369
		include_once WC_ABSPATH . 'includes/class-wc-order-query.php';
370
		include_once WC_ABSPATH . 'includes/class-wc-product-factory.php';
371
		include_once WC_ABSPATH . 'includes/class-wc-product-query.php';
372
		include_once WC_ABSPATH . 'includes/class-wc-payment-tokens.php';
373
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zone.php';
374
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php';
375
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php';
376
		include_once WC_ABSPATH . 'includes/class-wc-countries.php';
377
		include_once WC_ABSPATH . 'includes/class-wc-integrations.php';
378
		include_once WC_ABSPATH . 'includes/class-wc-cache-helper.php';
379
		include_once WC_ABSPATH . 'includes/class-wc-https.php';
380
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php';
381
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php';
382
		include_once WC_ABSPATH . 'includes/class-wc-background-emailer.php';
383
		include_once WC_ABSPATH . 'includes/class-wc-discounts.php';
384
		include_once WC_ABSPATH . 'includes/class-wc-cart-totals.php';
385
		include_once WC_ABSPATH . 'includes/customizer/class-wc-shop-customizer.php';
386
		include_once WC_ABSPATH . 'includes/class-wc-regenerate-images.php';
387
		include_once WC_ABSPATH . 'includes/class-wc-privacy.php';
388
		include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
389
		include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
390
		include_once WC_ABSPATH . 'includes/class-wc-logger.php';
391
		include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
392
		include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
393
		include_once WC_ABSPATH . 'includes/admin/marketplace-suggestions/class-wc-marketplace-updater.php';
394
395
		/**
396
		 * Data stores - used to store and retrieve CRUD object data from the database.
397
		 */
398
		include_once WC_ABSPATH . 'includes/class-wc-data-store.php';
399
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-data-store-wp.php';
400
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php';
401
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-data-store-cpt.php';
402
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php';
403
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php';
404
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php';
405
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php';
406
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php';
407
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php';
408
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php';
409
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-data-store.php';
410
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php';
411
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-tax-data-store.php';
412
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-payment-token-data-store.php';
413
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php';
414
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php';
415
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-data-store.php';
416
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-log-data-store.php';
417
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-shipping-zone-data-store.php';
418
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-data-store-cpt.php';
419
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-data-store-cpt.php';
420
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-refund-data-store-cpt.php';
421
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-webhook-data-store.php';
422
423
		/**
424
		 * REST API.
425
		 */
426
		include_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-api.php';
427
		include_once WC_ABSPATH . 'includes/class-wc-api.php';
428
		include_once WC_ABSPATH . 'includes/class-wc-auth.php';
429
		include_once WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php';
430
431
		/**
432
		 * Blocks.
433
		 */
434
		if ( file_exists( WC_ABSPATH . 'includes/blocks/class-wc-block-library.php' ) ) {
435
			include_once WC_ABSPATH . 'includes/blocks/class-wc-block-library.php';
436
		}
437
438
		/**
439
		 * Libraries
440
		 */
441
		include_once WC_ABSPATH . 'includes/libraries/action-scheduler/action-scheduler.php';
442
443
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
444
			include_once WC_ABSPATH . 'includes/class-wc-cli.php';
445
		}
446
447
		if ( $this->is_request( 'admin' ) ) {
448
			include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
449
		}
450
451
		if ( $this->is_request( 'frontend' ) ) {
452
			$this->frontend_includes();
453
		}
454
455
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
456
			include_once WC_ABSPATH . 'includes/class-wc-tracker.php';
457
		}
458
459
		$this->theme_support_includes();
460
		$this->query = new WC_Query();
461
		$this->api   = new WC_API();
462
	}
463
464
	/**
465
	 * Include classes for theme support.
466
	 *
467
	 * @since 3.3.0
468
	 */
469
	private function theme_support_includes() {
470
		if ( wc_is_active_theme( array( 'twentynineteen', 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' ) ) ) {
471
			switch ( get_template() ) {
472
				case 'twentyten':
473
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-ten.php';
474
					break;
475
				case 'twentyeleven':
476
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-eleven.php';
477
					break;
478
				case 'twentytwelve':
479
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twelve.php';
480
					break;
481
				case 'twentythirteen':
482
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-thirteen.php';
483
					break;
484
				case 'twentyfourteen':
485
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fourteen.php';
486
					break;
487
				case 'twentyfifteen':
488
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fifteen.php';
489
					break;
490
				case 'twentysixteen':
491
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-sixteen.php';
492
					break;
493
				case 'twentyseventeen':
494
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-seventeen.php';
495
					break;
496
				case 'twentynineteen':
497
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-nineteen.php';
498
					break;
499
			}
500
		}
501
	}
502
503
	/**
504
	 * Include required frontend files.
505
	 */
506
	public function frontend_includes() {
507
		include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
508
		include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
509
		include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
510
		include_once WC_ABSPATH . 'includes/class-wc-template-loader.php';
511
		include_once WC_ABSPATH . 'includes/class-wc-frontend-scripts.php';
512
		include_once WC_ABSPATH . 'includes/class-wc-form-handler.php';
513
		include_once WC_ABSPATH . 'includes/class-wc-cart.php';
514
		include_once WC_ABSPATH . 'includes/class-wc-tax.php';
515
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zones.php';
516
		include_once WC_ABSPATH . 'includes/class-wc-customer.php';
517
		include_once WC_ABSPATH . 'includes/class-wc-embed.php';
518
		include_once WC_ABSPATH . 'includes/class-wc-session-handler.php';
519
	}
520
521
	/**
522
	 * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
523
	 */
524
	public function include_template_functions() {
525
		include_once WC_ABSPATH . 'includes/wc-template-functions.php';
526
	}
527
528
	/**
529
	 * Init WooCommerce when WordPress Initialises.
530
	 */
531
	public function init() {
532
		// Before init action.
533
		do_action( 'before_woocommerce_init' );
534
535
		// Set up localisation.
536
		$this->load_plugin_textdomain();
537
538
		// Load class instances.
539
		$this->product_factory                     = new WC_Product_Factory();
540
		$this->order_factory                       = new WC_Order_Factory();
541
		$this->countries                           = new WC_Countries();
542
		$this->integrations                        = new WC_Integrations();
543
		$this->structured_data                     = new WC_Structured_Data();
544
		$this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks();
545
		$this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks();
546
547
		// Classes/actions loaded for the frontend and for ajax requests.
548
		if ( $this->is_request( 'frontend' ) ) {
549
			wc_load_cart();
550
		}
551
552
		$this->load_webhooks();
553
554
		// Init action.
555
		do_action( 'woocommerce_init' );
556
	}
557
558
	/**
559
	 * Load Localisation files.
560
	 *
561
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
562
	 *
563
	 * Locales found in:
564
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
565
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
566
	 */
567 1
	public function load_plugin_textdomain() {
568 1
		$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
569 1
		$locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
570
571 1
		unload_textdomain( 'woocommerce' );
572 1
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
573 1
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
574
	}
575
576
	/**
577
	 * Ensure theme and server variable compatibility and setup image sizes.
578
	 */
579
	public function setup_environment() {
580
		/**
581
		 * WC_TEMPLATE_PATH constant.
582
		 *
583
		 * @deprecated 2.2 Use WC()->template_path() instead.
584
		 */
585
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
586
587
		$this->add_thumbnail_support();
588
	}
589
590
	/**
591
	 * Ensure post thumbnail support is turned on.
592
	 */
593
	private function add_thumbnail_support() {
594
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
595
			add_theme_support( 'post-thumbnails' );
596
		}
597
		add_post_type_support( 'product', 'thumbnail' );
598
	}
599
600
	/**
601
	 * Add WC Image sizes to WP.
602
	 *
603
	 * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
604
	 * and defining an array of args. If these are not defined, we will use defaults. This is
605
	 * handled in wc_get_image_size function.
606
	 *
607
	 * 3.3 sizes:
608
	 *
609
	 * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
610
	 * woocommerce_single - Used on single product pages for the main image.
611
	 *
612
	 * @since 2.3
613
	 */
614
	public function add_image_sizes() {
615
		$thumbnail         = wc_get_image_size( 'thumbnail' );
616
		$single            = wc_get_image_size( 'single' );
617
		$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
618
619
		add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
620
		add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
621
		add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
622
623
		/**
624
		 * Legacy image sizes.
625
		 *
626
		 * @deprecated These sizes will be removed in 4.0.
627
		 */
628
		add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
629
		add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
630
		add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
631
	}
632
633
	/**
634
	 * Get the plugin url.
635
	 *
636
	 * @return string
637
	 */
638 24
	public function plugin_url() {
639 24
		return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
640
	}
641
642
	/**
643
	 * Get the plugin path.
644
	 *
645
	 * @return string
646
	 */
647 43
	public function plugin_path() {
648 43
		return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
649
	}
650
651
	/**
652
	 * Get the template path.
653
	 *
654
	 * @return string
655
	 */
656 25
	public function template_path() {
657 25
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
658
	}
659
660
	/**
661
	 * Get Ajax URL.
662
	 *
663
	 * @return string
664
	 */
665
	public function ajax_url() {
666
		return admin_url( 'admin-ajax.php', 'relative' );
667
	}
668
669
	/**
670
	 * Return the WC API URL for a given request.
671
	 *
672
	 * @param string    $request Requested endpoint.
673
	 * @param bool|null $ssl     If should use SSL, null if should auto detect. Default: null.
674
	 * @return string
675
	 */
676
	public function api_request_url( $request, $ssl = null ) {
677
		if ( is_null( $ssl ) ) {
678
			$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
679
		} elseif ( $ssl ) {
680
			$scheme = 'https';
681
		} else {
682
			$scheme = 'http';
683
		}
684
685
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
686
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
687
		} elseif ( get_option( 'permalink_structure' ) ) {
688
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
689
		} else {
690
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
691
		}
692
693
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
694
	}
695
696
	/**
697
	 * Load & enqueue active webhooks.
698
	 *
699
	 * @since 2.2
700
	 */
701
	private function load_webhooks() {
702
703
		if ( ! is_blog_installed() ) {
704
			return;
705
		}
706
707
		/**
708
		 * Hook: woocommerce_load_webhooks_limit.
709
		 *
710
		 * @since 3.6.0
711
		 * @param int $limit Used to limit how many webhooks are loaded. Default: no limit.
712
		 */
713
		$limit = apply_filters( 'woocommerce_load_webhooks_limit', null );
714
715
		wc_load_webhooks( 'active', $limit );
716
	}
717
718
	/**
719
	 * Initialize the customer and cart objects and setup customer saving on shutdown.
720
	 *
721
	 * @since 3.6.4
722
	 * @return void
723
	 */
724 1
	public function initialize_cart() {
725
		// Cart needs customer info.
726 1
		if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) {
727 1
			$this->customer = new WC_Customer( get_current_user_id(), true );
728
			// Customer should be saved during shutdown.
729 1
			add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
730
		}
731 1
		if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) {
732 1
			$this->cart = new WC_Cart();
733
		}
734
	}
735
736
	/**
737
	 * Initialize the session class.
738
	 *
739
	 * @since 3.6.4
740
	 * @return void
741
	 */
742 1
	public function initialize_session() {
743
		// Session class, handles session data for users - can be overwritten if custom handler is needed.
744 1
		$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
745 1
		if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) {
746 1
			$this->session = new $session_class();
747 1
			$this->session->init();
748
		}
749
	}
750
751
	/**
752
	 * Set tablenames inside WPDB object.
753
	 */
754
	public function wpdb_table_fix() {
755
		$this->define_tables();
756
	}
757
758
	/**
759
	 * Ran when any plugin is activated.
760
	 *
761
	 * @since 3.6.0
762
	 * @param string $filename The filename of the activated plugin.
763
	 */
764
	public function activated_plugin( $filename ) {
765
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
766
767
		WC_Helper::activated_plugin( $filename );
768
	}
769
770
	/**
771
	 * Ran when any plugin is deactivated.
772
	 *
773
	 * @since 3.6.0
774
	 * @param string $filename The filename of the deactivated plugin.
775
	 */
776
	public function deactivated_plugin( $filename ) {
777
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
778
779
		WC_Helper::deactivated_plugin( $filename );
780
	}
781
782
	/**
783
	 * Get queue instance.
784
	 *
785
	 * @return WC_Queue_Interface
786
	 */
787
	public function queue() {
788
		return WC_Queue::instance();
789
	}
790
791
	/**
792
	 * Get Checkout Class.
793
	 *
794
	 * @return WC_Checkout
795
	 */
796 1
	public function checkout() {
797 1
		return WC_Checkout::instance();
798
	}
799
800
	/**
801
	 * Get gateways class.
802
	 *
803
	 * @return WC_Payment_Gateways
804
	 */
805 102
	public function payment_gateways() {
806 102
		return WC_Payment_Gateways::instance();
807
	}
808
809
	/**
810
	 * Get shipping class.
811
	 *
812
	 * @return WC_Shipping
813
	 */
814 569
	public function shipping() {
815 569
		return WC_Shipping::instance();
816
	}
817
818
	/**
819
	 * Email Class.
820
	 *
821
	 * @return WC_Emails
822
	 */
823 1
	public function mailer() {
824 1
		return WC_Emails::instance();
825
	}
826
}
827