Completed
Push — master ( 6608ea...fdd8a3 )
by Mike
52:15 queued 44:27
created

WooCommerce::on_plugins_loaded()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * WooCommerce setup
4
 *
5
 * @package WooCommerce
6
 * @since   3.2.0
7
 */
8
9
defined( 'ABSPATH' ) || exit;
10
11
/**
12
 * Main WooCommerce Class.
13
 *
14
 * @class WooCommerce
15
 */
16
final class WooCommerce {
17
18
	/**
19
	 * WooCommerce version.
20
	 *
21
	 * @var string
22
	 */
23
	public $version = '3.6.0';
24
25
	/**
26
	 * The single instance of the class.
27
	 *
28
	 * @var WooCommerce
29
	 * @since 2.1
30
	 */
31
	protected static $_instance = null;
32
33
	/**
34
	 * Session instance.
35
	 *
36
	 * @var WC_Session|WC_Session_Handler
37
	 */
38
	public $session = null;
39
40
	/**
41
	 * Query instance.
42
	 *
43
	 * @var WC_Query
44
	 */
45
	public $query = null;
46
47
	/**
48
	 * Product factory instance.
49
	 *
50
	 * @var WC_Product_Factory
51
	 */
52
	public $product_factory = null;
53
54
	/**
55
	 * Countries instance.
56
	 *
57
	 * @var WC_Countries
58
	 */
59
	public $countries = null;
60
61
	/**
62
	 * Integrations instance.
63
	 *
64
	 * @var WC_Integrations
65
	 */
66
	public $integrations = null;
67
68
	/**
69
	 * Cart instance.
70
	 *
71
	 * @var WC_Cart
72
	 */
73
	public $cart = null;
74
75
	/**
76
	 * Customer instance.
77
	 *
78
	 * @var WC_Customer
79
	 */
80
	public $customer = null;
81
82
	/**
83
	 * Order factory instance.
84
	 *
85
	 * @var WC_Order_Factory
86
	 */
87
	public $order_factory = null;
88
89
	/**
90
	 * Structured data instance.
91
	 *
92
	 * @var WC_Structured_Data
93
	 */
94
	public $structured_data = null;
95
96
	/**
97
	 * Array of deprecated hook handlers.
98
	 *
99
	 * @var array of WC_Deprecated_Hooks
100
	 */
101
	public $deprecated_hook_handlers = array();
102
103
	/**
104
	 * Main WooCommerce Instance.
105
	 *
106
	 * Ensures only one instance of WooCommerce is loaded or can be loaded.
107
	 *
108
	 * @since 2.1
109
	 * @static
110
	 * @see WC()
111
	 * @return WooCommerce - Main instance.
112
	 */
113 777
	public static function instance() {
114 777
		if ( is_null( self::$_instance ) ) {
115
			self::$_instance = new self();
116
		}
117 777
		return self::$_instance;
118
	}
119
120
	/**
121
	 * Cloning is forbidden.
122
	 *
123
	 * @since 2.1
124
	 */
125
	public function __clone() {
126
		wc_doing_it_wrong( __FUNCTION__, __( 'Cloning is forbidden.', 'woocommerce' ), '2.1' );
127
	}
128
129
	/**
130
	 * Unserializing instances of this class is forbidden.
131
	 *
132
	 * @since 2.1
133
	 */
134
	public function __wakeup() {
135
		wc_doing_it_wrong( __FUNCTION__, __( 'Unserializing instances of this class is forbidden.', 'woocommerce' ), '2.1' );
136
	}
137
138
	/**
139
	 * Auto-load in-accessible properties on demand.
140
	 *
141
	 * @param mixed $key Key name.
142
	 * @return mixed
143
	 */
144 547
	public function __get( $key ) {
145 547
		if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ), true ) ) {
146 547
			return $this->$key();
147
		}
148
	}
149
150
	/**
151
	 * WooCommerce Constructor.
152
	 */
153
	public function __construct() {
154
		$this->define_constants();
155
		$this->includes();
156
		$this->init_hooks();
157
	}
158
159
	/**
160
	 * When WP has loaded all plugins, trigger the `woocommerce_loaded` hook.
161
	 *
162
	 * This ensures `woocommerce_loaded` is called only after all other plugins
163
	 * are loaded, to avoid issues caused by plugin directory naming changing
164
	 * the load order. See #21524 for details.
165
	 *
166
	 * @since 3.6.0
167
	 */
