Completed
Push — master ( ab284c...4fdc7c )
by Justin
07:07
created

wpsc-installer.php ➔ wpsc_install()   F

Complexity

Conditions 27
Paths > 20000

Size

Total Lines 302
Code Lines 191

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 27
eloc 191
c 2
b 0
f 0
nc 2621440
nop 0
dl 0
loc 302
rs 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
function wpsc_auto_update() {
4
	global $wpdb;
5
6
	include( WPSC_FILE_PATH . '/wpsc-updates/updating_tasks.php' );
7
8
	wpsc_create_or_update_tables();
9
	wpsc_create_upload_directories();
10
	wpsc_product_files_htaccess();
11
	wpsc_check_and_copy_files();
12
13
	$wpsc_version = get_option( 'wpsc_version' );
14
	$wpsc_minor_version = get_option( 'wpsc_minor_version' );
15
16
	if ( $wpsc_version === false )
17
		add_option( 'wpsc_version', WPSC_VERSION, '', 'no' );
18
	else
19
		update_option( 'wpsc_version', WPSC_VERSION );
20
21
	if ( $wpsc_minor_version === false )
22
		add_option( 'wpsc_minor_version', WPSC_MINOR_VERSION, '', 'no' );
23
	else
24
		update_option( 'wpsc_minor_version', WPSC_MINOR_VERSION );
25
26
	if ( version_compare( $wpsc_version, '3.8', '<' ) )
27
		update_option( 'wpsc_needs_update', true );
28
	else
29
		update_option( 'wpsc_needs_update', false );
30
}
31
32
function wpsc_install() {
33
	global $wpdb, $user_level, $wp_rewrite, $wp_version, $wpsc_page_titles;
34
35
	$table_name    = $wpdb->prefix . "wpsc_product_list";
36
37
	if( $wpdb->get_var("SHOW TABLES LIKE '$table_name'") !== $table_name ) {
38
		// Table doesn't exist
39
		add_option( 'wpsc_purchaselogs_fixed', true );
40
	}
41
42
	// the only consistent and reliable way to detect whether this is a fresh install is by checking
43
	// whether WPSC_TABLE_CART_CONTENTS exists. This is an unfortunate hack, but we can do away with
44
	// it in 3.9 as we'll drop support for 3.7.x then
45
	if ( $wpdb->get_var( "SHOW TABLES LIKE '" . WPSC_TABLE_CART_CONTENTS . "'" ) != WPSC_TABLE_CART_CONTENTS )
46
		add_option( 'wpsc_db_version', WPSC_DB_VERSION, '', 'no' );
47
48
	// run the create or update code here.
49
	wpsc_create_or_update_tables();
50
	wpsc_create_upload_directories();
51
52
	// All code to add new database tables and columns must be above here
53
	$wpsc_version       = get_option( 'wpsc_version', 0 );
54
	$wpsc_minor_version = get_option( 'wpsc_minor_version', 0 );
55
56
	if ( $wpsc_version === false ) {
57
		add_option( 'wpsc_version', WPSC_VERSION, '', 'no' );
58
	} else {
59
		update_option( 'wpsc_version', WPSC_VERSION );
60
	}
61
62
	if ( $wpsc_minor_version === false )
63
		add_option( 'wpsc_minor_version', WPSC_MINOR_VERSION, '', 'no' );
64
	else
65
		update_option( 'wpsc_minor_version', WPSC_MINOR_VERSION );
66
67
	if ( version_compare( $wpsc_version, '3.8', '<' ) )
68
		update_option( 'wpsc_needs_update', true );
69
	else
70
		update_option( 'wpsc_needs_update', false );
71
72
	if('' == get_option('show_subcatsprods_in_cat'))
73
		update_option('show_subcatsprods_in_cat',0);
74
75
	if('' == get_option('wpsc_share_this'))
76
		update_option('wpsc_share_this',0);
77
78
	if('' == get_option('wpsc_crop_thumbnails'))
79
		update_option('wpsc_crop_thumbnails',0);
80
81
	if('' == get_option('wpsc_products_per_page'))
82
		update_option('wpsc_products_per_page',0);
83
84
	if('' == get_option('wpsc_force_ssl'))
85
		update_option('wpsc_force_ssl',0);
86
87
	if('' == get_option('use_pagination'))
88
		update_option('use_pagination',0);
89
90
	if('' == get_option('hide_name_link'))
91
		update_option('hide_name_link',0);
92
93
	if('' == get_option('wpsc_enable_comments'))
94
		update_option('wpsc_enable_comments',0);
95
96
	if('' == get_option('multi_add'))
97
		update_option('multi_add',1);
98
99
	if('' == get_option('hide_addtocart_button'))
100
		update_option('hide_addtocart_button',0);
101
102
	if('' == get_option('wpsc_addtocart_or_buynow'))
103
		update_option('wpsc_addtocart_or_buynow',0);
104
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
105
106
	add_option( 'show_thumbnails', 1, '', 'no' );
107
	add_option( 'show_thumbnails_thickbox', 1, '', 'no' );
108
109
	require_once( WPSC_FILE_PATH . '/wpsc-core/wpsc-functions.php' );
110
	require_once( WPSC_FILE_PATH . '/wpsc-includes/wpsc-theme-engine-bootstrap.php' );
111
112
	$te = get_option( 'wpsc_get_active_theme_engine', '1.0' );
113
114
	if ( '1.0' == $te ) {
115
		add_option( 'product_list_url', '', '', 'no' );
116
		add_option( 'shopping_cart_url', '', '', 'no' );
117
		add_option( 'checkout_url', '', '', 'no' );
118
		add_option( 'transact_url', '', '', 'no' );
119
		/*
120
		 * This part creates the pages and automatically puts their URLs into the options page.
121
		 * As you can probably see, it is very easily extendable, just pop in your page and the deafult content in the array and you are good to go.
122
		 */
123
		$post_date = date( "Y-m-d H:i:s" );
124
		$post_date_gmt = gmdate( "Y-m-d H:i:s" );
125
126
		$pages = array(
127
			'products-page' => array(
128
				'name' => 'products-page',
129
				'title' => __( 'Products Page', 'wp-e-commerce' ),
130
				'tag' => '[productspage]',
131
				'option' => 'product_list_url'
132
			),
133
			'checkout' => array(
134
				'name' => 'checkout',
135
				'title' => __( 'Checkout', 'wp-e-commerce' ),
136
				'tag' => '[shoppingcart]',
137
				'option' => 'shopping_cart_url'
138
			),
139
			'transaction-results' => array(
140
				'name' => 'transaction-results',
141
				'title' => __( 'Transaction Results', 'wp-e-commerce' ),
142
				'tag' => '[transactionresults]',
143
				'option' => 'transact_url'
144
			),
145
			'your-account' => array(
146
				'name' => 'your-account',
147
				'title' => __( 'Your Account', 'wp-e-commerce' ),
148
				'tag' => '[userlog]',
149
				'option' => 'user_account_url'
150
			)
151
		);
152
153
		//indicator. if we will create any new pages we need to flush.. :)
154
		$newpages = false;
155
156
		//get products page id. if there's no products page then create one
157
		$products_page_id = $wpdb->get_var("SELECT id FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%" . $pages['products-page']['tag'] . "%'	AND `post_type` != 'revision'");
158
		if( empty($products_page_id) ){
159
			$products_page_id = wp_insert_post( array(
160
				'post_title' 	=>	$pages['products-page']['title'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
161
				'post_type' 	=>	'page',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
162
				'post_name'		=>	$pages['products-page']['name'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
163
				'comment_status'=>	'closed',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
164
				'ping_status' 	=>	'closed',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
165
				'post_content' 	=>	$pages['products-page']['tag'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
166
				'post_status' 	=>	'publish',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
167
				'post_author' 	=>	1,
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
168
				'menu_order'	=>	0
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
169
			));
170
			$newpages = true;
171
		}
172
		update_option( $pages['products-page']['option'], _get_page_link($products_page_id) );
173
		//done. products page created. no we can unset products page data and create all other pages.
174
175
		//unset products page
176
		unset($pages['products-page']);
177
178
		//create other pages
179
		foreach( (array)$pages as $page ){
180
			//check if page exists and get it's ID
181
			$page_id = $wpdb->get_var("SELECT id FROM `" . $wpdb->posts . "` WHERE `post_content` LIKE '%" . $page['tag'] . "%'	AND `post_type` != 'revision'");
182
			//if there's no page - create
183
			if( empty($page_id) ){
184
				$page_id = wp_insert_post( array(
185
					'post_title' 	=>	$page['title'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
186
					'post_type' 	=>	'page',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
187
					'post_name'		=>	$page['name'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
188
					'comment_status'=>	'closed',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
189
					'ping_status' 	=>	'closed',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
190
					'post_content' 	=>	$page['tag'],
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
191
					'post_status' 	=>	'publish',
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
192
					'post_author' 	=>	1,
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
193
					'menu_order'	=>	0,
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
194
					'post_parent'	=>	$products_page_id
0 ignored issues
show
introduced by
Expected 1 space after "=>"; 2 found
Loading history...
195
				));
196
				$newpages = true;
197
			}
198
			//update option
199
			update_option( $page['option'], get_permalink( $page_id ) );
200
			//also if this is shopping_cart, then update checkout url option
201
			if ( $page['option'] == 'shopping_cart_url' )
202
				update_option( 'checkout_url', get_permalink( $page_id ) );
203
		}
204
205
		//if we have created any new pages, then flush... do we need to do this? probably should be removed
206
		if ( $newpages ) {
207
			wp_cache_delete( 'all_page_ids', 'pages' );
208
			wpsc_update_permalink_slugs();
209
		}
210
	}
211
212
	add_option( 'payment_gateway', '','', 'no' );
213
214
	$default_payment_gateways_names = array(
215
		'chronopay'						=> '',
216
		'wpsc_merchant_paypal_express'	=> '',
217
		'wpsc_merchant_paypal_pro'		=> '',
218
		'wpsc_merchant_paypal_standard'	=> '',
219
		'amazon-payments'           	=> ''
220
	);
221
222
	$existing_payment_gateways_names = get_option( 'payment_gateway_names' );
223
224
	$new_payment_gateways_name = array_merge( $default_payment_gateways_names, (array) $existing_payment_gateways_names);
225
	update_option( 'payment_gateway_names', $new_payment_gateways_name );
226
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
227
228
	if ( function_exists( 'register_sidebar' ) )
229
		add_option( 'cart_location', '4','', 'no' );
230
	else
231
		add_option( 'cart_location', '1', '', 'no' );
232
233
	add_option( 'currency_type', '136','', 'no' );
234
	add_option( 'currency_sign_location', '3', '', 'no' );
235
236
	add_option( 'gst_rate', '1','', 'no' );
237
238
	add_option( 'max_downloads', '1','', 'no' );
239
240
	add_option( 'display_pnp', '1', '', 'no' );
241
242
	add_option( 'display_specials', '1', '', 'no' );
243
	add_option( 'do_not_use_shipping', '0', '', 'no' );
244
245
	add_option( 'postage_and_packaging', '0','', 'no' );
246
    add_option( 'shipwire', '0', '', 'no' );
247
    add_option( 'shipwire_test_server', '0', '', 'no' );
248
249
	add_option( 'purch_log_email', get_option( 'admin_email', '' ), '', 'no' );
250
	add_option( 'return_email', '', '', 'no' );
251
	add_option( 'terms_and_conditions', '', '', 'no' );
252
253
	add_option( 'default_brand', 'none', '', 'no' );
254
	add_option( 'wpsc_default_category', 'all', '', 'no' );
255
256
	add_option( 'product_view', 'default', "", 'no' );
257
	add_option( 'add_plustax', 'default', "", '1' );
258
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
259
260
	if ( !((get_option( 'show_categorybrands' ) > 0) && (get_option( 'show_categorybrands' ) < 3)) )
261
		update_option( 'show_categorybrands', 2 );
262
263
	// PayPal options
264
	add_option( 'paypal_business', '', '', 'no' );
265
	add_option( 'paypal_url', '', '', 'no' );
266
	add_option( 'paypal_ipn', '1', '', 'no' );
267
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
268
269
	add_option( 'paypal_multiple_business', '', '', 'no' );
270
271
	add_option( 'paypal_multiple_url', "https://www.paypal.com/cgi-bin/webscr" );
272
273
	add_option( 'product_ratings', '0', '', 'no' );
274
	add_option( 'wpsc_email_receipt', __( 'Thank you for purchasing with %shop_name%, any items to be shipped will be processed as soon as possible, any items that can be downloaded can be downloaded using the links on this page. All prices include tax and postage and packaging where applicable.
275
You ordered these items:
276
%product_list%%total_shipping%%total_price%', 'wp-e-commerce' ), '', 'no' );
277
278
	add_option( 'wpsc_email_admin', __( '%product_list%%total_shipping%%total_price%', 'wp-e-commerce' ), '','no' );
279
280
	add_option( 'wpsc_selected_theme', 'default', '', 'no' );
281
282
	add_option( 'product_image_height', 148);
283
	add_option( 'product_image_width', 148);
284
285
	add_option( 'category_image_height', 148 );
286
	add_option( 'category_image_width', 148 );
287
288
	add_option( 'single_view_image_height', 148 );
289
	add_option( 'single_view_image_width', 148 );
290
291
	add_option( 'wpsc_gallery_image_height', 31 );
292
	add_option( 'wpsc_gallery_image_width', 31 );
293
294
	add_option( 'wpsc_thousands_separator', ',' );
295
	add_option( 'wpsc_decimal_separator', '.' );
296
297
	add_option( 'custom_gateway_options', array( 'wpsc_merchant_testmode' ), '', 'no' );
298
299
	add_option( 'wpsc_category_url_cache', array(), '', 'no' );
300
301
	// add in some default tax settings
302
	add_option( 'wpec_taxes_inprice', 'exclusive' );
303
304
	add_option( 'wpec_taxes_product', 'replace' );
305
306
	add_option( 'wpec_taxes_logic', 'billing' );
307
308
	wpsc_product_files_htaccess();
309
310
	// Product categories, temporarily register them to create first default category if none exist
311
	// @todo: investigate those require once lines and move them to right place (not from here, but from their original location, which seems to be wrong, since i cant access wpsc_register_post_types and wpsc_update_categorymeta here) - Vales <[email protected]>
312
	wpsc_core_load_page_titles();
313
	wpsc_register_post_types();
314
	$category_list = get_terms( 'wpsc_product_category', 'hide_empty=0&parent=0' );
315
	if ( count( $category_list ) == 0 ) {
316
		require_once( WPSC_FILE_PATH . '/wpsc-includes/meta.functions.php' );
317
318
		$new_category = wp_insert_term( __( 'Product Category', 'wp-e-commerce' ), 'wpsc_product_category', "parent=0" );
319
		$category_id = $new_category['term_id'];
320
		$term = get_term_by( 'id', $new_category['term_id'], 'wpsc_product_category' );
321
		$url_name = $term->slug;
322
323
		wpsc_update_categorymeta( $category_id, 'nice-name', $url_name );
324
		wpsc_update_categorymeta( $category_id, 'description', __( "This is a description", 'wp-e-commerce' ) );
325
		wpsc_update_categorymeta( $category_id, 'image', '' );
326
		wpsc_update_categorymeta( $category_id, 'fee', '0' );
327
		wpsc_update_categorymeta( $category_id, 'active', '1' );
328
		wpsc_update_categorymeta( $category_id, 'order', '0' );
329
	}
330
331
	flush_rewrite_rules( false );
332
	wpsc_theme_engine_v2_activate();
333
}
334
335
function wpsc_product_files_htaccess() {
336
	if ( !is_file( WPSC_FILE_DIR . ".htaccess" ) ) {
337
		$htaccess = "order deny,allow\n\r";
338
		$htaccess .= "deny from all\n\r";
339
		$htaccess .= "allow from none\n\r";
340
		$filename = WPSC_FILE_DIR . ".htaccess";
341
		$file_handle = @ fopen( $filename, 'w+' );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
342
		@ fwrite( $file_handle, $htaccess );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using fwrite()
Loading history...
343
		@ fclose( $file_handle );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
344
		@ chmod( $file_handle, 0665 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using chmod()
Loading history...
345
	}
346
}
347
348
function wpsc_check_and_copy_files() {
349
	$upload_path = 'wp-content/plugins/' . WPSC_DIR_NAME;
350
351
	$wpsc_dirs['files']['old'] = ABSPATH . "{$upload_path}/files/";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$wpsc_dirs was never initialized. Although not strictly required by PHP, it is generally a good practice to add $wpsc_dirs = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
352
	$wpsc_dirs['files']['new'] = WPSC_FILE_DIR;
353
354
	$wpsc_dirs['previews']['old'] = ABSPATH . "{$upload_path}/preview_clips/";
355
	$wpsc_dirs['previews']['new'] = WPSC_PREVIEW_DIR;
356
357
	// I don't include the thumbnails directory in this list, as it is a subdirectory of the images directory and is moved along with everything else
358
	$wpsc_dirs['images']['old'] = ABSPATH . "{$upload_path}/product_images/";
359
	$wpsc_dirs['images']['new'] = WPSC_IMAGE_DIR;
360
361
	$wpsc_dirs['categories']['old'] = ABSPATH . "{$upload_path}/category_images/";
362
	$wpsc_dirs['categories']['new'] = WPSC_CATEGORY_DIR;
363
	$incomplete_file_transfer = false;
364
365
	foreach ( $wpsc_dirs as $wpsc_dir ) {
366
		if ( is_dir( $wpsc_dir['old'] ) ) {
367
			$files_in_dir = glob( $wpsc_dir['old'] . "*" );
368
			$stat = stat( $wpsc_dir['new'] );
369
370
			if ( count( $files_in_dir ) > 0 ) {
371
				foreach ( $files_in_dir as $file_in_dir ) {
372
					$file_name = str_replace( $wpsc_dir['old'], '', $file_in_dir );
373
					if ( @ rename( $wpsc_dir['old'] . $file_name, $wpsc_dir['new'] . $file_name ) ) {
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using rename()
Loading history...
374
						if ( is_dir( $wpsc_dir['new'] . $file_name ) ) {
375
							$perms = $stat['mode'] & 0000775;
376
						} else {
377
							$perms = $stat['mode'] & 0000665;
378
						}
379
380
						@ chmod( ($wpsc_dir['new'] . $file_name ), $perms );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using chmod()
Loading history...
381
					} else {
382
						$incomplete_file_transfer = true;
383
					}
384
				}
385
			}
386
		}
387
	}
388
	if ( $incomplete_file_transfer == true ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
389
		add_option( 'wpsc_incomplete_file_transfer', 'default', "", 'true' );
390
	}
391
}
392
393
function wpsc_create_upload_directories() {
394
395
	// Create the required folders
396
	$folders = array(
397
		WPSC_UPLOAD_DIR,
398
		WPSC_FILE_DIR,
399
		WPSC_PREVIEW_DIR,
400
		WPSC_IMAGE_DIR,
401
		WPSC_THUMBNAIL_DIR,
402
		WPSC_CATEGORY_DIR,
403
		WPSC_USER_UPLOADS_DIR,
404
		WPSC_CACHE_DIR,
405
		WPSC_UPGRADES_DIR,
406
		// WPSC_THEMES_PATH
407
	);
408
	foreach ( $folders as $folder ) {
409
		wp_mkdir_p( $folder );
410
		@ chmod( $folder, 0775 );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
introduced by
Filesystem writes are forbidden, you should not be using chmod()
Loading history...
411
	}
412
}
413
414
function wpsc_copy_themes_to_uploads() {
415
	$old_theme_path = WPSC_CORE_THEME_PATH;
416
	$new_theme_path = WPSC_THEMES_PATH;
417
	$new_dir = @ opendir( $new_theme_path );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
418
	$num = 0;
419
	$file_names = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
420
	while ( ($file = @ readdir( $new_dir )) !== false ) {
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
421
		if ( is_dir( $new_theme_path . $file ) && ($file != "..") && ($file != ".") ) {
422
			$file_names[] = $file;
423
		}
424
	}
425
	if ( count( $file_names ) < 1 ) {
426
		$old_dir = @ opendir( $old_theme_path );
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
427
		while ( ($file = @ readdir( $old_dir )) !== false ) {
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
428
			if ( is_dir( $old_theme_path . $file ) && ($file != "..") && ($file != ".") ) {
429
				@ wpsc_recursive_copy( $old_theme_path . $file, $new_theme_path . $file );
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
Coding Style introduced by
Silencing errors is discouraged
Loading history...
430
			}
431
		}
432
	}
433
}
434
435
/**
436
 * wpsc_create_or_update_tables count function,
437
 * * @return boolean true on success, false on failure
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean|array? Also, consider making the array more specific, something like array<String>, or String[].

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

If the return type contains the type array, this check recommends the use of a more specific type like String[] or array<String>.

Loading history...
438
 */
439
function wpsc_create_or_update_tables( $debug = false ) {
440
	global $wpdb;
441
	// creates or updates the structure of the shopping cart tables
442
443
	include( WPSC_FILE_PATH . '/wpsc-updates/database_template.php' );
444
445
	$template_hash = sha1( serialize( $wpsc_database_template ) );
0 ignored issues
show
Bug introduced by
The variable $wpsc_database_template seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
446
447
	// Filter for adding to or altering the wpsc database template, make sure you return the array your function gets passed, else you will break updating the database tables
448
	$wpsc_database_template = apply_filters( 'wpsc_alter_database_template', $wpsc_database_template );
0 ignored issues
show
Bug introduced by
The variable $wpsc_database_template seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
449
450
	$failure_reasons = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
451
	$upgrade_failed = false;
452
	foreach ( (array)$wpsc_database_template as $table_name => $table_data ) {
453
		// check that the table does not exist under the correct name, then checkk if there was a previous name, if there was, check for the table under that name too.
454
		if ( !$wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) && (!isset( $table_data['previous_names'] ) || (isset( $table_data['previous_names'] ) && !$wpdb->get_var( "SHOW TABLES LIKE '{$table_data['previous_names']}'" )) ) ) {
455
			//if the table does not exixt, create the table
456
			$constructed_sql_parts = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
457
			$constructed_sql = "CREATE TABLE `{$table_name}` (\n";
458
459
			// loop through the columns
460
			foreach ( (array)$table_data['columns'] as $column => $properties ) {
461
				$constructed_sql_parts[] = "`$column` $properties";
462
			}
463
			// then through the indexes
464
			foreach ( (array)$table_data['indexes'] as $properties ) {
465
				$constructed_sql_parts[] = "$properties";
466
			}
467
			$constructed_sql .= implode( ",\n", $constructed_sql_parts );
468
			$constructed_sql .= "\n) ENGINE=MyISAM";
469
0 ignored issues
show
Coding Style introduced by
Functions must not contain multiple empty lines in a row; found 2 empty lines
Loading history...
470
471
			// if mySQL is new enough, set the character encoding
472
			if ( method_exists( $wpdb, 'db_version' ) && version_compare( $wpdb->db_version(), '4.1', '>=' ) ) {
473
				$constructed_sql .= " CHARSET=utf8";
474
			}
475
			$constructed_sql .= ";";
476
477
			if ( !$wpdb->query( $constructed_sql ) ) {
478
				$upgrade_failed = true;
479
				$failure_reasons[] = $wpdb->last_error;
480
			}
481
482
			if ( isset( $table_data['actions']['after']['all'] ) && is_callable( $table_data['actions']['after']['all'] ) ) {
483
				$table_data['actions']['after']['all']();
484
			}
485
		} else {
486
			// check to see if the new table name is in use
487
			if ( !$wpdb->get_var( "SHOW TABLES LIKE '$table_name'" ) && (isset( $table_data['previous_names'] ) && $wpdb->get_var( "SHOW TABLES LIKE '{$table_data['previous_names']}'" )) ) {
488
				$wpdb->query( "ALTER TABLE	`{$table_data['previous_names']}` RENAME TO `{$table_name}`;" );
489
				$failure_reasons[] = $wpdb->last_error;
490
			}
491
492
			//check to see if the table needs updating
493
			$existing_table_columns = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
494
			//check and possibly update the character encoding
495
			if ( method_exists( $wpdb, 'db_version' ) && version_compare( $wpdb->db_version(), '4.1', '>=' ) ) {
496
				$table_status_data = $wpdb->get_row( "SHOW TABLE STATUS LIKE '$table_name'", ARRAY_A );
497
				if ( $table_status_data['Collation'] != 'utf8_general_ci' ) {
498
					$wpdb->query( "ALTER TABLE `$table_name`	DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci" );
499
				}
500
			}
501
502
			if ( isset( $table_data['actions']['before']['all'] ) && is_callable( $table_data['actions']['before']['all'] ) ) {
503
				$table_data['actions']['before']['all']();
504
			}
505
506
			//get the column list
507
			$existing_table_column_data = $wpdb->get_results( "SHOW FULL COLUMNS FROM `$table_name`", ARRAY_A );
508
509
			foreach ( (array)$existing_table_column_data as $existing_table_column ) {
510
				$column_name = $existing_table_column['Field'];
511
				$existing_table_columns[] = $column_name;
512
513
				$null_match = false;
514
				if ( 'NO' == $existing_table_column['Null'] ) {
515
					if ( isset( $table_data['columns'][$column_name] ) && stristr( $table_data['columns'][$column_name], "NOT NULL" ) !== false ) {
516
						$null_match = true;
517
					}
518
				} else {
519
					if ( isset( $table_data['columns'][$column_name] ) && stristr( $table_data['columns'][$column_name], "NOT NULL" ) === false ) {
520
						$null_match = true;
521
					}
522
				}
523
524
				if ( isset( $table_data['columns'][$column_name] ) && ((stristr( $table_data['columns'][$column_name], $existing_table_column['Type'] ) === false) || ($null_match != true)) ) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
525
					if ( isset( $table_data['actions']['before'][$column_name] ) && is_callable( $table_data['actions']['before'][$column_name] ) ) {
526
						$table_data['actions']['before'][$column_name]( $column_name );
527
					}
528
					if ( !$wpdb->query( "ALTER TABLE `$table_name` CHANGE `$column_name` `$column_name` {$table_data['columns'][$column_name]} " ) ) {
529
						$upgrade_failed = true;
530
						$failure_reasons[] = $wpdb->last_error;
531
					}
532
				}
533
			}
534
			$supplied_table_columns = array_keys( $table_data['columns'] );
535
536
			// compare the supplied and existing columns to find the differences
537
			$missing_or_extra_table_columns = array_diff( $supplied_table_columns, $existing_table_columns );
538
539
			if ( count( $missing_or_extra_table_columns ) > 0 ) {
540
				foreach ( (array)$missing_or_extra_table_columns as $missing_or_extra_table_column ) {
541
					if ( isset( $table_data['columns'][$missing_or_extra_table_column] ) ) {
542
						//table column is missing, add it
543
						$index = array_search( $missing_or_extra_table_column, $supplied_table_columns ) - 1;
544
545
						$previous_column = isset( $supplied_table_columns[$index] ) ? $supplied_table_columns[$index] : '';
546
						if ( $previous_column != '' ) {
547
							$previous_column = "AFTER `$previous_column`";
548
						}
549
						$constructed_sql = "ALTER TABLE `$table_name` ADD `$missing_or_extra_table_column` " . $table_data['columns'][$missing_or_extra_table_column] . " $previous_column;";
550
						if ( !$wpdb->query( $constructed_sql ) ) {
551
							$upgrade_failed = true;
552
							$failure_reasons[] = $wpdb->last_error;
553
						}
554
						// run updating functions to do more complex work with default values and the like
555
						if ( isset( $table_data['actions']['after'][$missing_or_extra_table_column] ) && is_callable( $table_data['actions']['after'][$missing_or_extra_table_column] ) ) {
556
							$table_data['actions']['after'][$missing_or_extra_table_column]( $missing_or_extra_table_column );
557
						}
558
					}
559
				}
560
			}
561
562
			if ( isset( $table_data['actions']['after']['all'] ) && is_callable( $table_data['actions']['after']['all'] ) ) {
563
				$table_data['actions']['after']['all']();
564
			}
565
			// get the list of existing indexes
566
			$existing_table_index_data = $wpdb->get_results( "SHOW INDEX FROM `$table_name`", ARRAY_A );
567
			$existing_table_indexes = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
568
			foreach ( $existing_table_index_data as $existing_table_index ) {
569
				$existing_table_indexes[] = $existing_table_index['Key_name'];
570
			}
571
572
			$existing_table_indexes = array_unique( $existing_table_indexes );
573
			$supplied_table_indexes = array_keys( $table_data['indexes'] );
574
575
			// compare the supplied and existing indxes to find the differences
576
			$missing_or_extra_table_indexes = array_diff( $supplied_table_indexes, $existing_table_indexes );
577
578
			if ( count( $missing_or_extra_table_indexes ) > 0 ) {
579
				foreach ( $missing_or_extra_table_indexes as $missing_or_extra_table_index ) {
580
					if ( isset( $table_data['indexes'][$missing_or_extra_table_index] ) ) {
581
						$constructed_sql = "ALTER TABLE `$table_name` ADD " . $table_data['indexes'][$missing_or_extra_table_index] . ";";
582
						if ( !$wpdb->query( $constructed_sql ) ) {
583
							$upgrade_failed = true;
584
							$failure_reasons[] = $wpdb->last_error;
585
						}
586
					}
587
				}
588
			}
589
		}
590
	}
591
592
	if ( $upgrade_failed !== true ) {
593
		update_option( 'wpsc_database_check', $template_hash );
594
		return true;
595
	} else {
596
		return $failure_reasons;
597
	}
598
}
599
600
/**
601
 * The following functions are used exclusively in database_template.php
602
 */
603
604
/**
605
 * wpsc_add_currency_list function,	converts values to decimal to satisfy mySQL strict mode
606
 * * @return boolean true on success, false on failure
607
 */
608
function wpsc_add_currency_list() {
609
	global $wpdb, $currency_sql;
610
	require_once(WPSC_FILE_PATH . "/wpsc-updates/currency_list.php");
611
	$currency_data = $wpdb->get_var( "SELECT COUNT(*) AS `count` FROM `" . WPSC_TABLE_CURRENCY_LIST . "`" );
612
	if ( $currency_data == 0 ) {
613
		$currency_array = explode( "\n", $currency_sql );
614
		foreach ( $currency_array as $currency_row ) {
615
			$wpdb->query( $currency_row );
616
		}
617
	}
618
}
619
620
/**
621
 * wpsc_add_region_list function,	converts values to decimal to satisfy mySQL strict mode
622
 * * @return boolean true on success, false on failure
623
 */
624
function wpsc_add_region_list() {
625
	global $wpdb;
626
	$add_regions = $wpdb->get_var( "SELECT COUNT(*) AS `count` FROM `" . WPSC_TABLE_REGION_TAX . "`" );
627
	if ( $add_regions < 1 ) {
628
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Alberta', 'AB', '0')" );
629
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'British Columbia', 'BC', '0')" );
630
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Manitoba', 'MB', '0')" );
631
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'New Brunswick', 'NB', '0')" );
632
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Newfoundland and Labrador', 'NL', '0')" );
633
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Northwest Territories', 'NT', '0')" );
634
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Nova Scotia', 'NS', '0')" );
635
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Nunavut', 'NU', '0')" );
636
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Ontario', 'ON', '0')" );
637
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Prince Edward Island', 'PE', '0')" );
638
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Quebec', 'QC', '0')" );
639
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Saskatchewan', 'SK', '0')" );
640
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '100', 'Yukon', 'YK', '0')" );
641
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Alabama', 'AL', '0')" );
642
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Alaska', 'AK', '0')" );
643
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Arizona', 'AZ', '0')" );
644
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Arkansas', 'AR', '0')" );
645
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'California', 'CA', '0')" );
646
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Colorado', 'CO', '0')" );
647
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Connecticut', 'CT', '0')" );
648
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Delaware', 'DE', '0')" );
649
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Florida', 'FL', '0')" );
650
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Georgia', 'GA', '0')" );
651
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Hawaii', 'HI', '0')" );
652
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Idaho', 'ID', '0')" );
653
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Illinois', 'IL', '0')" );
654
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Indiana', 'IN', '0')" );
655
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Iowa', 'IA', '0')" );
656
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Kansas', 'KS', '0')" );
657
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Kentucky', 'KY', '0')" );
658
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Louisiana', 'LA', '0')" );
659
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Maine', 'ME', '0')" );
660
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Maryland', 'MD', '0')" );
661
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Massachusetts', 'MA', '0')" );
662
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Michigan', 'MI', '0')" );
663
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Minnesota', 'MN', '0')" );
664
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Mississippi', 'MS', '0')" );
665
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Missouri', 'MO', '0')" );
666
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Montana', 'MT', '0')" );
667
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Nebraska', 'NE', '0')" );
668
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Nevada', 'NV', '0')" );
669
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'New Hampshire', 'NH', '0')" );
670
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'New Jersey', 'NJ', '0')" );
671
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'New Mexico', 'NM', '0')" );
672
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'New York', 'NY', '0')" );
673
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'North Carolina', 'NC', '0')" );
674
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'North Dakota', 'ND', '0')" );
675
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Ohio', 'OH', '0')" );
676
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Oklahoma', 'OK', '0')" );
677
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Oregon', 'OR', '0')" );
678
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Pennsylvania', 'PA', '0')" );
679
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Rhode Island', 'RI', '0')" );
680
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'South Carolina', 'SC', '0')" );
681
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'South Dakota', 'SD', '0')" );
682
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Tennessee', 'TN', '0')" );
683
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Texas', 'TX', '0')" );
684
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Utah', 'UT', '0')" );
685
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Vermont', 'VT', '0')" );
686
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Virginia', 'VA', '0')" );
687
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Washington', 'WA', '0')" );
688
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Washington DC', 'DC', '0')" );
689
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'West Virginia', 'WV', '0')" );
690
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Wisconsin', 'WI', '0')" );
691
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_REGION_TAX . "` ( `country_id` , `name` ,`code`, `tax` ) VALUES ( '136', 'Wyoming', 'WY', '0')" );
692
	}
