Completed
Push — master ( c63cd8...c4c8fb )
by Claudio
10:23
created

WooCommerce::includes()   B

Complexity

Conditions 7
Paths 16

Size

Total Lines 153

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

Changes 0
Metric Value
cc 7
nc 16
nop 0
dl 0
loc 153
ccs 0
cts 116
cp 0
crap 56
rs 7.0666
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 407
	public static function instance() {
114 407
		if ( is_null( self::$_instance ) ) {
115
			self::$_instance = new self();
116
		}
117 407
		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 60
	public function __get( $key ) {
145 60
		if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) {
146 60
			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( 'admin_notices', array( $this, 'build_dependencies_notice' ) );
184
		add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
185
		add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
186
		add_action( 'init', array( $this, 'init' ), 0 );
187
		add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
188
		add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
189
		add_action( 'init', array( $this, 'add_image_sizes' ) );
190
		add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
191
		add_action( 'activated_plugin', array( $this, 'activated_plugin' ) );
192
		add_action( 'deactivated_plugin', array( $this, 'deactivated_plugin' ) );
193
	}
194
195
	/**
196
	 * Ensures fatal errors are logged so they can be picked up in the status report.
197
	 *
198
	 * @since 3.2.0
199
	 */
200
	public function log_errors() {
201
		$error = error_get_last();
202
		if ( in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
203
			$logger = wc_get_logger();
204
			$logger->critical(
205
				/* translators: 1: error message 2: file name and path 3: line number */
206
				sprintf( __( '%1$s in %2$s on line %3$s', 'woocommerce' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
207
				array(
208
					'source' => 'fatal-errors',
209
				)
210
			);
211
			do_action( 'woocommerce_shutdown_error', $error );
212
		}
213
	}
214
215
	/**
216
	 * Define WC Constants.
217
	 */
218
	private function define_constants() {
219
		$upload_dir = wp_upload_dir( null, false );
220
221
		$this->define( 'WC_ABSPATH', dirname( WC_PLUGIN_FILE ) . '/' );
222
		$this->define( 'WC_PLUGIN_BASENAME', plugin_basename( WC_PLUGIN_FILE ) );
223
		$this->define( 'WC_VERSION', $this->version );
224
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
225
		$this->define( 'WC_ROUNDING_PRECISION', 6 );
226
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
227
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
228
		$this->define( 'WC_DELIMITER', '|' );
229
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
230
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
231
		$this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
232
		$this->define( 'WC_NOTICE_MIN_PHP_VERSION', '5.6.20' );
233
		$this->define( 'WC_NOTICE_MIN_WP_VERSION', '4.9' );
234
	}
235
236
	/**
237
	 * Register custom tables within $wpdb object.
238
	 */
239 2
	private function define_tables() {
240
		global $wpdb;
241
242
		// List of tables without prefixes.
243
		$tables = array(
244 2
			'payment_tokenmeta'      => 'woocommerce_payment_tokenmeta',
245
			'order_itemmeta'         => 'woocommerce_order_itemmeta',
246
			'wc_product_meta_lookup' => 'wc_product_meta_lookup',
247
			'wc_tax_rate_classes'    => 'wc_tax_rate_classes',
248
		);
249
250 2
		foreach ( $tables as $name => $table ) {
251 2
			$wpdb->$name    = $wpdb->prefix . $table;
252 2
			$wpdb->tables[] = $table;
253
		}
254
	}
255
256
	/**
257
	 * Define constant if not already set.
258
	 *
259
	 * @param string      $name  Constant name.
260
	 * @param string|bool $value Constant value.
261
	 */
262
	private function define( $name, $value ) {
263
		if ( ! defined( $name ) ) {
264
			define( $name, $value );
265
		}
266
	}
267
268
	/**
269
	 * Returns true if the request is a non-legacy REST API request.
270
	 *
271
	 * Legacy REST requests should still run some extra code for backwards compatibility.
272
	 *
273
	 * @todo: replace this function once core WP function is available: https://core.trac.wordpress.org/ticket/42061.
274
	 *
275
	 * @return bool
276
	 */
277 19
	public function is_rest_api_request() {
278 19
		if ( empty( $_SERVER['REQUEST_URI'] ) ) {
279 19
			return false;
280
		}
281
282
		$rest_prefix         = trailingslashit( rest_get_url_prefix() );
283
		$is_rest_api_request = ( false !== strpos( $_SERVER['REQUEST_URI'], $rest_prefix ) ); // phpcs:disable WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
284
285
		return apply_filters( 'woocommerce_is_rest_api_request', $is_rest_api_request );
286
	}
287
288
	/**
289
	 * What type of request is this?
290
	 *
291
	 * @param  string $type admin, ajax, cron or frontend.
292
	 * @return bool
293
	 */
294
	private function is_request( $type ) {
295
		switch ( $type ) {
296
			case 'admin':
297
				return is_admin();
298
			case 'ajax':
299
				return defined( 'DOING_AJAX' );
300
			case 'cron':
301
				return defined( 'DOING_CRON' );
302
			case 'frontend':
303
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! $this->is_rest_api_request();
304
		}
305
	}
306
307
	/**
308
	 * Include required core files used in admin and on the frontend.
309
	 */
310
	public function includes() {
311
		/**
312
		 * Class autoloader.
313
		 */
314
		include_once WC_ABSPATH . 'includes/class-wc-autoloader.php';
315
316
		/**
317
		 * Interfaces.
318
		 */
319
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-abstract-order-data-store-interface.php';
320
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-coupon-data-store-interface.php';
321
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-data-store-interface.php';
322
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-data-store-interface.php';
323
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-log-data-store-interface.php';
324
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-object-data-store-interface.php';
325
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-data-store-interface.php';
326
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-data-store-interface.php';
327
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-product-data-store-interface.php';
328
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-type-data-store-interface.php';
329
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-refund-data-store-interface.php';
330
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-payment-token-data-store-interface.php';
331
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-data-store-interface.php';
332
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-variable-data-store-interface.php';
333
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-shipping-zone-data-store-interface.php';
334
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-logger-interface.php';
335
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-log-handler-interface.php';
336
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-webhooks-data-store-interface.php';
337
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-queue-interface.php';
338
339
		/**
340
		 * Abstract classes.
341
		 */
342
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php';
343
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-object-query.php';
344
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php';
345
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php';
346
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php';
347
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php';
348
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php';
349
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php';
350
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php';
351
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-log-handler.php';
352
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-deprecated-hooks.php';
353
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-session.php';
354
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-privacy.php';
355
356
		/**
357
		 * Core classes.
358
		 */
359
		include_once WC_ABSPATH . 'includes/wc-core-functions.php';
360
		include_once WC_ABSPATH . 'includes/class-wc-datetime.php';
361
		include_once WC_ABSPATH . 'includes/class-wc-post-types.php';
362
		include_once WC_ABSPATH . 'includes/class-wc-install.php';
363
		include_once WC_ABSPATH . 'includes/class-wc-geolocation.php';
364
		include_once WC_ABSPATH . 'includes/class-wc-download-handler.php';
365
		include_once WC_ABSPATH . 'includes/class-wc-comments.php';
366
		include_once WC_ABSPATH . 'includes/class-wc-post-data.php';
367
		include_once WC_ABSPATH . 'includes/class-wc-ajax.php';
368
		include_once WC_ABSPATH . 'includes/class-wc-emails.php';
369
		include_once WC_ABSPATH . 'includes/class-wc-data-exception.php';
370
		include_once WC_ABSPATH . 'includes/class-wc-query.php';
371
		include_once WC_ABSPATH . 'includes/class-wc-meta-data.php';
372
		include_once WC_ABSPATH . 'includes/class-wc-order-factory.php';
373
		include_once WC_ABSPATH . 'includes/class-wc-order-query.php';
374
		include_once WC_ABSPATH . 'includes/class-wc-product-factory.php';
375
		include_once WC_ABSPATH . 'includes/class-wc-product-query.php';
376
		include_once WC_ABSPATH . 'includes/class-wc-payment-tokens.php';
377
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zone.php';
378
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php';
379
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php';
380
		include_once WC_ABSPATH . 'includes/class-wc-countries.php';
381
		include_once WC_ABSPATH . 'includes/class-wc-integrations.php';
382
		include_once WC_ABSPATH . 'includes/class-wc-cache-helper.php';
383
		include_once WC_ABSPATH . 'includes/class-wc-https.php';
384
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php';
385
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php';
386
		include_once WC_ABSPATH . 'includes/class-wc-background-emailer.php';
387
		include_once WC_ABSPATH . 'includes/class-wc-discounts.php';
388
		include_once WC_ABSPATH . 'includes/class-wc-cart-totals.php';
389
		include_once WC_ABSPATH . 'includes/customizer/class-wc-shop-customizer.php';
390
		include_once WC_ABSPATH . 'includes/class-wc-regenerate-images.php';
391
		include_once WC_ABSPATH . 'includes/class-wc-privacy.php';
392
		include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
393
		include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
394
		include_once WC_ABSPATH . 'includes/class-wc-logger.php';
395
		include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
396
		include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
397
		include_once WC_ABSPATH . 'includes/admin/marketplace-suggestions/class-wc-marketplace-updater.php';
398
399
		/**
400
		 * Data stores - used to store and retrieve CRUD object data from the database.
401
		 */
402
		include_once WC_ABSPATH . 'includes/class-wc-data-store.php';
403
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-data-store-wp.php';
404
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php';
405
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-data-store-cpt.php';
406
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php';
407
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php';
408
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php';
409
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php';
410
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php';
411
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php';
412
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php';
413
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-data-store.php';
414
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php';
415
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-tax-data-store.php';
416
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-payment-token-data-store.php';
417
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php';
418
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php';
419
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-data-store.php';
420
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-log-data-store.php';
421
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-shipping-zone-data-store.php';
422
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-data-store-cpt.php';
423
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-data-store-cpt.php';
424
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-refund-data-store-cpt.php';
425
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-webhook-data-store.php';
426
427
		/**
428
		 * REST API.
429
		 */
430
		include_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-api.php';
431
		include_once WC_ABSPATH . 'includes/class-wc-api.php';
432
		include_once WC_ABSPATH . 'includes/class-wc-rest-authentication.php';
433
		include_once WC_ABSPATH . 'includes/class-wc-rest-exception.php';
434
		include_once WC_ABSPATH . 'includes/class-wc-auth.php';
435
		include_once WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php';
436
437
		/**
438
		 * Libraries
439
		 */
440
		include_once WC_ABSPATH . 'includes/libraries/action-scheduler/action-scheduler.php';
441
442
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
443
			include_once WC_ABSPATH . 'includes/class-wc-cli.php';
444
		}
445
446
		if ( $this->is_request( 'admin' ) ) {
447
			include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
448
		}
449
450
		if ( $this->is_request( 'frontend' ) ) {
451
			$this->frontend_includes();
452
		}
453
454
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
455
			include_once WC_ABSPATH . 'includes/class-wc-tracker.php';
456
		}
457
458
		$this->theme_support_includes();
459
		$this->query = new WC_Query();
460
		$this->api   = new WC_API();
461
		$this->api->init();
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
		if ( function_exists( 'determine_locale' ) ) {
569 1
			$locale = determine_locale();
570
		} else {
571
			// @todo Remove when start supporting WP 5.0 or later.
572
			$locale = is_admin() ? get_user_locale() : get_locale();
573
		}
574
575 1
		$locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
576
577 1
		unload_textdomain( 'woocommerce' );
578 1
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
579 1
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
580
	}
581
582
	/**
583
	 * Ensure theme and server variable compatibility and setup image sizes.
584
	 */
585
	public function setup_environment() {
586
		/**
587
		 * WC_TEMPLATE_PATH constant.
588
		 *
589
		 * @deprecated 2.2 Use WC()->template_path() instead.
590
		 */
591
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
592
593
		$this->add_thumbnail_support();
594
	}
595
596
	/**
597
	 * Ensure post thumbnail support is turned on.
598
	 */
599
	private function add_thumbnail_support() {
600
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
601
			add_theme_support( 'post-thumbnails' );
602
		}
603
		add_post_type_support( 'product', 'thumbnail' );
604
	}