168
	public function on_plugins_loaded() {
169
		do_action( 'woocommerce_loaded' );
170
	}
171
172
	/**
173
	 * Hook into actions and filters.
174
	 *
175
	 * @since 2.3
176
	 */
177
	private function init_hooks() {
178
		register_activation_hook( WC_PLUGIN_FILE, array( 'WC_Install', 'install' ) );
179
		register_shutdown_function( array( $this, 'log_errors' ) );
180
181
		add_action( 'plugins_loaded', array( $this, 'on_plugins_loaded' ), -1 );
182
		add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
183
		add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
184
		add_action( 'init', array( $this, 'init' ), 0 );
185
		add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
186
		add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
187
		add_action( 'init', array( $this, 'wpdb_table_fix' ), 0 );
188
		add_action( 'init', array( $this, 'add_image_sizes' ) );
189
		add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
190
	}
191
192
	/**
193
	 * Ensures fatal errors are logged so they can be picked up in the status report.
194
	 *
195
	 * @since 3.2.0
196
	 */
197
	public function log_errors() {
198
		$error = error_get_last();
199
		if ( in_array( $error['type'], array( E_ERROR, E_PARSE, E_COMPILE_ERROR, E_USER_ERROR, E_RECOVERABLE_ERROR ), true ) ) {
200
			$logger = wc_get_logger();
201
			$logger->critical(
202
				/* translators: 1: error message 2: file name and path 3: line number */
203
				sprintf( __( '%1$s in %2$s on line %3$s', 'woocommerce' ), $error['message'], $error['file'], $error['line'] ) . PHP_EOL,
204
				array(
205
					'source' => 'fatal-errors',
206
				)
207
			);
208
			do_action( 'woocommerce_shutdown_error', $error );
209
		}
210
	}
211
212
	/**
213
	 * Define WC Constants.
214
	 */
215
	private function define_constants() {
216
		$upload_dir = wp_upload_dir( null, false );
217
218
		$this->define( 'WC_ABSPATH', dirname( WC_PLUGIN_FILE ) . '/' );
219
		$this->define( 'WC_PLUGIN_BASENAME', plugin_basename( WC_PLUGIN_FILE ) );
220
		$this->define( 'WC_VERSION', $this->version );
221
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
222
		$this->define( 'WC_ROUNDING_PRECISION', 6 );
223
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
224
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
225
		$this->define( 'WC_DELIMITER', '|' );
226
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
227
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
228
		$this->define( 'WC_TEMPLATE_DEBUG_MODE', false );
229
	}
230
231
	/**
232
	 * Define constant if not already set.
233
	 *
234
	 * @param string      $name  Constant name.
235
	 * @param string|bool $value Constant value.
236
	 */
237
	private function define( $name, $value ) {
238
		if ( ! defined( $name ) ) {
239
			define( $name, $value );
240
		}
241
	}
242
243
	/**
244
	 * What type of request is this?
245
	 *
246
	 * @param  string $type admin, ajax, cron or frontend.
247
	 * @return bool
248
	 */
249
	private function is_request( $type ) {
250
		switch ( $type ) {
251
			case 'admin':
252
				return is_admin();
253
			case 'ajax':
254
				return defined( 'DOING_AJAX' );
255
			case 'cron':
256
				return defined( 'DOING_CRON' );
257
			case 'frontend':
258
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' ) && ! defined( 'REST_REQUEST' );
259
		}
260
	}
261
262
	/**
263
	 * Include required core files used in admin and on the frontend.
264
	 */
