PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.6.8
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.6.8
4.9.1 4.9.0 2.0.0 2.1.0 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.1 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 4.1.1 4.2.0 4.2.1 4.3.0 4.3.1 4.4.0 4.5.0 4.5.1 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.6.8 4.6.9 4.7.0 4.7.1 4.7.2 4.7.3 4.7.4 4.7.5 4.7.6 4.7.7 4.7.8 4.7.9 4.8.0 4.8.1 4.8.2 4.8.3 4.8.4 4.8.5 4.8.6 4.8.7 4.8.8 4.8.9 trunk 0.1.2-beta 0.1.3-beta 0.1.4-beta 1.0.0 1.1.0 1.1.1 1.1.2 1.1.3 1.2.0 1.2.1 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.5.0 1.5.1 1.6.0 1.6.1 1.7.0 1.8.0 1.8.1 1.9.0
shopengine / core / query-modifier.php
shopengine / core Last commit date
builders 1 year ago elementor-controls 1 year ago export-import 3 years ago multi-language 3 years ago onboard 3 years ago page-templates 2 years ago register 2 years ago sample-designs 2 years ago service-providers 3 years ago settings 1 year ago theme-support 3 years ago wc-customizer 2 years ago query-modifier.php 1 year ago service-provider-manager.php 3 years ago template-cpt.php 2 years ago
query-modifier.php
174 lines
1 <?php
2
3 namespace ShopEngine\Core;
4
5 use ShopEngine\Traits\Singleton;
6 use ShopEngine\Core\Register\Widget_List;
7
8 class Query_Modifier
9 {
10
11 use Singleton;
12
13 private $custom_query = [];
14
15 public function init()
16 {
17
18 add_action('pre_get_posts', [$this, 'modify_query']);
19 }
20
21 public function modify_query($query)
22 {
23 if (is_admin() || !$query->is_main_query() || $query->is_single === true) {
24 return;
25 }
26
27 if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') {
28 return;
29 }
30
31
32 // query filter begins
33
34 // update query for product per page filter
35 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
36 if (!empty($_GET['shopengine_products_per_page'])) {
37 //phpcs:ignore WordPress.Security.NonceVerification.Recommended
38 $query->set('posts_per_page', absint(intval($_GET['shopengine_products_per_page'])));
39 }
40
41 // checking product filter widget active or not
42 $active_widgets = Widget_List::instance()->get_list(true, 'active');
43 if (!isset($active_widgets['product-filters'])) {
44
45 return;
46 }
47
48 $color_prefix = 'shopengine_filter_color_';
49
50 $attribute_prefix = 'shopengine_filter_attribute_';
51
52 $image_prefix = 'shopengine_filter_image_';
53
54 $label_prefix = 'shopengine_filter_label_';
55
56 $shipping_prefix = 'shopengine_filter_shipping_';
57
58 $category_prefix = 'shopengine_filter_category';
59
60 $stock_prefix = 'shopengine_filter_stock';
61
62 $sale_prefix = 'shopengine_filter_onsale';
63
64 $meta_query = ['relation' => 'AND'];
65
66 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
67 foreach ($_GET as $key => $value) {
68
69 if ($key === 'rating_filter') {
70 $meta_query[] = [
71 'key' => '_wc_average_rating',
72 'value' => explode(',', trim($value)),
73 'type' => 'numeric',
74 'compare' => 'IN'
75 ];
76 }
77
78 if ($key === $category_prefix) {
79
80 $query->query['product_cat'] = '';
81 $query->query_vars['product_cat'] = '';
82 $query = $this->query($key . 'product_cat', $category_prefix, $value, $query);
83
84 } elseif (strpos($key, $color_prefix) !== false) {
85
86 $query = $this->query($key, $color_prefix, $value, $query);
87
88 } elseif (strpos($key, $attribute_prefix) !== false) {
89
90 $query = $this->query($key, $attribute_prefix, $value, $query);
91
92 } elseif (strpos($key, $image_prefix) !== false) {
93
94 $query = $this->query($key, $image_prefix, $value, $query);
95
96 } elseif (strpos($key, $label_prefix) !== false) {
97
98 $query = $this->query($key, $label_prefix, $value, $query);
99
100 } elseif (strpos($key, $shipping_prefix) !== false) {
101
102 $query = $this->query($key, $shipping_prefix, $value, $query);
103
104 } elseif ($key === $stock_prefix) {
105
106 $meta_query[] = [
107 'key' => '_stock_status',
108 'value' => $value,
109 'compare' => 'IN'
110 ];
111
112
113 } elseif ($key === $sale_prefix) {
114
115 $s = explode(',', $value);
116
117 foreach ($s as $v) {
118
119 if ($v === 'on_sale') {
120
121 $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products
122 $query->set( 'post__in', (array) $product_ids_on_sale );
123
124 } else {
125 $meta_query[] = [
126 'key' => '_sale_price',
127 'compare' => 'NOT EXISTS',
128 'operator' => 'OR',
129 ];
130 }
131 }
132 }
133 }
134
135 if (!empty($meta_query)) {
136 $query->set('meta_query', $meta_query);
137 }
138
139 $product_visibility_terms = wc_get_product_visibility_term_ids();
140 $product_visibility_not_in = array( $product_visibility_terms['exclude-from-catalog'] );
141
142 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
143 $product_visibility_not_in[] = $product_visibility_terms['outofstock'];
144 }
145 $this->custom_query['tax_query'][] = apply_filters('shopengine-product-visibility-modifier',[
146 'taxonomy' => 'product_visibility',
147 'terms' => $product_visibility_not_in,
148 'field' => 'term_taxonomy_id',
149 'operator' => 'NOT IN',
150 ]);
151
152 $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query));
153 }
154
155 public function query($key, $prefix, $values, $query)
156 {
157
158 $taxonomy = str_replace($prefix, '', $key);
159
160 $values = explode(',', trim($values));
161
162 $this->custom_query['relation'] = 'AND';
163
164 $this->custom_query[] = [
165 'taxonomy' => $taxonomy,
166 'field' => 'slug',
167 'terms' => $values,
168 'operator' => 'IN',
169 ];
170
171 return $query;
172 }
173 }
174