1 <?php
  2 /**
  3  * Contains the query functions for WooCommerce which alter the front-end post queries and loops.
  4  *
  5  * @class       WC_Query
  6  * @version     1.6.4
  7  * @package     WooCommerce/Classes
  8  * @category    Class
  9  * @author      WooThemes
 10  */
 11 
 12 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
 13 
 14 if ( ! class_exists( 'WC_Query' ) ) :
 15 
 16 /**
 17  * WC_Query Class
 18  */
 19 class WC_Query {
 20 
 21     /** @public array Query vars to add to wp */
 22     public $query_vars = array();
 23 
 24     /** @public array Unfiltered product ids (before layered nav etc) */
 25     public $unfiltered_product_ids  = array();
 26 
 27     /** @public array Filtered product ids (after layered nav) */
 28     public $filtered_product_ids    = array();
 29 
 30     /** @public array Filtered product ids (after layered nav, per taxonomy) */
 31     public $filtered_product_ids_for_taxonomy   = array();
 32 
 33     /** @public array Product IDs that match the layered nav + price filter */
 34     public $post__in        = array();
 35 
 36     /** @public array The meta query for the page */
 37     public $meta_query      = '';
 38 
 39     /** @public array Post IDs matching layered nav only */
 40     public $layered_nav_post__in    = array();
 41 
 42     /** @public array Stores post IDs matching layered nav, so price filter can find max price in view */
 43     public $layered_nav_product_ids = array();
 44 
 45     /**
 46      * Constructor for the query class. Hooks in methods.
 47      *
 48      * @access public
 49      */
 50     public function __construct() {
 51         add_action( 'init', array( $this, 'add_endpoints' ) );
 52         add_action( 'init', array( $this, 'layered_nav_init' ) );
 53         add_action( 'init', array( $this, 'price_filter_init' ) );
 54 
 55         if ( ! is_admin() ) {
 56             add_action( 'init', array( $this, 'get_errors' ) );
 57             add_filter( 'query_vars', array( $this, 'add_query_vars'), 0 );
 58             add_action( 'parse_request', array( $this, 'parse_request'), 0 );
 59             add_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
 60             add_filter( 'the_posts', array( $this, 'the_posts' ), 11, 2 );
 61             add_action( 'wp', array( $this, 'remove_product_query' ) );
 62             add_action( 'wp', array( $this, 'remove_ordering_args' ) );
 63         }
 64 
 65         $this->init_query_vars();
 66     }
 67 
 68     /**
 69      * Init query vars by loading options.
 70      */
 71     public function init_query_vars() {
 72         // Query vars to add to WP
 73         $this->query_vars = array(
 74             // Checkout actions
 75             'order-pay'          => get_option( 'woocommerce_checkout_pay_endpoint', 'order-pay' ),
 76             'order-received'     => get_option( 'woocommerce_checkout_order_received_endpoint', 'order-received' ),
 77 
 78             // My account actions
 79             'view-order'         => get_option( 'woocommerce_myaccount_view_order_endpoint', 'view-order' ),
 80             'edit-account'       => get_option( 'woocommerce_myaccount_edit_account_endpoint', 'edit-account' ),
 81             'edit-address'       => get_option( 'woocommerce_myaccount_edit_address_endpoint', 'edit-address' ),
 82             'lost-password'      => get_option( 'woocommerce_myaccount_lost_password_endpoint', 'lost-password' ),
 83             'customer-logout'    => get_option( 'woocommerce_logout_endpoint', 'customer-logout' ),
 84             'add-payment-method' => get_option( 'woocommerce_myaccount_add_payment_method_endpoint', 'add-payment-method' ),
 85         );
 86     }
 87 
 88     /**
 89      * Get any errors from querystring
 90      */
 91     public function get_errors() {
 92         if ( ! empty( $_GET['wc_error'] ) && ( $error = sanitize_text_field( $_GET['wc_error'] ) ) && ! wc_has_notice( $error, 'error' ) )
 93             wc_add_notice( $error, 'error' );
 94     }
 95 
 96     /**
 97      * Add endpoints for query vars
 98      */
 99     public function add_endpoints() {
100         foreach ( $this->query_vars as $key => $var )
101             add_rewrite_endpoint( $var, EP_PAGES );
102     }
103 
104     /**
105      * add_query_vars function.
106      *
107      * @access public
108      * @param array $vars
109      * @return array
110      */
111     public function add_query_vars( $vars ) {
112         foreach ( $this->query_vars as $key => $var )
113             $vars[] = $key;
114 
115         return $vars;
116     }
117 
118     /**
119      * Get query vars
120      * @return array()
121      */
122     public function get_query_vars() {
123         return $this->query_vars;
124     }
125 
126     /**
127      * Parse the request and look for query vars - endpoints may not be supported
128      */
129     public function parse_request() {
130         global $wp;
131 
132         // Map query vars to their keys, or get them if endpoints are not supported
133         foreach ( $this->query_vars as $key => $var ) {
134             if ( isset( $_GET[ $var ] ) ) {
135                 $wp->query_vars[ $key ] = $_GET[ $var ];
136             }
137 
138             elseif ( isset( $wp->query_vars[ $var ] ) ) {
139                 $wp->query_vars[ $key ] = $wp->query_vars[ $var ];
140             }
141         }
142     }
143 
144     /**
145      * Hook into pre_get_posts to do the main product query
146      *
147      * @access public
148      * @param mixed $q query object
149      * @return void
150      */
151     public function pre_get_posts( $q ) {
152         // We only want to affect the main query
153         if ( ! $q->is_main_query() )
154             return;
155 
156         // When orderby is set, WordPress shows posts. Get around that here.
157         if ( $q->is_home() && 'page' == get_option('show_on_front') && get_option('page_on_front') == wc_get_page_id('shop') ) {
158             $_query = wp_parse_args( $q->query );
159             if ( empty( $_query ) || ! array_diff( array_keys( $_query ), array( 'preview', 'page', 'paged', 'cpage', 'orderby' ) ) ) {
160                 $q->is_page = true;
161                 $q->is_home = false;
162                 $q->set( 'page_id', get_option('page_on_front') );
163                 $q->set( 'post_type', 'product' );
164             }
165         }
166 
167         // Special check for shops with the product archive on front
168         if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == wc_get_page_id('shop') ) {
169 
170             // This is a front-page shop
171             $q->set( 'post_type', 'product' );
172             $q->set( 'page_id', '' );
173             if ( isset( $q->query['paged'] ) )
174                 $q->set( 'paged', $q->query['paged'] );
175 
176             // Define a variable so we know this is the front page shop later on
177             define( 'SHOP_IS_ON_FRONT', true );
178 
179             // Get the actual WP page to avoid errors and let us use is_front_page()
180             // This is hacky but works. Awaiting http://core.trac.wordpress.org/ticket/21096
181             global $wp_post_types;
182 
183             $shop_page  = get_post( wc_get_page_id('shop') );
184             $q->is_page = true;
185 
186             $wp_post_types['product']->ID           = $shop_page->ID;
187             $wp_post_types['product']->post_title   = $shop_page->post_title;
188             $wp_post_types['product']->post_name    = $shop_page->post_name;
189             $wp_post_types['product']->post_type    = $shop_page->post_type;
190             $wp_post_types['product']->ancestors    = get_ancestors( $shop_page->ID, $shop_page->post_type );
191 
192             // Fix conditional Functions like is_front_page
193             $q->is_singular = false;
194             $q->is_post_type_archive = true;
195             $q->is_archive = true;
196 
197             // Fix WP SEO
198             if ( class_exists( 'WPSEO_Meta' ) ) {
199                 add_filter( 'wpseo_metadesc', array( $this, 'wpseo_metadesc' ) );
200                 add_filter( 'wpseo_metakey', array( $this, 'wpseo_metakey' ) );
201             }
202 
203         } else {
204 
205             // Only apply to product categories, the product post archive, the shop page, product tags, and product attribute taxonomies
206             if  ( ! $q->is_post_type_archive( 'product' ) && ! $q->is_tax( get_object_taxonomies( 'product' ) ) )
207                 return;
208 
209         }
210 
211         $this->product_query( $q );
212 
213         if ( is_search() ) {
214             add_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
215             add_filter( 'wp', array( $this, 'remove_posts_where' ) );
216         }
217 
218         add_filter( 'posts_where', array( $this, 'exclude_protected_products' ) );
219 
220         // We're on a shop page so queue the woocommerce_get_products_in_view function
221         add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
222 
223         // And remove the pre_get_posts hook
224         $this->remove_product_query();
225     }
226 
227     /**
228      * search_post_excerpt function.
229      *
230      * @access public
231      * @param string $where (default: '')
232      * @return string (modified where clause)
233      */
234     public function search_post_excerpt( $where = '' ) {
235         global $wp_the_query;
236 
237         // If this is not a WC Query, do not modify the query
238         if ( empty( $wp_the_query->query_vars['wc_query'] ) || empty( $wp_the_query->query_vars['s'] ) )
239             return $where;
240 
241         $where = preg_replace(
242             "/post_title\s+LIKE\s*(\'\%[^\%]+\%\')/",
243             "post_title LIKE $1) OR (post_excerpt LIKE $1", $where );
244 
245         return $where;
246     }
247 
248     /**
249      * Prevent password protected products appearing in the loops
250      *
251      * @param  string $where
252      * @return string
253      */
254     public function exclude_protected_products( $where ) {
255         global $wpdb;
256         $where .= " AND {$wpdb->posts}.post_password = ''";
257         return $where;
258     }
259 
260     /**
261      * wpseo_metadesc function.
262      * Hooked into wpseo_ hook already, so no need for function_exist
263      *
264      * @access public
265      * @return string
266      */
267     public function wpseo_metadesc() {
268         return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id('shop') );
269 
270     }
271 
272 
273     /**
274      * wpseo_metakey function.
275      * Hooked into wpseo_ hook already, so no need for function_exist
276      *
277      * @access public
278      * @return string
279      */
280     public function wpseo_metakey() {
281         return WPSEO_Meta::get_value( 'metakey', wc_get_page_id('shop') );
282     }
283 
284 
285     /**
286      * Hook into the_posts to do the main product query if needed - relevanssi compatibility
287      *
288      * @access public
289      * @param array $posts
290      * @param WP_Query|bool $query (default: false)
291      * @return array
292      */
293     public function the_posts( $posts, $query = false ) {
294         // Abort if there's no query
295         if ( ! $query )
296             return $posts;
297 
298         // Abort if we're not filtering posts
299         if ( empty( $this->post__in ) )
300             return $posts;
301 
302         // Abort if this query has already been done
303         if ( ! empty( $query->wc_query ) )
304             return $posts;
305 
306         // Abort if this isn't a search query
307         if ( empty( $query->query_vars["s"] ) )
308             return $posts;
309 
310         // Abort if we're not on a post type archive/product taxonomy
311         if  ( ! $query->is_post_type_archive( 'product' ) && ! $query->is_tax( get_object_taxonomies( 'product' ) ) )
312             return $posts;
313 
314         $filtered_posts = array();
315         $queried_post_ids = array();
316 
317         foreach ( $posts as $post ) {
318             if ( in_array( $post->ID, $this->post__in ) ) {
319                 $filtered_posts[] = $post;
320                 $queried_post_ids[] = $post->ID;
321             }
322         }
323 
324         $query->posts = $filtered_posts;
325             $query->post_count = count( $filtered_posts );
326 
327             // Ensure filters are set
328             $this->unfiltered_product_ids = $queried_post_ids;
329             $this->filtered_product_ids = $queried_post_ids;
330 
331             if ( sizeof( $this->layered_nav_post__in ) > 0 ) {
332                 $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
333             } else {
334                 $this->layered_nav_product_ids = $this->unfiltered_product_ids;
335             }
336 
337         return $filtered_posts;
338     }
339 
340 
341     /**
342      * Query the products, applying sorting/ordering etc. This applies to the main wordpress loop
343      *
344      * @access public
345      * @param mixed $q
346      * @return void
347      */
348     public function product_query( $q ) {
349 
350         // Meta query
351         $meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
352 
353         // Ordering
354         $ordering   = $this->get_catalog_ordering_args();
355 
356         // Get a list of post id's which match the current filters set (in the layered nav and price filter)
357         $post__in   = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
358 
359         // Ordering query vars
360         $q->set( 'orderby', $ordering['orderby'] );
361         $q->set( 'order', $ordering['order'] );
362         if ( isset( $ordering['meta_key'] ) )
363             $q->set( 'meta_key', $ordering['meta_key'] );
364 
365         // Query vars that affect posts shown
366         $q->set( 'meta_query', $meta_query );
367         $q->set( 'post__in', $post__in );
368         $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' ) ) );
369 
370         // Set a special variable
371         $q->set( 'wc_query', true );
372 
373         // Store variables
374         $this->post__in   = $post__in;
375         $this->meta_query = $meta_query;
376 
377         do_action( 'woocommerce_product_query', $q, $this );
378     }
379 
380 
381     /**
382      * Remove the query
383      *
384      * @access public
385      * @return void
386      */
387     public function remove_product_query() {
388         remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
389     }
390 
391     /**
392      * Remove ordering queries
393      */
394     public function remove_ordering_args() {
395         remove_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
396         remove_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
397     }
398 
399     /**
400      * Remove the posts_where filter
401      *
402      * @access public
403      * @return void
404      */
405     public function remove_posts_where() {
406         remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
407     }
408 
409 
410     /**
411      * Get an unpaginated list all product ID's (both filtered and unfiltered). Makes use of transients.
412      *
413      * @access public
414      * @return void
415      */
416     public function get_products_in_view() {
417         global $wp_the_query;
418 
419         $unfiltered_product_ids = array();
420 
421         // Get main query
422         $current_wp_query = $wp_the_query->query;
423 
424         // Get WP Query for current page (without 'paged')
425         unset( $current_wp_query['paged'] );
426 
427         // Generate a transient name based on current query
428         $transient_name = 'wc_uf_pid_' . md5( http_build_query( $current_wp_query ) );
429         $transient_name = ( is_search() ) ? $transient_name . '_s' : $transient_name;
430 
431         if ( false === ( $unfiltered_product_ids = get_transient( $transient_name ) ) ) {
432 
433             // Get all visible posts, regardless of filters
434             $unfiltered_product_ids = get_posts(
435                 array_merge(
436                     $current_wp_query,
437                     array(
438                         'post_type'     => 'product',
439                         'numberposts'   => -1,
440                         'post_status'   => 'publish',
441                         'meta_query'    => $this->meta_query,
442                         'fields'        => 'ids',
443                         'no_found_rows' => true,
444                         'update_post_meta_cache' => false,
445                         'update_post_term_cache' => false
446                     )
447                 )
448             );
449 
450             set_transient( $transient_name, $unfiltered_product_ids, YEAR_IN_SECONDS );
451         }
452 
453         // Store the variable
454         $this->unfiltered_product_ids = $unfiltered_product_ids;
455 
456         // Also store filtered posts ids...
457         if ( sizeof( $this->post__in ) > 0 )
458             $this->filtered_product_ids = array_intersect( $this->unfiltered_product_ids, $this->post__in );
459         else
460             $this->filtered_product_ids = $this->unfiltered_product_ids;
461 
462         // And filtered post ids which just take layered nav into consideration (to find max price in the price widget)
463         if ( sizeof( $this->layered_nav_post__in ) > 0 )
464             $this->layered_nav_product_ids = array_intersect( $this->unfiltered_product_ids, $this->layered_nav_post__in );
465         else
466             $this->layered_nav_product_ids = $this->unfiltered_product_ids;
467     }
468 
469 
470     /**
471      * Returns an array of arguments for ordering products based on the selected values
472      *
473      * @access public
474      * @return array
475      */
476     public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
477         // Get ordering from query string unless defined
478         if ( ! $orderby ) {
479             $orderby_value = isset( $_GET['orderby'] ) ? wc_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );
480 
481             // Get order + orderby args from string
482             $orderby_value = explode( '-', $orderby_value );
483             $orderby       = esc_attr( $orderby_value[0] );
484             $order         = ! empty( $orderby_value[1] ) ? $orderby_value[1] : $order;
485         }
486 
487         $orderby = strtolower( $orderby );
488         $order   = strtoupper( $order );
489 
490         $args = array();
491 
492         // default - menu_order
493         $args['orderby']  = 'menu_order title';
494         $args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
495         $args['meta_key'] = '';
496 
497         switch ( $orderby ) {
498             case 'rand' :
499                 $args['orderby']  = 'rand';
500             break;
501             case 'date' :
502                 $args['orderby']  = 'date';
503                 $args['order']    = $order == 'ASC' ? 'ASC' : 'DESC';
504             break;
505             case 'price' :
506                 $args['orderby']  = 'meta_value_num';
507                 $args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
508                 $args['meta_key'] = '_price';
509             break;
510             case 'popularity' :
511                 $args['meta_key'] = 'total_sales';
512 
513                 // Sorting handled later though a hook
514                 add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
515             break;
516             case 'rating' :
517                 // Sorting handled later though a hook
518                 add_filter( 'posts_clauses', array( $this, 'order_by_rating_post_clauses' ) );
519             break;
520             case 'title' :
521                 $args['orderby']  = 'title';
522                 $args['order']    = $order == 'DESC' ? 'DESC' : 'ASC';
523             break;
524         }
525 
526         return apply_filters( 'woocommerce_get_catalog_ordering_args', $args );
527     }
528 
529     /**
530      * WP Core doens't let us change the sort direction for invidual orderby params - http://core.trac.wordpress.org/ticket/17065
531      *
532      * This lets us sort by meta value desc, and have a second orderby param.
533      *
534      * @access public
535      * @param array $args
536      * @return array
537      */
538     public function order_by_popularity_post_clauses( $args ) {
539         global $wpdb;
540 
541         $args['orderby'] = "$wpdb->postmeta.meta_value+0 DESC, $wpdb->posts.post_date DESC";
542 
543         return $args;
544     }
545 
546     /**
547      * order_by_rating_post_clauses function.
548      *
549      * @access public
550      * @param array $args
551      * @return array
552      */
553     public function order_by_rating_post_clauses( $args ) {
554         global $wpdb;
555 
556         $args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
557 
558         $args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
559 
560         $args['join'] .= "
561             LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
562             LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
563         ";
564 
565         $args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC";
566 
567         $args['groupby'] = "$wpdb->posts.ID";
568 
569         return $args;
570     }
571 
572     /**
573      * Appends meta queries to an array.
574      * @access public
575      * @param array $meta_query
576      * @return array
577      */
578     public function get_meta_query( $meta_query = array() ) {
579         if ( ! is_array( $meta_query ) )
580             $meta_query = array();
581 
582         $meta_query[] = $this->visibility_meta_query();
583         $meta_query[] = $this->stock_status_meta_query();
584 
585         return array_filter( $meta_query );
586     }
587 
588     /**
589      * Returns a meta query to handle product visibility
590      *
591      * @access public
592      * @param string $compare (default: 'IN')
593      * @return array
594      */
595     public function visibility_meta_query( $compare = 'IN' ) {
596         if ( is_search() )
597             $in = array( 'visible', 'search' );
598         else
599             $in = array( 'visible', 'catalog' );
600 
601         $meta_query = array(
602             'key'     => '_visibility',
603             'value'   => $in,
604             'compare' => $compare
605         );
606 
607         return $meta_query;
608     }
609 
610     /**
611      * Returns a meta query to handle product stock status
612      *
613      * @access public
614      * @param string $status (default: 'instock')
615      * @return array
616      */
617     public function stock_status_meta_query( $status = 'instock' ) {
618         $meta_query = array();
619         if ( get_option( 'woocommerce_hide_out_of_stock_items' ) == 'yes' ) {
620              $meta_query = array(
621                 'key'       => '_stock_status',
622                 'value'     => $status,
623                 'compare'   => '='
624             );
625         }
626         return $meta_query;
627     }
628 
629     /**
630      * Layered Nav Init
631      */
632     public function layered_nav_init( ) {
633 
634         if ( is_active_widget( false, false, 'woocommerce_layered_nav', true ) && ! is_admin() ) {
635 
636             global $_chosen_attributes;
637 
638             $_chosen_attributes = array();
639 
640             $attribute_taxonomies = wc_get_attribute_taxonomies();
641             if ( $attribute_taxonomies ) {
642                 foreach ( $attribute_taxonomies as $tax ) {
643 
644                     $attribute       = wc_sanitize_taxonomy_name( $tax->attribute_name );
645                     $taxonomy        = wc_attribute_taxonomy_name( $attribute );
646                     $name            = 'filter_' . $attribute;
647                     $query_type_name = 'query_type_' . $attribute;
648 
649                     if ( ! empty( $_GET[ $name ] ) && taxonomy_exists( $taxonomy ) ) {
650 
651                         $_chosen_attributes[ $taxonomy ]['terms'] = explode( ',', $_GET[ $name ] );
652 
653                         if ( empty( $_GET[ $query_type_name ] ) || ! in_array( strtolower( $_GET[ $query_type_name ] ), array( 'and', 'or' ) ) )
654                             $_chosen_attributes[ $taxonomy ]['query_type'] = apply_filters( 'woocommerce_layered_nav_default_query_type', 'and' );
655                         else
656                             $_chosen_attributes[ $taxonomy ]['query_type'] = strtolower( $_GET[ $query_type_name ] );
657 
658                     }
659                 }
660             }
661 
662             add_filter('loop_shop_post_in', array( $this, 'layered_nav_query' ) );
663         }
664     }
665 
666     /**
667      * Layered Nav post filter
668      *
669      * @param array $filtered_posts
670      * @return array
671      */
672     public function layered_nav_query( $filtered_posts ) {
673         global $_chosen_attributes, $wp_query;
674 
675         if ( sizeof( $_chosen_attributes ) > 0 ) {
676 
677             $matched_products   = array(
678                 'and' => array(),
679                 'or'  => array()
680             );
681             $filtered_attribute = array(
682                 'and' => false,
683                 'or'  => false
684             );
685 
686             foreach ( $_chosen_attributes as $attribute => $data ) {
687                 $matched_products_from_attribute = array();
688                 $filtered = false;
689 
690                 if ( sizeof( $data['terms'] ) > 0 ) {
691                     foreach ( $data['terms'] as $value ) {
692 
693                         $posts = get_posts(
694                             array(
695                                 'post_type'     => 'product',
696                                 'numberposts'   => -1,
697                                 'post_status'   => 'publish',
698                                 'fields'        => 'ids',
699                                 'no_found_rows' => true,
700                                 'tax_query' => array(
701                                     array(
702                                         'taxonomy'  => $attribute,
703                                         'terms'     => $value,
704                                         'field'     => 'id'
705                                     )
706                                 )
707                             )
708                         );
709 
710                         if ( ! is_wp_error( $posts ) ) {
711 
712                             if ( sizeof( $matched_products_from_attribute ) > 0 || $filtered )
713                                 $matched_products_from_attribute = $data['query_type'] == 'or' ? array_merge( $posts, $matched_products_from_attribute ) : array_intersect( $posts, $matched_products_from_attribute );
714                             else
715                                 $matched_products_from_attribute = $posts;
716 
717                             $filtered = true;
718                         }
719                     }
720                 }
721 
722                 if ( sizeof( $matched_products[ $data['query_type'] ] ) > 0 || $filtered_attribute[ $data['query_type'] ] === true ) {
723                     $matched_products[ $data['query_type'] ] = ( $data['query_type'] == 'or' ) ? array_merge( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] ) : array_intersect( $matched_products_from_attribute, $matched_products[ $data['query_type'] ] );
724                 } else {
725                     $matched_products[ $data['query_type'] ] = $matched_products_from_attribute;
726                 }
727 
728                 $filtered_attribute[ $data['query_type'] ] = true;
729 
730                 $this->filtered_product_ids_for_taxonomy[ $attribute ] = $matched_products_from_attribute;
731             }
732 
733             // Combine our AND and OR result sets
734             if ( $filtered_attribute['and'] && $filtered_attribute['or'] )
735                 $results = array_intersect( $matched_products[ 'and' ], $matched_products[ 'or' ] );
736             else
737                 $results = array_merge( $matched_products[ 'and' ], $matched_products[ 'or' ] );
738 
739             if ( $filtered ) {
740 
741                 WC()->query->layered_nav_post__in   = $results;
742                 WC()->query->layered_nav_post__in[] = 0;
743 
744                 if ( sizeof( $filtered_posts ) == 0 ) {
745                     $filtered_posts   = $results;
746                     $filtered_posts[] = 0;
747                 } else {
748                     $filtered_posts   = array_intersect( $filtered_posts, $results );
749                     $filtered_posts[] = 0;
750                 }
751 
752             }
753         }
754         return (array) $filtered_posts;
755     }
756 
757     /**
758      * Price filter Init
759      */
760     public function price_filter_init() {
761         if ( is_active_widget( false, false, 'woocommerce_price_filter', true ) && ! is_admin() ) {
762 
763             $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
764 
765             wp_register_script( 'wc-price-slider', WC()->plugin_url() . '/assets/js/frontend/price-slider' . $suffix . '.js', array( 'jquery-ui-slider' ), WC_VERSION, true );
766 
767             wp_localize_script( 'wc-price-slider', 'woocommerce_price_slider_params', array(
768                 'currency_symbol'   => get_woocommerce_currency_symbol(),
769                 'currency_pos'      => get_option( 'woocommerce_currency_pos' ),
770                 'min_price'         => isset( $_GET['min_price'] ) ? esc_attr( $_GET['min_price'] ) : '',
771                 'max_price'         => isset( $_GET['max_price'] ) ? esc_attr( $_GET['max_price'] ) : ''
772             ) );
773 
774             add_filter( 'loop_shop_post_in', array( $this, 'price_filter' ) );
775         }
776     }
777 
778     /**
779      * Price Filter post filter
780      *
781      * @param array $filtered_posts
782      * @return array
783      */
784     public function price_filter( $filtered_posts ) {
785         global $wpdb;
786 
787         if ( isset( $_GET['max_price'] ) && isset( $_GET['min_price'] ) ) {
788 
789             $matched_products = array();
790             $min    = floatval( $_GET['min_price'] );
791             $max    = floatval( $_GET['max_price'] );
792 
793             $matched_products_query = apply_filters( 'woocommerce_price_filter_results', $wpdb->get_results( $wpdb->prepare("
794                 SELECT DISTINCT ID, post_parent, post_type FROM $wpdb->posts
795                 INNER JOIN $wpdb->postmeta ON ID = post_id
796                 WHERE post_type IN ( 'product', 'product_variation' ) AND post_status = 'publish' AND meta_key = %s AND meta_value BETWEEN %d AND %d
797             ", '_price', $min, $max ), OBJECT_K ), $min, $max );
798 
799             if ( $matched_products_query ) {
800                 foreach ( $matched_products_query as $product ) {
801                     if ( $product->post_type == 'product' )
802                         $matched_products[] = $product->ID;
803                     if ( $product->post_parent > 0 && ! in_array( $product->post_parent, $matched_products ) )
804                         $matched_products[] = $product->post_parent;
805                 }
806             }
807 
808             // Filter the id's
809             if ( sizeof( $filtered_posts ) == 0) {
810                 $filtered_posts = $matched_products;
811                 $filtered_posts[] = 0;
812             } else {
813                 $filtered_posts = array_intersect( $filtered_posts, $matched_products );
814                 $filtered_posts[] = 0;
815             }
816 
817         }
818 
819         return (array) $filtered_posts;
820     }
821 
822 }
823 
824 endif;
825 
826 return new WC_Query();
827 
WooCommerce API documentation generated by ApiGen 2.8.0