Completed
Pull Request — master (#2038)
by Ben
15:48
created

WP_eCommerce::_register_core_components()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
/**
3
  * Plugin Name: WP eCommerce
4
  * Plugin URI: http://wpecommerce.org/
5
  * Description: A plugin that provides a WordPress Shopping Cart. See also: <a href="http://wpecommerce.org" target="_blank">WPeCommerce.org</a> | <a href="https://wordpress.org/support/plugin/wp-e-commerce/" target="_blank">Support Forum</a> | <a href="http://docs.wpecommerce.org/" target="_blank">Documentation</a>
6
  * Version: 3.11.2
7
  * Author: WP eCommerce
8
  * Author URI: http://wpecommerce.org/
9
  * Text Domain: wp-e-commerce
10
  **/
11
12
/**
13
 * WP_eCommerce
14
 *
15
 * Main WPEC Plugin Class
16
 *
17
 * @package wp-e-commerce
18
 */
19
class WP_eCommerce {
20
	private $components = array(
21
		'merchant'     => array(),
22
		'marketplace'  => array(),
23
		'theme-engine' => array(),
24
	);
25
26
	/**
27
	 * Start WPEC on plugins loaded
28
	 *
29
	 * @uses add_action()   Attaches to 'plugins_loaded' hook
30
	 * @uses add_filter()   Attaches to 'wpsc_components' hook
31
	 */
32
	public function __construct() {
33
		add_action( 'plugins_loaded' , array( $this, 'init' ), 8 );
34
		add_filter( 'wpsc_components', array( $this, '_register_core_components' ) );
35
	}
36
37
	/**
38
	 * Takes care of loading up WPEC
39
	 *
40
	 * @uses WP_eCommerce::start()      Initializes basic WPEC constants
41
	 * @uses WP_eCommerce::constants()  Setup WPEC core constants
42
	 * @uses WP_eCommerce::includes()   Includes the WPEC files
43
	 * @uses WP_eCommerce::load()       Setup WPEC Core
44
	 * @uses do_action()                Calls 'wpsc_pre_init' which runs before WPEC initializes
45
	 * @uses do_action()                Calls 'wpsc_init' runs just after WPEC initializes
46
	 */
47
	public function init() {
48
		// Previous to initializing
49
		do_action( 'wpsc_pre_init' );
50
		// Initialize
51
		$this->start();
52
		$this->constants();
53
		$this->includes();
54
		$this->load();
55
56
		// Finished initializing
57
		do_action( 'wpsc_init' );
58
	}
59
60
	/**
61
	 * New WPSC components API.
62
	 *
63
	 * Allows for modular coupling of different functionalities within WPSC.
64
	 * This is the way we'll be introducing cutting-edge APIs.
65
	 *
66
	 * @since 3.8.9.5
67
	 *
68
	 * @param  array $components
69
	 * @return array $components
70
	 */
71
	public function _register_core_components( $components ) {
72
		$components['merchant']['core-v2'] = array(
73
			'title'    => __( 'WP eCommerce Merchant API v2', 'wp-e-commerce' ),
74
			'includes' =>
75
				WPSC_FILE_PATH . '/wpsc-components/merchant-core-v2/merchant-core-v2.php'
76
		);
77
78
		$components['merchant']['core-v3'] = array(
79
			'title'    => __( 'WP eCommerce Merchant API v3', 'wp-e-commerce' ),
80
			'includes' =>
81
				WPSC_FILE_PATH . '/wpsc-components/merchant-core-v3/merchant-core-v3.php'
82
		);
83
84
		$components['marketplace']['core-v1'] = array(
85
			'title'    => __( 'WP eCommerce Marketplace API v1', 'wp-e-commerce' ),
86
			'includes' =>
87
				WPSC_FILE_PATH . '/wpsc-components/marketplace-core-v1/marketplace-core-v1.php'
88
		);
89
90
		$components['fancy-notifications']['fancy-notifications-v1'] = array(
91
			'title'    => __( 'Fancy Notifications v1', 'wpsc' ),
92
			'includes' => WPSC_FILE_PATH . '/wpsc-components/fancy-notifications/fancy-notifications.php'
93
		);
94
95
		return $components;
96
	}
97
98
	/**
99
	 * Initialize the basic WP eCommerce constants
100
	 *
101
	 * @uses plugins_url()              Retrieves url to plugins directory
102
	 * @uses load_plugin_textdomain()   Loads plugin transations strings
103
	 * @uses plugin_basename()          Gets the basename of a plugin (extracts the name of a plugin from its filename)
104
	 * @uses do_action()                Calls 'wpsc_started' which runs after WPEC has started
105
	 */
106
	public function start() {
107
		// Set the core file path
108
		define( 'WPSC_FILE_PATH', dirname( __FILE__ ) );
109
110
		// Define the path to the plugin folder
111
		define( 'WPSC_DIR_NAME',  basename( WPSC_FILE_PATH ) );
112
		define( 'WPSC_PLUGIN_BASENAME', plugin_basename( __FILE__ ) );
113
114
		// Define the URL to the plugin folder
115
		define( 'WPSC_FOLDER',    dirname( WPSC_PLUGIN_BASENAME ) );
116
		define( 'WPSC_URL',       plugins_url( '', __FILE__ ) );
117
118
		add_filter( 'load_textdomain_mofile', array( $this, 'load_old_textdomain' ), 10, 2 );
119
120
		//load text domain
121
		if ( ! load_plugin_textdomain( 'wp-e-commerce', false, '../languages/' ) ) {
122
			load_plugin_textdomain( 'wp-e-commerce', false, dirname( WPSC_PLUGIN_BASENAME ) . '/wpsc-languages/' );
123
		}
124
125
		// Finished starting
126
		do_action( 'wpsc_started' );
127
	}
128
129
	/**
130
	 * Load a .mo file for the old textdomain if one exists
131
	 * Necessary to maintain backwards compatibility after changing text-domains.
132
	 *
133
	 * @since  3.11.0
134
	 * @link: https://github.com/10up/grunt-wp-plugin/issues/21#issuecomment-62003284
135
	 */
136
	public function load_old_textdomain( $mofile, $textdomain ) {
137
		if ( $textdomain === 'wp-e-commerce' && ! file_exists( $mofile ) ) {
138
			$mofile = dirname( $mofile ) . DIRECTORY_SEPARATOR . str_replace( $textdomain, 'wpsc', basename( $mofile ) );
139
		}
140
		return $mofile;
141
	}
142
143
	/**
144
	 * Sets table names as WPDB properties.
145
	 *
146
	 * @since  4.0
147
	 * @return array Array of custom tables
148
	 */
149
	public function setup_table_names() {
150
		global $wpdb;
151
152
		$wpdb->wpsc_meta                = WPSC_TABLE_META;
153
		$wpdb->wpsc_also_bought         = WPSC_TABLE_ALSO_BOUGHT;
154
		$wpdb->wpsc_region_tax          = WPSC_TABLE_REGION_TAX;
155
		$wpdb->wpsc_coupon_codes        = WPSC_TABLE_COUPON_CODES;
156
		$wpdb->wpsc_cart_contents       = WPSC_TABLE_CART_CONTENTS;
157
		$wpdb->wpsc_claimed_stock       = WPSC_TABLE_CLAIMED_STOCK;
158
		$wpdb->wpsc_currency_list       = WPSC_TABLE_CURRENCY_LIST;
159
		$wpdb->wpsc_purchase_logs       = WPSC_TABLE_PURCHASE_LOGS;
160
		$wpdb->wpsc_checkout_forms      = WPSC_TABLE_CHECKOUT_FORMS;
161
		$wpdb->wpsc_product_rating      = WPSC_TABLE_PRODUCT_RATING;
162
		$wpdb->wpsc_download_status     = WPSC_TABLE_DOWNLOAD_STATUS;
163
		$wpdb->wpsc_submitted_form_data = WPSC_TABLE_SUBMITTED_FORM_DATA;
164
		$wpdb->wpsc_cart_itemmeta       = WPSC_TABLE_CART_ITEM_META;
165
		$wpdb->wpsc_purchasemeta        = WPSC_TABLE_PURCHASE_META;
166
		$wpdb->wpsc_visitors            = WPSC_TABLE_VISITORS;
167
		$wpdb->wpsc_visitormeta         = WPSC_TABLE_VISITOR_META;
168
169
		return array(
170
			$wpdb->wpsc_meta               ,
171
			$wpdb->wpsc_also_bought        ,
172
			$wpdb->wpsc_region_tax         ,
173
			$wpdb->wpsc_coupon_codes       ,
174
			$wpdb->wpsc_cart_contents      ,
175
			$wpdb->wpsc_claimed_stock      ,
176
			$wpdb->wpsc_currency_list      ,
177
			$wpdb->wpsc_purchase_logs      ,
178
			$wpdb->wpsc_checkout_forms     ,
179
			$wpdb->wpsc_product_rating     ,
180
			$wpdb->wpsc_download_status    ,
181
			$wpdb->wpsc_submitted_form_data,
182
			$wpdb->wpsc_cart_itemmeta      ,
183
			$wpdb->wpsc_purchasemeta       ,
184
			$wpdb->wpsc_visitors           ,
185
			$wpdb->wpsc_visitormeta        ,
186
		);
187
	}
188
189
	/**
190
	 * Setup the WPEC core constants
191
	 *
192
	 * @uses wpsc_core_constants()                      Loads the WPEC Core constants
193
	 * @uses wpsc_core_is_multisite()                   Checks if this is a multisite install. True if is multisite
194
	 * @uses wpsc_core_load_session()                   Loads the WPEC core session
195
	 * @uses wpsc_core_constants_version_processing()   Checks and sets a constant for WordPress version
196
	 * @uses wpsc_core_constants_table_names()          Sets constants for WPEC table names
197
	 * @uses wpsc_core_constants_uploads()              Set the upload related constants
198
	 * @uses do_action()                                Calls 'wpsc_constants' which runs after the WPEC constants are defined
199
	 */
200
	public function constants() {
201
		// Define globals and constants used by wp-e-commerce
202
		require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-constants.php' );
203
204
		// Load the WPEC core constants
205
		wpsc_core_constants();
206
207
		// Is WordPress Multisite
208
		wpsc_core_is_multisite();
209
210
		// Start the wpsc session
211
		wpsc_core_load_session();
212
213
		// Which version of WPEC
214
		wpsc_core_constants_version_processing();
215
216
		// WPEC Table names and related constants
217
		wpsc_core_constants_table_names();
218
219
		// setup wpdb table name attributes
220
		$this->setup_table_names();
221
222
		// Uploads directory info
223
		wpsc_core_constants_uploads();
224
225
		// Any additional constants can hook in here
226
		do_action( 'wpsc_constants' );
227
	}
228
229
	/**
230
	 * Include the rest of WPEC's files
231
	 *
232
	 * @uses apply_filters()    Calls 'wpsc_components' private merchant components
233
	 * @uses do_action()        Calls 'wpsc_includes' which runs after WPEC files have been included
234
	 */
235
	public function includes() {
236
		require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-util.php'      );
237
		require_once( WPSC_FILE_PATH . '/wpsc-includes/customer.php'            );
238
		require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-customer.php'  );
239
		require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-visitor.php'   );
240
		require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-meta-cart-item.php' );
241
		require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-functions.php' );
242
		require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-installer.php' );
243
		require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-includes.php'  );
244
245
		$this->components = apply_filters( 'wpsc_components', $this->components );
246
247
		foreach ( $this->components as $type => $registered ) {
248
			foreach ( $registered as $component ) {
249
250
				if ( ! is_array( $component['includes'] ) ) {
251
					$component['includes'] = array( $component['includes'] );
252
				}
253
254
				foreach ( $component['includes'] as $include ) {
255
					require_once( $include );
256
				}
257
			}
258
		}
259
260
		// Any additional file includes can hook in here
261
		do_action( 'wpsc_includes' );
262
	}
263
264
	/**
265
	 * Setup the WPEC core
266
	 *
267
	 * @uses do_action()                            Calls 'wpsc_pre_load' which runs before WPEC setup
268
	 * @uses do_action()                            Calls 'wpsc_before_init' which is a legacy action
269
	 * @uses _wpsc_action_create_customer_id()      Sets up a customer id just in case we don't have it
270
	 * @uses wpsc_core_setup_globals()              Sets up the WPEC core globals
271
	 * @uses wpsc_core_setup_cart()                 Sets up the WPEC core cart
272
	 * @uses wpsc_core_load_thumbnail_sizes()       Sets up the core WPEC thumbnail sizes
273
	 * @uses wpsc_core_load_purchase_log_statuses() Loads the statuses for the purchase logs
274
	 * @uses wpsc_core_load_checkout_data()         Sets up the core WPEC form checkout data
275
	 * @uses wpsc_core_load_gateways()              Loads the merchants from the directory
276
	 * @uses wpsc_core_load_shipping_modules()      Gets shipping modules from the shipping directory
277
	 * @uses wpsc_core_load_page_titles()           Loads the core WPEC pagetitles
278
	 * @uses do_action()                            Calls 'wpsc_loaded' which runs after WPEC is fully loaded
279
	 */
280
	public function load() {
281
		// Before setup
282
		do_action( 'wpsc_pre_load' );
283
284
		// Legacy action
285
		do_action( 'wpsc_before_init' );
286
287
		// Setup the core WPEC globals
288
		wpsc_core_setup_globals();
289
290
		add_action( 'init', '_wpsc_action_setup_customer', 1 );
291
292
		// WPEC is ready to use as soon as WordPress and customer is setup and loaded
293
		add_action( 'init', array( $this, '_wpsc_fire_ready_action' ), 100 );
294
295
		// Load the purchase log statuses
296
		wpsc_core_load_purchase_log_statuses();
297
298
		// Load unique names and checkout form types
299
		wpsc_core_load_checkout_data();
300
301
		// Load the gateways
302
		wpsc_core_load_gateways();
303
304
		// Load the shipping modules
305
		wpsc_core_load_shipping_modules();
306
307
		// WPEC is fully loaded
308
		do_action( 'wpsc_loaded' );
309
	}
310
311
	public function _wpsc_fire_ready_action() {
312
		// WPEC is ready to use as soon as WordPress and customer is setup and loaded
313
		do_action( 'wpsc_ready' );
314
	}
315
316
	/**
317
	 * WPEC Activation Hook
318
	 *
319
	 * @uses deactivate_plugins()     Deactivates plugins by string
320
	 * @uses wp_die()                 Kills loading and returns the HTML
321
	 * @uses wpsc_install()           Performs checks to see if this is a clean install or not
322
	 */
323
	public function install() {
324
325
		if ( ! defined( 'WPSC_FILE_PATH' ) ) {
326
			define( 'WPSC_FILE_PATH', dirname( __FILE__ ) );
327
		}
328
329
		require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-installer.php' );
330
		$this->constants();
331
		wpsc_install();
332
	}
333
334
	/**
335
	 * Runs the WPEC deactivation routines.
336
	 *
337
	 * @uses wp_get_schedules()           Retrieves all filtered WP_Cron recurrences
338
	 * @uses wp_clear_scheduled_hook()    Removes any hooks on WP_Cron
339
	 */
340
	public function deactivate() {
341
		foreach ( wp_get_schedules() as $cron => $schedule ) {
342
			wp_clear_scheduled_hook( "wpsc_{$cron}_cron_task" );
343
		}
344
	}
345
}
346
347
// Start WPEC
348
$wpec = new WP_eCommerce();
349
350
// Activation
351
register_activation_hook( __FILE__  , array( $wpec, 'install' ) );
352
register_deactivation_hook( __FILE__, array( $wpec, 'deactivate' ) );