1 <?php
2 3 4 5 6 7 8 9 10
11
12 if ( ! defined( 'ABSPATH' ) ) exit;
13
14 if ( ! class_exists( 'WC_Query' ) ) :
15
16 17 18
19 class WC_Query {
20
21
22 public $query_vars = array();
23
24
25 public $unfiltered_product_ids = array();
26
27
28 public $filtered_product_ids = array();
29
30
31 public $filtered_product_ids_for_taxonomy = array();
32
33
34 public $post__in = array();
35
36
37 public $meta_query = '';
38
39
40 public $layered_nav_post__in = array();
41
42
43 public $layered_nav_product_ids = array();
44
45 46 47 48 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 70
71 public function init_query_vars() {
72
73 $this->query_vars = array(
74
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
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 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 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 106 107 108 109 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 120 121
122 public function get_query_vars() {
123 return $this->query_vars;
124 }
125
126 127 128
129 public function parse_request() {
130 global $wp;
131
132
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 146 147 148 149 150
151 public function pre_get_posts( $q ) {
152
153 if ( ! $q->is_main_query() )
154 return;
155
156
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
168 if ( $q->is_page() && 'page' == get_option( 'show_on_front' ) && $q->get('page_id') == wc_get_page_id('shop') ) {
169
170
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
177 define( 'SHOP_IS_ON_FRONT', true );
178
179
180
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
193 $q->is_singular = false;
194 $q->is_post_type_archive = true;
195 $q->is_archive = true;
196
197
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
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
221 add_action( 'wp', array( $this, 'get_products_in_view' ), 2);
222
223
224 $this->remove_product_query();
225 }
226
227 228 229 230 231 232 233
234 public function search_post_excerpt( $where = '' ) {
235 global $wp_the_query;
236
237
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 250 251 252 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 262 263 264 265 266
267 public function wpseo_metadesc() {
268 return WPSEO_Meta::get_value( 'metadesc', wc_get_page_id('shop') );
269
270 }
271
272
273 274 275 276 277 278 279
280 public function wpseo_metakey() {
281 return WPSEO_Meta::get_value( 'metakey', wc_get_page_id('shop') );
282 }
283
284
285 286 287 288 289 290 291 292
293 public function the_posts( $posts, $query = false ) {
294
295 if ( ! $query )
296 return $posts;
297
298
299 if ( empty( $this->post__in ) )
300 return $posts;
301
302
303 if ( ! empty( $query->wc_query ) )
304 return $posts;
305
306
307 if ( empty( $query->query_vars["s"] ) )
308 return $posts;
309
310
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
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 343 344 345 346 347
348 public function product_query( $q ) {
349
350
351 $meta_query = $this->get_meta_query( $q->get( 'meta_query' ) );
352
353
354 $ordering = $this->get_catalog_ordering_args();
355
356
357 $post__in = array_unique( apply_filters( 'loop_shop_post_in', array() ) );
358
359
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
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
371 $q->set( 'wc_query', true );
372
373
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 383 384 385 386
387 public function remove_product_query() {
388 remove_filter( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
389 }
390
391 392 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 401 402 403 404
405 public function remove_posts_where() {
406 remove_filter( 'posts_where', array( $this, 'search_post_excerpt' ) );
407 }
408
409
410 411 412 413 414 415
416 public function get_products_in_view() {
417 global $wp_the_query;
418
419 $unfiltered_product_ids = array();
420
421
422 $current_wp_query = $wp_the_query->query;
423
424
425 unset( $current_wp_query['paged'] );
426
427
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
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
454 $this->unfiltered_product_ids = $unfiltered_product_ids;
455
456
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
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 472 473 474 475
476 public function get_catalog_ordering_args( $orderby = '', $order = '' ) {
477
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
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
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
514 add_filter( 'posts_clauses', array( $this, 'order_by_popularity_post_clauses' ) );
515 break;
516 case 'rating' :
517
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 531 532 533 534 535 536 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 548 549 550 551 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 574 575 576 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 590 591 592 593 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 612 613 614 615 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 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 668 669 670 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
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 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 780 781 782 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
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