Completed
Push — master ( 8ef395...730019 )
by Mike
08:12
created

WooCommerce::wpdb_table_fix()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 0
loc 9
rs 9.6666
c 1
b 1
f 0
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 31 and the first side effect is on line 20.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Plugin Name: WooCommerce
4
 * Plugin URI: https://www.woothemes.com/woocommerce/
5
 * Description: An e-commerce toolkit that helps you sell anything. Beautifully.
6
 * Version: 2.6.0-beta-4
7
 * Author: WooThemes
8
 * Author URI: https://woothemes.com
9
 * Requires at least: 4.1
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.0';
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_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
182
		$this->define( 'WC_VERSION', $this->version );
183
		$this->define( 'WOOCOMMERCE_VERSION', $this->version );
184
		$this->define( 'WC_ROUNDING_PRECISION', 4 );
185
		$this->define( 'WC_DISCOUNT_ROUNDING_MODE', 2 );
186
		$this->define( 'WC_TAX_ROUNDING_MODE', 'yes' === get_option( 'woocommerce_prices_include_tax', 'no' ) ? 2 : 1 );
187
		$this->define( 'WC_DELIMITER', '|' );
188
		$this->define( 'WC_LOG_DIR', $upload_dir['basedir'] . '/wc-logs/' );
189
		$this->define( 'WC_SESSION_CACHE_GROUP', 'wc_session_id' );
190
	}
191
192
	/**
193
	 * Define constant if not already set.
194
	 *
195
	 * @param  string $name
196
	 * @param  string|bool $value
197
	 */
198
	private function define( $name, $value ) {
199
		if ( ! defined( $name ) ) {
200
			define( $name, $value );
201
		}
202
	}
203
204
	/**
205
	 * What type of request is this?
206
	 *
207
	 * @param  string $type admin, ajax, cron or frontend.
208
	 * @return bool
209
	 */
210
	private function is_request( $type ) {
211
		switch ( $type ) {
212
			case 'admin' :
213
				return is_admin();
214
			case 'ajax' :
215
				return defined( 'DOING_AJAX' );
216
			case 'cron' :
217
				return defined( 'DOING_CRON' );
218
			case 'frontend' :
219
				return ( ! is_admin() || defined( 'DOING_AJAX' ) ) && ! defined( 'DOING_CRON' );
220
		}
221
	}
222
223
	/**
224
	 * Include required core files used in admin and on the frontend.
225
	 */
