Completed
Pull Request — master (#10315)
by Mike
08:27
created

WC_Query::layered_nav_query()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
1 ignored issue
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 21 and the first side effect is on line 13.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/**
3
 * Contains the query functions for WooCommerce which alter the front-end post queries and loops
4
 *
5
 * @class 		WC_Query
6
 * @version		2.6.0
7
 * @package		WooCommerce/Classes
8
 * @category	Class
9
 * @author 		WooThemes
10
 */
11
12
if ( ! defined( 'ABSPATH' ) ) {
13
	exit;
14
}
15
16
if ( ! class_exists( 'WC_Query' ) ) :
17
18
/**
19
 * WC_Query Class.
20
 */
21
class WC_Query {
22
23
	/** @public array Query vars to add to wp */
24
	public $query_vars = array();
25
26
	/**
27
	 * Stores chosen attributes
28
	 * @var array
29
	 */
30
	private static $_chosen_attributes;
31
32
	/**
33
	 * Constructor for the query class. Hooks in methods.
34
	 *
35
	 * @access public
36
	 */
37
	public function __construct() {
38
		add_action( 'init', array( $this, 'add_endpoints' ) );
39
		if ( ! is_admin() ) {
40
			add_action( 'wp_loaded', array( $this, 'get_errors' ), 20 );
41
			add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
42
			add_action( 'parse_request', array( $this, 'parse_request'), 0 );
43
			add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
44
			add_action( 'wp', array( $this, 'remove_product_query' ) );
45
			add_action( 'wp', array( $this, 'remove_ordering_args' ) );
46
		}
47
		$this->init_query_vars();
48
	}
49
50
	/**
51
	 * Get any errors from querystring.
52
	 */
53
	public function get_errors() {
54
		if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) ) {
55
			wc_add_notice( $error, 'error' );
56
		}
57
	}
58
59
	/**
60
	 * Init query vars by loading options.
61
	 */
62
	public function init_query_vars() {
63
		// Query vars to add to WP
64
		$this->query_vars = array(
65
			// Checkout actions
66
			'order-pay'          => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
67
			'order-received'     => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
68
69
			// My account actions
70
			'view-order'         => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
71
			'edit-account'       => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
72
			'edit-address'       => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
73
			'lost-password'      => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
74
			'customer-logout'    => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
75
			'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
76
		);
77
	}
78
79
	/**
80
	 * Get page title for an endpoint.
81
	 * @param  string
82
	 * @return string
83
	 */
84
	public function get_endpoint_title( $endpoint ) {
85
		global $wp;
86
87
		switch ( $endpoint ) {
88
			case 'order-pay' :
89
				$title = __( 'Pay for Order', 'woocommerce' );
90
			break;
91
			case 'order-received' :
92
				$title = __( 'Order Received', 'woocommerce' );
93
			break;
94
			case 'view-order' :
95
				$order = wc_get_order( $wp->query_vars['view-order'] );
96
				$title = ( $order ) ? sprintf( __( 'Order #%s', 'woocommerce' ), $order->get_order_number() ) : '';
97
			break;
98
			case 'edit-account' :
99
				$title = __( 'Edit Account Details', 'woocommerce' );
100
			break;
101
			case 'edit-address' :
102
				$title = __( 'Edit Address', 'woocommerce' );
103
			break;
104
			case 'add-payment-method' :
105
				$title = __( 'Add Payment Method', 'woocommerce' );
106
			break;
107
			case 'lost-password' :
108
				$title = __( 'Lost Password', 'woocommerce' );
109
			break;
110
			default :
111
				$title = '';
112
			break;
113
		}
114
		return $title;
115
	}
116
117
	/**
118
	 * Add endpoints for query vars.
119
	 */
120
	public function add_endpoints() {
121
		foreach ( $this->query_vars as $key => $var ) {
122
			add_rewrite_endpoint( $var, EP_ROOT | EP_PAGES );
123
		}
124
	}