265
	public function includes() {
266
		/**
267
		 * Class autoloader.
268
		 */
269
		include_once WC_ABSPATH . 'includes/class-wc-autoloader.php';
270
271
		/**
272
		 * Interfaces.
273
		 */
274
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-abstract-order-data-store-interface.php';
275
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-coupon-data-store-interface.php';
276
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-data-store-interface.php';
277
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-data-store-interface.php';
278
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-customer-download-log-data-store-interface.php';
279
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-object-data-store-interface.php';
280
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-data-store-interface.php';
281
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-data-store-interface.php';
282
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-product-data-store-interface.php';
283
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-item-type-data-store-interface.php';
284
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-order-refund-data-store-interface.php';
285
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-payment-token-data-store-interface.php';
286
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-data-store-interface.php';
287
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-product-variable-data-store-interface.php';
288
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-shipping-zone-data-store-interface.php';
289
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-logger-interface.php';
290
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-log-handler-interface.php';
291
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-webhooks-data-store-interface.php';
292
		include_once WC_ABSPATH . 'includes/interfaces/class-wc-queue-interface.php';
293
294
		/**
295
		 * Abstract classes.
296
		 */
297
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php';
298
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-object-query.php';
299
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php';
300
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php';
301
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php';
302
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php';
303
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php';
304
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php';
305
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php';
306
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-log-handler.php';
307
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-deprecated-hooks.php';
308
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-session.php';
309
		include_once WC_ABSPATH . 'includes/abstracts/abstract-wc-privacy.php';
310
311
		/**
312
		 * Core classes.
313
		 */
314
		include_once WC_ABSPATH . 'includes/wc-core-functions.php';
315
		include_once WC_ABSPATH . 'includes/class-wc-datetime.php';
316
		include_once WC_ABSPATH . 'includes/class-wc-post-types.php';
317
		include_once WC_ABSPATH . 'includes/class-wc-install.php';
318
		include_once WC_ABSPATH . 'includes/class-wc-geolocation.php';
319
		include_once WC_ABSPATH . 'includes/class-wc-download-handler.php';
320
		include_once WC_ABSPATH . 'includes/class-wc-comments.php';
321
		include_once WC_ABSPATH . 'includes/class-wc-post-data.php';
322
		include_once WC_ABSPATH . 'includes/class-wc-ajax.php';
323
		include_once WC_ABSPATH . 'includes/class-wc-emails.php';
324
		include_once WC_ABSPATH . 'includes/class-wc-data-exception.php';
325
		include_once WC_ABSPATH . 'includes/class-wc-query.php';
326
		include_once WC_ABSPATH . 'includes/class-wc-meta-data.php';
327
		include_once WC_ABSPATH . 'includes/class-wc-order-factory.php';
328
		include_once WC_ABSPATH . 'includes/class-wc-order-query.php';
329
		include_once WC_ABSPATH . 'includes/class-wc-product-factory.php';
330
		include_once WC_ABSPATH . 'includes/class-wc-product-query.php';
331
		include_once WC_ABSPATH . 'includes/class-wc-payment-tokens.php';
332
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zone.php';
333
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php';
334
		include_once WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php';
335
		include_once WC_ABSPATH . 'includes/class-wc-countries.php';
336
		include_once WC_ABSPATH . 'includes/class-wc-integrations.php';
337
		include_once WC_ABSPATH . 'includes/class-wc-cache-helper.php';
338
		include_once WC_ABSPATH . 'includes/class-wc-https.php';
339
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-action-hooks.php';
340
		include_once WC_ABSPATH . 'includes/class-wc-deprecated-filter-hooks.php';
341
		include_once WC_ABSPATH . 'includes/class-wc-background-emailer.php';
342
		include_once WC_ABSPATH . 'includes/class-wc-discounts.php';
343
		include_once WC_ABSPATH . 'includes/class-wc-cart-totals.php';
344
		include_once WC_ABSPATH . 'includes/customizer/class-wc-shop-customizer.php';
345
		include_once WC_ABSPATH . 'includes/class-wc-regenerate-images.php';
346
		include_once WC_ABSPATH . 'includes/class-wc-privacy.php';
347
		include_once WC_ABSPATH . 'includes/class-wc-structured-data.php';
348
		include_once WC_ABSPATH . 'includes/class-wc-shortcodes.php';
349
		include_once WC_ABSPATH . 'includes/class-wc-logger.php';
350
		include_once WC_ABSPATH . 'includes/queue/class-wc-action-queue.php';
351
		include_once WC_ABSPATH . 'includes/queue/class-wc-queue.php';
352
353
		/**
354
		 * Data stores - used to store and retrieve CRUD object data from the database.
355
		 */
356
		include_once WC_ABSPATH . 'includes/class-wc-data-store.php';
357
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-data-store-wp.php';
358
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-coupon-data-store-cpt.php';
359
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-data-store-cpt.php';
360
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-grouped-data-store-cpt.php';
361
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variable-data-store-cpt.php';
362
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-product-variation-data-store-cpt.php';
363
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-item-type-data-store.php';
364
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-data-store.php';
365
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-coupon-data-store.php';
366
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-fee-data-store.php';
367
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-product-data-store.php';
368
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-shipping-data-store.php';
369
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-item-tax-data-store.php';
370
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-payment-token-data-store.php';
371
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store.php';
372
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-data-store-session.php';
373
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-data-store.php';
374
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-customer-download-log-data-store.php';
375
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-shipping-zone-data-store.php';
376
		include_once WC_ABSPATH . 'includes/data-stores/abstract-wc-order-data-store-cpt.php';
377
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-data-store-cpt.php';
378
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-order-refund-data-store-cpt.php';
379
		include_once WC_ABSPATH . 'includes/data-stores/class-wc-webhook-data-store.php';
380
381
		/**
382
		 * REST API.
383
		 */
384
		include_once WC_ABSPATH . 'includes/legacy/class-wc-legacy-api.php';
385
		include_once WC_ABSPATH . 'includes/class-wc-api.php';
386
		include_once WC_ABSPATH . 'includes/class-wc-auth.php';
387
		include_once WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php';
388
389
		/**
390
		 * Libraries
391
		 */
392
		include_once WC_ABSPATH . 'includes/libraries/action-scheduler/action-scheduler.php';
393
394
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
395
			include_once WC_ABSPATH . 'includes/class-wc-cli.php';
396
		}
