Completed
Push — master ( ddf90f...7a2154 )
by Mike
14:46
created

WooCommerce::init()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 16
c 0
b 0
f 0
nc 4
nop 0
dl 0
loc 31
rs 8.5806
1
<?php
2
/**
3
 * Plugin Name: WooCommerce
4
 * Plugin URI: https://woocommerce.com/
5
 * Description: An e-commerce toolkit that helps you sell anything. Beautifully.
6
 * Version: 2.7.0-dev
7
 * Author: WooThemes
8
 * Author URI: https://woothemes.com
9
 * Requires at least: 4.4
10
 * Tested up to: 4.5
11
 *
12
 * Text Domain: woocommerce
13
 * Domain Path: /i18n/languages/
14
 *
15
 * @package WooCommerce
16
 * @category Core
17
 * @author WooThemes
18
 */
19
if ( ! defined( 'ABSPATH' ) ) {
20
	exit; // Exit if accessed directly.
21
}
22
23
if ( ! class_exists( 'WooCommerce' ) ) :
24
25
/**
26
 * Main WooCommerce Class.
27
 *
28
 * @class WooCommerce
29
 * @version	2.6.0
30
 */
31
final class WooCommerce {
32
33
	/**
34
	 * WooCommerce version.
35
	 *
36
	 * @var string
37
	 */
38
	public $version = '2.6.3';
39
40
	/**
41
	 * The single instance of the class.
42
	 *
43
	 * @var WooCommerce
44
	 * @since 2.1
45
	 */
46
	protected static $_instance = null;
47
48
	/**
49
	 * Session instance.
50
	 *
51
	 * @var WC_Session
52
	 */
53
	public $session = null;
54
55
	/**
56
	 * Query instance.
57
	 *
58
	 * @var WC_Query
59
	 */
60
	public $query = null;
61
62
	/**
63
	 * Product factory instance.
64
	 *
65
	 * @var WC_Product_Factory
66
	 */
67
	public $product_factory = null;
68
69
	/**
70
	 * Countries instance.
71
	 *
72
	 * @var WC_Countries
73
	 */
74
	public $countries = null;
75
76
	/**
77
	 * Integrations instance.
78
	 *
79
	 * @var WC_Integrations
80
	 */
81
	public $integrations = null;
82
83
	/**
84
	 * Cart instance.
85
	 *
86
	 * @var WC_Cart
87
	 */
88
	public $cart = null;
89
90
	/**
91
	 * Customer instance.
92
	 *
93
	 * @var WC_Customer
94
	 */
95
	public $customer = null;
96
97
	/**
98
	 * Order factory instance.
99
	 *
100
	 * @var WC_Order_Factory
101
	 */
102
	public $order_factory = null;
103
104
	/**
105
	 * Main WooCommerce Instance.
106
	 *
107
	 * Ensures only one instance of WooCommerce is loaded or can be loaded.
108
	 *
109
	 * @since 2.1
110
	 * @static
111
	 * @see WC()
112
	 * @return WooCommerce - Main instance.
113
	 */
114
	public static function instance() {
115
		if ( is_null( self::$_instance ) ) {
116
			self::$_instance = new self();
117
		}
118
		return self::$_instance;
119
	}
120
121
	/**
122
	 * Cloning is forbidden.
123
	 * @since 2.1
124
	 */
125
	public function __clone() {
126
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
127
	}
128
129
	/**
130
	 * Unserializing instances of this class is forbidden.
131
	 * @since 2.1
132
	 */
133
	public function __wakeup() {
134
		_doing_it_wrong( __FUNCTION__, __( 'Cheatin&#8217; huh?', 'woocommerce' ), '2.1' );
135
	}
136
137
	/**
138
	 * Auto-load in-accessible properties on demand.
139
	 * @param mixed $key
140
	 * @return mixed
141
	 */
142
	public function __get( $key ) {
143
		if ( in_array( $key, array( 'payment_gateways', 'shipping', 'mailer', 'checkout' ) ) ) {
144
			return $this->$key();
145
		}
146
	}
147
148
	/**
149
	 * WooCommerce Constructor.
150
	 */
151
	public function __construct() {
152
		$this->define_constants();
153
		$this->includes();
154
		$this->init_hooks();
155
156
		do_action( 'woocommerce_loaded' );
157
	}
158
159
	/**
160
	 * Hook into actions and filters.
161
	 * @since  2.3
162
	 */
163
	private function init_hooks() {
164
		register_activation_hook( __FILE__, array( 'WC_Install', 'install' ) );
165
		add_action( 'after_setup_theme', array( $this, 'setup_environment' ) );
166
		add_action( 'after_setup_theme', array( $this, 'include_template_functions' ), 11 );
167
		add_action( 'init', array( $this, 'init' ), 0 );
168
		add_action( 'init', array( 'WC_Shortcodes', 'init' ) );
169
		add_action( 'init', array( 'WC_Emails', 'init_transactional_emails' ) );
170
		add_action( 'init', array( $this, 'wpdb_table_fix' ), 0 );
171
		add_action( 'switch_blog', array( $this, 'wpdb_table_fix' ), 0 );
172
	}
173
174
	/**
175
	 * Define WC Constants.
176
	 */
177
	private function define_constants() {
178
		$upload_dir = wp_upload_dir();
179
180
		$this->define( 'WC_PLUGIN_FILE', __FILE__ );
181
		$this->define( 'WC_ABSPATH', dirname( __FILE__ ) . '/' );
182
		$this->define( 'WC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
183
		$this->define( 'WC_VERSION', $this->version );
184
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
185
		$this->define( 'WC_ROUNDING_PRECISION', 4 );
186
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
187
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
188
		$this->define( 'WC_DELIMITER', '|' );
189
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
190
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
191
	}
192
193
	/**
194
	 * Define constant if not already set.
195
	 *
196
	 * @param  string $name
197
	 * @param  string|bool $value
198
	 */
199
	private function define( $name, $value ) {
200
		if ( ! defined( $name ) ) {
201
			define( $name, $value );
202
		}
203
	}
204
205
	/**
206
	 * What type of request is this?
207
	 *
208
	 * @param  string $type admin, ajax, cron or frontend.
209
	 * @return bool
210
	 */
211
	private function is_request( $type ) {
212
		switch ( $type ) {
213
			case 'admin' :
214
				return is_admin();
215
			case 'ajax' :
216
				return defined( 'DOING_AJAX' );
217
			case 'cron' :
218
				return defined( 'DOING_CRON' );
219
			case 'frontend' :
220
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
221
		}
222
	}
223
224
	/**
225
	 * Include required core files used in admin and on the frontend.
226
	 */
227
	public function includes() {
228
		include_once( WC_ABSPATH . 'includes/class-wc-autoloader.php' );
229
		include_once( WC_ABSPATH . 'includes/wc-core-functions.php' );
230
		include_once( WC_ABSPATH . 'includes/class-wc-register-wp-admin-settings.php' );
231
		include_once( WC_ABSPATH . 'includes/wc-widget-functions.php' );
232
		include_once( WC_ABSPATH . 'includes/wc-webhook-functions.php' );
233
		include_once( WC_ABSPATH . 'includes/class-wc-install.php' );
234
		include_once( WC_ABSPATH . 'includes/class-wc-geolocation.php' );
235
		include_once( WC_ABSPATH . 'includes/class-wc-download-handler.php' );
236
		include_once( WC_ABSPATH . 'includes/class-wc-comments.php' );
237
		include_once( WC_ABSPATH . 'includes/class-wc-post-data.php' );
238
		include_once( WC_ABSPATH . 'includes/class-wc-ajax.php' );
239
240
		include_once( 'includes/abstracts/abstract-wc-data.php' ); // WC_Data for CRUD
241
242
		if ( $this->is_request( 'admin' ) ) {
243
			include_once( WC_ABSPATH . 'includes/admin/class-wc-admin.php' );
244
		}
245
246
		if ( $this->is_request( 'frontend' ) ) {
247
			$this->frontend_includes();
248
		}
249
250
		if ( $this->is_request( 'frontend' ) || $this->is_request( 'cron' ) ) {
251
			include_once( WC_ABSPATH . 'includes/class-wc-session-handler.php' );
252
		}
253
254
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
255
			include_once( WC_ABSPATH . 'includes/class-wc-tracker.php' );
256
		}
257
258
		include_once( WC_ABSPATH . 'includes/class-wc-query.php' ); // The main query class
259
		include_once( WC_ABSPATH . 'includes/class-wc-api.php' ); // API Class
260
		include_once( WC_ABSPATH . 'includes/class-wc-auth.php' ); // Auth Class
261
		include_once( WC_ABSPATH . 'includes/class-wc-post-types.php' ); // Registers post types
262
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-data.php' );				 // WC_Data for CRUD
263
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-token.php' ); // Payment Tokens
264
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-product.php' ); // Products
265
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-order.php' ); // Orders
266
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-settings-api.php' ); // Settings API (for gateways, shipping, and integrations)
267
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-shipping-method.php' ); // A Shipping method
268
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-payment-gateway.php' ); // A Payment gateway
269
		include_once( WC_ABSPATH . 'includes/abstracts/abstract-wc-integration.php' ); // An integration with a service
270
		include_once( WC_ABSPATH . 'includes/class-wc-product-factory.php' ); // Product factory
271
		include_once( WC_ABSPATH . 'includes/class-wc-payment-tokens.php' ); // Payment tokens controller
272
		include_once( WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-cc.php' ); // CC Payment Gateway
273
		include_once( WC_ABSPATH . 'includes/gateways/class-wc-payment-gateway-echeck.php' ); // eCheck Payment Gateway
274
		include_once( WC_ABSPATH . 'includes/class-wc-countries.php' ); // Defines countries and states
275
		include_once( WC_ABSPATH . 'includes/class-wc-integrations.php' ); // Loads integrations
276
		include_once( WC_ABSPATH . 'includes/class-wc-cache-helper.php' ); // Cache Helper
277
		include_once( WC_ABSPATH . 'includes/class-wc-https.php' ); // https Helper
278
279
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
280
			include_once( WC_ABSPATH . 'includes/class-wc-cli.php' );
281
		}
282
283
		$this->query = new WC_Query();
284
		$this->api   = new WC_API();
285
	}
286
287
	/**
288
	 * Include required frontend files.
289
	 */
290
	public function frontend_includes() {
291
		include_once( WC_ABSPATH . 'includes/wc-cart-functions.php' );
292
		include_once( WC_ABSPATH . 'includes/wc-notice-functions.php' );
293
		include_once( WC_ABSPATH . 'includes/wc-template-hooks.php' );
294
		include_once( WC_ABSPATH . 'includes/class-wc-template-loader.php' );                // Template Loader
295
		include_once( WC_ABSPATH . 'includes/class-wc-frontend-scripts.php' );               // Frontend Scripts
296
		include_once( WC_ABSPATH . 'includes/class-wc-form-handler.php' );                   // Form Handlers
297
		include_once( WC_ABSPATH . 'includes/class-wc-cart.php' );                           // The main cart class
298
		include_once( WC_ABSPATH . 'includes/class-wc-tax.php' );                            // Tax class
299
		include_once( WC_ABSPATH . 'includes/class-wc-shipping-zones.php' );                 // Shipping Zones class
300
		include_once( WC_ABSPATH . 'includes/class-wc-customer.php' );                       // Customer class
301
		include_once( WC_ABSPATH . 'includes/class-wc-shortcodes.php' );                     // Shortcodes class
302
		include_once( WC_ABSPATH . 'includes/class-wc-embed.php' );                          // Embeds
303
	}
304
305
	/**
306
	 * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
307
	 */
308
	public function include_template_functions() {
309
		include_once( WC_ABSPATH . 'includes/wc-template-functions.php' );
310
	}
311
312
	/**
313
	 * Init WooCommerce when WordPress Initialises.
314
	 */
315
	public function init() {
316
		// Before init action.
317
		do_action( 'before_woocommerce_init' );
318
319
		// Set up localisation.
320
		$this->load_plugin_textdomain();
321
322
		// Load class instances.
323
		$this->product_factory = new WC_Product_Factory();                      // Product Factory to create new product instances
324
		$this->order_factory   = new WC_Order_Factory();                        // Order Factory to create new order instances
325
		$this->countries       = new WC_Countries();                            // Countries class
326
		$this->integrations    = new WC_Integrations();                         // Integrations class
327
328
		// Session class, handles session data for users - can be overwritten if custom handler is needed.
329
		if ( $this->is_request( 'frontend' ) || $this->is_request( 'cron' ) ) {
330
			$session_class  = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
331
			$this->session  = new $session_class();
332
		}
333
334
		// Classes/actions loaded for the frontend and for ajax requests.
335
		if ( $this->is_request( 'frontend' ) ) {
336
			$this->cart     = new WC_Cart();                                    // Cart class, stores the cart contents
337
			$this->customer = new WC_Customer();                                // Customer class, handles data such as customer location
338
			$this->customer->load_session();
339
		}
340
341
		$this->load_webhooks();
342
343
		// Init action.
344
		do_action( 'woocommerce_init' );
345
	}
346
347
	/**
348
	 * Load Localisation files.
349
	 *
350
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
351
	 *
352
	 * Locales found in:
353
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
354
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
355
	 */
356
	public function load_plugin_textdomain() {
357
		$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce' );
358
359
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
360
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( __FILE__ ) ) . '/i18n/languages' );
361
	}
