Post.php
4.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<?php
namespace ACP\Search\Query;
use ACP\Search\Query;
use ACP\Search\Query\Bindings\QueryArguments;
use WP_Query;
class Post extends Query
{
/**
* Register post callback functions
*/
public function register(): void
{
add_filter('posts_where', [$this, 'cast_decimal_precision'], 20, 2);
add_filter('posts_where', [$this, 'callback_where'], 20, 2);
add_filter('posts_join', [$this, 'callback_join'], 20, 2);
add_filter('posts_groupby', [$this, 'callback_group_by'], 20, 2);
add_filter('posts_orderby', [$this, 'callback_order_by'], 20, 2);
add_action('pre_get_posts', [$this, 'callback_meta_query'], 20);
add_action('pre_get_posts', [$this, 'callback_tax_query'], 20);
add_action('pre_get_posts', [$this, 'callback_mime_type_query'], 20);
add_action('pre_get_posts', [$this, 'callback_query_arguments'], 20);
}
/**
* Add precision parameters to DECIMAL query
*
* @param string $where
* @param WP_Query $query
*
* @return string
*/
function cast_decimal_precision($where, WP_Query $query)
{
if ( ! $query->is_main_query()) {
return $where;
}
return str_replace('DECIMAL)', 'DECIMAL(10,2))', $where);
}
/**
* Function to serve the template functions that need this hook
*
* @param string $where
* @param WP_Query $query
*
* @return string
*/
public function callback_where($where, WP_Query $query)
{
if ( ! $query->is_main_query()) {
return $where;
}
foreach ($this->bindings as $binding) {
if ($binding->get_where()) {
$where .= "\nAND " . $binding->get_where();
}
}
return $where;
}
public function callback_join($join, WP_Query $query)
{
if ( ! $query->is_main_query()) {
return $join;
}
foreach ($this->bindings as $binding) {
if ($binding->get_join()) {
$join .= "\n" . $binding->get_join();
}
}
return $join;
}
public function callback_group_by($group_by, WP_Query $query)
{
if ( ! $query->is_main_query()) {
return $group_by;
}
foreach ($this->bindings as $binding) {
if ($binding->get_group_by()) {
$group_by = $binding->get_group_by();
break;
}
}
return $group_by;
}
public function callback_order_by($order_by, WP_Query $query)
{
if ( ! $query->is_main_query()) {
return $order_by;
}
foreach ($this->bindings as $binding) {
if ($binding->get_order_by()) {
$order_by = $binding->get_order_by();
break;
}
}
return $order_by;
}
public function callback_meta_query(WP_Query $query): void
{
if ( ! $query->is_main_query()) {
return;
}
$meta_query = $this->get_meta_query();
if ( ! $meta_query) {
return;
}
if ($query->get('meta_query')) {
$meta_query[] = $query->get('meta_query');
}
$query->set('meta_query', $meta_query);
}
public function callback_tax_query(WP_Query $query): void
{
if ( ! $query->is_main_query()) {
return;
}
$tax_query = [];
foreach ($this->bindings as $binding) {
if ($binding instanceof Query\Bindings\Post && $binding->get_tax_query()) {
$tax_query[] = $binding->get_tax_query();
}
}
$tax_query = array_filter($tax_query);
if ( ! $tax_query) {
return;
}
$tax_query['relation'] = 'AND';
if ($query->get('tax_query')) {
$tax_query[] = $query->get('tax_query');
}
$query->set('tax_query', $tax_query);
}
public function callback_mime_type_query(WP_Query $query): void
{
if ( ! $query->is_main_query()) {
return;
}
$mime_types = [];
foreach ($this->bindings as $binding) {
if ($binding instanceof Query\Bindings\Media && $binding->get_mime_types()) {
$mime_types = $binding->get_mime_types();
}
}
$mime_types = array_filter($mime_types);
if ( ! $mime_types) {
return;
}
$query->set('post_mime_type', $mime_types);
}
public function callback_query_arguments(WP_Query $query): void
{
foreach ($this->bindings as $binding) {
if ($binding instanceof QueryArguments) {
foreach ($binding->get_query_arguments() as $query_var => $value) {
$query->set($query_var, $value);
}
}
}
}
}