125
126
	/**
127
	 * Add query vars.
128
	 *
129
	 * @access public
130
	 * @param array $vars
131
	 * @return array
132
	 */
133
	public function add_query_vars( $vars ) {
134
		foreach ( $this->query_vars as $key => $var ) {
135
			$vars[] = $key;
136
		}
137
		return $vars;
138
	}
139
140
	/**
141
	 * Get query vars.
142
	 *
143
	 * @return array
144
	 */
145
	public function get_query_vars() {
146
		return $this->query_vars;
147
	}
148
149
	/**
150
	 * Get query current active query var.
151
	 *
152
	 * @return string
153
	 */
154
	public function get_current_endpoint() {
155
		global $wp;
156
		foreach ( $this->get_query_vars() as $key => $value ) {
157
			if ( isset( $wp->query_vars[ $key ] ) ) {
158
				return $key;
159
			}
160
		}
161
		return '';
162
	}
163
164
	/**
165
	 * Parse the request and look for query vars - endpoints may not be supported.
166
	 */
167
	public function parse_request() {
168
		global $wp;
169
170
		// Map query vars to their keys, or get them if endpoints are not supported
171
		foreach ( $this->query_vars as $key => $var ) {
172
			if ( isset( $_GET[ $var ] ) ) {
173
				$wp->query_vars[ $key ] = $_GET[ $var ];
174
			}
175
176
			elseif ( isset( $wp->query_vars[ $var ] ) ) {
177
				$wp->query_vars[ $key ] = $wp->query_vars[ $var ];
178
			}
179
		}
180
	}
181
182
	/**
183
	 * Hook into pre_get_posts to do the main product query.
184
	 *
185
	 * @param mixed $q query object
186
	 */
187
	public function pre_get_posts( $q ) {
188
		// We only want to affect the main query
189
		if ( ! $q->is_main_query() ) {
190
			return;
191
		}
192
193
		// Fix for verbose page rules
194
		if ( $GLOBALS['wp_rewrite']->use_verbose_page_rules && isset( $q->queried_object->ID ) && $q->queried_object->ID === wc_get_page_id( 'shop' ) ) {
195
			$q->set( 'post_type', 'product' );
196
			$q->set( 'page', '' );
197
			$q->set( 'pagename', '' );
198
199
			// Fix conditional Functions
200
			$q->is_archive           = true;
201
			$q->is_post_type_archive = true;
202
			$q->is_singular          = false;
203
			$q->is_page              = false;
204
		}
205
206
		// Fix for endpoints on the homepage
207
		if ( $q->is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) !== absint( $q->get( 'page_id' ) ) ) {
208
			$_query = wp_parse_args( $q->query );
209
			if ( ! empty( $_query ) && array_intersect( array_keys( $_query ), array_keys( $this->query_vars ) ) ) {
210
				$q->is_page     = true;
211
				$q->is_home     = false;
212
				$q->is_singular = true;
213
				$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
214
				add_filter( 'redirect_canonical', '__return_false' );
215
			}
216
		}
217
218
		// When orderby is set, WordPress shows posts. Get around that here.
219
		if ( $q->is_home() && 'page' === get_option( 'show_on_front' ) && absint( get_option( 'page_on_front' ) ) === wc_get_page_id( 'shop' ) ) {
220
			$_query = wp_parse_args( $q->query );
221
			if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
222
				$q->is_page = true;
223
				$q->is_home = false;
224
				$q->set( 'page_id', (int) get_option( 'page_on_front' ) );
225
				$q->set( 'post_type', 'product' );
226
			}
227
		}
228
229
		// Special check for shops with the product archive on front