226
	public function includes() {
227
		include_once( 'includes/class-wc-autoloader.php' );
228
		include_once( 'includes/wc-core-functions.php' );
229
		include_once( 'includes/wc-widget-functions.php' );
230
		include_once( 'includes/wc-webhook-functions.php' );
231
		include_once( 'includes/class-wc-install.php' );
232
		include_once( 'includes/class-wc-geolocation.php' );
233
		include_once( 'includes/class-wc-download-handler.php' );
234
		include_once( 'includes/class-wc-comments.php' );
235
		include_once( 'includes/class-wc-post-data.php' );
236
		include_once( 'includes/class-wc-ajax.php' );
237
238
		if ( $this->is_request( 'admin' ) ) {
239
			include_once( 'includes/admin/class-wc-admin.php' );
240
		}
241
242
		if ( $this->is_request( 'frontend' ) ) {
243
			$this->frontend_includes();
244
		}
245
246
		if ( $this->is_request( 'frontend' ) || $this->is_request( 'cron' ) ) {
247
			include_once( 'includes/class-wc-session-handler.php' );
248
		}
249
250
		if ( $this->is_request( 'cron' ) && 'yes' === get_option( 'woocommerce_allow_tracking', 'no' ) ) {
251
			include_once( 'includes/class-wc-tracker.php' );
252
		}
253
254
		$this->query = include( 'includes/class-wc-query.php' );                 // The main query class
255
		$this->api   = include( 'includes/class-wc-api.php' );                   // API Class
256
257
		include_once( 'includes/class-wc-auth.php' );                            // Auth Class
258
		include_once( 'includes/class-wc-post-types.php' );                      // Registers post types
259
		include_once( 'includes/abstracts/abstract-wc-data.php' );				 // WC_Data for CRUD
260
		include_once( 'includes/abstracts/abstract-wc-payment-token.php' );      // Payment Tokens
261
		include_once( 'includes/abstracts/abstract-wc-product.php' );            // Products
262
		include_once( 'includes/abstracts/abstract-wc-order.php' );              // Orders
263
		include_once( 'includes/abstracts/abstract-wc-settings-api.php' );       // Settings API (for gateways, shipping, and integrations)
264
		include_once( 'includes/abstracts/abstract-wc-shipping-method.php' );    // A Shipping method
265
		include_once( 'includes/abstracts/abstract-wc-payment-gateway.php' );    // A Payment gateway
266
		include_once( 'includes/abstracts/abstract-wc-integration.php' );        // An integration with a service
267
		include_once( 'includes/class-wc-product-factory.php' );                 // Product factory
268
		include_once( 'includes/class-wc-payment-tokens.php' );                  // Payment tokens controller
269
		include_once( 'includes/gateways/class-wc-payment-gateway-cc.php' );     // CC Payment Gateway
270
		include_once( 'includes/gateways/class-wc-payment-gateway-echeck.php' ); // eCheck Payment Gateway
271
		include_once( 'includes/class-wc-countries.php' );                       // Defines countries and states
272
		include_once( 'includes/class-wc-integrations.php' );                    // Loads integrations
273
		include_once( 'includes/class-wc-cache-helper.php' );                    // Cache Helper
274
		include_once( 'includes/class-wc-https.php' );                          // https Helper
275
276
		if ( defined( 'WP_CLI' ) && WP_CLI ) {
277
			include_once( 'includes/class-wc-cli.php' );
278
		}
279
	}
280
281
	/**
282
	 * Include required frontend files.
283
	 */
284
	public function frontend_includes() {
285
		include_once( 'includes/wc-cart-functions.php' );
286
		include_once( 'includes/wc-notice-functions.php' );
287
		include_once( 'includes/wc-template-hooks.php' );
288
		include_once( 'includes/class-wc-template-loader.php' );                // Template Loader
289
		include_once( 'includes/class-wc-frontend-scripts.php' );               // Frontend Scripts
290
		include_once( 'includes/class-wc-form-handler.php' );                   // Form Handlers
291
		include_once( 'includes/class-wc-cart.php' );                           // The main cart class
292
		include_once( 'includes/class-wc-tax.php' );                            // Tax class
293
		include_once( 'includes/class-wc-shipping-zones.php' );                 // Shipping Zones class
294
		include_once( 'includes/class-wc-customer.php' );                       // Customer class
295
		include_once( 'includes/class-wc-shortcodes.php' );                     // Shortcodes class
296
		include_once( 'includes/class-wc-embed.php' );                          // Embeds
297
	}
298
299
	/**
300
	 * Function used to Init WooCommerce Template Functions - This makes them pluggable by plugins and themes.
301
	 */
302
	public function include_template_functions() {
303
		include_once( 'includes/wc-template-functions.php' );
304
	}
305
306
	/**
307
	 * Init WooCommerce when WordPress Initialises.
308
	 */
