Completed
Push — master ( 07941a...292650 )
by Gerhard
23:29 queued 14:49
created

WooCommerce::define_tables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 15
ccs 0
cts 5
cp 0
crap 6
rs 9.7666
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 797
	public static function instance() {
114 797
		if ( is_null( self::$_instance ) ) {
115
			self::$_instance = new self();
116
		}
117 797
		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
			// Session class, handles session data for users - can be overwritten if custom handler is needed.
550
			$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
551
			$this->session = new $session_class();
552
			$this->session->init();
553
554
			$this->customer = new WC_Customer( get_current_user_id(), true );
555
			// Cart needs the customer info.
556
			$this->cart = new WC_Cart();
557
558
			// Customer should be saved during shutdown.
559
			add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
560
		}
561
562
		$this->load_webhooks();
563
564
		// Init action.
565
		do_action( 'woocommerce_init' );
566
	}
567
568
	/**
569
	 * Load Localisation files.
570
	 *
571
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
572
	 *
573
	 * Locales found in:
574
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
575
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
576
	 */
577 1
	public function load_plugin_textdomain() {
578 1
		$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
579 1
		$locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
580
581 1
		unload_textdomain( 'woocommerce' );
582 1
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
583 1
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
584
	}
585
586
	/**
587
	 * Ensure theme and server variable compatibility and setup image sizes.
588
	 */
589
	public function setup_environment() {
590
		/**
591
		 * WC_TEMPLATE_PATH constant.
592
		 *
593
		 * @deprecated 2.2 Use WC()->template_path() instead.
594
		 */
595
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
596
597
		$this->add_thumbnail_support();
598
	}
599
600
	/**
601
	 * Ensure post thumbnail support is turned on.
602
	 */
603
	private function add_thumbnail_support() {
604
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
605
			add_theme_support( 'post-thumbnails' );
606
		}
607
		add_post_type_support( 'product', 'thumbnail' );
608
	}
609
610
	/**
611
	 * Add WC Image sizes to WP.
612
	 *
613
	 * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
614
	 * and defining an array of args. If these are not defined, we will use defaults. This is
615
	 * handled in wc_get_image_size function.
616
	 *
617
	 * 3.3 sizes:
618
	 *
619
	 * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
620
	 * woocommerce_single - Used on single product pages for the main image.
621
	 *
622
	 * @since 2.3
623
	 */
624
	public function add_image_sizes() {
625
		$thumbnail         = wc_get_image_size( 'thumbnail' );
626
		$single            = wc_get_image_size( 'single' );
627
		$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
628
629
		add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
630
		add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
631
		add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
632
633
		/**
634
		 * Legacy image sizes.
635
		 *
636
		 * @deprecated These sizes will be removed in 4.0.
637
		 */
638
		add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
639
		add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
640
		add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
641
	}
642
643
	/**
644
	 * Get the plugin url.
645
	 *
646
	 * @return string
647
	 */
648 24
	public function plugin_url() {
649 24
		return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
650
	}
651
652
	/**
653
	 * Get the plugin path.
654
	 *
655
	 * @return string
656
	 */
657 43
	public function plugin_path() {
658 43
		return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
659
	}
660
661
	/**
662
	 * Get the template path.
663
	 *
664
	 * @return string
665
	 */
666 25
	public function template_path() {
667 25
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
668
	}
669
670
	/**
671
	 * Get Ajax URL.
672
	 *
673
	 * @return string
674
	 */
675
	public function ajax_url() {
676
		return admin_url( 'admin-ajax.php', 'relative' );
677
	}
678
679
	/**
680
	 * Return the WC API URL for a given request.
681
	 *
682
	 * @param string    $request Requested endpoint.
683
	 * @param bool|null $ssl     If should use SSL, null if should auto detect. Default: null.
684
	 * @return string
685
	 */
686
	public function api_request_url( $request, $ssl = null ) {
687
		if ( is_null( $ssl ) ) {
688
			$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
689
		} elseif ( $ssl ) {
690
			$scheme = 'https';
691
		} else {
692
			$scheme = 'http';
693
		}
694
695
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
696
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
697
		} elseif ( get_option( 'permalink_structure' ) ) {
698
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
699
		} else {
700
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
701
		}
702
703
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
704
	}
705
706
	/**
707
	 * Load & enqueue active webhooks.
708
	 *
709
	 * @since 2.2
710
	 */
711
	private function load_webhooks() {
712
713
		if ( ! is_blog_installed() ) {
714
			return;
715
		}
716
717
		/**
718
		 * Hook: woocommerce_load_webhooks_limit.
719
		 *
720
		 * @since 3.6.0
721
		 * @param int $limit Used to limit how many webhooks are loaded. Default: no limit.
722
		 */
723
		$limit = apply_filters( 'woocommerce_load_webhooks_limit', null );
724
725
		wc_load_webhooks( 'active', $limit );
726
	}
727
728
	/**
729
	 * Set tablenames inside WPDB object.
730
	 */
731
	public function wpdb_table_fix() {
732
		$this->define_tables();
733
	}
734
735
	/**
736
	 * Ran when any plugin is activated.
737
	 *
738
	 * @since 3.6.0
739
	 * @param string $filename The filename of the activated plugin.
740
	 */
741
	public function activated_plugin( $filename ) {
742
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
743
744
		WC_Helper::activated_plugin( $filename );
745
	}
746
747
	/**
748
	 * Ran when any plugin is deactivated.
749
	 *
750
	 * @since 3.6.0
751
	 * @param string $filename The filename of the deactivated plugin.
752
	 */
753
	public function deactivated_plugin( $filename ) {
754
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
755
756
		WC_Helper::deactivated_plugin( $filename );
757
	}
758
759
	/**
760
	 * Get queue instance.
761
	 *
762
	 * @return WC_Queue_Interface
763
	 */
764
	public function queue() {
765
		return WC_Queue::instance();
766
	}
767
768
	/**
769
	 * Get Checkout Class.
770
	 *
771
	 * @return WC_Checkout
772
	 */
773 1
	public function checkout() {
774 1
		return WC_Checkout::instance();
775
	}
776
777
	/**
778
	 * Get gateways class.
779
	 *
780
	 * @return WC_Payment_Gateways
781
	 */
782 102
	public function payment_gateways() {
783 102
		return WC_Payment_Gateways::instance();
784
	}
785
786
	/**
787
	 * Get shipping class.
788
	 *
789
	 * @return WC_Shipping
790
	 */
791 569
	public function shipping() {
792 569
		return WC_Shipping::instance();
793
	}
794
795
	/**
796
	 * Email Class.
797
	 *
798
	 * @return WC_Emails
799
	 */
800 1
	public function mailer() {
801 1
		return WC_Emails::instance();
802
	}
803
}
804