230
		if ( $q->is_page() && 'page' === get_option( 'show_on_front' ) && absint( $q->get( 'page_id' ) ) === wc_get_page_id( 'shop' ) ) {
231
232
			// This is a front-page shop
233
			$q->set( 'post_type', 'product' );
234
			$q->set( 'page_id', '' );
235
236
			if ( isset( $q->query['paged'] ) ) {
237
				$q->set( 'paged', $q->query['paged'] );
238
			}
239
240
			// Define a variable so we know this is the front page shop later on
241
			define( 'SHOP_IS_ON_FRONT', true );
242
243
			// Get the actual WP page to avoid errors and let us use is_front_page()
244
			// This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
245
			global $wp_post_types;
246
247
			$shop_page 	= get_post( wc_get_page_id( 'shop' ) );
248
249
			$wp_post_types['product']->ID 			= $shop_page->ID;
250
			$wp_post_types['product']->post_title 	= $shop_page->post_title;
251
			$wp_post_types['product']->post_name 	= $shop_page->post_name;
252
			$wp_post_types['product']->post_type    = $shop_page->post_type;
253
			$wp_post_types['product']->ancestors    = get_ancestors( $shop_page->ID, $shop_page->post_type );
254
255
			// Fix conditional Functions like is_front_page
256
			$q->is_singular          = false;
257
			$q->is_post_type_archive = true;
258
			$q->is_archive           = true;
259
			$q->is_page              = true;
260
261
			// Remove post type archive name from front page title tag
262
			add_filter( 'post_type_archive_title', '__return_empty_string', 5 );
263
264
			// Fix WP SEO
265
			if ( class_exists( 'WPSEO_Meta' ) ) {
266
				add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
267
				add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
268
			}
269
270
		// Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
271
		} elseif ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) ) {
272
			return;
273
		}
274
275
		$this->product_query( $q );
276
277
		if ( is_search() ) {
278
			add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
279
			add_filter( 'wp', array( $this, 'remove_posts_where' ) );
280
		}
281
282
		// And remove the pre_get_posts hook
283
		$this->remove_product_query();
284
	}
285
286
	/**
287
	 * Search post excerpt.
288
	 *
289
	 * @access public
290
	 * @param string $where (default: '')
291
	 * @return string (modified where clause)
292
	 */
293
	public function search_post_excerpt( $where = '' ) {
294
		global $wp_the_query;
295
296
		// If this is not a WC Query, do not modify the query
297
		if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
298
			return $where;
299
300
		$where = preg_replace(
301
			"/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
302
			"post_title LIKE $1) OR (post_excerpt LIKE $1", $where );
303
304
		return $where;
305
	}
306
307
	/**
308
	 * WP SEO meta description.
309
	 *
310
	 * Hooked into wpseo_ hook already, so no need for function_exist.
311
	 *
312
	 * @access public
313
	 * @return string
314
	 */
315
	public function wpseo_metadesc() {
316
		return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id( 'shop' ) );
317
	}
318
319
	/**
320
	 * WP SEO meta key.
321
	 *
322
	 * Hooked into wpseo_ hook already, so no need for function_exist.
323
	 *
324
	 * @access public
325
	 * @return string
326
	 */
327
	public function wpseo_metakey() {
328
		return WPSEO_Meta::get_value( 'metakey', wc_get_page_id( 'shop' ) );
329
	}
330
331
	/**
332
	 * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop.
333
	 *
334
	 * @param mixed $q
335
	 */
336
	public function product_query( $q ) {
337
		// Ordering query vars
338
		$ordering  = $this->get_catalog_ordering_args();
339
		$q->set( 'orderby', $ordering['orderby'] );
340
		$q->set( 'order', $ordering['order'] );
341
		if ( isset( $ordering['meta_key'] ) ) {
342
			$q->set( 'meta_key', $ordering['meta_key'] );
343
		}
344
345
		// Query vars that affect posts shown
346
		$q->set( 'meta_query', $this->get_meta_query( $q->get( 'meta_query' ) ) );
347
		$q->set( 'tax_query', $this->get_tax_query( $q->get( 'tax_query' ) ) );
348
		$q->set( 'posts_per_page', $q->get( 'posts_per_page' ) ? $q->get( 'posts_per_page' ) : apply_filters( 'loop_shop_per_page', get_option( 'posts_per_page' ) ) );
349
		$q->set( 'wc_query', 'product_query' );
350
		$q->set( 'post__in', array_unique( apply_filters( 'loop_shop_post_in', array() ) ) );
351
352
		do_action( 'woocommerce_product_query', $q, $this );
353
	}
