forked from keesiemeijer/meta-date-archive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquery.php
More file actions
293 lines (233 loc) · 7.59 KB
/
Copy pathquery.php
File metadata and controls
293 lines (233 loc) · 7.59 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
<?php
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
class Meta_Date_Archive_Query {
public $start;
public $end;
public $dates;
public $filters;
public $orderby_key;
function __construct() {
add_action( 'wp_loaded', array( $this, 'setup' ) );
}
/**
* Sets up properties for a meta date query.
*
* @since 1.0
* @return void
*/
public function setup() {
if ( is_admin() ) {
return;
}
$this->start = apply_filters( 'meta_date_archive_start', 'meta_start_date' );
$this->end = apply_filters( 'meta_date_archive_end', 'meta_end_date' );
add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ), 70 );
add_filter( 'query_vars', array( $this, 'query_vars' ) );
}
/**
* Meta date query.
*
* @since 1.0
* @param object $query The WP_Query instance.
* @return void
*/
public function pre_get_posts( $query ) {
$this->dates = array();
$this->orderby_key = '';
$this->filters = "meta_date_archive";
$start_date = $query->get( 'meta_archive_start_date' );
$end_date = $query->get( 'meta_archive_end_date' );
$dates = array();
$meta_date_query = false;
if ( $query->is_main_query() ) {
// main queries
$date_archives = $query->get( 'meta_date_archives' );
$date_archives = apply_filters( 'meta_date_archives', $date_archives );
if ( is_date() && $date_archives ) {
// main query for date archive
$dates = meta_date_archive_dates();
$meta_date_query = true;
}
} else {
// external queries
if ( !empty( $start_date ) || !empty( $end_date ) ) {
// one or both of the dates is provided
$dates = meta_date_archive_start_end_date( $start_date, $end_date );
$meta_date_query = true;
} else {
// not the query we're looking for
return;
}
}
// not a meta date query
if ( !$meta_date_query ) {
return;
}
if ( isset( $query->query_vars['suppress_filters'] ) ) {
$this->filters = $query->query_vars['suppress_filters'];
}
// we need the filters for a meta date query
unset( $query->query_vars['suppress_filters'] );
if ( empty( $dates ) ) {
// fake no posts found
//
// not a date archives main query
// or an external query with dates where the dates didn't validate
// or an external query where one of the dates is missing
$this->empty_query();
$this->cleanup_query();
return;
}
$this->dates = $dates;
$reset_query_vars = array(
'second' , 'minute', 'hour',
'day', 'monthnum', 'year',
'w', 'm', 'meta_query'
);
// reset query vars.
foreach ( $reset_query_vars as $var ) {
$query->set( $var, '' );
}
$meta_query = array(
'relation' => 'AND',
// needs to be two meta key arrays
array(
'key' => $this->start,
'compare' => '>=',
'value' => $this->dates['start_date'],
'type' => 'DATE'
),
array(
'key' => $this->end,
'compare' => '<=',
'value' => $this->dates['end_date'],
'type' => 'DATE'
),
);
$query->set( 'meta_query', $meta_query );
// orderby 'meta_key'
$this->orderby_key = $query->get( 'meta_key' );
// add the filter
add_filter( 'get_meta_sql', array( $this, 'get_meta_sql' ), 10, 2 );
// remove the filters down the road
$this->cleanup_query();
}
/**
* Returns meta date archive meta sql.
*
* @since 1.0
* @param array $pieces Array containing the query's JOIN and WHERE clauses.
* @param array $queries Array of meta queries.
* @return array Array containing the query's JOIN and WHERE clauses.
*/
public function get_meta_sql( $pieces, $queries ) {
global $wpdb;
$start_date = $this->dates['start_date'];
$end_date = $this->dates['end_date'];
$where = "";
$start_alias = "$wpdb->postmeta";
$end_alias = "mt1";
$join = " INNER JOIN $wpdb->postmeta ON ($wpdb->posts.ID = $wpdb->postmeta.post_id)
INNER JOIN $wpdb->postmeta AS mt1 ON ( $wpdb->posts.ID = mt1.post_id )";
if ( !empty( $this->orderby_key ) ) {
$where .= $wpdb->prepare( " AND ($wpdb->postmeta.meta_key = %s)", $this->orderby_key );
$join .= " INNER JOIN $wpdb->postmeta AS mt2 ON ($wpdb->posts.ID = mt2.post_id)";
$start_alias = "mt1";
$end_alias = "mt2";
}
$start_key = $wpdb->prepare( "{$start_alias}.meta_key = %s", $this->start );
$end_key = $wpdb->prepare( "{$end_alias}.meta_key = %s", $this->end );
// after start date AND before end date
$query = " AND ( (
( $start_key AND ( CAST($start_alias.meta_value AS DATE) >= %s) )
AND ( $end_key AND ( CAST($end_alias.meta_value AS DATE) <= %s) )
)";
$where .= $wpdb->prepare( $query, $start_date, $end_date );
// OR before start date AND after end end date
$query = " OR (
( $start_key AND ( CAST($start_alias.meta_value AS DATE) <= %s) )
AND ( $end_key AND ( CAST($end_alias.meta_value AS DATE) >= %s) ))";
$where .= $wpdb->prepare( $query, $start_date, $end_date );
// OR before start date AND (before end date AND end date after start date)
$query = " OR (
( $start_key AND ( CAST($start_alias.meta_value AS DATE) <= %s) )
AND ( $end_key
AND ( CAST($end_alias.meta_value AS DATE) <= %s )
AND ( CAST($end_alias.meta_value AS DATE) >= %s )
))";
$where .= $wpdb->prepare( $query, $start_date, $end_date, $start_date );
// OR after end date AND (after start date AND start date before end date) )
$query = "OR (
( $end_key AND ( CAST($end_alias.meta_value AS DATE) >= %s ) )
AND ( $start_key AND ( CAST($start_alias.meta_value AS DATE) >= %s )
AND ( CAST($start_alias.meta_value AS DATE) <= %s )
)))";
$where .= $wpdb->prepare( $query, $end_date, $start_date, $end_date );
$pieces['join'] = $join;
$pieces['where'] = $where;
return $pieces;
}
/**
* Adds the end and start date variables to the public query variables.
*
* @since 1.0
* @param array $query_vars The array of whitelisted query variables.
* @return array The array of whitelisted query variables.
*/
public function query_vars( $query_vars ) {
$query_vars[] = 'meta_date_archives';
$query_vars[] = 'meta_archive_start_date';
$query_vars[] = 'meta_archive_end_date';
return $query_vars;
}
/**
* Adds a filter to 'the_posts' to remove filters used in pre_get_posts.
*
* @since 1.0
* @param object $query The WP_Query instance.
* @return [type] [description]
*/
private function cleanup_query() {
add_filter( 'the_posts', array( $this, 'the_posts' ), 99, 2 );
}
/**
* Removes filters used in pre_get_posts.
* Restores the suppress_filters parameter if it was used.
*
* @since 1.0
* @param array $posts Array with post objects.
* @return array Array with post objects.
*/
public function the_posts( $posts, $_this ) {
$this->dates = array();
if ( 'meta_date_archive' !== $this->filters ) {
$_this->query_vars['suppress_filters'] = $this->filters;
}
remove_filter( 'get_meta_sql', array( $this, 'get_meta_sql' ), 10, 2 );
remove_filter( 'posts_where', array( $this, 'posts_where' ) );
remove_filter( 'the_posts', array( $this, 'the_posts' ) );
return $posts;
}
/**
* Adds a filter to 'posts_where' to have the query return no posts.
*
* @since 1.0
* @return void
*/
private function empty_query() {
add_filter( 'posts_where', array( $this, 'posts_where' ) );
}
/**
* Adds 1=0 to the where clause to have the query return no posts.
*
* @since 1.0
* @return void
*/
public function posts_where( $where ) {
return $where . 'AND 1 = 0';
}
}
$meta_date_archive = new Meta_Date_Archive_Query();