PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.8.4
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.8.4
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 2 years ago export-import 3 years ago multi-language 3 years ago onboard 11 months ago page-templates 9 months ago register 11 months ago sample-designs 9 months ago service-providers 3 years ago settings 9 months ago theme-support 3 years ago wc-customizer 2 years ago query-modifier.php 9 months ago service-provider-manager.php 3 years ago template-cpt.php 2 years ago
query-modifier.php
185 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
24
25 if (is_admin() || !$query->is_main_query() || $query->is_single === true) {
26 return;
27 }
28
29 if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') {
30 return;
31 }
32
33 // query filter begins
34
35 // update query for product per page filter
36 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
37 if (!empty($_GET['shopengine_products_per_page'])) {
38 //phpcs:ignore WordPress.Security.NonceVerification.Recommended
39 $query->set('posts_per_page', absint(intval($_GET['shopengine_products_per_page'])));
40 }
41
42 // checking product filter widget active or not
43 $active_widgets = Widget_List::instance()->get_list(true, 'active');
44 if (!isset($active_widgets['product-filters'])) {
45
46 return;
47 }
48
49 $color_prefix = 'shopengine_filter_color_';
50
51 $attribute_prefix = 'shopengine_filter_attribute_';
52
53 $image_prefix = 'shopengine_filter_image_';
54
55 $label_prefix = 'shopengine_filter_label_';
56
57 $shipping_prefix = 'shopengine_filter_shipping_';
58
59 $category_prefix = 'shopengine_filter_category';
60
61 $stock_prefix = 'shopengine_filter_stock';
62
63 $sale_prefix = 'shopengine_filter_onsale';
64
65 $brand_prefix = 'shopengine_filter_brand';
66
67 $meta_query = ['relation' => 'AND'];
68
69 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
70 foreach ($_GET as $key => $value) {
71
72 if ($key === 'rating_filter') {
73 $meta_query[] = [
74 'key' => '_wc_average_rating',
75 'value' => explode(',', trim($value)),
76 'type' => 'numeric',
77 'compare' => 'IN'
78 ];
79 }
80
81 if ($key === $category_prefix) {
82
83 $query->query['product_cat'] = '';
84 $query->query_vars['product_cat'] = '';
85 $query = $this->query($key . 'product_cat', $category_prefix, $value, $query);
86
87 } elseif (strpos($key, $color_prefix) !== false) {
88
89 $query = $this->query($key, $color_prefix, $value, $query);
90
91 } elseif (strpos($key, $attribute_prefix) !== false) {
92
93 $query = $this->query($key, $attribute_prefix, $value, $query);
94
95 } elseif (strpos($key, $image_prefix) !== false) {
96
97 $query = $this->query($key, $image_prefix, $value, $query);
98
99 } elseif (strpos($key, $label_prefix) !== false) {
100
101 $query = $this->query($key, $label_prefix, $value, $query);
102
103 } elseif (strpos($key, $shipping_prefix) !== false) {
104
105 $query = $this->query($key, $shipping_prefix, $value, $query);
106
107 } elseif ($key === $brand_prefix) {
108
109 $query = $this->query($key, $brand_prefix, $value, $query);
110
111 } elseif ($key === $stock_prefix) {
112
113 $meta_query[] = [
114 'key' => '_stock_status',
115 'value' => $value,
116 'compare' => 'IN'
117 ];
118
119
120 } elseif ($key === $sale_prefix) {
121
122 $s = explode(',', $value);
123
124 foreach ($s as $v) {
125
126 if ($v === 'on_sale') {
127
128 $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products
129 $query->set( 'post__in', (array) $product_ids_on_sale );
130
131 } else {
132 $meta_query[] = [
133 'key' => '_sale_price',
134 'compare' => 'NOT EXISTS',
135 'operator' => 'OR',
136 ];
137 }
138 }
139 }
140 }
141
142 if (!empty($meta_query)) {
143 $query->set('meta_query', $meta_query);
144 }
145
146 $product_visibility_terms = wc_get_product_visibility_term_ids();
147 $product_visibility_not_in = array( $product_visibility_terms['exclude-from-catalog'] );
148
149 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
150 $product_visibility_not_in[] = $product_visibility_terms['outofstock'];
151 }
152 $this->custom_query['tax_query'][] = apply_filters('shopengine-product-visibility-modifier',[
153 'taxonomy' => 'product_visibility',
154 'terms' => $product_visibility_not_in,
155 'field' => 'term_taxonomy_id',
156 'operator' => 'NOT IN',
157 ]);
158
159 $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query));
160 }
161
162 public function query($key, $prefix, $values, $query)
163 {
164 // Handle brand filter specifically
165 if ($prefix === 'shopengine_filter_brand') {
166 $taxonomy = 'product_brand';
167 } else {
168 $taxonomy = str_replace($prefix, '', $key);
169 }
170
171 $values = explode(',', trim($values));
172
173 $this->custom_query['relation'] = 'AND';
174
175 $this->custom_query[] = [
176 'taxonomy' => $taxonomy,
177 'field' => 'slug',
178 'terms' => $values,
179 'operator' => 'IN',
180 ];
181
182 return $query;
183 }
184 }
185