309
	public function init() {
310
		// Before init action.
311
		do_action( 'before_woocommerce_init' );
312
313
		// Set up localisation.
314
		$this->load_plugin_textdomain();
315
316
		// Load class instances.
317
		$this->product_factory = new WC_Product_Factory();                      // Product Factory to create new product instances
318
		$this->order_factory   = new WC_Order_Factory();                        // Order Factory to create new order instances
319
		$this->countries       = new WC_Countries();                            // Countries class
320
		$this->integrations    = new WC_Integrations();                         // Integrations class
321
322
		// Session class, handles session data for users - can be overwritten if custom handler is needed.
323
		if ( $this->is_request( 'frontend' ) || $this->is_request( 'cron' ) ) {
324
			$session_class  = apply_filters( 'woocommerce_session_handler', 'WC_Session_Handler' );
325
			$this->session  = new $session_class();
326
		}
327
328
		// Classes/actions loaded for the frontend and for ajax requests.
329
		if ( $this->is_request( 'frontend' ) ) {
330
			$this->cart     = new WC_Cart();                                    // Cart class, stores the cart contents
331
			$this->customer = new WC_Customer();                                // Customer class, handles data such as customer location
332
		}
333
334
		$this->load_webhooks();
335
336
		// Init action.
337
		do_action( 'woocommerce_init' );
338
	}
339
340
	/**
341
	 * Load Localisation files.
342
	 *
343
	 * Note: the first-loaded translation file overrides any following ones if the same translation is present.
344
	 *
345
	 * Locales found in:
346
	 *      - WP_LANG_DIR/woocommerce/woocommerce-LOCALE.mo
347
	 *      - WP_LANG_DIR/plugins/woocommerce-LOCALE.mo
348
	 */
349
	public function load_plugin_textdomain() {
350
		$locale = apply_filters( 'plugin_locale', get_locale(), 'woocommerce' );
351
352
		load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' );
353
		load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( __FILE__ ) ) . '/i18n/languages' );
354
	}
355
356
	/**
357
	 * Ensure theme and server variable compatibility and setup image sizes.
358
	 */
359
	public function setup_environment() {
360
		/**
361
		 * @deprecated 2.2 Use WC()->template_path()
362
		 */
363
		$this->define( 'WC_TEMPLATE_PATH', $this->template_path() );
364
365
		$this->add_thumbnail_support();
366
		$this->add_image_sizes();
367
	}
368
369
	/**
370
	 * Ensure post thumbnail support is turned on.
371
	 */
372
	private function add_thumbnail_support() {
373
		if ( ! current_theme_supports( 'post-thumbnails' ) ) {
374
			add_theme_support( 'post-thumbnails' );
375
		}
376
		add_post_type_support( 'product', 'thumbnail' );
377
	}
378
379
	/**
380
	 * Add WC Image sizes to WP.
381
	 *
382
	 * @since 2.3
383
	 */
384
	private function add_image_sizes() {
385
		$shop_thumbnail = wc_get_image_size( 'shop_thumbnail' );
386
		$shop_catalog	= wc_get_image_size( 'shop_catalog' );
387
		$shop_single	= wc_get_image_size( 'shop_single' );
388
389
		add_image_size( 'shop_thumbnail', $shop_thumbnail['width'], $shop_thumbnail['height'], $shop_thumbnail['crop'] );
390
		add_image_size( 'shop_catalog', $shop_catalog['width'], $shop_catalog['height'], $shop_catalog['crop'] );
391
		add_image_size( 'shop_single', $shop_single['width'], $shop_single['height'], $shop_single['crop'] );
392
	}
393
394
	/**
395
	 * Get the plugin url.
396
	 * @return string
397
	 */
398
	public function plugin_url() {
399
		return untrailingslashit( plugins_url( '/', __FILE__ ) );
400
	}
401
402
	/**
403
	 * Get the plugin path.
404
	 * @return string
405
	 */
406
	public function plugin_path() {
407
		return untrailingslashit( plugin_dir_path( __FILE__ ) );
408
	}
409
410
	/**
411
	 * Get the template path.
412
	 * @return string
413
	 */
414
	public function template_path() {
415
		return apply_filters( 'woocommerce_template_path', 'woocommerce/' );
416
	}
417
418
	/**
419
	 * Get Ajax URL.
420
	 * @return string
421
	 */
422
	public function ajax_url() {
423
		return admin_url( 'admin-ajax.php', 'relative' );
424
	}
