Completed
Push — master ( a6313d...f538af )
by Justin
07:00
created

page.php ➔ wpsc_enqueue_user_script_and_css()   C

Complexity

Conditions 13
Paths 193

Size

Total Lines 58
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 13
eloc 37
c 3
b 0
f 0
nc 193
nop 0
dl 0
loc 58
rs 5.8796

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
add_action( 'wpsc_loaded', 'wpsc_core_load_page_titles' );
4
add_action( 'init', 'wpsc_register_core_theme_files' );
5
add_action( 'wpsc_move_theme', 'wpsc_flush_theme_transients', 10, true );
6
add_action( 'wpsc_switch_theme', 'wpsc_flush_theme_transients', 10, true );
7
add_action( 'switch_theme', 'wpsc_flush_theme_transients', 10, true );
8
add_action( 'admin_init','wpsc_theme_admin_notices');
9
add_action( 'update_option_product_image_width'     , 'wpsc_cache_to_upload' );
10
add_action( 'update_option_product_image_height'    , 'wpsc_cache_to_upload' );
11
add_action( 'update_option_single_view_image_width' , 'wpsc_cache_to_upload' );
12
add_action( 'update_option_single_view_image_height', 'wpsc_cache_to_upload' );
13
add_action( 'update_option_category_image_width'    , 'wpsc_cache_to_upload' );
14
add_action( 'update_option_category_image_height'   , 'wpsc_cache_to_upload' );
15
add_action( 'template_redirect', 'wpsc_all_products_on_page');
16
add_filter( 'aioseop_description', 'wpsc_set_aioseop_description' );
17
add_filter( 'request', 'wpsc_remove_page_from_query_string');
18
19
//Potentially unnecessary, as I believe this option is deprecated
20
add_action( 'update_option_show_categorybrands'     , 'wpsc_cache_to_upload' );
21
22
if ( ! is_admin() )
23
	add_action( 'init', 'wpsc_enqueue_user_script_and_css' );
24
25
if ( isset( $_REQUEST['wpsc_flush_theme_transients'] ) && ( $_REQUEST['wpsc_flush_theme_transients'] == 'true' ) )
26
	add_action( 'admin_init', 'wpsc_force_flush_theme_transients' );
27
28
if ( isset( $_GET['wpsc_user_dynamic_css'] ) && 'true' == $_GET['wpsc_user_dynamic_css'] )
29
	add_action( 'plugins_loaded', 'wpsc_user_dynamic_css', 1 );
30
31
if ( ! is_admin() )
32
	add_filter('request','wpec_remap_shop_subpages');
33
34
if(get_option( 'wpsc_hide_featured_products' ) == 1)
35
	add_action( 'wpsc_top_of_products_page', 'wpsc_display_featured_products_page', 12 );
36
37
/**
38
 * wpsc_register_theme_file( $file_name )
39
 *
40
 * Adds a file name to a global list of
41
 *
42
 * @param string $file_name Name of file to add to global list of files
43
 */
44
function wpsc_register_theme_file( $file_name ) {
45
	global $wpec_theme_files;
46
47
	if ( !in_array( $file_name, (array)$wpec_theme_files ) )
48
		$wpec_theme_files[] = $file_name;
49
}
50
51
/**
52
 * wpsc_register_core_theme_files()
53
 *
54
 * Registers the core WPEC files into the global array
55
 */
56
function wpsc_register_core_theme_files() {
57
	wpsc_register_theme_file( 'wpsc-single_product.php' );
58
	wpsc_register_theme_file( 'wpsc-grid_view.php' );
59
	wpsc_register_theme_file( 'wpsc-list_view.php' );
60
	wpsc_register_theme_file( 'wpsc-products_page.php' );
61
	wpsc_register_theme_file( 'wpsc-shopping_cart_page.php' );
62
	wpsc_register_theme_file( 'wpsc-transaction_results.php' );
63
	wpsc_register_theme_file( 'wpsc-user-log.php' );
64
	wpsc_register_theme_file( 'wpsc-cart_widget.php' );
65
	wpsc_register_theme_file( 'wpsc-featured_product.php' );
66
	wpsc_register_theme_file( 'wpsc-category-list.php' );
67
	wpsc_register_theme_file( 'wpsc-category_widget.php' );
68
	wpsc_register_theme_file( 'wpsc-account-downloads.php' );
69
	wpsc_register_theme_file( 'wpsc-account-edit-profile.php' );
70
	wpsc_register_theme_file( 'wpsc-account-purchase-history.php' );
71
72
	/* Let other plugins register their theme files */
73
	do_action( 'wpsc_register_core_theme_files' );
74
}
75
76
/**
77
 * wpsc_get_theme_files()
78
 *
79
 * Returns the global wpec_theme_files
80
 *
81
 * @global array $wpec_theme_files
82
 * @return array
83
 */
84
function wpsc_get_theme_files() {
85
	global $wpec_theme_files;
86
	if ( empty( $wpec_theme_files ) )
87
		return array();
88
	else
89
		return apply_filters( 'wpsc_get_theme_files', (array)array_values( $wpec_theme_files ) );
90
}
91
92
/**
93
 * wpsc_flush_theme_transients()
94
 *
95
 * This function will delete the temporary values stored in WordPress transients
96
 * for all of the additional WPEC theme files and their locations. This is
97
 * mostly used when the active theme changes, or when files are moved around. It
98
 * does a complete flush of all possible path/url combinations of files.
99
 *
100
 * @uses wpsc_get_theme_files
101
 */
102
function wpsc_flush_theme_transients( $force = false ) {
103
104
	if ( true === $force || isset( $_REQUEST['wpsc_flush_theme_transients'] ) && !empty( $_REQUEST['wpsc_flush_theme_transients'] ) ) {
105
106
		// Loop through current theme files and remove transients
107
		if ( $theme_files = wpsc_get_theme_files() ) {
108
			foreach( $theme_files as $file ) {
109
				delete_transient( WPEC_TRANSIENT_THEME_PATH_PREFIX . $file );
110
				delete_transient( WPEC_TRANSIENT_THEME_URL_PREFIX . $file );
111
			}
112
113
			delete_transient( 'wpsc_theme_path' );
114
115
			return true;
116
		}
117
	}
118
119
	// No files were registered so return false
120
	return false;
121
}
122
123
function wpsc_force_flush_theme_transients() {
124
125
	if ( ! wpsc_is_store_admin() ) {
126
		return;
127
	}
128
129
	// Flush transients
130
	wpsc_flush_theme_transients( true );
131
132
	// Bounce back
133
	$sendback = wp_get_referer();
134
	wp_redirect( $sendback );
135
136
	exit();
137
}
138
139
/**
140
 * wpsc_check_theme_location()
141
 *
142
 * Check theme location, compares the active theme and the themes within WPSC_CORE_THEME_PATH
143
 * finds files of the same name.
144
 *
145
 * @access public
146
 * @since 3.8
147
 * @param null
148
 * @return $results (Array) of Files OR false if no similar files are found
0 ignored issues
show
Documentation introduced by
The doc-type $results could not be parsed: Unknown type name "$results" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
149
 */
150
function wpsc_check_theme_location() {
151
	// Get the current theme
152
	$current_theme       = get_stylesheet_directory();
153
154
	// Load up the files in the current theme
155
	$current_theme_files = wpsc_list_product_templates( $current_theme . '/' );
156
157
	// Load up the files in the wpec themes folder
158
	$wpsc_template_files = wpsc_list_product_templates( WPSC_CORE_THEME_PATH );
159
160
	// Compare the two
161
	$results             = array_intersect( $current_theme_files, $wpsc_template_files );
162
163
	// Return the differences
164
	if ( count( $results ) > 0 )
165
		return $results;
166
167
	// No differences so return false
168
	else
169
		return false;
170
}
171
172
/**
173
 * wpsc_list_product_templates( $path = '' )
174
 *
175
 * Lists the files within the WPSC_CORE_THEME_PATH directory
176
 *
177
 * @access public
178
 * @since 3.8
179
 * @param $path - you can provide a path to find the files within it
180
 * @return $templates (Array) List of files
0 ignored issues
show
Documentation introduced by
The doc-type $templates could not be parsed: Unknown type name "$templates" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
181
 */
