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

query.php ➔ wpsc_start_the_query()   F

Complexity

Conditions 36
Paths 17353

Size

Total Lines 99
Code Lines 67

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 36
eloc 67
nc 17353
nop 0
dl 0
loc 99
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
// switch $wp_query and $wpsc_query at the beginning and the end of wp_nav_menu()
3
add_action( 'pre_get_posts', '_wpsc_pre_get_posts_reset_taxonomy_globals', 1 );
4
add_action( 'template_redirect', 'wpsc_start_the_query', 8 );
5
add_action( 'wp', 'wpsc_force_ssl' );
6
7
if ( is_ssl() ) {
8
	add_filter( 'option_product_list_url',  'set_url_scheme' );
9
	add_filter( 'option_shopping_cart_url', 'set_url_scheme' );
10
	add_filter( 'option_transact_url',      'set_url_scheme' );
11
	add_filter( 'option_user_account_url',  'set_url_scheme' );
12
}
13
14
add_filter( 'wp_nav_menu_args', 'wpsc_switch_the_query', 99 );
15
add_filter( 'request', 'wpsc_filter_query_request' );
16
add_filter( 'pre_get_posts', 'wpsc_split_the_query', 8 );
17
add_filter( 'parse_query', 'wpsc_mark_product_query', 12 );
18
add_filter( 'query_vars', 'wpsc_query_vars' );
19
20
if ( get_option( 'product_category_hierarchical_url' ) ) {
21
	add_filter( 'request', 'wpsc_filter_request' );
22
}
23
24
/**
25
 * Fixes for some inconsistencies about $wp_query when viewing WPEC pages.
26
 *
27
 * Causes the following URLs to work (with pagination enabled):
28
 *
29
 * /products-page/ (product listing)
30
 * /products-page/car-audio/ (existing product category)
31
 * /products-page/car-audio/page/2/ (existing product category, page 2)
32
 * /products-page/page/2/  (product listing, page 2)
33
 * /products-page/checkout/  (existing built-in sub page)
34
 * /products-page/anotherpage/  (another sub page that may exist)
35
 *
36
 * @param string $q Query String
0 ignored issues
show
Bug introduced by
There is no parameter named $q. 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...
37
 */