362
363
	/**
364
	 * Ensure theme and server variable compatibility and setup image sizes.
365
	 */
366
	public function setup_environment() {
367
		/**
368
		 * @deprecated 2.2 Use WC()->template_path()
369
		 */
370
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
371
372
		$this->add_thumbnail_support();
373
		$this->add_image_sizes();
374
	}
375
376
	/**
377
	 * Ensure post thumbnail support is turned on.
378
	 */
379
	private function add_thumbnail_support() {
380
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
381
			add_theme_support( 'post-thumbnails' );
382
		}
383
		add_post_type_support( 'product', 'thumbnail' );
384
	}
385
386
	/**
387
	 * Add WC Image sizes to WP.
388
	 *
389
	 * @since 2.3
390
	 */
391
	private function add_image_sizes() {
392
		$shop_thumbnail = wc_get_image_size( 'shop_thumbnail' );
393
		$shop_catalog	= wc_get_image_size( 'shop_catalog' );
394
		$shop_single	= wc_get_image_size( 'shop_single' );
395
396
		add_image_size( 'shop_thumbnail', $shop_thumbnail['width'], $shop_thumbnail['height'], $shop_thumbnail['crop'] );
397
		add_image_size( 'shop_catalog', $shop_catalog['width'], $shop_catalog['height'], $shop_catalog['crop'] );
398
		add_image_size( 'shop_single', $shop_single['width'], $shop_single['height'], $shop_single['crop'] );
399
	}