182
function wpsc_list_product_templates( $path = '' ) {
183
184
	$selected_theme = get_option( 'wpsc_selected_theme' );
185
	$templates      = array();
186
	// If no path, then try to make some assuptions
187
	if ( empty( $path ) ) {
188
		if ( file_exists( WPSC_OLD_THEMES_PATH . $selected_theme . '/' . $selected_theme . '.css' ) ) {
189
			$path = WPSC_OLD_THEMES_PATH . $selected_theme . '/';
190
		} else {
191
			$path = WPSC_CORE_THEME_PATH;
192
		}
193
	}
194
195
	// Open the path and get the file names
196
	$dh = opendir( $path );
197
	while ( ( $file = readdir( $dh ) ) !== false ) {
198
		if ( $file != "." && $file != ".." && !strstr( $file, ".svn" ) && !strstr( $file, "images" ) && is_file( $path . $file ) ) {
199
			$templates[] = $file;
200
		}
201
	}
202
203
	// Return template names
204
	return $templates;
205
}
206
207
/**
208
 * Displays the theme upgrade notice
209
 * @access public
210
 *
211
 * @since 3.8
212
 * @param null
213
 * @return null
214
 */
215
function wpsc_theme_upgrade_notice() { ?>
216
217
	<div id="message" class="updated fade">
218
		<p><?php printf( __( '<strong>WP eCommerce is ready</strong>. If you plan on editing the look of your site, you should <a href="%1s">update your active theme</a> to include the additional WP eCommerce files. <a href="%2s">Click here</a> to ignore and remove this box.', 'wp-e-commerce' ), admin_url( 'admin.php?page=wpsc-settings&tab=presentation' ), admin_url( 'admin.php?page=wpsc-settings&tab=presentation&wpsc_notices=theme_ignore' ) ) ?></p>
219
	</div>
220
221
<?php
222
}
223
224
/**
225
 * Displays the database update notice
226
 * @access public
227
 *
228
 * @since 3.8
229
 * @param null
230
 * @return null
231
 */
232
function wpsc_database_update_notice() { ?>
233
234
	<div class="error fade">
235
		<p><?php printf( __( '<strong>Your WP eCommerce data needs to be updated</strong>. You\'ve upgraded from a previous version of the WP eCommerce plugin, and your store needs updating.<br>You should <a href="%1s">update your database</a> for your store to continue working.', 'wp-e-commerce' ), admin_url( 'index.php?page=wpsc-update' ) ) ?></p>
236
	</div>
237
238
<?php
239
}
240
241
242
function wpsc_theme_admin_notices() {
243
244
	// Check to see if WP eCommerce is installed before showing upgrade notices.
245
	if ( false !== get_option( 'wpsc_version' ) ) {
246
247
		// Database update notice is most important
248
		if ( version_compare( get_option( 'wpsc_version' ), 3.8, '<' ) ) {
249
250
			add_action( 'admin_notices', 'wpsc_database_update_notice' );
251
252
		// If that's not an issue check if theme updates required
253
		} else {
254
255
			if ( '' === get_option( 'wpsc_ignore_theme', '' ) ) {
256
				add_option( 'wpsc_ignore_theme', false );
257
			}
258
259
			if ( ! get_option( 'wpsc_ignore_theme' ) ) {
260
				add_action( 'admin_notices', 'wpsc_theme_upgrade_notice' );
261
			}
262
		}
263
	}
264
265
	// Flag config inconsistencies
266
	if ( 1 == get_option( 'require_register' ) && 1 != get_option( 'users_can_register' ) ) {
267
		add_action( 'admin_notices', 'wpsc_turn_on_wp_register' );
268
	}
269
}
270
271
function wpsc_turn_on_wp_register() {?>
272
273
	<div id="message" class="updated fade">
274
		<p><?php printf( __( '<strong>Store Settings</strong>: You have set \'users must register before checkout\', for this to work you need to check \'Anyone can register\' in your WordPress <a href="%1s">General Settings</a>.', 'wp-e-commerce' ), admin_url( 'options-general.php' ) ) ?></p>
275
	</div>
276
277
<?php
278
279
280
}
281
282
/**
283
 * wpsc_get_template_file_url( $file )
284
 *
285
 * Checks the active theme folder for the particular file, if it exists then
286
 * return the active theme url, otherwise return the global wpsc_theme_url
287
 *
288
 * @access public
289
 * @since 3.8
290
 * @param $file string filename
291
 * @return PATH to the file
292
 */
293
function wpsc_get_template_file_url( $file = '' ) {
294
	// If we're not looking for a file, do not proceed
295
	if ( empty( $file ) )
296
		return;
297
298
	// Look for file in stylesheet
299
	if ( file_exists( get_stylesheet_directory() . '/' . $file ) ) {
300
		$file_url = get_stylesheet_directory_uri() . '/' . $file;
301
302
	// Look for file in template
303
	} elseif ( file_exists( get_template_directory() . '/' . $file ) ) {
304
		$file_url = get_template_directory_uri() . '/' . $file;
305
306
	// Backwards compatibility
307
	} else {
308
		// Look in old theme url
309
		$selected_theme_check = WPSC_OLD_THEMES_PATH . get_option( 'wpsc_selected_theme' ) . '/' . str_ireplace( 'wpsc-', '', $file );
310
		// Check the selected theme
311
		if ( file_exists( $selected_theme_check ) ) {
312
313
			$file_url = WPSC_OLD_THEMES_URL . get_option( 'wpsc_selected_theme' ) . '/' . str_ireplace( 'wpsc-', '', $file );
314
		// Use the bundled theme CSS
315
		} else {
316
			$file_url = WPSC_CORE_THEME_URL . $file;
317
		}
318
	}
319
320
	$file_url = set_url_scheme( $file_url );
321
322
	// Return filtered result
323
	return apply_filters( WPEC_TRANSIENT_THEME_URL_PREFIX . $file, $file_url );
324
}
325
326
/**
327
 * wpsc_user_enqueues products function,
328
 * enqueue all javascript and CSS for wp ecommerce
329
 */