605
606
	/**
607
	 * Add WC Image sizes to WP.
608
	 *
609
	 * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
610
	 * and defining an array of args. If these are not defined, we will use defaults. This is
611
	 * handled in wc_get_image_size function.
612
	 *
613
	 * 3.3 sizes:
614
	 *
615
	 * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
616
	 * woocommerce_single - Used on single product pages for the main image.
617
	 *
618
	 * @since 2.3
619
	 */
620
	public function add_image_sizes() {
621
		$thumbnail         = wc_get_image_size( 'thumbnail' );
622
		$single            = wc_get_image_size( 'single' );
623
		$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
624
625
		add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
626
		add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
627
		add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
628
629
		/**
630
		 * Legacy image sizes.
631
		 *
632
		 * @deprecated These sizes will be removed in 4.0.
633
		 */
634
		add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
635
		add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
636
		add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
637
	}
638
639
	/**
640
	 * Get the plugin url.
641
	 *
642
	 * @return string
643
	 */
644 4
	public function plugin_url() {
645 4
		return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
646
	}
647
648
	/**
649
	 * Get the plugin path.
650
	 *
651
	 * @return string
652
	 */
653 27
	public function plugin_path() {
654 27
		return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
655
	}
656
657
	/**
658
	 * Get the template path.
659
	 *
660
	 * @return string
661
	 */