354
355
356
	/**
357
	 * Remove the query.
358
	 */
359
	public function remove_product_query() {
360
		remove_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
361
	}
362
363
	/**
364
	 * Remove ordering queries.
365
	 */
366
	public function remove_ordering_args() {
367
		remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
368
		remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
369
	}
370
371
	/**
372
	 * Remove the posts_where filter.
373
	 */
374
	public function remove_posts_where() {
375
		remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
376
	}
377
378
	/**
379
	 * Returns an array of arguments for ordering products based on the selected values.
380
	 *
381
	 * @access public
382
	 * @return array
383
	 */
384
	public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
385
		global $wpdb;
386
387
		// Get ordering from query string unless defined
388
		if ( ! $orderby ) {
389
			$orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
390
391
			// Get order + orderby args from string
392
			$orderby_value = explode( '-', $orderby_value );
393
			$orderby       = esc_attr( $orderby_value[0] );
394
			$order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
395
		}
396
397
		$orderby = strtolower( $orderby );
398
		$order   = strtoupper( $order );
399
		$args    = array();
400
401
		// default - menu_order
402
		$args['orderby']  = 'menu_order title';
403
		$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
404
		$args['meta_key'] = '';
405
406
		switch ( $orderby ) {
407
			case 'rand' :
408
				$args['orderby']  = 'rand';
409
			break;
410
			case 'date' :
411
				$args['orderby']  = 'date';
412
				$args['order']    = $order == 'ASC' ? 'ASC' : 'DESC';
413
			break;
414
			case 'price' :
415
				$args['orderby']  = "meta_value_num ID";
416
				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
417
				$args['meta_key'] = '_price';
418
			break;
419
			case 'popularity' :
420
				$args['meta_key'] = 'total_sales';
421
422
				// Sorting handled later though a hook
423
				add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
424
			break;
425
			case 'rating' :
426
				// Sorting handled later though a hook
427
				add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
428
			break;
429
			case 'title' :
430
				$args['orderby']  = 'title';
431
				$args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
432
			break;
433
		}
434
435
		return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
436
	}
437
438
	/**
439
	 * WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065.
440
	 *
441
	 * This lets us sort by meta value desc, and have a second orderby param.
442
	 *
443
	 * @access public
444
	 * @param array $args
445
	 * @return array
446
	 */
447
	public function order_by_popularity_post_clauses( $args ) {
448
		global $wpdb;
449
		$args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
450
		return $args;
451
	}
452
453
	/**
454
	 * Order by rating post clauses.
455
	 *
456
	 * @access public
457
	 * @param array $args
458
	 * @return array
459
	 */
460
	public function order_by_rating_post_clauses( $args ) {
461
		global $wpdb;
462
463
		$args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
464
		$args['where']  .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
465
		$args['join']   .= "
466
			LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
467
			LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
468
		";
469
		$args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC";
470
		$args['groupby'] = "$wpdb->posts.ID";
471
472
		return $args;
473
	}
474
475
	/**
476
	 * Appends meta queries to an array.
477
	 * @access public
478
	 * @param array $meta_query
479
	 * @return array
480
	 */
481
	public function get_meta_query( $meta_query = array() ) {
482
		if ( ! is_array( $meta_query ) ) {
483
			$meta_query = array();
484
		}
485
486
		$meta_query[] = $this->visibility_meta_query();
487
		$meta_query[] = $this->stock_status_meta_query();
488
		$meta_query[] = $this->price_filter_meta_query();
489
		$meta_query[] = $this->rating_filter_meta_query();
490
491
		return array_filter( apply_filters( 'woocommerce_product_query_meta_query', $meta_query, $this ) );
492
	}