330
function wpsc_enqueue_user_script_and_css() {
331
	global $wp_styles, $wpsc_theme_url, $wp_query;
332
	/**
333
	 * added by xiligroup.dev to be compatible with touchshop
334
	 */
335
	if ( has_filter( 'wpsc_enqueue_user_script_and_css' ) && apply_filters( 'wpsc_mobile_scripts_css_filters', false ) ) {
336
		do_action( 'wpsc_enqueue_user_script_and_css' );
337
	} else {
338
		/**
339
		 * end of added by xiligroup.dev to be compatible with touchshop
340
		 */
341
		$version_identifier = WPSC_VERSION . '.' . WPSC_MINOR_VERSION;
342
343
		$category_id = wpsc_get_current_category_id();
344
345
		if ( get_option( 'wpsc_share_this' ) == 1 ) {
346
			$remote_protocol = is_ssl() ? 'https://ws' : 'http://w';
347
			wp_enqueue_script( 'sharethis', $remote_protocol . '.sharethis.com/button/buttons.js', array(), false, true );
348
		}
349
350
		wp_enqueue_script( 'jQuery' );
351
		wp_enqueue_script( 'wp-e-commerce', WPSC_CORE_JS_URL . '/wp-e-commerce.js', array( 'jquery' ), $version_identifier );
352
353
		if ( defined( 'WPEC_LOAD_DEPRECATED_JS' ) && WPEC_LOAD_DEPRECATED_JS ) {
354
			wp_enqueue_script( 'wpsc-deprecated', WPSC_CORE_JS_URL . '/wpsc-deprecated.js', 'wp-e-commerce', $version_identifier );
355
			wp_localize_script( 'wpsc-deprecated', 'wpsc_deprecated_js_vars', _wpsc_deprecated_javascript_localization_vars() );
356
		}
357
358
		wp_enqueue_script( 'wp-e-commerce', WPSC_CORE_JS_URL . '/wp-e-commerce.js', array( 'jquery' ), $version_identifier );
359
		wp_localize_script( 'wp-e-commerce', 'wpsc_vars', wpsc_javascript_localizations() );
360
361
		if ( get_option( 'product_ratings' ) == 1 )
362
			wp_enqueue_script( 'jquery-rating',               WPSC_CORE_JS_URL 	. '/jquery.rating.js',                 array( 'jquery' ), $version_identifier );
363
		wp_enqueue_script( 'wp-e-commerce-legacy',        WPSC_CORE_JS_URL 	. '/user.js',                          array( 'jquery' ), WPSC_VERSION . WPSC_MINOR_VERSION );
364
		if ( get_option( 'show_thumbnails_thickbox' ) == 1 ){
365
			$lightbox = get_option('wpsc_lightbox', 'thickbox');
366
			if( $lightbox == 'thickbox' ) {
367
				wp_enqueue_script( 'wpsc-thickbox',				WPSC_CORE_JS_URL . '/thickbox.js',                      array( 'jquery' ), 'Instinct_e-commerce' );
368
				wp_enqueue_style( 'wpsc-thickbox',				WPSC_CORE_JS_URL . '/thickbox.css',						false, $version_identifier, 'all' );
369
			} elseif( $lightbox == 'colorbox' ) {
370
				wp_enqueue_script( 'colorbox-min',				WPSC_CORE_JS_URL . '/jquery.colorbox-min.js',			array( 'jquery' ), 'Instinct_e-commerce' );
371
				wp_enqueue_script( 'wpsc_colorbox',				WPSC_CORE_JS_URL . '/wpsc_colorbox.js',					array( 'jquery', 'colorbox-min' ), 'Instinct_e-commerce' );
372
				wp_enqueue_style( 'wpsc-colorbox-css',				WPSC_CORE_JS_URL . '/wpsc_colorbox.css',			false, $version_identifier, 'all' );
373
			}
374
		}
375
		wp_enqueue_style( 'wpsc-theme-css',               wpsc_get_template_file_url( 'wpsc-' . get_option( 'wpsc_selected_theme' ) . '.css' ), false, $version_identifier, 'all' );
376
		wp_enqueue_style( 'wpsc-theme-css-compatibility', wpsc_get_template_file_url( 'compatibility.css' ),                                    array( 'wpsc-theme-css' ), $version_identifier, 'all' );
377
378
		if ( function_exists( 'wp_add_inline_style' ) )
379
			wp_add_inline_style( 'wpsc-theme-css', wpsc_get_user_dynamic_css() );
380
		else
381
			wp_enqueue_style( 'wp-e-commerce-dynamic', wpsc_get_dynamic_user_css_url(), array( 'wpsc-theme-css' ), $version_identifier );
382
383
		if( get_option( 'product_ratings' ) == 1 )
384
			wp_enqueue_style( 'wpsc-product-rater',           WPSC_CORE_JS_URL 	. '/product_rater.css',                                       false, $version_identifier, 'all' );
385
386
	}
387
}
388
389
390
function wpsc_legacy_add_mp3_preview( $product_id = '', &$product_data ) {
0 ignored issues
show
Unused Code introduced by
The parameter $product_id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
391
392
 	if ( defined( 'WPSC_MP3_MODULE_USES_HOOKS' ) || ! function_exists( 'listen_button' ) ) {
393
		return;
394
	}
395
396
	global $wpdb;
397
398
	if ( function_exists( 'listen_button' ) ) {
399
		$file_data = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM `" . WPSC_TABLE_PRODUCT_FILES . "` WHERE `id` = %d LIMIT 1", $product_data['file'] ), ARRAY_A );
400
		if ( $file_data != null ) {
401
			echo listen_button( $file_data['idhash'], $file_data['id'] );
402
		}
403
	}
404
}
405
406
add_action( 'wpsc_product_before_description', 'wpsc_legacy_add_mp3_preview', 10, 2 );
407
408
/**
409
 * Checks the category slug for a display type, if none set returns default
410
 * << May need reworking to be more specific to the taxonomy type >>
411
 * @access public
412
 *
413
 * @since 3.8
414
 * @param $slug(string)
415
 * @return $slug either from db or 'default' if none set
416
 */
0 ignored issues
show
Documentation introduced by
The doc-type $slug could not be parsed: Unknown type name "$slug" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
417
418
function wpsc_get_the_category_display($slug){
419
	global $wpdb;
420
	$default_display_type = get_option('product_view');
421
	if ( !empty($slug) && is_string($slug) ) {
422
		$category_id = wpsc_get_the_category_id($slug , 'slug');
423
		$display_type = wpsc_get_categorymeta( $category_id, 'display_type' );
424
	}
425
	if(!empty($display_type))
426
		return $display_type;
427
	else
428
		return  $default_display_type;
429
}
430
431
/**
432
 * wpsc display products function
433
 * @return string - html displaying one or more products
434
 */
435
function wpsc_display_products_page( $query ) {
436
	global $wpdb, $wpsc_query,$wp_query, $wp_the_query;
437
438
	remove_filter('the_title','wpsc_the_category_title');
439
440
	// If the data is coming from a shortcode parse the values into the args variable,
441
	// I did it this was to preserve backwards compatibility
442
	if(!empty($query)){
443
		$args = array();
444
445
		$args['post_type'] = 'wpsc-product';
446
		if(!empty($query['product_id']) && is_array($query['product_id'])){
447
			$args['post__in'] = $query['product_id'];
448
		}elseif(is_string($query['product_id'])){
449
			$args['post__in'] = (array)$query['product_id'];
450
		}
451
		if(!empty($query['old_product_id'])){
452
			$post_id = wpsc_get_the_new_id($query['old_product_id']);
453
			$args['post__in'] = (array)$post_id;
454
		}
455
		if(!empty($query['price']) && 'sale' != $query['price']){
456
			$args['meta_key'] = '_wpsc_price';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
457
			$args['meta_value'] = $query['price'];
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
458
		}elseif(!empty($query['price']) && 'sale' == $query['price']){
459
			$args['meta_key'] = '_wpsc_special_price';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
460
			$args['meta_compare'] = '>=';
461
			$args['meta_value'] = '1';
0 ignored issues
show
introduced by
Detected usage of meta_value, possible slow query.
Loading history...
462
		}
463
		if(!empty($query['product_name'])){
464
			$args['pagename'] = $query['product_name'];
465
		}
466
		if(!empty($query['category_id'])){
467
			$term = get_term($query['category_id'],'wpsc_product_category');
468
			$id = wpsc_get_meta($query['category_id'], 'category_id','wpsc_old_category');
469
			if( !empty($id)){
470
				$term = get_term($id,'wpsc_product_category');
471
				$args['wpsc_product_category'] = $term->slug;
472
				$args['wpsc_product_category__in'] = $term->term_id;
473
			}else{
474
				$args['wpsc_product_category'] = $term->slug;
475
				$args['wpsc_product_category__in'] = $term->term_id;
476
			}
477
		}
478
		if(!empty($query['category_url_name'])){
479
			$args['wpsc_product_category'] = $query['category_url_name'];
480
		}
481
		$orderby = ( !empty($query['sort_order']) ) ? $query['sort_order'] : null;
482
483
		$args = array_merge( $args, wpsc_product_sort_order_query_vars($orderby) );
484
485
		if(!empty($query['order'])){
486
			$args['order'] = $query['order'];
487
		}
488
		if(!empty($query['limit_of_items']) && '1' == get_option('use_pagination')){
489
			$args['posts_per_page'] = $query['limit_of_items'];
490
		}
491
		if(!empty($query['number_per_page']) && '1' == get_option('use_pagination')){
492
			$args['posts_per_page'] = $query['number_per_page'];
493
			$args['paged'] = $query['page'];
494
		}
495
		if( '0' == get_option('use_pagination') ){
496
			$args['nopaging'] = true;
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to true ever.
Loading history...
497
			$args['posts_per_page'] = '-1';
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set posts_per_page to -1 ever.
Loading history...
498
		}
499
500
		if ( ! empty( $query['tag'] ) ) {
501
			$args['product_tag'] = $query['tag'];
502
		}
503
504
		query_posts( $args );
0 ignored issues
show
Coding Style introduced by
The use of function query_posts() is discouraged; use WP_Query() instead
Loading history...
505
506
	}
507
508
	// swap the wpsc_query objects
509
510
	$GLOBALS['nzshpcrt_activateshpcrt'] = true;
511
512
	// Pretty sure this single_product code is legacy...but fixing it up just in case.
513
	// get the display type for the selected category
514
	if(!empty($wpsc_query->query_vars['term']))
515
		$display_type = wpsc_get_the_category_display($wpsc_query->query_vars['term']);
516
	elseif( !empty( $args['wpsc_product_category'] ) )
517
		$display_type = wpsc_get_the_category_display($args['wpsc_product_category']);
518
	else
519
		$display_type = 'default';
520
521
	$saved_display = wpsc_get_customer_meta( 'display_type' );
522
	$display_type  = ! empty( $saved_display ) ? $saved_display : $display_type;
523
524
	ob_start();
525
526
	do_action( 'wpsc_display_products_page', $display_type );
527
528
	if ( ( isset( $wp_query->post ) && 'wpsc-product' == $wp_query->post->post_type ) && ! is_archive() && $wp_query->post_count <= 1 )
529
		include( wpsc_get_template_file_path( 'wpsc-single_product.php' ) );
530
	else
531
		wpsc_include_products_page_template( $display_type );
532
533
	$output = ob_get_contents();
534
	ob_end_clean();
535
	$output = str_replace( '\$','$', $output );
536
537
	if ( ! empty( $query ) ) {
538
		wp_reset_query();
0 ignored issues
show
Coding Style introduced by
The use of function wp_reset_query() is discouraged; use wp_reset_postdata() instead
Loading history...
539
		wp_reset_postdata();
540
	}
541
542
	return $output;
543
}
544
545
/**
546
 * Checks if wpsc-single_product.php has been moved to the active theme, if it has then include the
547
 * template from the active theme.
548
 * @access public
549
 *
550
 * @since 3.8
551
 * @param $content content of the page
552
 * @return $content with wpsc-single_product content if its a single product
0 ignored issues
show
Documentation introduced by
The doc-type $content could not be parsed: Unknown type name "$content" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
553
 */