397
398
		if ( $this->is_request( 'admin' ) ) {
399
			include_once WC_ABSPATH . 'includes/admin/class-wc-admin.php';
400
		}
401
402
		if ( $this->is_request( 'frontend' ) ) {
403
			$this->frontend_includes();
404
		}
405
406
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
407
			include_once WC_ABSPATH . 'includes/class-wc-tracker.php';
408
		}
409
410
		$this->theme_support_includes();
411
		$this->query = new WC_Query();
412
		$this->api   = new WC_API();
413
	}
414
415
	/**
416
	 * Include classes for theme support.
417
	 *
418
	 * @since 3.3.0
419
	 */
420
	private function theme_support_includes() {
421
		if ( wc_is_active_theme( array( 'twentynineteen', 'twentyseventeen', 'twentysixteen', 'twentyfifteen', 'twentyfourteen', 'twentythirteen', 'twentyeleven', 'twentytwelve', 'twentyten' ) ) ) {
422
			switch ( get_template() ) {
423
				case 'twentyten':
424
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-ten.php';
425
					break;
426
				case 'twentyeleven':
427
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-eleven.php';
428
					break;
429
				case 'twentytwelve':
430
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-twelve.php';
431
					break;
432
				case 'twentythirteen':
433
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-thirteen.php';
434
					break;
435
				case 'twentyfourteen':
436
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fourteen.php';
437
					break;
438
				case 'twentyfifteen':
439
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-fifteen.php';
440
					break;
441
				case 'twentysixteen':
442
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-sixteen.php';
443
					break;
444
				case 'twentyseventeen':
445
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-seventeen.php';
446
					break;
447
				case 'twentynineteen':
448
					include_once WC_ABSPATH . 'includes/theme-support/class-wc-twenty-nineteen.php';
449
					break;
450
			}
451
		}
452
	}
453
454
	/**
455
	 * Include required frontend files.
456
	 */
457
	public function frontend_includes() {
458
		include_once WC_ABSPATH . 'includes/wc-cart-functions.php';
459
		include_once WC_ABSPATH . 'includes/wc-notice-functions.php';
460
		include_once WC_ABSPATH . 'includes/wc-template-hooks.php';
461
		include_once WC_ABSPATH . 'includes/class-wc-template-loader.php';
462
		include_once WC_ABSPATH . 'includes/class-wc-frontend-scripts.php';
463
		include_once WC_ABSPATH . 'includes/class-wc-form-handler.php';
464
		include_once WC_ABSPATH . 'includes/class-wc-cart.php';
465
		include_once WC_ABSPATH . 'includes/class-wc-tax.php';
466
		include_once WC_ABSPATH . 'includes/class-wc-shipping-zones.php';
467
		include_once WC_ABSPATH . 'includes/class-wc-customer.php';
468
		include_once WC_ABSPATH . 'includes/class-wc-embed.php';
469
		include_once WC_ABSPATH . 'includes/class-wc-session-handler.php';
470
	}