693
694
	if ( $wpdb->get_var( "SELECT COUNT(*) FROM `" . WPSC_TABLE_REGION_TAX . "` WHERE `code`=''" ) > 0 ) {
695
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'AB' WHERE `name` IN('Alberta') LIMIT 1 ;" );
696
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'BC' WHERE `name` IN('British Columbia') LIMIT 1 ;" );
697
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'MB' WHERE `name` IN('Manitoba') LIMIT 1 ;" );
698
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'NK' WHERE `name` IN('New Brunswick') LIMIT 1 ;" );
699
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'NF' WHERE `name` IN('Newfoundland') LIMIT 1 ;" );
700
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'NT' WHERE `name` IN('Northwest Territories') LIMIT 1 ;" );
701
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'NS' WHERE `name` IN('Nova Scotia') LIMIT 1 ;" );
702
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'ON' WHERE `name` IN('Ontario') LIMIT 1 ;" );
703
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'PE' WHERE `name` IN('Prince Edward Island') LIMIT 1 ;" );
704
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'PQ' WHERE `name` IN('Quebec') LIMIT 1 ;" );
705
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'SN' WHERE `name` IN('Saskatchewan') LIMIT 1 ;" );
706
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'YT' WHERE `name` IN('Yukon') LIMIT 1 ;" );
707
		$wpdb->query( "UPDATE `" . WPSC_TABLE_REGION_TAX . "` SET `code` = 'NU' WHERE `name` IN('Nunavut') LIMIT 1 ;" );
708
	}