554
function wpsc_single_template( $content ) {
555
	global $wpdb, $post, $wp_query, $wpsc_query, $_wpsc_is_in_custom_loop;
556
	if ( ! in_the_loop() )
557
		return $content;
558
559
	//if we dont belong here exit out straight away
560
	if((!isset($wp_query->is_product)) && !isset($wp_query->query_vars['wpsc_product_category']))return $content;
561
562
	// If we are a single products page
563
	if ( !is_archive() && $wp_query->post_count > 0 && 'wpsc-product' == $wp_query->post->post_type && $wp_query->post_count <= 1 ) {
564
		remove_filter( "the_content", "wpsc_single_template", 12 );
565
		$single_theme_path = wpsc_get_template_file_path( 'wpsc-single_product.php' );
566
567
		$is_preview = isset( $wp_query->query_vars['preview'] ) && $wp_query->query_vars['preview'];
568
569
		$post_type_object = get_post_type_object( 'wpsc-product' );
570
571
		/**
572
		 * Filter post_status on product query.
573
		 *
574
		 * Assumes publish, if user has edit_posts capability, can override
575
		 *
576
		 * @since 4.1.0
577
		 *
578
		 * @param array array() Allowed post_status values.
579
		 */
580
		$permitted_post_statuses = current_user_can( $post_type_object->cap->edit_posts ) ? apply_filters( 'wpsc_product_display_status', array( 'publish' ) ) : array( 'publish' );
581
582
		// If previewing a product, ensure post status can be draft
583
		if ( $wp_query->is_preview() ) {
584
			$permitted_post_statuses[] = 'draft';
585
		}
586
587
		$wpsc_temp_query = new WP_Query(
588
			array(
589
				'p'              => $wp_query->post->ID ,
590
				'post_type'      => 'wpsc-product',
591
				'posts_per_page' => 1,
592
				'preview'        => $is_preview,
593
				'post_status'    => $permitted_post_statuses,
594
			)
595
		);
596
597
		list( $wp_query, $wpsc_temp_query ) = array( $wpsc_temp_query, $wp_query ); // swap the wpsc_query object
598
		$_wpsc_is_in_custom_loop = true;
599
		ob_start();
600
		include( $single_theme_path );
601
		$content = ob_get_contents();
602
		ob_end_clean();
603
		list( $wp_query, $wpsc_temp_query ) = array( $wpsc_temp_query, $wp_query ); // swap the wpsc_query objects back
0 ignored issues
show
Unused Code introduced by
The assignment to $wpsc_temp_query is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
604
		$_wpsc_is_in_custom_loop = false;
605
	}
606
607
	return $content;
608
}
609
610
function wpsc_is_viewable_taxonomy(){
611
	global $wp_query;
612
	if(isset($wp_query->query_vars['taxonomy']) && ('wpsc_product_category' == $wp_query->query_vars['taxonomy'] ||  'product_tag' == $wp_query->query_vars['taxonomy'] ) || isset($wp_query->query_vars['wpsc_product_category']))
0 ignored issues
show
Coding Style introduced by
The if-else statement can be simplified to return isset($wp_query->...sc_product_category']);.
Loading history...
613
		return true;
614
	else
615
		return false;
616
}
617
618
function _wpsc_is_in_custom_loop() {
619
	global $_wpsc_is_in_custom_loop;
620
	return (bool) $_wpsc_is_in_custom_loop;
621
}
622
623
/**
624
 * Checks and replaces the Page title with the category title if on a category page
625
 *
626
 * @since 3.8
627
 * @access public
628
 *
629
 * @param string    $title      The Page Title
630
 * @param int       $id         The Page ID
0 ignored issues
show
Documentation introduced by
Should the type for parameter $id not be string|integer?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
631
 * @return string   $title      The new title
632
 *
633
 * @uses in_the_loop()                  Returns true if you are  in the loop
634
 * @uses _wpsc_is_in_custom_loop()      Returns true if in the WPSC custom loop
635
 * @uses is_tax()                       Returns true if you are on the supplied registered taxonomy
636
 * @uses get_term_by()                  Gets term object by defined item, and what you pass
637
 * @uses get_query_var()                Gets query var from wp_query
638
 */
639
function wpsc_the_category_title( $title = '', $id = '' ) {
640
641
	if ( ! empty( $id ) )
642
		_wpsc_deprecated_argument( __FUNCTION__, '3.8.10', 'The $id param is not used. If you are trying to get the title of the category use get_term' );
643
644
	if ( ! in_the_loop() || _wpsc_is_in_custom_loop() )
645
		return $title;
646
647
	$term = null;
648
	if ( is_tax( 'wpsc_product_category' ) ){
649
		$term = get_term_by( 'slug', get_query_var( 'wpsc_product_category' ),'wpsc_product_category' );
650
	} elseif ( is_tax( 'product_tag' ) ){
651
		$term = get_term_by( 'slug', get_query_var( 'term' ),'product_tag' );
652
	} // is_tax
653
654
	if ( $term )
655
		return $term->name;
656
657
	return $title;
658
659
}
660
661
//handles replacing the tags in the pages
662
function wpsc_products_page( $content = '' ) {
663
	global $wpdb, $wp_query, $wpsc_query, $wpsc_query_vars, $_wpsc_is_in_custom_loop;
664
	$output = '';
665
	if ( ! in_the_loop() )
666
		return $content;
667
	if ( preg_match( "/\[productspage\]/", $content ) ) {
668
		global $more;
669
		$more = 0;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
670
		remove_filter( 'the_content', 'wpautop' );
671
672
		list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query object
673
		$_wpsc_is_in_custom_loop = true;
674
675
		$GLOBALS['nzshpcrt_activateshpcrt'] = true;
676
677
		// get the display type for the productspage
678
		$saved_display = wpsc_get_customer_meta( 'display_type' );
679
		$display_type  = ! empty( $saved_display ) ? $saved_display : wpsc_check_display_type();
680
681
		ob_start();
682
683
		do_action( 'wpsc_display_products_page', $display_type );
684
685
		wpsc_include_products_page_template($display_type);
686
		$is_single = false;
687
		$output .= ob_get_contents();
688
		ob_end_clean();
689
		$output = str_replace( '$', '\$', $output );
690
691
		if ( $wp_query->post_count > 0 ) {
692
			$product_id = $wp_query->post->ID;
693
			$product_meta = get_post_meta( $product_id, '_wpsc_product_metadata', true );
694
695
			list($wp_query, $wpsc_query) = array( $wpsc_query, $wp_query ); // swap the wpsc_query objects back
696
697
			$_wpsc_is_in_custom_loop = false;
698
699
			if ( ($is_single == false) || ($product_meta['enable_comments'] == '0') )
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...
700
				wp_reset_postdata();
701
		}
702
		$wp_query->current_post = $wp_query->post_count;
703
		return preg_replace( "/(<p>)*\[productspage\](<\/p>)*/", $output, $content );
704
	} elseif(is_archive() && wpsc_is_viewable_taxonomy()){
705
706
		remove_filter( 'the_content', 'wpautop' );
707
		return wpsc_products_page('[productspage]');
708
	} else {
709
		return $content;
710
	}
711
}
712
713
/**
714
 * wpsc_the_category_template swaps the template used for product categories with pageif archive template is being used use
715
 * @access public
716
 *
717
 * @since 3.8
718
 * @param $template (string) template path
719
 * @return $template (string)
0 ignored issues
show
Documentation introduced by
The doc-type $template could not be parsed: Unknown type name "$template" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
720
 */
721
function wpsc_the_category_template($template){
722
	global $wp_query;
723
	//this bit of code makes sure we use a nice standard page template for our products
724
	if(wpsc_is_viewable_taxonomy() && false !== strpos($template,'archive'))
725
		return str_ireplace('archive', 'page',$template);
726
	else
727
		return $template;
728
729
}
730
731
/**
732
 * Returns current product category ID or default category ID if one is set.  If one is not set and there is no current category, returns empty string
733
 * @return mixed
734
 */
735
function wpsc_get_current_category_id() {
736
	global $wp_query;
737
738
	$category_id = '';
739
740
	if ( isset( $wp_query ) && isset( $wp_query->query_vars['taxonomy'] ) && ('wpsc_product_category' == $wp_query->query_vars['taxonomy'] ) || is_numeric( get_option( 'wpsc_default_category' ) ) )
741
		$category_id = isset( $wp_query->query_vars['term'] ) && is_string( $wp_query->query_vars['term'] ) ? wpsc_get_category_id( $wp_query->query_vars['term'], 'slug' ) : get_option( 'wpsc_default_category' );
742
743
	return $category_id;
744
}
745
746
/**
747
 * Returns Dynamic User CSS URL
748
 *
749
 * This produces the cached CSS file if it exists and the uploads folder is writeable.
750
 * If the folder is not writeable, we return the dynamic URL
751
 * If the folder is writeable, but for some reason a cached copy of the CSS doesn't exist, we attempt to create it and return that URL.
752
 *
753
 * @since 3.8.9
754
 * @return string
755
 */
756
757
function wpsc_get_dynamic_user_css_url() {
758
759
	$uploads_dir     = wp_upload_dir();
760
	$upload_folder   = $uploads_dir['path'];
761
762
	if ( is_writable( $upload_folder ) && file_exists( $upload_folder . '/wpsc_cached_styles.css' ) )
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using is_writable()
Loading history...
763
		return esc_url( add_query_arg( 'timestamp', get_option( 'wpsc_dynamic_css_hash', time() ), $uploads_dir['url'] . '/wpsc_cached_styles.css' ) );
764
765
	if ( ! is_writable( $upload_folder ) )
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using is_writable()
Loading history...
766
		return esc_url( add_query_arg( 'wpsc_user_dynamic_css', 'true', home_url( 'index.php' ) ) );
767
768
	if ( is_writable( $upload_folder ) && ! file_exists( $upload_folder . '/wpsc_cached_styles.css' ) )
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using is_writable()
Loading history...
769
		return wpsc_cache_to_upload();
770
}
771
772
/**
773
 * Moves dynamically generated input into a file in the uploads folder.
774
 * Also updates CSS hash timestamp.  Timestamp is appended to URL
775
 *
776
 * @since 3.8.9
777
 * @return mixed File URL on successful move, false on failure
778
 */
779
function wpsc_cache_to_upload() {
780
781
	$uploads_dir     = wp_upload_dir();
782
	$upload_folder   = $uploads_dir['path'];
783
	$path            = $upload_folder . '/wpsc_cached_styles.css';
784
785
	if ( ! is_writable( $upload_folder ) )
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using is_writable()
Loading history...
786
		return false;
787
788
	if ( false === file_put_contents( $path, wpsc_get_user_dynamic_css() ) )
0 ignored issues
show
introduced by
Filesystem writes are forbidden, you should not be using file_put_contents()
Loading history...
789
		return false;
790
791
	$timestamp = time();
792
793
	update_option( 'wpsc_dynamic_css_hash', $timestamp );
794
795
	return esc_url( add_query_arg( 'timestamp', $timestamp, $uploads_dir['url'] . '/wpsc_cached_styles.css' ) );
796
797
}
798
799
/**
800
 * Prints dynamic CSS.  This function is run either when the dynamic URL is hit, or when we need to grab new CSS to cache.
801
 *
802
 * @since 3.8.9
803
 * @return CSS
804
 */
805
function wpsc_user_dynamic_css() {
806
807
	header( 'Content-Type: text/css' );
808
	header( 'Expires: ' . gmdate( 'r', mktime( 0, 0, 0, date( 'm' ), ( date( 'd' ) + 12 ), date( 'Y' ) ) ) );
809
	header( 'Cache-Control: public, must-revalidate, max-age=86400' );
810
	header( 'Pragma: public' );
811
812
	echo wpsc_get_user_dynamic_css();
813
	exit;
814
}
815
816
/**
817
 * Returns dynamic CSS as string.  This function is run either when the dynamic URL is hit, or when we need to grab new CSS to cache.
818
 *
819
 * @since 3.8.9
820
 * @return string
821
 */
822
function wpsc_get_user_dynamic_css() {
823
	global $wpdb;
824
825
	ob_start();
826
827
	if ( ! defined( 'WPSC_DISABLE_IMAGE_SIZE_FIXES' ) || (constant( 'WPSC_DISABLE_IMAGE_SIZE_FIXES' ) != true ) ) {
828
829
		$thumbnail_width = get_option( 'product_image_width' );
830
831
		if ( $thumbnail_width <= 0 )
832
			$thumbnail_width = 96;
833
834
		$thumbnail_height = get_option( 'product_image_height' );
835
836
		if ( $thumbnail_height <= 0 )
837
			$thumbnail_height = 96;
838
839
		$single_thumbnail_width  = get_option( 'single_view_image_width' );
840
		$single_thumbnail_height = get_option( 'single_view_image_height' );
841
842
		if ( $single_thumbnail_width <= 0 )
843
			$single_thumbnail_width = 128;
844
845
		$category_height = get_option( 'category_image_height' );
846
		$category_width  = get_option( 'category_image_width' );
847
?>
848
849
		/*
850
		* Default View Styling
851
		*/
852
		div.default_product_display div.textcol{
853
			margin-left: <?php echo $thumbnail_width + 10; ?>px !important;
854
			min-height: <?php echo $thumbnail_height; ?>px;
855
			_height: <?php echo $thumbnail_height; ?>px;
856
		}
857
858
		div.default_product_display  div.textcol div.imagecol{
859
			position:absolute;
860
			top:0px;
861
			left: 0px;
862
			margin-left: -<?php echo $thumbnail_width + 10; ?>px !important;
863
		}
864
865
		div.default_product_display  div.textcol div.imagecol a img {
866
			width: <?php echo $thumbnail_width; ?>px;
867
			height: <?php echo $thumbnail_height; ?>px;
868
		}
869
870
		.wpsc_category_grid_item  {
871
			display:block;
872
			float:left;
873
			width: <?php echo $category_width; ?>px;
874
			height: <?php echo $category_height; ?>px;
875
		}
876
		.wpsc_category_grid_item  span{
877
			position:relative;
878
			top:<?php echo ($thumbnail_height - 2)/9; ?>px;
879
		}
880
		div.default_product_display div.item_no_image a  {
881
			width: <?php echo $thumbnail_width - 2; ?>px;
882
		}
883
884
		div.default_product_display .imagecol img.no-image, #content div.default_product_display .imagecol img.no-image {
885
			width: <?php echo $thumbnail_width; ?>px;
886
			height: <?php echo $thumbnail_height; ?>px;
887
        }
888
889
		<?php do_action( 'wpsc_dynamic_css' ); ?>
890
891
		/*
892
		* Single View Styling
893
		*/
894
895
		div.single_product_display div.item_no_image  {
896
			width: <?php echo $single_thumbnail_width - 2; ?>px;
897
			height: <?php echo $single_thumbnail_height - 2; ?>px;
898
		}
899
		div.single_product_display div.item_no_image a  {
900
			width: <?php echo $single_thumbnail_width - 2; ?>px;
901
		}
902
903
		div.single_product_display div.textcol{
904
			margin-left: <?php echo $single_thumbnail_width + 10; ?>px !important;
905
			min-height: <?php echo $single_thumbnail_height; ?>px;
906
			_height: <?php echo $single_thumbnail_height; ?>px;
907
		}
908
909
910
		div.single_product_display  div.textcol div.imagecol{
911
			position:absolute;
912
913
			margin-left: -<?php echo $single_thumbnail_width + 10; ?>px !important;
914
		}
915
916
		div.single_product_display  div.textcol div.imagecol a img {
917
			width: <?php echo $single_thumbnail_width; ?>px;
918
			height: <?php echo $single_thumbnail_height; ?>px;
919
		}
920
921
<?php
922
}
923
	if ( (isset($_GET['brand']) && is_numeric( $_GET['brand'] )) || (get_option( 'show_categorybrands' ) == 3) ) {
924
		$brandstate = 'block';
925
		$categorystate = 'none';
926
	} else {
927
		$brandstate = 'none';
928
		$categorystate = 'block';
929
	}
930
?>
931
	div#categorydisplay{
932
		display: <?php echo $categorystate; ?>;
933
	}