493
494
	/**
495
	 * Return a meta query for filtering by price.
496
	 * @return array
497
	 */
498
	private function price_filter_meta_query() {
499
		if ( isset( $_GET['max_price'] ) || isset( $_GET['min_price'] ) ) {
500
			$min = isset( $_GET['min_price'] ) ? floatval( $_GET['min_price'] ) : 0;
501
			$max = isset( $_GET['max_price'] ) ? floatval( $_GET['max_price'] ) : 9999999999;
502
503
			// If displaying prices in the shop including taxes, but prices don't include taxes..
504 View Code Duplication
			if ( wc_tax_enabled() && 'incl' === get_option( 'woocommerce_tax_display_shop' ) && ! wc_prices_include_tax() ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
505
				$tax_classes = array_merge( array( '' ), WC_Tax::get_tax_classes() );
506
507
				foreach ( $tax_classes as $tax_class ) {
508
					$tax_rates = WC_Tax::get_rates( $tax_class );
509
					$class_min = $min - WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $min, $tax_rates ) );
510
					$class_max = $max - WC_Tax::get_tax_total( WC_Tax::calc_inclusive_tax( $max, $tax_rates ) );
511
					if ( $class_min < $min ) {
512
						$min = $class_min;
513
					}
514
					if ( $class_max > $max ) {
515
						$max = $class_max;
516
					}
517
				}
518
			}
519
520
			return array(
521
				'key'          => '_price',
522
				'value'        => array( $min, $max ),
523
				'compare'      => 'BETWEEN',
524
				'type'         => 'DECIMAL',
525
				'price_filter' => true,
526
			);
527
		}
528
		return array();
529
	}
530
531
	/**
532
	 * Return a meta query for filtering by rating.
533
	 * @return array
534
	 */
535
	public function rating_filter_meta_query() {
536
		return isset( $_GET['min_rating'] ) ? array(
537
			'key'           => '_wc_average_rating',
538
			'value'         => isset( $_GET['min_rating'] ) ? floatval( $_GET['min_rating'] ) : 0,
539
			'compare'       => '>=',
540
			'type'          => 'DECIMAL',
541
			'rating_filter' => true,
542
		) : array();
543
	}
544
545
	/**
546
	 * Returns a meta query to handle product visibility.
547
	 * @param string $compare (default: 'IN')
548
	 * @return array
549
	 */
550
	public function visibility_meta_query( $compare = 'IN' ) {
551
		return array(
552
			'key'     => '_visibility',
553
			'value'   => is_search() ? array( 'visible', 'search' ) : array( 'visible', 'catalog' ),
554
			'compare' => $compare,
555
		);
556
	}
557
558
	/**
559
	 * Returns a meta query to handle product stock status.
560
	 *
561
	 * @access public
562
	 * @param string $status (default: 'instock')
563
	 * @return array
564
	 */
565
	public function stock_status_meta_query( $status = 'instock' ) {
566
		return 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ? array(
567
			'key' 		=> '_stock_status',
568
			'value' 	=> $status,
569
			'compare' 	=> '=',
570
		) : array();
571
	}
572
573
	/**
574
	 * Appends tax queries to an array.
575
	 * @param array $tax_query
576
	 * @return array
577
	 */
578
	public function get_tax_query( $tax_query = array() ) {
579
		if ( ! is_array( $tax_query ) ) {
580
			$tax_query = array();
581
		}
582
583
		// Layered nav filters on terms
584
		if ( $_chosen_attributes = $this->get_layered_nav_chosen_attributes() ) {
585
			foreach ( $_chosen_attributes as $taxonomy => $data ) {
586
				$tax_query[] = array(
587
					'taxonomy' => $taxonomy,
588
					'field'    => 'slug',
589
					'terms'    => $data['terms'],
590
					'operator' => 'and' === $data['query_type'] ? 'AND' : 'IN',
591
					'include_children' => false,
592
				);
593
			}
594
		}
595
596
		return array_filter( apply_filters( 'woocommerce_product_query_tax_query', $tax_query, $this ) );
597
	}