400
401
	/**
402
	 * Get the plugin url.
403
	 * @return string
404
	 */
405
	public function plugin_url() {
406
		return untrailingslashit( plugins_url( '/', __FILE__ ) );
407
	}
408
409
	/**
410
	 * Get the plugin path.
411
	 * @return string
412
	 */
413
	public function plugin_path() {
414
		return untrailingslashit( plugin_dir_path( __FILE__ ) );
415
	}
416
417
	/**
418
	 * Get the template path.
419
	 * @return string
420
	 */
421
	public function template_path() {
422
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
423
	}
424
425
	/**
426
	 * Get Ajax URL.
427
	 * @return string
428
	 */
429
	public function ajax_url() {
430
		return admin_url( 'admin-ajax.php', 'relative' );
431
	}
432
433
	/**
434
	 * Return the WC API URL for a given request.
435
	 *
436
	 * @param string $request
437
	 * @param mixed $ssl (default: null)
438
	 * @return string
439
	 */
440
	public function api_request_url( $request, $ssl = null ) {
441
		if ( is_null( $ssl ) ) {
442
			$scheme = parse_url( home_url(), PHP_URL_SCHEME );
443
		} elseif ( $ssl ) {
444
			$scheme = 'https';
445
		} else {
446
			$scheme = 'http';
447
		}
448
449
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
450
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
451
		} elseif ( get_option( 'permalink_structure' ) ) {
452
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
453
		} else {
454
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
455
		}