425
426
	/**
427
	 * Return the WC API URL for a given request.
428
	 *
429
	 * @param string $request
430
	 * @param mixed $ssl (default: null)
431
	 * @return string
432
	 */
433
	public function api_request_url( $request, $ssl = null ) {
434
		if ( is_null( $ssl ) ) {
435
			$scheme = parse_url( home_url(), PHP_URL_SCHEME );
436
		} elseif ( $ssl ) {
437
			$scheme = 'https';
438
		} else {
439
			$scheme = 'http';
440
		}
441
442
		if ( strstr( get_option( 'permalink_structure' ), '/index.php/' ) ) {
443
			$api_request_url = trailingslashit( home_url( '/index.php/wc-api/' . $request, $scheme ) );
444
		} elseif ( get_option( 'permalink_structure' ) ) {
445
			$api_request_url = trailingslashit( home_url( '/wc-api/' . $request, $scheme ) );
446
		} else {
447
			$api_request_url = add_query_arg( 'wc-api', $request, trailingslashit( home_url( '', $scheme ) ) );
448
		}
449
450
		return esc_url_raw( apply_filters( 'woocommerce_api_request_url', $api_request_url, $request, $ssl ) );
451
	}
452
453
	/**
454
	 * Load & enqueue active webhooks.
455
	 *
456
	 * @since 2.2
457
	 */
458
	private function load_webhooks() {
459
		if ( false === ( $webhooks = get_transient( 'woocommerce_webhook_ids' ) ) ) {
460
			$webhooks = get_posts( array(
461
				'fields'         => 'ids',
462
				'post_type'      => 'shop_webhook',
463
				'post_status'    => 'publish',
464
				'posts_per_page' => -1
465
			) );
466
			set_transient( 'woocommerce_webhook_ids', $webhooks );
467
		}
468
		foreach ( $webhooks as $webhook_id ) {
469
			$webhook = new WC_Webhook( $webhook_id );
470
			$webhook->enqueue();
471
		}
472
	}
473
474
	/**
475
	 * WooCommerce Payment Token Meta API and Term/Order item Meta - set table names.
476
	 */
477
	public function wpdb_table_fix() {
478
		global $wpdb;
479
		$wpdb->payment_tokenmeta    = $wpdb->prefix . 'woocommerce_payment_tokenmeta';
480
		$wpdb->woocommerce_termmeta = $wpdb->prefix . 'woocommerce_termmeta';
481
		$wpdb->order_itemmeta       = $wpdb->prefix . 'woocommerce_order_itemmeta';
482
		$wpdb->tables[]             = 'woocommerce_payment_tokenmeta';
483
		$wpdb->tables[]             = 'woocommerce_termmeta';
484
		$wpdb->tables[]             = 'woocommerce_order_itemmeta';
485
	}
486
487
	/**
488
	 * Get Checkout Class.
489
	 * @return WC_Checkout
490
	 */
491
	public function checkout() {
492
		return WC_Checkout::instance();
493
	}
494
495
	/**
496
	 * Get gateways class.
497
	 * @return WC_Payment_Gateways
498
	 */
499
	public function payment_gateways() {
500
		return WC_Payment_Gateways::instance();
501
	}
502
503
	/**
504
	 * Get shipping class.
505
	 * @return WC_Shipping
506
	 */
507
	public function shipping() {
508
		return WC_Shipping::instance();
509
	}
510
511
	/**
512
	 * Email Class.
513
	 * @return WC_Emails
514
	 */
515
	public function mailer() {
516
		return WC_Emails::instance();
517
	}
518
}
519
520
endif;
521
522
/**
523
 * Main instance of WooCommerce.
524
 *
525
 * Returns the main instance of WC to prevent the need to use globals.
526
 *
527
 * @since  2.1
528
 * @return WooCommerce
529
 */
530
function WC() {
531
	return WooCommerce::instance();
532
}
533
534
// Global for backwards compatibility.
535
$GLOBALS['woocommerce'] = WC();
536