38
function wpsc_filter_query_request( $args ) {
39
	global $wpsc_page_titles;
40
	if ( is_admin() )
41
		return $args;
42
43
	$is_sub_page =    ! empty( $args['wpsc_product_category'] )
0 ignored issues
show
introduced by
Expected 1 space after "="; 4 found
Loading history...
introduced by
Expected 1 space before "!"; 4 found
Loading history...
44
				   &&   'page' != $args['wpsc_product_category']
45
				   && ! term_exists( $args['wpsc_product_category'], 'wpsc_product_category' );
46
47
	// Make sure no 404 error is thrown for any sub pages of products-page
48
	if ( $is_sub_page ) {
49
		// Probably requesting a page that is a sub page of products page
50
		$pagename = "{$wpsc_page_titles['products']}/{$args['wpsc_product_category']}";
51
		if ( isset($args['name']) ) {
52
			$pagename .= "/{$args['name']}";
53
		}
54
		$args = array();
55
		$args['pagename'] = $pagename;
56
	}
57
58
	// When product page is set to display all products or a category, and pagination is enabled, $wp_query is messed up
59
	// and is_home() is true. This fixes that.
60
	$needs_pagination_fix =      isset( $args['post_type'] )
0 ignored issues
show
introduced by
Expected 1 space after "="; 6 found
Loading history...
61
							&& ! empty( $args['wpsc_product_category'] )
62
							&&   'wpsc-product' == $args['post_type']
63
							&& ! empty( $args['wpsc-product'] )
64
							&&   'page' == $args['wpsc_product_category'];
65
	if ( $needs_pagination_fix ) {
66
		$default_category = get_option( 'wpsc_default_category' );
67
		if ( $default_category == 'all' || $default_category != 'list' ) {
68
			$page = $args['wpsc-product'];
69
			$args = array();
70
			$args['pagename'] = "{$wpsc_page_titles['products']}";
71
			$args['page'] = $page;
72
		}
73
	}
74
	return $args;
75
}
76
77
function _wpsc_menu_exists( $args ) {
78
	$args = (object) $args;
79
	// Get the nav menu based on the requested menu
80
	$menu = wp_get_nav_menu_object( $args->menu );
81
82
	// Get the nav menu based on the theme_location
83
	if ( ! $menu && $args->theme_location && ( $locations = get_nav_menu_locations() ) && isset( $locations[ $args->theme_location ] ) )
84
		$menu = wp_get_nav_menu_object( $locations[ $args->theme_location ] );
85
86
	// get the first menu that has items if we still can't find a menu
87
	if ( ! $menu && ! $args->theme_location ) {
88
		$menus = wp_get_nav_menus();
89
		foreach ( $menus as $menu_maybe ) {
90
			if ( $menu_items = wp_get_nav_menu_items( $menu_maybe->term_id ) ) {
91
				$menu = $menu_maybe;
92
				break;
93
			}
94
		}
95
	}
96
97
	// If the menu exists, get its items.
98
	if ( $menu && ! is_wp_error( $menu ) && ! isset( $menu_items ) )
99
		$menu_items = wp_get_nav_menu_items( $menu->term_id );
100
101
	// If no menu was found or if the menu has no items and no location was requested, call the fallback_cb if it exists
102
	if ( ( ! $menu || is_wp_error( $menu ) || ( isset( $menu_items ) && empty( $menu_items ) && ! $args->theme_location ) ) )
103
		return false;
104
105
	// If no fallback function was specified and the menu doesn't exists, bail.
106
	if ( ! $menu || is_wp_error( $menu ) || empty( $menu_items ) )
107
		return false;
108
109
	return (bool) $menu;
110
}
111
112
function _wpsc_switch_the_query( $stuff = '' ) {
113
	global $wp_query, $wpsc_query;
114
	list( $wp_query, $wpsc_query ) = array( $wpsc_query, $wp_query );
115
	return $stuff;
116
}
117
118
/**
119
 * Switch $wp_query and $wpsc_query when outputting the navigation menu, but only if we're on a product
120
 * category page.
121
 *
122
 * We need to do this because the function _wp_menu_item_classes_by_context(), which generates classes
123
 * for menu items, depends on $wp_query. As a result, without this fix, when viewing a product category
124
 * page, the corresponding product category menu item will not be highlighted.
125
 *
126
 * Because there are no action hooks in wp_nav_menu(), we have to use two filters that are applied at
127
 * the beginning and the end of the function.
128
 *
129
 * @param mixed $stuff
0 ignored issues
show
Bug introduced by
There is no parameter named $stuff. 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...
130
 * @return mixed
131
 */
132
function wpsc_switch_the_query( $args ) {
133
	global $wp_query, $wpsc_query;
134
	if ( ! empty ( $wpsc_query ) ) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
135
		$qv = $wpsc_query->query_vars;
136
		if ( ! empty( $qv ) ) {
137
			if ( ! empty( $qv['wpsc_product_category'] ) && ! empty( $qv['taxonomy'] ) && ! empty( $qv['term'] ) && ! is_single() && _wpsc_menu_exists( $args ) ) {
138
				_wpsc_switch_the_query();
139
				add_filter( 'wp_nav_menu', '_wpsc_switch_the_query', 99 );
140
			}
141
		}
142
	}
143
	return $args;
144
}
145
146
function _wpsc_pre_get_posts_reset_taxonomy_globals( $query ) {
147
	global $wp_the_query;
148
149
	if ( is_admin() || $query !== $wp_the_query )
150
		return;
151
152
	if ( ! $query->get( 'page' ) && ! $query->get( 'paged' ) )
153
		return;
154
155
	if ( ! get_option( 'use_pagination' ) )
156
		return;
157
158
	if ( ! is_page( wpsc_get_the_post_id_by_shortcode( '[productspage]' ) ) && ! $query->get( 'wpsc_product_category' ) )
159
		return;
160
161
	$query->set( 'posts_per_page', get_option( 'wpsc_products_per_page' ) );
162
163
	$post_type_object = get_post_type_object( 'wpsc-product' );
164
165
	if ( current_user_can( $post_type_object->cap->edit_posts ) )
166
		$query->set( 'post_status', apply_filters( 'wpsc_product_display_status', array( 'publish' ) ) );
167
	else
168
		$query->set( 'post_status', 'publish' );
169
}
170
171
/**
172
 * wpsc_start_the_query
173
 */
