Completed
Push — master ( 805ac7...d01f1e )
by Claudio
18:30
created

WooCommerce::payment_gateways()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

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