662 9
	public function template_path() {
663 9
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
664
	}
665
666
	/**
667
	 * Get Ajax URL.
668
	 *
669
	 * @return string
670
	 */
671
	public function ajax_url() {
672
		return admin_url( 'admin-ajax.php', 'relative' );
673
	}
674
675
	/**
676
	 * Return the WC API URL for a given request.
677
	 *
678
	 * @param string    $request Requested endpoint.
679
	 * @param bool|null $ssl     If should use SSL, null if should auto detect. Default: null.
680
	 * @return string
681
	 */
682
	public function api_request_url( $request, $ssl = null ) {
683
		if ( is_null( $ssl ) ) {
684
			$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
685
		} elseif ( $ssl ) {
686
			$scheme = 'https';
687
		} else {
688
			$scheme = 'http';
689
		}
690
691
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
692
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
693
		} elseif ( get_option( 'permalink_structure' ) ) {
694
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
695
		} else {
696
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
697
		}
698
699
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
700
	}
701
702
	/**
703
	 * Load & enqueue active webhooks.
704
	 *
705
	 * @since 2.2
706
	 */
707
	private function load_webhooks() {
708
709
		if ( ! is_blog_installed() ) {
710
			return;
711
		}
712
713
		/**
714
		 * Hook: woocommerce_load_webhooks_limit.
715
		 *
716
		 * @since 3.6.0
717
		 * @param int $limit Used to limit how many webhooks are loaded. Default: no limit.
718
		 */
719
		$limit = apply_filters( 'woocommerce_load_webhooks_limit', null );
720
721
		wc_load_webhooks( 'active', $limit );
722
	}