471
472
	/**
473
	 * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
474
	 */
475
	public function include_template_functions() {
476
		include_once WC_ABSPATH . 'includes/wc-template-functions.php';
477
	}
478
479
	/**
480
	 * Init WooCommerce when WordPress Initialises.
481
	 */
482
	public function init() {
483
		// Before init action.
484
		do_action( 'before_woocommerce_init' );
485
486
		// Set up localisation.
487
		$this->load_plugin_textdomain();
488
489
		// Load class instances.
490
		$this->product_factory                     = new WC_Product_Factory();
491
		$this->order_factory                       = new WC_Order_Factory();
492
		$this->countries                           = new WC_Countries();
493
		$this->integrations                        = new WC_Integrations();
494
		$this->structured_data                     = new WC_Structured_Data();
495
		$this->deprecated_hook_handlers['actions'] = new WC_Deprecated_Action_Hooks();
496
		$this->deprecated_hook_handlers['filters'] = new WC_Deprecated_Filter_Hooks();
497
498
		// Classes/actions loaded for the frontend and for ajax requests.
499
		if ( $this->is_request( 'frontend' ) ) {
500
			// Session class, handles session data for users - can be overwritten if custom handler is needed.
501
			$session_class = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
502
			$this->session = new $session_class();
503
			$this->session->init();
504
505
			$this->customer = new WC_Customer( get_current_user_id(), true );
506
			// Cart needs the customer info.
507
			$this->cart = new WC_Cart();
508
509
			// Customer should be saved during shutdown.
510
			add_action( 'shutdown', array( $this->customer, 'save' ), 10 );
511
		}
512
513
		$this->load_webhooks();
514
515 1
		// Init action.
516 1
		do_action( 'woocommerce_init' );
517 1
	}
518
519 1
	/**
520 1
	 * Load Localisation files.
521 1
	 *
522
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
523
	 *
524
	 * Locales found in:
525
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
526
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
527
	 */
528
	public function load_plugin_textdomain() {
529
		$locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale();
530
		$locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' );
531
532
		unload_textdomain( 'woocommerce' );
533
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
534
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( WC_PLUGIN_FILE ) ) . '/i18n/languages' );
535
	}
536
537
	/**
538
	 * Ensure theme and server variable compatibility and setup image sizes.
539
	 */
540
	public function setup_environment() {
541
		/**
542
		 * WC_TEMPLATE_PATH constant.
543
		 *
544
		 * @deprecated 2.2 Use WC()->template_path() instead.
545
		 */
546
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
547
548
		$this->add_thumbnail_support();
549
	}
550
551
	/**
552
	 * Ensure post thumbnail support is turned on.
553
	 */
554
	private function add_thumbnail_support() {
555
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
556
			add_theme_support( 'post-thumbnails' );
557
		}
558
		add_post_type_support( 'product', 'thumbnail' );
559
	}
560
561
	/**
562
	 * Add WC Image sizes to WP.
563
	 *
564
	 * As of 3.3, image sizes can be registered via themes using add_theme_support for woocommerce
565
	 * and defining an array of args. If these are not defined, we will use defaults. This is
566
	 * handled in wc_get_image_size function.
567
	 *
568
	 * 3.3 sizes:
569
	 *
570
	 * woocommerce_thumbnail - Used in product listings. We assume these work for a 3 column grid layout.
571
	 * woocommerce_single - Used on single product pages for the main image.
572
	 *
573
	 * @since 2.3
574
	 */