934
935
	div#branddisplay{
936
		display: <?php echo $brandstate; ?>;
937
	}
938
<?php
939
940
	$css = ob_get_contents();
941
942
	ob_end_clean();
943
944
	return $css;
945
}
946
947
function wpsc_get_the_new_id($prod_id){
948
	global $wpdb;
949
950
	$post_id = (int) $wpdb->get_var($wpdb->prepare( "SELECT `post_id` FROM `{$wpdb->postmeta}` WHERE meta_key = %s AND `meta_value` = %d LIMIT 1", '_wpsc_original_id', $prod_id ));
951
952
	return $post_id;
953
}
954
955
/**
956
 * This switched between the 3 view types on category and products pages and includes the necessary template part
957
 * @access public
958
 *
959
 * @since 3.8
960
 * @param $display_type
961
 * @return NULL
962
 */
963
function wpsc_include_products_page_template( $display_type = 'default' ) {
964
965
	if ( isset( $_GET['view_type'] ) ) {
966
		switch ( $_GET['view_type'] ) {
967
968
			case 'grid':
969
				$display_type = 'grid';
970
				wpsc_update_customer_meta( 'display_type', $display_type );
971
				break;
972
973
			case 'list':
974
				$display_type = 'list';
975
				wpsc_update_customer_meta( 'display_type', $display_type );
976
				break;
977
978
			case 'default':
979
				$display_type = 'default';
980
				wpsc_update_customer_meta( 'display_type', $display_type );
981
				break;
982
983
			default:
984
				break;
985
		}
986
	}
987
988
	// switch the display type, based on the display type variable...
989
	switch ( $display_type ) {
990
		case "grid":
991
			include( wpsc_get_template_file_path( 'wpsc-grid_view.php' ) );
992
			break; // only break if we have the function;
993
994
		case "list":
995
			include( wpsc_get_template_file_path( 'wpsc-list_view.php' ) );
996
			break; // only break if we have the file;
997
		default:
998
			include( wpsc_get_template_file_path( 'wpsc-products_page.php' ) );
999
			break;
1000
	}
1001
1002
}
1003
1004
function wpsc_thesis_compat( $loop ) {
1005
	$loop[1] = 'page';
1006
	return $loop;
1007
}
1008
1009
// Template tags
1010
function wpsc_all_products_on_page(){
1011
1012
	if ( is_404() ) {
1013
		return;
1014
	}
1015
1016
	global $wp_query,$wpsc_query;
1017
	do_action('wpsc_swap_the_template');
1018
	$products_page_id = wpsc_get_the_post_id_by_shortcode('[productspage]');
1019
	$term = get_query_var( 'wpsc_product_category' );
1020
	$tax_term = get_query_var( 'product_tag' );
1021
	$obj = $wp_query->get_queried_object();
1022
1023
	$id = isset( $obj->ID ) ? $obj->ID : null;
1024
1025
	if( get_query_var( 'post_type' ) == 'wpsc-product' || $term || $tax_term || ( $id == $products_page_id )){
1026
1027
		$templates = array();
1028
1029
		if ( $term && ! is_single() ) {
1030
			array_push( $templates, "taxonomy-wpsc_product_category-{$term}.php", 'taxonomy-wpsc_product_category.php' );
1031
		}
1032
1033
		if ( $tax_term && ! is_single() ) {
1034
			array_push( $templates, "taxonomy-product_tag-{$tax_term}.php", 'taxonomy-product_tag.php' );
1035
		}
1036
1037
		// Attempt to use the [productspage]'s custom page template as a higher priority than the normal page.php template
1038
		if ( false !== $productspage_page_template = get_post_meta($products_page_id, '_wp_page_template', true) )
1039
			array_push( $templates, $productspage_page_template );
1040
1041
		array_push( $templates, 'page.php', 'single.php' );
1042
1043
		if ( is_single() )
1044
			array_unshift( $templates, 'single-wpsc-product.php' );
1045
1046
		// have to pass 'page' as the template type. This is lame, btw, and needs a rewrite in 4.0
1047
		if ( ! $template = get_query_template( 'page', $templates ) )
1048
			$template = get_index_template();
1049
1050
		add_filter( 'thesis_custom_loop', 'wpsc_thesis_compat' );
1051
1052
		include( $template );
1053
		exit;
1054
	}
1055
}
1056
1057
/**
1058
 * wpsc_count_themes_in_uploads_directory, does exactly what the name says
1059
 */