723
724
	/**
725
	 * Initialize the customer and cart objects and setup customer saving on shutdown.
726
	 *
727
	 * @since 3.6.4
728
	 * @return void
729
	 */
730 1
	public function initialize_cart() {
731
		// Cart needs customer info.
732 1
		if ( is_null( $this->customer ) || ! $this->customer instanceof WC_Customer ) {
733 1
			$this->customer = new WC_Customer( get_current_user_id(), true );
734
			// Customer should be saved during shutdown.
735 1
			add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
736
		}
737 1
		if ( is_null( $this->cart ) || ! $this->cart instanceof WC_Cart ) {
738 1
			$this->cart = new WC_Cart();
739
		}
740
	}
741
742
	/**
743
	 * Initialize the session class.
744
	 *
745
	 * @since 3.6.4
746
	 * @return void
747
	 */
748 1
	public function initialize_session() {
749
		// Session class, handles session data for users - can be overwritten if custom handler is needed.
750 1
		$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
751 1
		if ( is_null( $this->session ) || ! $this->session instanceof $session_class ) {
752 1
			$this->session = new $session_class();
753 1
			$this->session->init();
754
		}
755
	}
756
757
	/**
758
	 * Set tablenames inside WPDB object.
759
	 */
760 2
	public function wpdb_table_fix() {
761 2
		$this->define_tables();
762
	}
