PluginProbe ʕ •ᴥ•ʔ
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution / 4.6.0
ShopEngine Elementor WooCommerce Builder Addon – All in One WooCommerce Solution v4.6.0
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 2 years ago elementor-controls 3 years 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 2 years ago theme-support 3 years ago wc-customizer 2 years ago query-modifier.php 2 years ago service-provider-manager.php 3 years ago template-cpt.php 2 years ago
query-modifier.php
160 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
113 $product_ids_on_sale = wc_get_product_ids_on_sale(); // including varriation products
114 $query->set( 'post__in', (array) $product_ids_on_sale );
115
116 } else {
117 $meta_query[] = [
118 'key' => '_sale_price',
119 'compare' => 'NOT EXISTS',
120 'operator' => 'OR',
121 ];
122 }
123 }
124 }
125 }
126
127 if (!empty($meta_query)) {
128 $query->set('meta_query', $meta_query);
129 }
130
131 $this->custom_query[] = apply_filters('shopengine-product-visibility-modifier', [
132 'taxonomy' => 'product_visibility',
133 'terms' => [ 'exclude-from-catalog' ],
134 'field' => 'name',
135 'operator' => 'NOT IN',
136 ]);
137
138 $query->set('tax_query', apply_filters('shopengine-tax-query-modifier', $this->custom_query));
139 }
140
141 public function query($key, $prefix, $values, $query)
142 {
143
144 $taxonomy = str_replace($prefix, '', $key);
145
146 $values = explode(',', trim($values));
147
148 $this->custom_query['relation'] = 'AND';
149
150 $this->custom_query[] = [
151 'taxonomy' => $taxonomy,
152 'field' => 'slug',
153 'terms' => $values,
154 'operator' => 'IN',
155 ];
156
157 return $query;
158 }
159 }
160