1060
function wpsc_count_themes_in_uploads_directory() {
1061
	$uploads_dir = false;
1062
	if ( is_dir( WPSC_OLD_THEMES_PATH.get_option('wpsc_selected_theme').'/' ) )
1063
		$uploads_dir = @opendir( WPSC_OLD_THEMES_PATH.get_option('wpsc_selected_theme').'/' ); // might cause problems if dir doesnt exist
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
1064
1065
	if ( !$uploads_dir )
1066
		return false;
1067
1068
	$file_names = array();
1069
	while ( ($file = @readdir( $uploads_dir )) !== false ) {
0 ignored issues
show
Coding Style introduced by
Silencing errors is discouraged
Loading history...
1070
		if ( is_dir( WPSC_OLD_THEMES_PATH . get_option('wpsc_selected_theme') . '/' . $file ) && ($file != "..") && ($file != ".") && ($file != ".svn") )
1071
			$file_names[] = $file;
1072
1073
	}
1074
	@closedir( $uploads_dir );
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...
1075
	return count( $file_names );
1076
}
1077
1078
function wpsc_place_shopping_cart( $content = '' ) {
1079
	if ( ! in_the_loop() )
1080
		return $content;
1081
1082
	if ( preg_match( "/\[shoppingcart\]/", $content ) ) {
1083
		// BEGIN: compatibility fix for outdated theme files still relying on sessions
1084
		$_SESSION['coupon_numbers'                    ] = wpsc_get_customer_meta( 'coupon'                       );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
1085
		$_SESSION['wpsc_checkout_misc_error_messages' ] = wpsc_get_customer_meta( 'checkout_misc_error_messages' );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
1086
		$_SESSION['categoryAndShippingCountryConflict'] = wpsc_get_customer_meta( 'category_shipping_conflict'   );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
1087
		$_SESSION['shippingSameBilling'               ] = wpsc_get_customer_meta( 'shippingSameBilling'          );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
1088
		$_SESSION['wpsc_checkout_user_error_messages' ] = wpsc_get_customer_meta( 'registration_error_messages'  );
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
1089
		// END: compatibility fix
1090
		$GLOBALS['nzshpcrt_activateshpcrt'] = true;
1091
		if ( ! defined( 'DONOTCACHEPAGE' ) )
1092
			define( 'DONOTCACHEPAGE', true );
1093
1094
		// call this function to detect conflicts when the cart page is first loaded, otherwise
1095
		// any conflict messages will only be displayed on the next page load
1096
		wpsc_get_acceptable_countries();
1097
		ob_start();
1098
		do_action( 'wpsc_before_shopping_cart_page' );
1099
		include( wpsc_get_template_file_path( 'wpsc-shopping_cart_page.php' ) );
1100
		do_action( 'wpsc_after_shopping_cart_page' );
1101
		$output = ob_get_contents();
1102
		ob_end_clean();
1103
		$output = str_replace( '$', '\$', $output );
1104
		wpsc_delete_customer_meta( 'checkout_misc_error_messages' );
1105
		wpsc_delete_customer_meta( 'category_shipping_conflict'   );
1106
		wpsc_delete_customer_meta( 'registration_error_messages'  );
1107
		wpsc_delete_customer_meta( 'checkout_error_messages'      );
1108
		wpsc_delete_customer_meta( 'gateway_error_messages'       );
1109
		return preg_replace( "/(<p>)*\[shoppingcart\](<\/p>)*/", $output, $content );
1110
	} else {
1111
		return $content;
1112
	}
1113
}
1114
1115
function wpsc_transaction_results( $content = '' ) {
1116
	if ( ! in_the_loop() )
1117
		return $content;
1118
1119
	if ( preg_match( "/\[transactionresults\]/", $content ) ) {
1120
1121
		if ( ! defined( 'DONOTCACHEPAGE' ) || ( defined( 'DONOTCACHEPAGE' ) && ! DONOTCACHEPAGE ) ) {
1122
			define( 'DONOTCACHEPAGE', true );
1123
		}
1124
1125
		ob_start();
1126
		include( wpsc_get_template_file_path( 'wpsc-transaction_results.php' ) );
1127
		$output = ob_get_contents();
1128
		ob_end_clean();
1129
		$output = preg_replace( '#(?<!\\\\)(\\$|\\\\)#', '\\\\$1', $output );
1130
		return preg_replace( "/(<p>)*\[transactionresults\](<\/p>)*/", $output, $content );
1131
	} else {
1132
		return $content;
1133
	}
1134
}
1135
1136
function wpsc_user_log( $content = '' ) {
1137
	if ( ! in_the_loop() )
1138
		return $content;
1139
	if ( preg_match( "/\[userlog\]/", $content ) ) {
1140
1141
		if ( ! defined( 'DONOTCACHEPAGE' ) ) {
1142
			define( 'DONOTCACHEPAGE', true );
1143
		}
1144
1145
		ob_start();
1146
1147
		include( wpsc_get_template_file_path('wpsc-user-log.php') );
1148
		$output = ob_get_clean();
1149
		$content = preg_replace( "/(<p>)*\[userlog\](<\/p>)*/", '[userlog]', $content );
1150
		return str_replace( '[userlog]', $output, $content );
1151
	} else {
1152
		return $content;
1153
	}
1154
}
1155
1156
function wpsc_get_the_post_id_by_shortcode( $shortcode ) {
1157
1158
	$shortcode_options = array(
1159
		'[productspage]'       => 'product_list_url',
1160
		'[shoppingcart]'       => 'shopping_cart_url',
1161
		'[checkout]'           => 'shopping_cart_url',
1162
		'[transactionresults]' => 'transact_url',
1163
		'[userlog]'            => 'user_account_url'
1164
	);
1165
1166
	if ( ! isset( $shortcode_options[ $shortcode ] ) ) {
1167
		return 0;
1168
	}
1169
1170
	$page_ids = get_option( 'wpsc_shortcode_page_ids', false );
1171
1172
	if ( $page_ids === false ) {
1173
		wpsc_update_permalink_slugs();
1174
		$page_ids = get_option( 'wpsc_shortcode_page_ids', false );
1175
	}
1176
1177
	$post_id = isset( $page_ids[ $shortcode ] ) ? $page_ids[ $shortcode ] : null;
1178
1179
	// For back compat
1180
	$post_id = apply_filters( 'wpec_get_the_post_id_by_shortcode', $post_id );
1181
1182
	return apply_filters( 'wpsc_get_the_post_id_by_shortcode', $post_id, $shortcode );
1183
1184
}
1185
1186
function wpec_remap_shop_subpages( $vars ) {
1187
	if ( empty( $vars ) ) {
1188
		return $vars;
1189
	}
1190
1191
	$reserved_names = array('[shoppingcart]','[userlog]','[transactionresults]');
1192
	foreach($reserved_names as $reserved_name){
1193
		if ( isset( $vars['taxonomy'] ) && $vars['taxonomy'] == 'wpsc_product_category' && isset( $vars['term'] ) && $vars['term'] == $page->post_name ) {
0 ignored issues
show
Bug introduced by
The variable $page 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...
1194
			$page_id = wpsc_get_the_post_id_by_shortcode( $reserved_name );
1195
			$page = get_post( $page_id );
1196
			return array( 'page_id' => $page->ID );
1197
		}
1198
	}
1199
	return $vars;
1200
}
1201
1202
function wpsc_remove_page_from_query_string( $query_string ) {
1203
1204
	if ( false === strpos( implode( ' ', $query_string ), 'wpsc' ) ) {
1205
		return $query_string;
1206
	}
1207
1208
	if ( isset( $query_string['name'] ) && $query_string['name'] == 'page' && isset( $query_string['page'] ) ) {
1209
		unset( $query_string['name'] );
1210
1211
		list( $delim, $page_index ) = explode( '/', $query_string['page'] );
0 ignored issues
show
Unused Code introduced by
The assignment to $delim is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
1212
1213
		$query_string['paged'] = $page_index;
1214
	}
1215
1216
	if ( isset( $query_string['wpsc-product'] ) && 'page' == $query_string['wpsc-product'] ) {
1217
		$query_string['wpsc-product'] = '';
1218
	}
1219
1220
	if ( isset( $query_string['name'] ) && is_numeric( $query_string['name'] ) ) {
1221
		$query_string['paged'] = $query_string['name'];
1222
		$query_string['page']  = '/'.$query_string['name'];
1223
1224
		$query_string['posts_per_page'] = get_option('wpsc_products_per_page');
1225
	}
1226
1227
	if ( isset( $query_string['wpsc-product'] ) && is_numeric( $query_string['wpsc-product'] ) ) {
1228
		unset( $query_string['wpsc-product'] );
1229
	}
1230
1231
	if ( isset( $query_string['wpsc_product_category'] ) && 'page' == $query_string['wpsc_product_category'] ) {
1232
		unset( $query_string['wpsc_product_category'] );
1233
	}
1234
1235
	if ( isset( $query_string['name'] ) && is_numeric( $query_string['name'] ) ) {
1236
		unset( $query_string['name'] );
1237
	}
1238
1239
	if ( isset($query_string['term']) && 'page' == $query_string['term'] )	{
1240
		unset( $query_string['term'] );
1241
		unset( $query_string['taxonomy'] );
1242
	}
1243
1244
	return $query_string;
1245
}
1246
1247
function is_products_page(){
1248
	global $post;
1249
1250
	if ( empty( $post ) || ! is_object( $post ) || empty( $post->ID ) )
1251
		return false;
1252
1253
	$product_page_id = wpsc_get_the_post_id_by_shortcode( '[productspage]' );
1254
1255
	return $post->ID == $product_page_id;
1256
1257
}
1258
1259
/**
1260
 * wpsc_display_products_page function.
1261
 *
1262
 * @access public
1263
 * @param mixed $query
0 ignored issues
show
Bug introduced by
There is no parameter named $query. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
1264
 * @return void
1265
 */