456
457
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
458
	}
459
460
	/**
461
	 * Load & enqueue active webhooks.
462
	 *
463
	 * @since 2.2
464
	 */
465
	private function load_webhooks() {
466
		if ( false === ( $webhooks = get_transient( 'woocommerce_webhook_ids' ) ) ) {
467
			$webhooks = get_posts( array(
468
				'fields'         => 'ids',
469
				'post_type'      => 'shop_webhook',
470
				'post_status'    => 'publish',
471
				'posts_per_page' => -1
472
			) );
473
			set_transient( 'woocommerce_webhook_ids', $webhooks );
474
		}
475
		foreach ( $webhooks as $webhook_id ) {
476
			$webhook = new WC_Webhook( $webhook_id );
477
			$webhook->enqueue();
478
		}
479
	}
480
481
	/**
482
	 * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names.
483
	 */
484
	public function wpdb_table_fix() {
485
		global $wpdb;
486
		$wpdb->payment_tokenmeta    = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
487
		$wpdb->order_itemmeta       = $wpdb->prefix . 'woocommerce_order_itemmeta';
488
		$wpdb->tables[]             = 'woocommerce_payment_tokenmeta';
489
		$wpdb->tables[]             = 'woocommerce_order_itemmeta';
490
491
		if ( get_option( 'db_version' ) < 34370 ) {
492
			$wpdb->woocommerce_termmeta = $wpdb->prefix . 'woocommerce_termmeta';
493
			$wpdb->tables[]             = 'woocommerce_termmeta';
494
		}
495
	}
496
497
	/**
498
	 * Get Checkout Class.
499
	 * @return WC_Checkout
500
	 */
501
	public function checkout() {
502
		return WC_Checkout::instance();
503
	}
504
505
	/**
506
	 * Get gateways class.
507
	 * @return WC_Payment_Gateways
508
	 */
509
	public function payment_gateways() {
510
		return WC_Payment_Gateways::instance();
511
	}
512
513
	/**
514
	 * Get shipping class.
515
	 * @return WC_Shipping
516
	 */
517
	public function shipping() {
518
		return WC_Shipping::instance();
519
	}
520
521
	/**
522
	 * Email Class.
523
	 * @return WC_Emails
524
	 */
525
	public function mailer() {
526
		return WC_Emails::instance();
527
	}
528
}
529
530
endif;
531
532
/**
533
 * Main instance of WooCommerce.
534
 *
535
 * Returns the main instance of WC to prevent the need to use globals.
536
 *
537
 * @since  2.1
538
 * @return WooCommerce
539
 */
540
function WC() {
541
	return WooCommerce::instance();
542
}
543
544
// Global for backwards compatibility.
545
$GLOBALS['woocommerce'] = WC();
546