598
599
	/**
600
	 * Get the tax query which was used by the main query.
601
	 * @return array
602
	 */
603
	public static function get_main_tax_query() {
604
		global $wp_the_query;
605
606
		$args      = $wp_the_query->query_vars;
607
		$tax_query = isset( $args['tax_query'] ) ? $args['tax_query'] : array();
608
609 View Code Duplication
		if ( ! empty( $args['taxonomy'] ) && ! empty( $args['term'] ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
610
			$tax_query[] = array(
611
				'taxonomy' => $args['taxonomy'],
612
				'terms'    => array( $args['term'] ),
613
				'field'    => 'slug',
614
			);
615
		}
616
617
		return $tax_query;
618
	}
619
620
	/**
621
	 * Get the meta query which was used by the main query.
622
	 * @return array
623
	 */
624
	public static function get_main_meta_query() {
625
		global $wp_the_query;
626
627
		$args       = $wp_the_query->query_vars;
628
		$meta_query = isset( $args['meta_query'] ) ? $args['meta_query'] : array();
629
630
		return $meta_query;
631
	}
632
633
	/**
634
	 * Layered Nav Init.
635
	 */
636
	public static function get_layered_nav_chosen_attributes() {
637
		if ( ! is_array( self::$_chosen_attributes ) ) {
638
			self::$_chosen_attributes = array();
639
640
			if ( $attribute_taxonomies = wc_get_attribute_taxonomies() ) {
641
				foreach ( $attribute_taxonomies as $tax ) {
642
					$attribute    = wc_sanitize_taxonomy_name( $tax->attribute_name );
643
					$taxonomy     = wc_attribute_taxonomy_name( $attribute );
644
					$filter_terms = ! empty( $_GET[ 'filter_' . $attribute ] ) ? explode( ',', wc_clean( $_GET[ 'filter_' . $attribute ] ) ) : array();
645
646
					if ( empty( $filter_terms ) || ! taxonomy_exists( $taxonomy ) ) {
647
						continue;
648
					}
649
650
					$query_type = ! empty( $_GET[ 'query_type_' . $attribute ] ) && in_array( $_GET[ 'query_type_' . $attribute ], array( 'and', 'or' ) ) ? wc_clean( $_GET[ 'query_type_' . $attribute ] ) : '';
651
					self::$_chosen_attributes[ $taxonomy ]['terms']      = array_map( 'sanitize_title', $filter_terms ); // Ensures correct encoding
652
					self::$_chosen_attributes[ $taxonomy ]['query_type'] = $query_type ? $query_type : apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
653
				}
654
			}
655
		}
656
		return self::$_chosen_attributes;
657
	}
658
659
	/**
660
	 * @deprecated 2.6.0
661
	 */
662
	public function layered_nav_init() {
663
		_deprecated_function( 'layered_nav_init', '2.6', '' );
664
	}
665
666
	/**
667
	 * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
668
	 * @deprecated 2.6.0 due to performance concerns
669
	 */
670
	public function get_products_in_view() {
671
		_deprecated_function( 'get_products_in_view', '2.6', '' );
672
	}
673
674
	/**
675
	 * Layered Nav post filter.
676
	 * @deprecated 2.6.0 due to performance concerns
677
	 */
678
	public function layered_nav_query( $filtered_posts ) {
0 ignored issues
show
Unused Code introduced by
The parameter $filtered_posts 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...
679
		_deprecated_function( 'layered_nav_query', '2.6', '' );
680
	}
681
}
682
683
endif;
684
685
return new WC_Query();
686