763
764
	/**
765
	 * Ran when any plugin is activated.
766
	 *
767
	 * @since 3.6.0
768
	 * @param string $filename The filename of the activated plugin.
769
	 */
770
	public function activated_plugin( $filename ) {
771
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
772
773
		WC_Helper::activated_plugin( $filename );
774
	}
775
776
	/**
777
	 * Ran when any plugin is deactivated.
778
	 *
779
	 * @since 3.6.0
780
	 * @param string $filename The filename of the deactivated plugin.
781
	 */
782
	public function deactivated_plugin( $filename ) {
783
		include_once dirname( __FILE__ ) . '/admin/helper/class-wc-helper.php';
784
785
		WC_Helper::deactivated_plugin( $filename );
786
	}
787
788
	/**
789
	 * Get queue instance.
790
	 *
791
	 * @return WC_Queue_Interface
792
	 */
793
	public function queue() {
794
		return WC_Queue::instance();
795
	}
796
797
	/**
798
	 * Get Checkout Class.
799
	 *
800
	 * @return WC_Checkout
801
	 */
802 1
	public function checkout() {
803 1
		return WC_Checkout::instance();
804
	}
805
806
	/**
807
	 * Get gateways class.
808
	 *
809
	 * @return WC_Payment_Gateways
810
	 */
811 60
	public function payment_gateways() {
812 60
		return WC_Payment_Gateways::instance();
813
	}
814
815
	/**
816
	 * Get shipping class.
817
	 *
818
	 * @return WC_Shipping
819
	 */
820 146
	public function shipping() {
821 146
		return WC_Shipping::instance();
822
	}
823
824
	/**
825
	 * Email Class.
826
	 *
827
	 * @return WC_Emails
828
	 */
829 1
	public function mailer() {
830 1
		return WC_Emails::instance();
831
	}
832
833
	/**
834
	 * Check if plugin assets are built and minified
835
	 *
836
	 * @return bool
837
	 */
838
	public function build_dependencies_satisfied() {
839
		// Check if we have compiled CSS.
840
		if ( ! file_exists( WC()->plugin_path() . '/assets/css/admin.css' ) ) {
841
			return false;
842
		}
843
844
		// Check if we have minified JS.
845
		if ( ! file_exists( WC()->plugin_path() . '/assets/js/admin/woocommerce_admin.min.js' ) ) {
846
			return false;
847
		}
848
849
		return true;
850
	}
851
852
	/**
853
	 * Output a admin notice when build dependencies not met.
854
	 *
855
	 * @return void
856
	 */
857
	public function build_dependencies_notice() {
858
		if ( $this->build_dependencies_satisfied() ) {
859
			return;
860
		}
861
862
		$message_one = __( 'You have installed a development version of WooCommerce which requires files to be built and minified. From the plugin directory, run <code>grunt assets</code> to build and minify assets.', 'woocommerce' );
863
		$message_two = sprintf(
864
			/* translators: 1: URL of WordPress.org Repository 2: URL of the GitHub Repository release page */
865
			__( 'Or you can download a pre-built version of the plugin from the <a href="%1$s">WordPress.org repository</a> or by visiting <a href="%2$s">the releases page in the GitHub repository</a>.', 'woocommerce' ),
866
			'https://wordpress.org/plugins/woocommerce/',
867
			'https://github.com/woocommerce/woocommerce/releases'
868
		);
869
		printf( '<div class="error"><p>%s %s</p></div>', $message_one, $message_two ); /* WPCS: xss ok. */
870
	}
871
}
872