709
}
710
711
/**
712
 * wpsc_add_checkout_fields function,	converts values to decimal to satisfy mySQL strict mode
713
 * * @return boolean true on success, false on failure
714
 */
715
function wpsc_add_checkout_fields() {
716
	global $wpdb;
717
	$data_forms = $wpdb->get_results( "SELECT COUNT(*) AS `count` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "`", ARRAY_A );
718
719
	if ( isset( $data_forms[0] ) && $data_forms[0]['count'] == 0 ) {
720
721
		$sql = " INSERT INTO `" . WPSC_TABLE_CHECKOUT_FORMS . "` ( `name`, `type`, `mandatory`, `display_log`, `default`, `active`, `checkout_order`, `unique_name`) VALUES ( '" . __( 'Your billing/contact details', 'wp-e-commerce' ) . "', 'heading', '0', '0', '1', '1', 1,''),
722
	( '" . __( 'First Name', 'wp-e-commerce' ) . "', 'text', '1', '1', '1', '1', 2,'billingfirstname'),
723
	( '" . __( 'Last Name', 'wp-e-commerce' ) . "', 'text', '1', '1', '1', '1', 3,'billinglastname'),
724
	( '" . __( 'Address', 'wp-e-commerce' ) . "', 'address', '1', '0', '1', '1', 4,'billingaddress'),
725
	( '" . __( 'City', 'wp-e-commerce' ) . "', 'city', '1', '0', '1', '1', 5,'billingcity'),
726
	( '" . __( 'State', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 6,'billingstate'),
727
	( '" . __( 'Country', 'wp-e-commerce' ) . "', 'country', '1', '0', '1', '1', 7,'billingcountry'),
728
	( '" . __( 'Postal Code', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 8,'billingpostcode'),
729
	( '" . __( 'Email', 'wp-e-commerce' ) . "', 'email', '1', '1', '1', '1', 9,'billingemail'),
730
	( '" . __( 'Shipping Address', 'wp-e-commerce' ) . "', 'heading', '0', '0', '1', '1', 10,'delivertoafriend'),
731
	( '" . __( 'First Name', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 11,'shippingfirstname'),
732
	( '" . __( 'Last Name', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 12,'shippinglastname'),
733
	( '" . __( 'Address', 'wp-e-commerce' ) . "', 'address', '0', '0', '1', '1', 13,'shippingaddress'),
734
	( '" . __( 'City', 'wp-e-commerce' ) . "', 'city', '0', '0', '1', '1', 14,'shippingcity'),
735
	( '" . __( 'State', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 15,'shippingstate'),
736
	( '" . __( 'Country', 'wp-e-commerce' ) . "', 'delivery_country', '0', '0', '1', '1', 16,'shippingcountry'),
737
	( '" . __( 'Postal Code', 'wp-e-commerce' ) . "', 'text', '0', '0', '1', '1', 17,'shippingpostcode');";
738
739
		$wpdb->query( $sql );
740
		$wpdb->query( "INSERT INTO `" . WPSC_TABLE_CHECKOUT_FORMS . "` ( `name`, `type`, `mandatory`, `display_log`, `default`, `active`, `checkout_order`, `unique_name` ) VALUES ( '" . __( 'Phone', 'wp-e-commerce' ) . "', 'text', '0', '0', '', '1', '8','billingphone');" );
741
	}
742
}
743
function wpsc_rename_checkout_column(){
744
	global $wpdb;
745
	$sql = "SHOW COLUMNS FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` LIKE 'checkout_order'";
746
	$col = $wpdb->get_results($sql);
747
	if(empty($col)){
748
		$sql = "ALTER TABLE  `" . WPSC_TABLE_CHECKOUT_FORMS . "` CHANGE  `order`  `checkout_order` INT( 10 ) UNSIGNED NOT NULL DEFAULT  '0'";
749
		$wpdb->query($sql);
750
	}
751
752
}
753
754
/**
755
 * In 3.8.8, we removed the ability for the user to delete or add core checkout fields (things like billingfirstname, billinglastname etc.) in order to reduce user error.
756
 * Mistakenly deleting or duplicating those fields could cause unexpected bugs with checkout form validation.
757
 *
758
 * Some users have encountered an issue where, if they had previously deleted a core checkout field, now they can't add it back again.
759
 * With this function, we should check to see whether any core fields are missing (by checking the uniquenames)
760
 * If there are some missing, we automatically generate those with the intended uniquename.
761
 *
762
 * We set the 'active' field to 0, so as to mitigate any unintended consequences of adding additional fields.
763
 *
764
 * @since 3.8.8.2
765
 * @return none
766
 */
767
function wpsc_3882_database_updates() {
768
	global $wpdb;
769
770
	// Check if we have done this before
771
	if ( version_compare( get_option( 'wpsc_version' ), '3.8.8.2', '>=' ) )
772
		return;
773
774
	$unique_names = array(
775
		'billingfirstname'  => __( 'First Name', 'wp-e-commerce' ),
776
		'billinglastname'   => __( 'Last Name', 'wp-e-commerce' ),
777
		'billingaddress'    => __( 'Address', 'wp-e-commerce' ),
778
		'billingcity'       => __( 'City', 'wp-e-commerce' ),
779
		'billingstate'      => __( 'State', 'wp-e-commerce' ),
780
		'billingcountry'    => __( 'Country', 'wp-e-commerce' ),
781
		'billingemail'      => __( 'Email', 'wp-e-commerce' ),
782
		'billingphone'      => __( 'Phone', 'wp-e-commerce' ),
783
		'billingpostcode'   => __( 'Postal Code', 'wp-e-commerce' ),
784
		'delivertoafriend'  => __( 'Shipping Address', 'wp-e-commerce' ),
785
		'shippingfirstname' => __( 'First Name', 'wp-e-commerce' ),
786
		'shippinglastname'  => __( 'Last Name', 'wp-e-commerce' ),
787
		'shippingaddress'   => __( 'Address', 'wp-e-commerce' ),
788
		'shippingcity'      => __( 'City', 'wp-e-commerce' ),
789
		'shippingstate'     => __( 'State', 'wp-e-commerce' ),
790
		'shippingcountry'   => __( 'Country', 'wp-e-commerce' ),
791
		'shippingpostcode'  => __( 'Postal Code', 'wp-e-commerce' ),
792
	);
793
794
	// Check if any uniquenames are missing
795
	$current_columns = array_filter( $wpdb->get_col( $wpdb->prepare( 'SELECT unique_name FROM ' . WPSC_TABLE_CHECKOUT_FORMS ) ) );
796
797
	$columns_to_add = array_diff_key( $unique_names, array_flip( $current_columns ) );
798
799
	if ( empty( $columns_to_add ) )
800
		return update_option( 'wpsc_version', '3.8.8.2' );
801
802
	foreach ( $columns_to_add as $unique_name => $name ) {
803
804
			// We need to add the row.  A few cases to check for type.  Quick and procedural felt like less overkill than a switch statement
805
			$type = 'text';
806
			$type = stristr( $unique_name, 'address' ) ? 'address'         : $type;
807
			$type = stristr( $unique_name, 'city' )    ? 'city'            : $type;
808
			$type = 'billingcountry'  == $unique_name  ? 'country'         : $type;
0 ignored issues
show
introduced by
Expected 1 space before "=="; 2 found
Loading history...
809
			$type = 'billingemail'    == $unique_name  ? 'email'           : $type;
0 ignored issues
show
introduced by
Expected 1 space before "=="; 4 found
Loading history...
810
			$type = 'shippingcountry' == $unique_name  ? 'deliverycountry' : $type;
811
812
			$wpdb->insert( WPSC_TABLE_CHECKOUT_FORMS,
813
				array( 'unique_name' => $unique_name, 'active' => '0', 'type' => $type, 'name' => $name, 'checkout_set' => '0' ),
814
				array( '%s', '%d', '%s', '%s', '%d' )
815
			);
816
	}
817
818
	// Update option to database to indicate that we have patched this.
819
	update_option( 'wpsc_version', '3.8.8.2' );
820
}
821
822
function wpsc_theme_engine_v2_activate() {
823
	$path = WPSC_FILE_PATH . '/wpsc-components/theme-engine-v2';
824
	require_once( $path . '/core.php' );
825
	_wpsc_te_v2_includes();
826
	wpsc_register_post_types();
827
	flush_rewrite_rules( true );
828
	update_option( 'transact_url', wpsc_get_checkout_url( 'results' ) );
829
	WPSC_Settings::get_instance();
830
	/**
831
	 * Runs after the WPSC Theme engine V2 is activated
832
	 */
833
	do_action( 'wpsc_theme_engine_v2_activate' );
834
}
835