174
function wpsc_start_the_query() {
175
	if ( is_404() ) {
176
		return;
177
	}
178
179
	global $wpsc_page_titles, $wp_query, $wpsc_query, $wpsc_query_vars;
180
181
	$is_404 = $wp_query->is_404;
182
	if ( null == $wpsc_query ) {
183
		if( ( $wp_query->is_404 && !empty($wp_query->query_vars['paged']) ) || (isset( $wp_query->query['pagename']) && strpos( $wp_query->query['pagename'] , $wpsc_page_titles['products'] ) !== false ) && !isset($wp_query->post)){
184
			global $post;
185
			$is_404 = true;
186
			if( !isset( $wp_query->query_vars['wpsc_product_category'] ) && ! isset( $wp_query->query_vars['product_tag'] ) )
187
				$wp_query = new WP_Query('post_type=wpsc-product&name='.$wp_query->query_vars['name']);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
188
189
			if(  isset( $wp_query->post ) && is_object( $wp_query->post ) && isset($wp_query->post->ID))
190
				$post = $wp_query->post;
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
191
			else
192
				$wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['name'];
193
		}
194
		if ( count( $wpsc_query_vars ) <= 1 ) {
195
			$wpsc_query_vars = array(
196
				'post_status' => apply_filters( 'wpsc_product_display_status', array( 'publish' ) ),
197
				'post_parent' => 0,
198
				'order'       => apply_filters( 'wpsc_product_order', get_option( 'wpsc_product_order', 'ASC' ) ),
199
				'post_type'   => apply_filters( 'wpsc_product_post_type', array( 'wpsc-product' ) ),
200
			);
201
			if($wp_query->query_vars['preview'])
202
				$wpsc_query_vars['post_status'] = 'any';
203
204
			if( isset( $_GET['product_order'] ) )
205
				$wpsc_query_vars['order'] = $_GET['product_order'];
206
207
			if(isset($wp_query->query_vars['product_tag'])){
208
				$wpsc_query_vars['product_tag'] = $wp_query->query_vars['product_tag'];
209
				$wpsc_query_vars['taxonomy'] = get_query_var( 'taxonomy' );
210
				$wpsc_query_vars['term'] = get_query_var( 'term' );
211
			}elseif( isset($wp_query->query_vars['wpsc_product_category']) ){
212
				$wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
213
				$wpsc_query_vars['taxonomy'] = get_query_var( 'taxonomy' );
214
				$wpsc_query_vars['term'] = get_query_var( 'term' );
215
			}else{
216
				$wpsc_query_vars['post_type'] = 'wpsc-product';
217
				$wpsc_query_vars['pagename']  = wpsc_get_page_slug( '[productspage]' );
218
			}
219
			if(1 == get_option('use_pagination')){
220
				$wpsc_query_vars['nopaging'] = false;
221
222
				$wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
223
224
				$wpsc_query_vars['paged'] = get_query_var('paged');
225
				if(isset($wpsc_query_vars['paged']) && empty($wpsc_query_vars['paged'])){
226
					$wpsc_query_vars['paged'] = get_query_var('page');
227
228
				}
0 ignored issues
show
introduced by
Blank line found after control structure
Loading history...
229
230
			}
231
232
			$orderby = ( isset( $_GET['product_order'] ) ) ? 'title' : null;
233
			$wpsc_query_vars = array_merge( $wpsc_query_vars, wpsc_product_sort_order_query_vars($orderby) );
234
235
			add_filter( 'pre_get_posts', 'wpsc_generate_product_query', 11 );
236
237
			$wpsc_query = new WP_Query( $wpsc_query_vars );
238
239
			//for 3.1 :|
240
			if(empty($wpsc_query->posts) && isset($wpsc_query->tax_query) && isset($wp_query->query_vars['wpsc_product_category'])){
241
				$wpsc_query_vars = array();
242
				$wpsc_query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
243
				if(1 == get_option('use_pagination')){
244
					$wpsc_query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
245
					$wpsc_query_vars['paged'] = get_query_var('paged');
246
					if(empty($wpsc_query_vars['paged']))
247
						$wpsc_query_vars['paged'] = get_query_var('page');
248
				}
249
				$wpsc_query = new WP_Query( $wpsc_query_vars );
250
251
			}
252
		}
253
	}
254
255
	if(  $is_404 || ( ( isset($wpsc_query->post_count) && $wpsc_query->post_count == 0 ) && isset($wpsc_query_vars['wpsc_product_category'] )  )){
256
257
		$args = array_merge($wp_query->query, array('posts_per_page' => get_option('wpsc_products_per_page')));
258
		$wp_query = new WP_Query($args);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
259
260
		if( empty( $wp_query->posts ) ){
261
			$product_page_id = wpsc_get_the_post_id_by_shortcode('[productspage]');
262
			$wp_query = new WP_Query( 'page_id='.$product_page_id);
0 ignored issues
show
introduced by
Overridding WordPress globals is prohibited
Loading history...
263
		}
264
	}
265
	if ( isset( $wp_query->post ) && is_object( $wp_query->post ) && isset( $wp_query->post->ID ) )
266
		$post_id = $wp_query->post->ID;
267
	else
268
		$post_id = 0;
269
270
	if ( get_permalink( $post_id ) == get_option( 'shopping_cart_url' ) )
271
		$_SESSION['wpsc_has_been_to_checkout'] = true;
0 ignored issues
show
introduced by
Usage of $_SESSION variable is prohibited.
Loading history...
272
}
273
274
/**
275
* Returns page slug that corresponds to a given WPEC-specific shortcode.
276
*
277
* @since 3.8.10
278
*
279
* @uses wpsc_get_the_post_id_by_shortcode() Gets page ID of shortcode.
280
* @uses get_post_field() Returns post name of page ID.
281
*
282
* @param string $shortcode Shortcode of WPEC-specific page, e.g. '[productspage]''
283
* @return string Post slug
284
*/
285
function wpsc_get_page_slug( $shortcode ) {
286
	$id = wpsc_get_the_post_id_by_shortcode( $shortcode );
287
	return get_post_field( 'post_name', $id );
288
}
289
290
/**
291
 * Obtain the necessary product sort order query variables based on the specified product sort order.
292
 * If no sort order is specified, the sort order configured in Dashboard -> Settings -> Store -> Presentation -> 'Sort Product By' is used.
293
 *
294
 * @param string $orderby optional product sort order
0 ignored issues
show
Documentation introduced by
Should the type for parameter $orderby not be string|null?

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...
295
 * @return array Array of query variables
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,string>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
296
 */