1266
function wpsc_display_featured_products_page() {
1267
	global $wp_query;
1268
	$output = '';
1269
	$sticky_array = get_option( 'sticky_products' );
1270
	if ( (is_front_page() || is_home() || is_products_page() ) && !empty( $sticky_array ) && $wp_query->post_count > 1) {
1271
		$query = get_posts( array(
1272
					'post__in' => $sticky_array,
1273
					'post_type' => 'wpsc-product',
1274
					'orderby' => 'rand',
1275
					'numberposts' => 1,
1276
					'posts_per_page' => 1
1277
				) );
1278
1279
		if ( count( $query ) > 0 ) {
1280
1281
			$GLOBALS['nzshpcrt_activateshpcrt'] = true;
1282
			$image_width = get_option( 'product_image_width' );
1283
			$image_height = get_option( 'product_image_height' );
1284
			$featured_product_theme_path = wpsc_get_template_file_path( 'wpsc-featured_product.php' );
1285
	ob_start();
1286
		include_once($featured_product_theme_path);
1287
		$is_single = false;
1288
		$output .= ob_get_contents();
1289
		ob_end_clean();
1290
1291
			//Begin outputting featured product.  We can worry about templating later, or folks can just CSS it up.
1292
			echo $output;
1293
			//End output
1294
		}
1295
	}
1296
}
1297
1298
/**
1299
 * Hide Subcat Products Init
1300
 *
1301
 * @deprecated  Since 4.0. Hiding subcategory products in parent categories is now handled by the private wpsc_hide_subcatsprods_in_cat_query() function.
1302
 */