575
	public function add_image_sizes() {
576
		$thumbnail         = wc_get_image_size( 'thumbnail' );
577
		$single            = wc_get_image_size( 'single' );
578 24
		$gallery_thumbnail = wc_get_image_size( 'gallery_thumbnail' );
579 24
580
		add_image_size( 'woocommerce_thumbnail', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
581
		add_image_size( 'woocommerce_single', $single['width'], $single['height'], $single['crop'] );
582
		add_image_size( 'woocommerce_gallery_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
583
584
		/**
585
		 * Legacy image sizes.
586
		 *
587 38
		 * @deprecated These sizes will be removed in 4.0.
588 38
		 */
589
		add_image_size( 'shop_catalog', $thumbnail['width'], $thumbnail['height'], $thumbnail['crop'] );
590
		add_image_size( 'shop_single', $single['width'], $single['height'], $single['crop'] );
591
		add_image_size( 'shop_thumbnail', $gallery_thumbnail['width'], $gallery_thumbnail['height'], $gallery_thumbnail['crop'] );
592
	}
593
594
	/**
595
	 * Get the plugin url.
596 23
	 *
597 23
	 * @return string
598
	 */
599
	public function plugin_url() {
600
		return untrailingslashit( plugins_url( '/', WC_PLUGIN_FILE ) );
601
	}
602
603
	/**
604
	 * Get the plugin path.
605
	 *
606
	 * @return string
607
	 */
608
	public function plugin_path() {
609
		return untrailingslashit( plugin_dir_path( WC_PLUGIN_FILE ) );
610
	}
611
612
	/**
613
	 * Get the template path.
614
	 *
615
	 * @return string
616
	 */
617
	public function template_path() {
618
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
619
	}
620
621
	/**
622
	 * Get Ajax URL.
623
	 *
624
	 * @return string
625
	 */
626
	public function ajax_url() {
627
		return admin_url( 'admin-ajax.php', 'relative' );
628
	}
629
630
	/**
631
	 * Return the WC API URL for a given request.
632
	 *
633
	 * @param string    $request Requested endpoint.
634
	 * @param bool|null $ssl     If should use SSL, null if should auto detect. Default: null.
635
	 * @return string
636
	 */
637
	public function api_request_url( $request, $ssl = null ) {
638
		if ( is_null( $ssl ) ) {
639
			$scheme = wp_parse_url( home_url(), PHP_URL_SCHEME );
640
		} elseif ( $ssl ) {
641
			$scheme = 'https';
642
		} else {
643
			$scheme = 'http';
644
		}
645
646
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
647
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
648
		} elseif ( get_option( 'permalink_structure' ) ) {
649
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
650
		} else {
651
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
652
		}
653
654
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
655
	}
656
657
	/**
658
	 * Load & enqueue active webhooks.
659
	 *
660
	 * @since 2.2
661
	 */
662
	private function load_webhooks() {
663
664
		if ( ! is_blog_installed() ) {
665
			return;
666
		}
667
668
		wc_load_webhooks();
669
	}
670
671
	/**
672
	 * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names.
673
	 */
674
	public function wpdb_table_fix() {
675
		global $wpdb;
676
		$wpdb->payment_tokenmeta = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
677
		$wpdb->order_itemmeta    = $wpdb->prefix . 'woocommerce_order_itemmeta';
678
		$wpdb->tables[]          = 'woocommerce_payment_tokenmeta';
679
		$wpdb->tables[]          = 'woocommerce_order_itemmeta';
680 1
681 1
		if ( get_option( 'db_version' ) < 34370 ) {
682
			$wpdb->woocommerce_termmeta = $wpdb->prefix . 'woocommerce_termmeta';
683
			$wpdb->tables[]             = 'woocommerce_termmeta';
684
		}
685
	}
686
687
	/**
688
	 * Get queue instance.
689 104
	 *
690 104
	 * @return WC_Queue_Interface
691
	 */
692
	public function queue() {
693
		return WC_Queue::instance();
694
	}
695
696
	/**
697
	 * Get Checkout Class.
698 547
	 *
699 547
	 * @return WC_Checkout
700
	 */
701
	public function checkout() {
702
		return WC_Checkout::instance();
703
	}
704
705
	/**
706
	 * Get gateways class.
707 1
	 *
708 1
	 * @return WC_Payment_Gateways
709
	 */
710
	public function payment_gateways() {
711
		return WC_Payment_Gateways::instance();
712
	}
713
714
	/**
715
	 * Get shipping class.
716
	 *
717
	 * @return WC_Shipping
718
	 */
719
	public function shipping() {
720
		return WC_Shipping::instance();
721
	}
722
723
	/**
724
	 * Email Class.
725
	 *
726
	 * @return WC_Emails
727
	 */
728
	public function mailer() {
729
		return WC_Emails::instance();
730
	}
731
}
732