PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 3.1.1
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v3.1.1
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 3 years ago elementor-controls 3 years ago export-import 3 years ago multi-language 3 years ago onboard 3 years ago page-templates 3 years ago register 3 years ago sample-designs 3 years ago service-providers 3 years ago settings 3 years ago theme-support 3 years ago query-modifier.php 3 years ago service-provider-manager.php 3 years ago template-cpt.php 3 years ago
query-modifier.php
163 lines
1 <?php
2
3 namespace ShopEngine\Core;
4
5 use ShopEngine\Traits\Singleton;
6
7 class Query_Modifier
8 {
9
10 use Singleton;
11
12 private $custom_query = [];
13
14 public function init()
15 {
16
17 add_action('pre_get_posts', [$this, 'modify_query']);
18 }
19
20 public function modify_query($query)
21 {
22 if (is_admin() || !$query->is_main_query() || $query->is_single === true) {
23 return;
24 }
25
26 if (!isset($query->query_vars['wc_query']) || $query->query_vars['wc_query'] != 'product_query') {
27 return;
28 }
29
30 // query filter begins
31
32 // update query for product per page filter
33 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
34 if (!empty($_GET['shopengine_products_per_page'])) {
35 //phpcs:ignore WordPress.Security.NonceVerification.Recommended
36 $query->set('posts_per_page', absint(intval($_GET['shopengine_products_per_page'])));
37 }
38
39
40 $color_prefix = 'shopengine_filter_color_';
41
42 $attribute_prefix = 'shopengine_filter_attribute_';
43
44 $image_prefix = 'shopengine_filter_image_';
45
46 $label_prefix = 'shopengine_filter_label_';
47
48 $shipping_prefix = 'shopengine_filter_shipping_';
49
50 $category_prefix = 'shopengine_filter_category';
51
52 $stock_prefix = 'shopengine_filter_stock';
53
54 $sale_prefix = 'shopengine_filter_onsale';
55
56 $meta_query = ['relation' => 'AND'];
57
58 //phpcs:ignore WordPress.Security.NonceVerification.Recommended -- It's a fronted user part, not possible to verify nonce here
59 foreach ($_GET as $key => $value) {
60
61 if ($key === 'rating_filter') {
62 $meta_query[] = [
63 'key' => '_wc_average_rating',
64 'value' => explode(',', trim($value)),
65 'type' => 'numeric',
66 'compare' => 'IN'
67 ];
68 }
69
70 if ($key === $category_prefix) {
71
72 $query->query['product_cat'] = '';
73 $query->query_vars['product_cat'] = '';
74 $query = $this->query($key . 'product_cat', $category_prefix, $value, $query);
75
76 } elseif (strpos($key, $color_prefix) !== false) {
77
78 $query = $this->query($key, $color_prefix, $value, $query);
79
80 } elseif (strpos($key, $attribute_prefix) !== false) {
81
82 $query = $this->query($key, $attribute_prefix, $value, $query);
83
84 } elseif (strpos($key, $image_prefix) !== false) {
85
86 $query = $this->query($key, $image_prefix, $value, $query);
87
88 } elseif (strpos($key, $label_prefix) !== false) {
89
90 $query = $this->query($key, $label_prefix, $value, $query);
91
92 } elseif (strpos($key, $shipping_prefix) !== false) {
93
94 $query = $this->query($key, $shipping_prefix, $value, $query);
95
96 } elseif ($key === $stock_prefix) {
97
98 $meta_query[] = [
99 'key' => '_stock_status',
100 'value' => $value,
101 'compare' => 'IN'
102 ];
103
104
105 } elseif ($key === $sale_prefix) {
106
107 $s = explode(',', $value);
108
109 foreach ($s as $v) {
110
111 if ($v === 'on_sale') {
112 $meta_query[] = [
113 'key' => '_sale_price',
114 'value' => 0,
115 'compare' => '>',
116 'type' => 'numeric',
117 'operator' => 'OR',
118 ];
119 } else {
120 $meta_query[] = [
121 'key' => '_sale_price',
122 'compare' => 'NOT EXISTS',
123 'operator' => 'OR',
124 ];
125 }
126 }
127 }
128 }
129
130 if (!empty($meta_query)) {
131 $query->set('meta_query', $meta_query);
132 }
133
134 $this->custom_query[] = apply_filters('shopengine-product-visibility-modifier', [
135 'taxonomy' => 'product_visibility',
136 'terms' => [ 'exclude-from-catalog' ],
137 'field' => 'name',
138 'operator' => 'NOT IN',
139 ]);
140
141 $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query));
142 }
143
144 public function query($key, $prefix, $values, $query)
145 {
146
147 $taxonomy = str_replace($prefix, '', $key);
148
149 $values = explode(',', trim($values));
150
151 $this->custom_query['relation'] = 'AND';
152
153 $this->custom_query[] = [
154 'taxonomy' => $taxonomy,
155 'field' => 'slug',
156 'terms' => $values,
157 'operator' => 'IN',
158 ];
159
160 return $query;
161 }
162 }
163