1303
function wpsc_hidesubcatprods_init() {
1304
1305
	_wpsc_doing_it_wrong( 'wpsc_hidesubcatprods_init', __( 'This function is deprecated. There is no direct replacement. Hiding subcategory products in parent categories is now handled by the private wpsc_hide_subcatsprods_in_cat_query() function.', 'wp-e-commerce' ), '4.0' );
1306
1307
}
1308
1309
// check for all in one SEO pack and the is_static_front_page function
1310
if ( is_callable( array( "All_in_One_SEO_Pack", 'is_static_front_page' ) ) ) {
1311
1312
	function wpsc_change_aioseop_home_title( $title ) {
1313
		global $aiosp, $aioseop_options;
1314
1315
		if ( (get_class( $aiosp ) == 'All_in_One_SEO_Pack') && $aiosp->is_static_front_page() ) {
1316
			$aiosp_home_title = $aiosp->internationalize( $aioseop_options['aiosp_home_title'] );
1317
			$new_title = wpsc_obtain_the_title();
1318
			if ( $new_title != '' ) {
1319
				$title = str_replace( $aiosp_home_title, $new_title, $title );
1320
			}
1321
		}
1322
		return $title;
1323
	}
1324
1325
	add_filter( 'aioseop_home_page_title', 'wpsc_change_aioseop_home_title' );
1326
}
1327
1328
/**
1329
 * wpsc_obtain_the_title function, for replaacing the page title with the category or product
1330
 * @return string - the new page title
1331
 */
1332
function wpsc_obtain_the_title() {
1333
	global $wpdb, $wp_query, $wpsc_title_data;
1334
	$output = null;
1335
	$category_id = null;
1336
	if( !isset( $wp_query->query_vars['wpsc_product_category']) &&  !isset( $wp_query->query_vars['wpsc-product']))
0 ignored issues
show
introduced by
Expected 1 space before "!"; 2 found
Loading history...
1337
		return;
1338
1339
	if ( !isset( $wp_query->query_vars['wpsc_product_category'] ) && isset($wp_query->query_vars['wpsc-product']) )
1340
		$wp_query->query_vars['wpsc_product_category'] = 0;
1341
1342
	if ( isset( $wp_query->query_vars['taxonomy'] ) && 'wpsc_product_category' ==  $wp_query->query_vars['taxonomy'] || isset($wp_query->query_vars['wpsc_product_category']))
0 ignored issues
show
introduced by
Expected 1 space after "=="; 2 found
Loading history...
1343
		$category_id = wpsc_get_the_category_id($wp_query->query_vars['wpsc_product_category'],'slug');
1344
1345
	if ( $category_id > 0 ) {
1346
1347
		if ( isset( $wpsc_title_data['category'][$category_id] ) ) {
1348
			$output = $wpsc_title_data['category'][$category_id];
1349
		} else {
1350
			$term = get_term($category_id, 'wpsc_product_category');
1351
			$output = $term->name;
1352
			$wpsc_title_data['category'][$category_id] = $output;
1353
		}
1354
	}
1355
1356
	if ( !isset( $_GET['wpsc-product'] ) )
1357
		$_GET['wpsc-product'] = 0;
1358
1359
	if ( !isset( $wp_query->query_vars['wpsc-product'] ) )
1360
		$wp_query->query_vars['wpsc-product'] = '';
1361
1362
	if ( isset( $wp_query->query_vars['wpsc-product'] ) || is_string( $_GET['wpsc-product'] ) ) {
1363
		$product_name = $wp_query->query_vars['wpsc-product'];
1364
		if ( isset( $wpsc_title_data['product'][$product_name] ) ) {
1365
			$product_list = array( );
0 ignored issues
show
introduced by
Empty array declaration must have no space between the parentheses
Loading history...
1366
			$full_product_name = $wpsc_title_data['product'][$product_name];
1367
		} else if ( $product_name != '' ) {
1368
			$product_id = $wp_query->post->ID;
1369
			$full_product_name = $wpdb->get_var( $wpdb->prepare( "SELECT `post_title` FROM `$wpdb->posts` WHERE `ID`= %d LIMIT 1", $product_id ) );
1370
			$wpsc_title_data['product'][$product_name] = $full_product_name;
1371
		} else {
1372
			if(isset($_REQUEST['product_id'])){
1373
				$product_id = absint( $_REQUEST['product_id'] );
1374
				$product_name = $wpdb->get_var( $wpdb->prepare( "SELECT `post_title` FROM `$wpdb->posts` WHERE `ID`= %d LIMIT 1", $product_id ) );
1375
				$full_product_name = $wpdb->get_var( $wpdb->prepare( "SELECT `post_title` FROM `$wpdb->posts` WHERE `ID`= %d LIMIT 1", $product_id ) );
1376
				$wpsc_title_data['product'][$product_name] = $full_product_name;
1377
			}else{
1378
				//This has to exist, otherwise we would have bailed earlier.
1379
				$category = $wp_query->query_vars['wpsc_product_category'];
1380
				$cat_term = get_term_by('slug',$wp_query->query_vars['wpsc_product_category'], 'wpsc_product_category');
1381
				$full_product_name = $cat_term->name;
1382
			}
1383
		}
1384
		$output = $full_product_name;
1385
	}
1386
1387
	if ( isset( $full_product_name ) && ($full_product_name != null) )
1388
		$output = esc_html(  $full_product_name );
1389
	$seperator = ' | ';
1390
	$seperator = apply_filters('wpsc_the_wp_title_seperator' , $seperator);
1391
	return $output.$seperator;
1392
}
1393
1394
/**
1395
 *	Return category or product description depending on queried item
1396
 */
1397
function wpsc_obtain_the_description() {
1398
1399
	$output = null;
1400
1401
	// Return Category Description
1402
	if ( is_numeric( get_query_var('category_id') ) ) {
1403
		$output = wpsc_get_categorymeta( get_query_var('category_id'), 'description' );
1404
	} else if ( ! empty($_GET['category']) ) {
1405
		$output = wpsc_get_categorymeta( absint( $_GET['category'] ), 'description' );
1406
	}
1407
1408
	// Return product content as description if product page
1409
	if ( !empty($_GET['product_id'] ) && is_numeric( $_GET['product_id'] ) ) {
1410
		$product = get_post(absint( $_GET['product_id'] ));
1411
		$output = $product->post_content;
1412
	}
1413
1414
	return $output;
1415
}
1416
1417
function wpsc_set_aioseop_description( $data ) {
1418
	$replacement_data = wpsc_obtain_the_description();
1419
	if ( $replacement_data != '' ) {
1420
		$data = $replacement_data;
1421
	}
1422
	return $data;
1423
}
1424
1425
/**
1426
 * 	this page url function, returns the URL of this page
1427
 * @return string - the URL of the current page
1428
 */
1429
function wpsc_this_page_url() {
1430
1431
	global $wpsc_query, $wp_query;
1432
1433
	if ( $wpsc_query->is_single === true ) {
1434
		$output = get_permalink( $wp_query->post->ID );
1435
	} else if ( isset( $wpsc_query->category ) && $wpsc_query->category != null ) {
1436
		$output = wpsc_category_url( $wpsc_query->category );
1437
		if ( $wpsc_query->query_vars['page'] > 1 ) {
1438
			if ( get_option( 'permalink_structure' ) ) {
1439
				$output .= "page/{$wpsc_query->query_vars['page']}/";
1440
			} else {
1441
				$output = add_query_arg( 'page_number', $wpsc_query->query_vars['page'], $output );
1442
			}
1443
		}
1444
	} else {
1445
		$output = get_permalink( get_the_ID() );
1446
	}
1447
1448
	return esc_url( $output );
1449
}
1450
1451
/**
1452
 * Strips shortcode placeholders from excerpts returned in search results (and excerpts returned anywhere).
1453
 *
1454
 * @param  string $text Trimmed excerpt
1455
 * @return string $text Trimmed excerpt, sans placeholders
1456
 *
1457
 * @since  3.9.0
1458
 */
1459
function wpsc_strip_shortcode_placeholders( $text ) {
1460
1461
    $is_wpsc_placeholder = false;
1462
1463
    $wpsc_shortcodes = array(
1464
        '[productspage]'      ,
1465
        '[shoppingcart]'      ,
1466
        '[checkout]'          ,
1467
        '[transactionresults]',
1468
        '[userlog]'           ,
1469
    );
1470
1471
    foreach ( $wpsc_shortcodes as $shortcode ) {
1472
1473
        if ( false !== strpos( $text, $shortcode ) ) {
1474
            $is_wpsc_placeholder = $shortcode;
1475
            break;
1476
        }
1477
    }
1478
1479
    if ( $is_wpsc_placeholder ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $is_wpsc_placeholder of type string|false is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
1480
        $text = str_replace( $is_wpsc_placeholder, '', $text );
1481
    }
1482
1483
    return $text;
1484
1485
}
1486
1487
add_filter( 'wp_trim_excerpt', 'wpsc_strip_shortcode_placeholders' );
1488