297
function wpsc_product_sort_order_query_vars( $orderby = null ) {
298
	if ( is_null( $orderby ) ) {
299
		$orderby = get_option( 'wpsc_sort_by' );
300
	}
301
302
	$query_vars = array();
303
304
	switch ( $orderby ) {
305
306
		case "dragndrop":
307
			$query_vars["orderby"] = 'menu_order';
308
			break;
309
310
		case "name":
311
			$query_vars["orderby"] = 'title';
312
			break;
313
314
		//This only works in WP 3.0.
315
		case "price":
316
			add_filter( 'posts_join', 'wpsc_add_meta_table' );
317
			add_filter( 'posts_where', 'wpsc_add_meta_table_where' );
318
			$query_vars["meta_key"] = '_wpsc_price';
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
319
			$query_vars["orderby"] = 'meta_value_num';
320
			break;
321
322
		case "id":
323
			$query_vars["orderby"] = 'ID';
324
			break;
325
		default:
326
			// Allow other WordPress 'ordery' values as defined in http://codex.wordpress.org/Class_Reference/WP_Query#Order_.26_Orderby_Parameters
327
			$query_vars["orderby"] = $orderby;
328
			break;
329
	}
330
	return $query_vars;
331
}
332
333
/**
334
 * wpsc_query_vars function.
335
 * adds in the post_type and wpsc_item query vars
336
 *
337
 * @since 3.8
338
 * @access public
339
 * @param mixed $vars
340
 * @return void
341
 */
