Post.php
3.52 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
<?php
namespace ACP\Search\Query;
use ACP\Search\Query;
use WP_Query;
class Post extends Query {
/**
* Register post callback functions
*/
public function register() {
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_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 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 .= ' AND ' . $binding->get_where();
}
}
return $where;
}
/**
* @param string $join
* @param WP_Query $query
*
* @return string
*/
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 = "\n" . $binding->get_group_by();
break;
}
}
return $group_by;
}
/**
* @param WP_Query $query
*/
public function callback_meta_query( WP_Query $query ) {
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 );
}
/**
* @param WP_Query $query
*
* @return void
*/
public function callback_tax_query( WP_Query $query ) {
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 );
}
/**
* @param WP_Query $query
*
* @return void
*/
public function callback_mime_type_query( WP_Query $query ) {
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 );
}
}