PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.7.6
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.7.6
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 3 years ago page-templates 1 year ago register 2 years ago sample-designs 2 years ago service-providers 3 years ago settings 2 years 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
175 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 $meta_query = ['relation' => 'AND'];
66
67 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
68 foreach ($_GET as $key => $value) {
69
70 if ($key === 'rating_filter') {
71 $meta_query[] = [
72 'key' => '_wc_average_rating',
73 'value' => explode(',', trim($value)),
74 'type' => 'numeric',
75 'compare' => 'IN'
76 ];
77 }
78
79 if ($key === $category_prefix) {
80
81 $query->query['product_cat'] = '';
82 $query->query_vars['product_cat'] = '';
83 $query = $this->query($key . 'product_cat', $category_prefix, $value, $query);
84
85 } elseif (strpos($key, $color_prefix) !== false) {
86
87 $query = $this->query($key, $color_prefix, $value, $query);
88
89 } elseif (strpos($key, $attribute_prefix) !== false) {
90
91 $query = $this->query($key, $attribute_prefix, $value, $query);
92
93 } elseif (strpos($key, $image_prefix) !== false) {
94
95 $query = $this->query($key, $image_prefix, $value, $query);
96
97 } elseif (strpos($key, $label_prefix) !== false) {
98
99 $query = $this->query($key, $label_prefix, $value, $query);
100
101 } elseif (strpos($key, $shipping_prefix) !== false) {
102
103 $query = $this->query($key, $shipping_prefix, $value, $query);
104
105 } elseif ($key === $stock_prefix) {
106
107 $meta_query[] = [
108 'key' => '_stock_status',
109 'value' => $value,
110 'compare' => 'IN'
111 ];
112
113
114 } elseif ($key === $sale_prefix) {
115
116 $s = explode(',', $value);
117
118 foreach ($s as $v) {
119
120 if ($v === 'on_sale') {
121
122 $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products
123 $query->set( 'post__in', (array) $product_ids_on_sale );
124
125 } else {
126 $meta_query[] = [
127 'key' => '_sale_price',
128 'compare' => 'NOT EXISTS',
129 'operator' => 'OR',
130 ];
131 }
132 }
133 }
134 }
135
136 if (!empty($meta_query)) {
137 $query->set('meta_query', $meta_query);
138 }
139
140 $product_visibility_terms = wc_get_product_visibility_term_ids();
141 $product_visibility_not_in = array( $product_visibility_terms['exclude-from-catalog'] );
142
143 if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
144 $product_visibility_not_in[] = $product_visibility_terms['outofstock'];
145 }
146 $this->custom_query['tax_query'][] = apply_filters('shopengine-product-visibility-modifier',[
147 'taxonomy' => 'product_visibility',
148 'terms' => $product_visibility_not_in,
149 'field' => 'term_taxonomy_id',
150 'operator' => 'NOT IN',
151 ]);
152
153 $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query));
154 }
155
156 public function query($key, $prefix, $values, $query)
157 {
158
159 $taxonomy = str_replace($prefix, '', $key);
160
161 $values = explode(',', trim($values));
162
163 $this->custom_query['relation'] = 'AND';
164
165 $this->custom_query[] = [
166 'taxonomy' => $taxonomy,
167 'field' => 'slug',
168 'terms' => $values,
169 'operator' => 'IN',
170 ];
171
172 return $query;
173 }
174 }
175