342
function wpsc_query_vars( $vars ) {
343
	// post_type is used to specify that we are looking for products
344
	$vars[] = "post_type";
345
	// wpsc_item is used to find items that could be either a product or a product category, it defaults to category, then tries products
346
	$vars[] = "wpsc_item";
347
	return $vars;
348
}
349
350
/**
351
 * wpsc_query_modifier function.
352
 *
353
 * @since 3.8
354
 * @access public
355
 * @param object - reference to $wp_query
356
 * @return $query
0 ignored issues
show
Documentation introduced by
The doc-type $query could not be parsed: Unknown type name "$query" 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...
357
 */
358
function wpsc_split_the_query( $query ) {
359
	global $wpsc_page_titles, $wpsc_query, $wpsc_query_vars;
360
	// These values are to be dynamically defined
361
	$products_page = $wpsc_page_titles['products'];
362
	$checkout_page = $wpsc_page_titles['checkout'];
363
	$userlog_page = $wpsc_page_titles['userlog'];
364
	$transaction_results_page = $wpsc_page_titles['transaction_results'];
365
366
	// otherwise, check if we are looking at a product, if so, duplicate the query and swap the old one out for a products page request
367
	// JS - 6.4.1020 - Added is_admin condition, as the products condition broke categories in backend
368
	if ( !empty($query->query_vars['pagename']) && ($query->query_vars['pagename'] == $products_page) || isset( $query->query_vars['products'] ) && !is_admin() ) {
369
		// store a copy of the wordpress query
370
		$wpsc_query_data = $query->query;
371
372
		// wipe and replace the query vars
373
		$query->query                   = array();
374
		$query->query['pagename']       = "$products_page";
375
		$query->query_vars['pagename']  = "$products_page";
376
		$query->query_vars['name']      = '';
377
		$query->query_vars['post_type'] = '';
378
379
		$query->queried_object = get_page_by_path( $query->query['pagename'] );
380
381
		if ( !empty( $query->queried_object ) )
382
			$query->queried_object_id = (int)$query->queried_object->ID;
383
		else
384
			unset( $query->queried_object );
385
386
		unset( $query->query_vars['products'] );
387
		unset( $query->query_vars['name'] );
388
		unset( $query->query_vars['taxonomy'] );
389
		unset( $query->query_vars['term'] );
390
		unset( $query->query_vars['wpsc_item'] );
391
392
		$query->is_singular = true;
393
		$query->is_page     = true;
394
		$query->is_tax      = false;
395
		$query->is_archive  = false;
396
		$query->is_single   = false;
397
398
		if ( ($wpsc_query_vars == null ) ) {
399
			unset( $wpsc_query_data['pagename'] );
400
			$wpsc_query_vars = $wpsc_query_data;
401
		}
402
	}
403
404
	add_filter( 'redirect_canonical', 'wpsc_break_canonical_redirects', 10, 2 );
405
	remove_filter( 'pre_get_posts', 'wpsc_split_the_query', 8 );
406
}
407
408
/**
409
 * wpsc_generate_product_query function.
410
 *
411
 * @access public
412
 * @param mixed $query
413
 * @return void
414
 */
415
function wpsc_generate_product_query( $query ) {
416
	global $wp_query;
417
	$prod_page = wpsc_get_the_post_id_by_shortcode('[productspage]');
418
	$prod_page = get_post($prod_page);
419
	remove_filter( 'pre_get_posts', 'wpsc_generate_product_query', 11 );
420
	$query->query_vars['taxonomy'] = null;
421
	$query->query_vars['term'] = null;
422
423
	// default product selection
424
	if ( $query->query_vars['pagename'] != '' ) {
425
		$query->query_vars['post_type'] = 'wpsc-product';
426
		$query->query_vars['pagename']  = '';
427
		$query->is_page     = false;
428
		$query->is_tax      = false;
429
		$query->is_archive  = true;
430
		$query->is_singular = false;
431
		$query->is_single   = false;
432
	}
433
434
	// If wpsc_item is not null, we are looking for a product or a product category, check for category
435
	if ( isset( $query->query_vars['wpsc_item'] ) && ($query->query_vars['wpsc_item'] != '') ) {
436
		$test_term = get_term_by( 'slug', $query->query_vars['wpsc_item'], 'wpsc_product_category' );
437
		if ( $test_term->slug == $query->query_vars['wpsc_item'] ) {
438
			// if category exists (slug matches slug), set products to value of wpsc_item
439
			$query->query_vars['products'] = $query->query_vars['wpsc_item'];
440
		} else {
441
			// otherwise set name to value of wpsc_item
442
			$query->query_vars['name'] = $query->query_vars['wpsc_item'];
443
		}
444
	}
445
446
	if ( isset( $query->query_vars['products'] ) && ($query->query_vars['products'] != null) && ($query->query_vars['name'] != null) ) {
447
		unset( $query->query_vars['taxonomy'] );
448
		unset( $query->query_vars['term'] );
449
		$query->query_vars['post_type'] = 'wpsc-product';
450
		$query->is_tax      = false;
451
		$query->is_archive  = true;
452
		$query->is_singular = false;
453
		$query->is_single   = false;
454
	}
455
	if( isset($wp_query->query_vars['wpsc_product_category']) && !isset($wp_query->query_vars['wpsc-product'])){
456
		$query->query_vars['wpsc_product_category'] = $wp_query->query_vars['wpsc_product_category'];
457
		$query->query_vars['taxonomy'] = $wp_query->query_vars['taxonomy'];
458
		$query->query_vars['term'] = $wp_query->query_vars['term'];
459
	}elseif( '' != ($default_category = get_option('wpsc_default_category')) && !isset($wp_query->query_vars['wpsc-product'])){
460
		$default_term = get_term($default_category,'wpsc_product_category');
461
		if(!empty($default_term) && empty($wp_query->query_vars['category_name'])){
462
			$query->query_vars['taxonomy'] = 'wpsc_product_category';
463
			$query->query_vars['term'] = $default_term->slug;
464
			$query->is_tax = true;
465
		}elseif(isset($wp_query->query_vars['name']) && $wp_query->is_404 && $wp_query->query_vars['category_name'] != $prod_page->post_name){
466
			unset( $query->query_vars['taxonomy'] );
467
			unset( $query->query_vars['term'] );
468
			$query->query_vars['wpsc-product'] = $wp_query->query_vars['name'];
469
			$query->query_vars['name'] = $wp_query->query_vars['name'];
470
471
		}else{
472
			$query->is_tax = true;
473
			$term =	get_term_by('slug',$wp_query->query_vars['name'], 'wpsc_product_category' );
0 ignored issues
show
introduced by
Expected 1 space after "="; 5 found
Loading history...
474
			if(!empty($term)){
475
				$query->query_vars['taxonomy'] = 'wpsc_product_category';
476
				$query->query_vars['wpsc_product_category__in'] = array($term->term_taxonomy_id);
477
				$query->query_vars['wpsc_product_category'] = $wp_query->query_vars['name'];
478
				$query->query_vars['term'] = $wp_query->query_vars['name'];
479
			}elseif(is_numeric($default_category)){
480
				$query->query_vars['taxonomy'] = 'wpsc_product_category';
481
			}else{
482
				$query->is_tax = false;
483
			}
484
		}
485
	}
486
	//If Product Tag Taxonomy
487
	if (isset($wp_query->query_vars['product_tag']) && $wp_query->query_vars['product_tag']){
488
		$query->query_vars['product_tag'] = $wp_query->query_vars['product_tag'];
489
		$query->query_vars['term'] = $wp_query->query_vars['term'];
490
		$query->query_vars['taxonomy'] = 'product_tag';
491
		$query->is_tax      = true;
492
	}
493
	if(1 == get_option('use_pagination')){
494
		$query->query_vars['posts_per_page'] = get_option('wpsc_products_per_page');
495
		if( isset( $_GET['items_per_page'] ) ) {
496
			if ( is_numeric( $_GET['items_per_page'] ) ) {
497
				$query->query_vars['posts_per_page'] = (int) $_GET['items_per_page'];
498
			} elseif ( $_GET['items_per_page'] == 'all' ) {
499
				$query->query_vars['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...
500
				$query->query_vars['nopaging'] = 1;
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to 1 ever.
Loading history...
501
			}
502
		}
503
	} else {
504
		$query->query_vars['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...
505
		$query->query_vars['nopaging'] = 1;
0 ignored issues
show
introduced by
Disabling pagination is prohibited in VIP context, do not set nopaging to 1 ever.
Loading history...
506
	}
507
508
	return $query;
509
}
510
511
function wpsc_mark_product_query( $query ) {
512
513
	if ( isset( $query->query_vars['post_type'] ) && ($query->query_vars['post_type'] == 'wpsc-product') )
514
		$query->is_product = true;
515
516
	return $query;
517
}
518
519
/**
520
 * add meta table where section for ordering by price
521
 *
522
 */
523
function wpsc_add_meta_table_where($where){
524
	global $wpdb;
525
526
	remove_filter( 'posts_where', 'wpsc_add_meta_table_where' );
527
528
	return $where . ' AND ' . $wpdb->postmeta . '.meta_key = "_wpsc_price"';
529
}
530
531
/**
532
 * add meta table join section for ordering by price
533
 *
534
 */
535
function wpsc_add_meta_table($join){
536
	global $wpdb;
537
	remove_filter( 'posts_join', 'wpsc_add_meta_table' );
538
	if(strpos($join, "INNER JOIN ON (".$wpdb->posts.".ID = ".$wpdb->postmeta.".post_id)") !== false){
539
		return  ' JOIN ' . $wpdb->postmeta . ' ON ' . $wpdb->posts. '.ID = ' . $wpdb->postmeta . '.post_id';
540
	}else{
541
		return $join;
542
	}
543
}
544
545
function wpsc_break_canonical_redirects( $redirect_url, $requested_url ) {
546
	global $wp_query;
547
548
	if ( ( isset( $wp_query->query_vars['products'] ) && ($wp_query->query_vars['products'] != '') ) || ( isset( $wp_query->query_vars['products'] ) && $wp_query->query_vars['products'] != 'wpsc_item') )
549
		return false;
550
551
	if ( stristr( $requested_url, $redirect_url ) )
552
		return false;
553
554
	return $redirect_url;
555
}
556
557
function wpsc_filter_request( $q ) {
558
	if ( empty( $q['wpsc-product'] ) )
559
		return $q;
560
561
	$components = explode( '/', $q['wpsc-product'] );
562
	$end_node = array_pop( $components );
563
	$parent_node = array_pop( $components );
564
565
	$posts = get_posts( array(
566
		'post_type' => 'wpsc-product',
567
		'name' => $end_node,
568
	) );
569
570
	if ( ! empty( $posts ) ) {
571
		$q['wpsc-product'] = $q['name'] = $end_node;
572
		if ( !empty( $parent_node ) )
573
			$q['wpsc_product_category'] = $parent_node;
574
	} else {
575
		$q['wpsc_product_category'] = $end_node;
576
		unset( $q['name'] );
577
		unset( $q['wpsc-product'] );
578
	}
579
	return $q;
580
}
581
582
/**
583
 * if the user is on a checkout page, force SSL if that option is so set
584
 */
585
function wpsc_force_ssl() {
586
587
	global $wp_query;
588
589
	if (
590
		! is_admin() &&
591
		'1' == get_option( 'wpsc_force_ssl' ) &&
592
		! is_ssl() &&
593
		! empty ( $wp_query->post->post_content ) &&
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
594
		false !== strpos( $wp_query->post->post_content, '[shoppingcart]' ) ) {
595
			$sslurl = 'https://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
596
			wp_redirect( $sslurl );
597
			exit;
598
	}
599
}
600
601
/**
602
 * wpsc_add_https_to_page_url_options( $url )
603
 *
604
 * Forces SSL onto option URLs
605
 *
606
 * @param string $url
607
 * @deprecated 3.8.14
608
 * @return string
609
 */
610
function wpsc_add_https_to_page_url_options( $url ) {
611
	return str_replace( 'http://', 'https://', $url );
612
}
613
614
/**
615
 * Checks if current page is shopping cart, and it should be SSL, but is not.
616
 * Used primarily for str_replacing links or content for https
617
 *
618
 * @since 3.8.8.1
619
 * @deprecated 3.8.8.2
620
 * @return boolean true if we're on the shopping cart page and should be ssl, false if not
621
 */
622
function wpsc_is_ssl() {
623
	global $wp_query;
624
625
	return '1' == get_option( 'wpsc_force_ssl' ) && ! is_ssl() && false !== strpos( $wp_query->post->post_content, '[shoppingcart]' );
626
}
627
628