author | ymh <ymh.work@gmail.com> |
Tue, 22 Oct 2019 16:11:46 +0200 | |
changeset 15 | 3d4e9c994f10 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
5 | 3 |
* Class for generating SQL clauses that filter a primary query according to date. |
4 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
5 |
* WP_Date_Query is a helper that allows primary query classes, such as WP_Query, to filter |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
6 |
* their results by date columns, by generating `WHERE` subclauses to be attached to the |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
7 |
* primary SQL query string. |
0 | 8 |
* |
5 | 9 |
* Attempting to filter by an invalid date value (eg month=13) will generate SQL that will |
10 |
* return no results. In these cases, a _doing_it_wrong() error notice is also thrown. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
11 |
* See WP_Date_Query::validate_date_values(). |
0 | 12 |
* |
5 | 13 |
* @link https://codex.wordpress.org/Function_Reference/WP_Query Codex page. |
0 | 14 |
* |
15 |
* @since 3.7.0 |
|
16 |
*/ |
|
17 |
class WP_Date_Query { |
|
18 |
/** |
|
5 | 19 |
* Array of date queries. |
20 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
21 |
* See WP_Date_Query::__construct() for information on date query arguments. |
0 | 22 |
* |
23 |
* @since 3.7.0 |
|
24 |
* @var array |
|
25 |
*/ |
|
26 |
public $queries = array(); |
|
27 |
||
28 |
/** |
|
5 | 29 |
* The default relation between top-level queries. Can be either 'AND' or 'OR'. |
0 | 30 |
* |
31 |
* @since 3.7.0 |
|
32 |
* @var string |
|
33 |
*/ |
|
34 |
public $relation = 'AND'; |
|
35 |
||
36 |
/** |
|
37 |
* The column to query against. Can be changed via the query arguments. |
|
38 |
* |
|
39 |
* @since 3.7.0 |
|
40 |
* @var string |
|
41 |
*/ |
|
42 |
public $column = 'post_date'; |
|
43 |
||
44 |
/** |
|
45 |
* The value comparison operator. Can be changed via the query arguments. |
|
46 |
* |
|
47 |
* @since 3.7.0 |
|
48 |
* @var array |
|
49 |
*/ |
|
50 |
public $compare = '='; |
|
51 |
||
52 |
/** |
|
5 | 53 |
* Supported time-related parameter keys. |
54 |
* |
|
55 |
* @since 4.1.0 |
|
56 |
* @var array |
|
57 |
*/ |
|
58 |
public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' ); |
|
59 |
||
60 |
/** |
|
0 | 61 |
* Constructor. |
62 |
* |
|
5 | 63 |
* Time-related parameters that normally require integer values ('year', 'month', 'week', 'dayofyear', 'day', |
64 |
* 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second') accept arrays of integers for some values of |
|
65 |
* 'compare'. When 'compare' is 'IN' or 'NOT IN', arrays are accepted; when 'compare' is 'BETWEEN' or 'NOT |
|
66 |
* BETWEEN', arrays of two valid values are required. See individual argument descriptions for accepted values. |
|
67 |
* |
|
68 |
* @since 3.7.0 |
|
69 |
* @since 4.0.0 The $inclusive logic was updated to include all times within the date range. |
|
70 |
* @since 4.1.0 Introduced 'dayofweek_iso' time type parameter. |
|
71 |
* |
|
0 | 72 |
* @param array $date_query { |
5 | 73 |
* Array of date query clauses. |
0 | 74 |
* |
75 |
* @type array { |
|
76 |
* @type string $column Optional. The column to query against. If undefined, inherits the value of |
|
5 | 77 |
* the `$default_column` parameter. Accepts 'post_date', 'post_date_gmt', |
78 |
* 'post_modified','post_modified_gmt', 'comment_date', 'comment_date_gmt'. |
|
79 |
* Default 'post_date'. |
|
80 |
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', '<', '<=', |
|
81 |
* 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. Default '='. |
|
82 |
* @type string $relation Optional. The boolean relationship between the date queries. Accepts 'OR' or 'AND'. |
|
83 |
* Default 'OR'. |
|
0 | 84 |
* @type array { |
5 | 85 |
* Optional. An array of first-order clause parameters, or another fully-formed date query. |
86 |
* |
|
87 |
* @type string|array $before { |
|
88 |
* Optional. Date to retrieve posts before. Accepts `strtotime()`-compatible string, |
|
89 |
* or array of 'year', 'month', 'day' values. |
|
0 | 90 |
* |
91 |
* @type string $year The four-digit year. Default empty. Accepts any four-digit year. |
|
92 |
* @type string $month Optional when passing array.The month of the year. |
|
93 |
* Default (string:empty)|(array:1). Accepts numbers 1-12. |
|
94 |
* @type string $day Optional when passing array.The day of the month. |
|
95 |
* Default (string:empty)|(array:1). Accepts numbers 1-31. |
|
96 |
* } |
|
5 | 97 |
* @type string|array $after { |
98 |
* Optional. Date to retrieve posts after. Accepts `strtotime()`-compatible string, |
|
99 |
* or array of 'year', 'month', 'day' values. |
|
0 | 100 |
* |
5 | 101 |
* @type string $year The four-digit year. Accepts any four-digit year. Default empty. |
102 |
* @type string $month Optional when passing array. The month of the year. Accepts numbers 1-12. |
|
103 |
* Default (string:empty)|(array:12). |
|
104 |
* @type string $day Optional when passing array.The day of the month. Accepts numbers 1-31. |
|
105 |
* Default (string:empty)|(array:last day of month). |
|
0 | 106 |
* } |
5 | 107 |
* @type string $column Optional. Used to add a clause comparing a column other than the |
108 |
* column specified in the top-level `$column` parameter. Accepts |
|
109 |
* 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', |
|
110 |
* 'comment_date', 'comment_date_gmt'. Default is the value of |
|
111 |
* top-level `$column`. |
|
112 |
* @type string $compare Optional. The comparison operator. Accepts '=', '!=', '>', '>=', |
|
113 |
* '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN'. 'IN', |
|
114 |
* 'NOT IN', 'BETWEEN', and 'NOT BETWEEN'. Comparisons support |
|
115 |
* arrays in some time-related parameters. Default '='. |
|
116 |
* @type bool $inclusive Optional. Include results from dates specified in 'before' or |
|
117 |
* 'after'. Default false. |
|
118 |
* @type int|array $year Optional. The four-digit year number. Accepts any four-digit year |
|
119 |
* or an array of years if `$compare` supports it. Default empty. |
|
120 |
* @type int|array $month Optional. The two-digit month number. Accepts numbers 1-12 or an |
|
121 |
* array of valid numbers if `$compare` supports it. Default empty. |
|
122 |
* @type int|array $week Optional. The week number of the year. Accepts numbers 0-53 or an |
|
123 |
* array of valid numbers if `$compare` supports it. Default empty. |
|
124 |
* @type int|array $dayofyear Optional. The day number of the year. Accepts numbers 1-366 or an |
|
125 |
* array of valid numbers if `$compare` supports it. |
|
126 |
* @type int|array $day Optional. The day of the month. Accepts numbers 1-31 or an array |
|
127 |
* of valid numbers if `$compare` supports it. Default empty. |
|
128 |
* @type int|array $dayofweek Optional. The day number of the week. Accepts numbers 1-7 (1 is |
|
129 |
* Sunday) or an array of valid numbers if `$compare` supports it. |
|
130 |
* Default empty. |
|
131 |
* @type int|array $dayofweek_iso Optional. The day number of the week (ISO). Accepts numbers 1-7 |
|
132 |
* (1 is Monday) or an array of valid numbers if `$compare` supports it. |
|
133 |
* Default empty. |
|
134 |
* @type int|array $hour Optional. The hour of the day. Accepts numbers 0-23 or an array |
|
135 |
* of valid numbers if `$compare` supports it. Default empty. |
|
136 |
* @type int|array $minute Optional. The minute of the hour. Accepts numbers 0-60 or an array |
|
137 |
* of valid numbers if `$compare` supports it. Default empty. |
|
138 |
* @type int|array $second Optional. The second of the minute. Accepts numbers 0-60 or an |
|
139 |
* array of valid numbers if `$compare` supports it. Default empty. |
|
0 | 140 |
* } |
141 |
* } |
|
142 |
* } |
|
9 | 143 |
* @param string $default_column Optional. Default column to query against. Default 'post_date'. |
144 |
* Accepts 'post_date', 'post_date_gmt', 'post_modified', 'post_modified_gmt', |
|
145 |
* 'comment_date', 'comment_date_gmt'. |
|
0 | 146 |
*/ |
5 | 147 |
public function __construct( $date_query, $default_column = 'post_date' ) { |
148 |
if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) { |
|
0 | 149 |
$this->relation = 'OR'; |
5 | 150 |
} else { |
0 | 151 |
$this->relation = 'AND'; |
5 | 152 |
} |
153 |
||
154 |
if ( ! is_array( $date_query ) ) { |
|
155 |
return; |
|
156 |
} |
|
0 | 157 |
|
5 | 158 |
// Support for passing time-based keys in the top level of the $date_query array. |
159 |
if ( ! isset( $date_query[0] ) && ! empty( $date_query ) ) { |
|
160 |
$date_query = array( $date_query ); |
|
161 |
} |
|
162 |
||
163 |
if ( empty( $date_query ) ) { |
|
164 |
return; |
|
165 |
} |
|
166 |
||
167 |
if ( ! empty( $date_query['column'] ) ) { |
|
168 |
$date_query['column'] = esc_sql( $date_query['column'] ); |
|
169 |
} else { |
|
170 |
$date_query['column'] = esc_sql( $default_column ); |
|
171 |
} |
|
0 | 172 |
|
173 |
$this->column = $this->validate_column( $this->column ); |
|
174 |
||
175 |
$this->compare = $this->get_compare( $date_query ); |
|
176 |
||
5 | 177 |
$this->queries = $this->sanitize_query( $date_query ); |
178 |
} |
|
179 |
||
180 |
/** |
|
181 |
* Recursive-friendly query sanitizer. |
|
182 |
* |
|
183 |
* Ensures that each query-level clause has a 'relation' key, and that |
|
184 |
* each first-order clause contains all the necessary keys from |
|
185 |
* `$defaults`. |
|
186 |
* |
|
187 |
* @since 4.1.0 |
|
188 |
* |
|
189 |
* @param array $queries |
|
190 |
* @param array $parent_query |
|
191 |
* |
|
192 |
* @return array Sanitized queries. |
|
193 |
*/ |
|
194 |
public function sanitize_query( $queries, $parent_query = null ) { |
|
195 |
$cleaned_query = array(); |
|
196 |
||
197 |
$defaults = array( |
|
198 |
'column' => 'post_date', |
|
199 |
'compare' => '=', |
|
200 |
'relation' => 'AND', |
|
201 |
); |
|
202 |
||
203 |
// Numeric keys should always have array values. |
|
204 |
foreach ( $queries as $qkey => $qvalue ) { |
|
205 |
if ( is_numeric( $qkey ) && ! is_array( $qvalue ) ) { |
|
206 |
unset( $queries[ $qkey ] ); |
|
207 |
} |
|
208 |
} |
|
0 | 209 |
|
5 | 210 |
// Each query should have a value for each default key. Inherit from the parent when possible. |
211 |
foreach ( $defaults as $dkey => $dvalue ) { |
|
212 |
if ( isset( $queries[ $dkey ] ) ) { |
|
0 | 213 |
continue; |
5 | 214 |
} |
0 | 215 |
|
5 | 216 |
if ( isset( $parent_query[ $dkey ] ) ) { |
217 |
$queries[ $dkey ] = $parent_query[ $dkey ]; |
|
218 |
} else { |
|
219 |
$queries[ $dkey ] = $dvalue; |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
// Validate the dates passed in the query. |
|
224 |
if ( $this->is_first_order_clause( $queries ) ) { |
|
225 |
$this->validate_date_values( $queries ); |
|
0 | 226 |
} |
5 | 227 |
|
228 |
foreach ( $queries as $key => $q ) { |
|
229 |
if ( ! is_array( $q ) || in_array( $key, $this->time_keys, true ) ) { |
|
230 |
// This is a first-order query. Trust the values and sanitize when building SQL. |
|
231 |
$cleaned_query[ $key ] = $q; |
|
232 |
} else { |
|
233 |
// Any array without a time key is another query, so we recurse. |
|
234 |
$cleaned_query[] = $this->sanitize_query( $q, $queries ); |
|
235 |
} |
|
236 |
} |
|
237 |
||
238 |
return $cleaned_query; |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Determine whether this is a first-order clause. |
|
243 |
* |
|
244 |
* Checks to see if the current clause has any time-related keys. |
|
245 |
* If so, it's first-order. |
|
246 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
* @since 4.1.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
* |
5 | 249 |
* @param array $query Query clause. |
250 |
* @return bool True if this is a first-order clause. |
|
251 |
*/ |
|
252 |
protected function is_first_order_clause( $query ) { |
|
253 |
$time_keys = array_intersect( $this->time_keys, array_keys( $query ) ); |
|
254 |
return ! empty( $time_keys ); |
|
0 | 255 |
} |
256 |
||
257 |
/** |
|
258 |
* Determines and validates what comparison operator to use. |
|
259 |
* |
|
260 |
* @since 3.7.0 |
|
261 |
* |
|
5 | 262 |
* @param array $query A date query or a date subquery. |
263 |
* @return string The comparison operator. |
|
0 | 264 |
*/ |
265 |
public function get_compare( $query ) { |
|
9 | 266 |
if ( ! empty( $query['compare'] ) && in_array( $query['compare'], array( '=', '!=', '>', '>=', '<', '<=', 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { |
0 | 267 |
return strtoupper( $query['compare'] ); |
9 | 268 |
} |
0 | 269 |
|
270 |
return $this->compare; |
|
271 |
} |
|
272 |
||
273 |
/** |
|
5 | 274 |
* Validates the given date_query values and triggers errors if something is not valid. |
275 |
* |
|
276 |
* Note that date queries with invalid date ranges are allowed to |
|
277 |
* continue (though of course no items will be found for impossible dates). |
|
278 |
* This method only generates debug notices for these cases. |
|
279 |
* |
|
280 |
* @since 4.1.0 |
|
281 |
* |
|
282 |
* @param array $date_query The date_query array. |
|
283 |
* @return bool True if all values in the query are valid, false if one or more fail. |
|
284 |
*/ |
|
285 |
public function validate_date_values( $date_query = array() ) { |
|
286 |
if ( empty( $date_query ) ) { |
|
287 |
return false; |
|
288 |
} |
|
289 |
||
290 |
$valid = true; |
|
291 |
||
292 |
/* |
|
293 |
* Validate 'before' and 'after' up front, then let the |
|
294 |
* validation routine continue to be sure that all invalid |
|
295 |
* values generate errors too. |
|
296 |
*/ |
|
9 | 297 |
if ( array_key_exists( 'before', $date_query ) && is_array( $date_query['before'] ) ) { |
5 | 298 |
$valid = $this->validate_date_values( $date_query['before'] ); |
299 |
} |
|
300 |
||
9 | 301 |
if ( array_key_exists( 'after', $date_query ) && is_array( $date_query['after'] ) ) { |
5 | 302 |
$valid = $this->validate_date_values( $date_query['after'] ); |
303 |
} |
|
304 |
||
305 |
// Array containing all min-max checks. |
|
306 |
$min_max_checks = array(); |
|
307 |
||
308 |
// Days per year. |
|
309 |
if ( array_key_exists( 'year', $date_query ) ) { |
|
310 |
/* |
|
311 |
* If a year exists in the date query, we can use it to get the days. |
|
312 |
* If multiple years are provided (as in a BETWEEN), use the first one. |
|
313 |
*/ |
|
314 |
if ( is_array( $date_query['year'] ) ) { |
|
315 |
$_year = reset( $date_query['year'] ); |
|
316 |
} else { |
|
317 |
$_year = $date_query['year']; |
|
318 |
} |
|
319 |
||
320 |
$max_days_of_year = date( 'z', mktime( 0, 0, 0, 12, 31, $_year ) ) + 1; |
|
321 |
} else { |
|
322 |
// otherwise we use the max of 366 (leap-year) |
|
323 |
$max_days_of_year = 366; |
|
324 |
} |
|
325 |
||
326 |
$min_max_checks['dayofyear'] = array( |
|
327 |
'min' => 1, |
|
9 | 328 |
'max' => $max_days_of_year, |
5 | 329 |
); |
330 |
||
331 |
// Days per week. |
|
332 |
$min_max_checks['dayofweek'] = array( |
|
333 |
'min' => 1, |
|
9 | 334 |
'max' => 7, |
5 | 335 |
); |
336 |
||
337 |
// Days per week. |
|
338 |
$min_max_checks['dayofweek_iso'] = array( |
|
339 |
'min' => 1, |
|
9 | 340 |
'max' => 7, |
5 | 341 |
); |
342 |
||
343 |
// Months per year. |
|
344 |
$min_max_checks['month'] = array( |
|
345 |
'min' => 1, |
|
9 | 346 |
'max' => 12, |
5 | 347 |
); |
348 |
||
349 |
// Weeks per year. |
|
350 |
if ( isset( $_year ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
* If we have a specific year, use it to calculate number of weeks. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
* Note: the number of weeks in a year is the date in which Dec 28 appears. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
$week_count = date( 'W', mktime( 0, 0, 0, 12, 28, $_year ) ); |
5 | 356 |
|
357 |
} else { |
|
358 |
// Otherwise set the week-count to a maximum of 53. |
|
359 |
$week_count = 53; |
|
360 |
} |
|
361 |
||
362 |
$min_max_checks['week'] = array( |
|
363 |
'min' => 1, |
|
9 | 364 |
'max' => $week_count, |
5 | 365 |
); |
366 |
||
367 |
// Days per month. |
|
368 |
$min_max_checks['day'] = array( |
|
369 |
'min' => 1, |
|
9 | 370 |
'max' => 31, |
5 | 371 |
); |
372 |
||
373 |
// Hours per day. |
|
374 |
$min_max_checks['hour'] = array( |
|
375 |
'min' => 0, |
|
9 | 376 |
'max' => 23, |
5 | 377 |
); |
378 |
||
379 |
// Minutes per hour. |
|
380 |
$min_max_checks['minute'] = array( |
|
381 |
'min' => 0, |
|
9 | 382 |
'max' => 59, |
5 | 383 |
); |
384 |
||
385 |
// Seconds per minute. |
|
386 |
$min_max_checks['second'] = array( |
|
387 |
'min' => 0, |
|
9 | 388 |
'max' => 59, |
5 | 389 |
); |
390 |
||
391 |
// Concatenate and throw a notice for each invalid value. |
|
392 |
foreach ( $min_max_checks as $key => $check ) { |
|
393 |
if ( ! array_key_exists( $key, $date_query ) ) { |
|
394 |
continue; |
|
395 |
} |
|
396 |
||
397 |
// Throw a notice for each failing value. |
|
398 |
foreach ( (array) $date_query[ $key ] as $_value ) { |
|
399 |
$is_between = $_value >= $check['min'] && $_value <= $check['max']; |
|
400 |
||
401 |
if ( ! is_numeric( $_value ) || ! $is_between ) { |
|
402 |
$error = sprintf( |
|
403 |
/* translators: Date query invalid date message: 1: invalid value, 2: type of value, 3: minimum valid value, 4: maximum valid value */ |
|
404 |
__( 'Invalid value %1$s for %2$s. Expected value should be between %3$s and %4$s.' ), |
|
405 |
'<code>' . esc_html( $_value ) . '</code>', |
|
406 |
'<code>' . esc_html( $key ) . '</code>', |
|
407 |
'<code>' . esc_html( $check['min'] ) . '</code>', |
|
408 |
'<code>' . esc_html( $check['max'] ) . '</code>' |
|
409 |
); |
|
410 |
||
411 |
_doing_it_wrong( __CLASS__, $error, '4.1.0' ); |
|
412 |
||
413 |
$valid = false; |
|
414 |
} |
|
415 |
} |
|
416 |
} |
|
417 |
||
418 |
// If we already have invalid date messages, don't bother running through checkdate(). |
|
419 |
if ( ! $valid ) { |
|
420 |
return $valid; |
|
421 |
} |
|
422 |
||
423 |
$day_month_year_error_msg = ''; |
|
424 |
||
425 |
$day_exists = array_key_exists( 'day', $date_query ) && is_numeric( $date_query['day'] ); |
|
426 |
$month_exists = array_key_exists( 'month', $date_query ) && is_numeric( $date_query['month'] ); |
|
427 |
$year_exists = array_key_exists( 'year', $date_query ) && is_numeric( $date_query['year'] ); |
|
428 |
||
429 |
if ( $day_exists && $month_exists && $year_exists ) { |
|
430 |
// 1. Checking day, month, year combination. |
|
431 |
if ( ! wp_checkdate( $date_query['month'], $date_query['day'], $date_query['year'], sprintf( '%s-%s-%s', $date_query['year'], $date_query['month'], $date_query['day'] ) ) ) { |
|
432 |
/* translators: 1: year, 2: month, 3: day of month */ |
|
433 |
$day_month_year_error_msg = sprintf( |
|
434 |
__( 'The following values do not describe a valid date: year %1$s, month %2$s, day %3$s.' ), |
|
435 |
'<code>' . esc_html( $date_query['year'] ) . '</code>', |
|
436 |
'<code>' . esc_html( $date_query['month'] ) . '</code>', |
|
437 |
'<code>' . esc_html( $date_query['day'] ) . '</code>' |
|
438 |
); |
|
439 |
||
440 |
$valid = false; |
|
441 |
} |
|
442 |
} elseif ( $day_exists && $month_exists ) { |
|
443 |
/* |
|
444 |
* 2. checking day, month combination |
|
445 |
* We use 2012 because, as a leap year, it's the most permissive. |
|
446 |
*/ |
|
447 |
if ( ! wp_checkdate( $date_query['month'], $date_query['day'], 2012, sprintf( '2012-%s-%s', $date_query['month'], $date_query['day'] ) ) ) { |
|
448 |
/* translators: 1: month, 2: day of month */ |
|
449 |
$day_month_year_error_msg = sprintf( |
|
450 |
__( 'The following values do not describe a valid date: month %1$s, day %2$s.' ), |
|
451 |
'<code>' . esc_html( $date_query['month'] ) . '</code>', |
|
452 |
'<code>' . esc_html( $date_query['day'] ) . '</code>' |
|
453 |
); |
|
454 |
||
455 |
$valid = false; |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
if ( ! empty( $day_month_year_error_msg ) ) { |
|
460 |
_doing_it_wrong( __CLASS__, $day_month_year_error_msg, '4.1.0' ); |
|
461 |
} |
|
462 |
||
463 |
return $valid; |
|
464 |
} |
|
465 |
||
466 |
/** |
|
0 | 467 |
* Validates a column name parameter. |
468 |
* |
|
5 | 469 |
* Column names without a table prefix (like 'post_date') are checked against a whitelist of |
470 |
* known tables, and then, if found, have a table prefix (such as 'wp_posts.') prepended. |
|
471 |
* Prefixed column names (such as 'wp_posts.post_date') bypass this whitelist check, |
|
472 |
* and are only sanitized to remove illegal characters. |
|
473 |
* |
|
0 | 474 |
* @since 3.7.0 |
475 |
* |
|
476 |
* @param string $column The user-supplied column name. |
|
477 |
* @return string A validated column name value. |
|
478 |
*/ |
|
479 |
public function validate_column( $column ) { |
|
5 | 480 |
global $wpdb; |
481 |
||
0 | 482 |
$valid_columns = array( |
9 | 483 |
'post_date', |
484 |
'post_date_gmt', |
|
485 |
'post_modified', |
|
486 |
'post_modified_gmt', |
|
487 |
'comment_date', |
|
488 |
'comment_date_gmt', |
|
489 |
'user_registered', |
|
490 |
'registered', |
|
491 |
'last_updated', |
|
0 | 492 |
); |
5 | 493 |
|
494 |
// Attempt to detect a table prefix. |
|
495 |
if ( false === strpos( $column, '.' ) ) { |
|
496 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
497 |
* Filters the list of valid date query columns. |
5 | 498 |
* |
499 |
* @since 3.7.0 |
|
500 |
* @since 4.1.0 Added 'user_registered' to the default recognized columns. |
|
501 |
* |
|
9 | 502 |
* @param string[] $valid_columns An array of valid date query columns. Defaults |
503 |
* are 'post_date', 'post_date_gmt', 'post_modified', |
|
504 |
* 'post_modified_gmt', 'comment_date', 'comment_date_gmt', |
|
505 |
* 'user_registered' |
|
5 | 506 |
*/ |
507 |
if ( ! in_array( $column, apply_filters( 'date_query_valid_columns', $valid_columns ) ) ) { |
|
508 |
$column = 'post_date'; |
|
509 |
} |
|
0 | 510 |
|
5 | 511 |
$known_columns = array( |
9 | 512 |
$wpdb->posts => array( |
5 | 513 |
'post_date', |
514 |
'post_date_gmt', |
|
515 |
'post_modified', |
|
516 |
'post_modified_gmt', |
|
517 |
), |
|
518 |
$wpdb->comments => array( |
|
519 |
'comment_date', |
|
520 |
'comment_date_gmt', |
|
521 |
), |
|
9 | 522 |
$wpdb->users => array( |
5 | 523 |
'user_registered', |
524 |
), |
|
9 | 525 |
$wpdb->blogs => array( |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
'registered', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
527 |
'last_updated', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
528 |
), |
5 | 529 |
); |
530 |
||
531 |
// If it's a known column name, add the appropriate table prefix. |
|
532 |
foreach ( $known_columns as $table_name => $table_columns ) { |
|
533 |
if ( in_array( $column, $table_columns ) ) { |
|
534 |
$column = $table_name . '.' . $column; |
|
535 |
break; |
|
536 |
} |
|
537 |
} |
|
538 |
} |
|
539 |
||
540 |
// Remove unsafe characters. |
|
541 |
return preg_replace( '/[^a-zA-Z0-9_$\.]/', '', $column ); |
|
0 | 542 |
} |
543 |
||
544 |
/** |
|
5 | 545 |
* Generate WHERE clause to be appended to a main query. |
0 | 546 |
* |
547 |
* @since 3.7.0 |
|
548 |
* |
|
5 | 549 |
* @return string MySQL WHERE clause. |
0 | 550 |
*/ |
551 |
public function get_sql() { |
|
5 | 552 |
$sql = $this->get_sql_clauses(); |
0 | 553 |
|
5 | 554 |
$where = $sql['where']; |
0 | 555 |
|
556 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
557 |
* Filters the date query WHERE clause. |
0 | 558 |
* |
559 |
* @since 3.7.0 |
|
560 |
* |
|
561 |
* @param string $where WHERE clause of the date query. |
|
562 |
* @param WP_Date_Query $this The WP_Date_Query instance. |
|
563 |
*/ |
|
564 |
return apply_filters( 'get_date_sql', $where, $this ); |
|
565 |
} |
|
566 |
||
567 |
/** |
|
5 | 568 |
* Generate SQL clauses to be appended to a main query. |
569 |
* |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
570 |
* Called by the public WP_Date_Query::get_sql(), this method is abstracted |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
571 |
* out to maintain parity with the other Query classes. |
5 | 572 |
* |
573 |
* @since 4.1.0 |
|
574 |
* |
|
575 |
* @return array { |
|
576 |
* Array containing JOIN and WHERE SQL clauses to append to the main query. |
|
577 |
* |
|
578 |
* @type string $join SQL fragment to append to the main JOIN clause. |
|
579 |
* @type string $where SQL fragment to append to the main WHERE clause. |
|
580 |
* } |
|
581 |
*/ |
|
582 |
protected function get_sql_clauses() { |
|
583 |
$sql = $this->get_sql_for_query( $this->queries ); |
|
584 |
||
585 |
if ( ! empty( $sql['where'] ) ) { |
|
586 |
$sql['where'] = ' AND ' . $sql['where']; |
|
587 |
} |
|
588 |
||
589 |
return $sql; |
|
590 |
} |
|
591 |
||
592 |
/** |
|
593 |
* Generate SQL clauses for a single query array. |
|
594 |
* |
|
595 |
* If nested subqueries are found, this method recurses the tree to |
|
596 |
* produce the properly nested SQL. |
|
597 |
* |
|
598 |
* @since 4.1.0 |
|
599 |
* |
|
600 |
* @param array $query Query to parse. |
|
601 |
* @param int $depth Optional. Number of tree levels deep we currently are. |
|
602 |
* Used to calculate indentation. Default 0. |
|
603 |
* @return array { |
|
604 |
* Array containing JOIN and WHERE SQL clauses to append to a single query array. |
|
0 | 605 |
* |
5 | 606 |
* @type string $join SQL fragment to append to the main JOIN clause. |
607 |
* @type string $where SQL fragment to append to the main WHERE clause. |
|
608 |
* } |
|
609 |
*/ |
|
610 |
protected function get_sql_for_query( $query, $depth = 0 ) { |
|
611 |
$sql_chunks = array( |
|
612 |
'join' => array(), |
|
613 |
'where' => array(), |
|
614 |
); |
|
615 |
||
616 |
$sql = array( |
|
617 |
'join' => '', |
|
618 |
'where' => '', |
|
619 |
); |
|
620 |
||
621 |
$indent = ''; |
|
622 |
for ( $i = 0; $i < $depth; $i++ ) { |
|
9 | 623 |
$indent .= ' '; |
5 | 624 |
} |
625 |
||
626 |
foreach ( $query as $key => $clause ) { |
|
627 |
if ( 'relation' === $key ) { |
|
628 |
$relation = $query['relation']; |
|
629 |
} elseif ( is_array( $clause ) ) { |
|
630 |
||
631 |
// This is a first-order clause. |
|
632 |
if ( $this->is_first_order_clause( $clause ) ) { |
|
633 |
$clause_sql = $this->get_sql_for_clause( $clause, $query ); |
|
634 |
||
635 |
$where_count = count( $clause_sql['where'] ); |
|
636 |
if ( ! $where_count ) { |
|
637 |
$sql_chunks['where'][] = ''; |
|
638 |
} elseif ( 1 === $where_count ) { |
|
639 |
$sql_chunks['where'][] = $clause_sql['where'][0]; |
|
640 |
} else { |
|
641 |
$sql_chunks['where'][] = '( ' . implode( ' AND ', $clause_sql['where'] ) . ' )'; |
|
642 |
} |
|
643 |
||
644 |
$sql_chunks['join'] = array_merge( $sql_chunks['join'], $clause_sql['join'] ); |
|
9 | 645 |
// This is a subquery, so we recurse. |
5 | 646 |
} else { |
647 |
$clause_sql = $this->get_sql_for_query( $clause, $depth + 1 ); |
|
648 |
||
649 |
$sql_chunks['where'][] = $clause_sql['where']; |
|
650 |
$sql_chunks['join'][] = $clause_sql['join']; |
|
651 |
} |
|
652 |
} |
|
653 |
} |
|
654 |
||
655 |
// Filter to remove empties. |
|
656 |
$sql_chunks['join'] = array_filter( $sql_chunks['join'] ); |
|
657 |
$sql_chunks['where'] = array_filter( $sql_chunks['where'] ); |
|
658 |
||
659 |
if ( empty( $relation ) ) { |
|
660 |
$relation = 'AND'; |
|
661 |
} |
|
662 |
||
663 |
// Filter duplicate JOIN clauses and combine into a single string. |
|
664 |
if ( ! empty( $sql_chunks['join'] ) ) { |
|
665 |
$sql['join'] = implode( ' ', array_unique( $sql_chunks['join'] ) ); |
|
666 |
} |
|
667 |
||
668 |
// Generate a single WHERE clause with proper brackets and indentation. |
|
669 |
if ( ! empty( $sql_chunks['where'] ) ) { |
|
670 |
$sql['where'] = '( ' . "\n " . $indent . implode( ' ' . "\n " . $indent . $relation . ' ' . "\n " . $indent, $sql_chunks['where'] ) . "\n" . $indent . ')'; |
|
671 |
} |
|
672 |
||
673 |
return $sql; |
|
674 |
} |
|
675 |
||
676 |
/** |
|
677 |
* Turns a single date clause into pieces for a WHERE clause. |
|
678 |
* |
|
679 |
* A wrapper for get_sql_for_clause(), included here for backward |
|
680 |
* compatibility while retaining the naming convention across Query classes. |
|
681 |
* |
|
682 |
* @since 3.7.0 |
|
683 |
* |
|
684 |
* @param array $query Date query arguments. |
|
685 |
* @return array { |
|
686 |
* Array containing JOIN and WHERE SQL clauses to append to the main query. |
|
687 |
* |
|
688 |
* @type string $join SQL fragment to append to the main JOIN clause. |
|
689 |
* @type string $where SQL fragment to append to the main WHERE clause. |
|
690 |
* } |
|
0 | 691 |
*/ |
692 |
protected function get_sql_for_subquery( $query ) { |
|
5 | 693 |
return $this->get_sql_for_clause( $query, '' ); |
694 |
} |
|
695 |
||
696 |
/** |
|
697 |
* Turns a first-order date query into SQL for a WHERE clause. |
|
698 |
* |
|
699 |
* @since 4.1.0 |
|
700 |
* |
|
701 |
* @param array $query Date query clause. |
|
702 |
* @param array $parent_query Parent query of the current date query. |
|
703 |
* @return array { |
|
704 |
* Array containing JOIN and WHERE SQL clauses to append to the main query. |
|
705 |
* |
|
706 |
* @type string $join SQL fragment to append to the main JOIN clause. |
|
707 |
* @type string $where SQL fragment to append to the main WHERE clause. |
|
708 |
* } |
|
709 |
*/ |
|
710 |
protected function get_sql_for_clause( $query, $parent_query ) { |
|
0 | 711 |
global $wpdb; |
712 |
||
5 | 713 |
// The sub-parts of a $where part. |
0 | 714 |
$where_parts = array(); |
715 |
||
716 |
$column = ( ! empty( $query['column'] ) ) ? esc_sql( $query['column'] ) : $this->column; |
|
717 |
||
718 |
$column = $this->validate_column( $column ); |
|
719 |
||
720 |
$compare = $this->get_compare( $query ); |
|
721 |
||
5 | 722 |
$inclusive = ! empty( $query['inclusive'] ); |
723 |
||
724 |
// Assign greater- and less-than values. |
|
0 | 725 |
$lt = '<'; |
726 |
$gt = '>'; |
|
5 | 727 |
|
728 |
if ( $inclusive ) { |
|
0 | 729 |
$lt .= '='; |
730 |
$gt .= '='; |
|
731 |
} |
|
732 |
||
5 | 733 |
// Range queries. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
734 |
if ( ! empty( $query['after'] ) ) { |
5 | 735 |
$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
736 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
737 |
if ( ! empty( $query['before'] ) ) { |
5 | 738 |
$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
739 |
} |
5 | 740 |
// Specific value queries. |
0 | 741 |
|
9 | 742 |
if ( isset( $query['year'] ) && $value = $this->build_value( $compare, $query['year'] ) ) { |
0 | 743 |
$where_parts[] = "YEAR( $column ) $compare $value"; |
9 | 744 |
} |
0 | 745 |
|
5 | 746 |
if ( isset( $query['month'] ) && $value = $this->build_value( $compare, $query['month'] ) ) { |
0 | 747 |
$where_parts[] = "MONTH( $column ) $compare $value"; |
5 | 748 |
} elseif ( isset( $query['monthnum'] ) && $value = $this->build_value( $compare, $query['monthnum'] ) ) { |
0 | 749 |
$where_parts[] = "MONTH( $column ) $compare $value"; |
5 | 750 |
} |
751 |
if ( isset( $query['week'] ) && false !== ( $value = $this->build_value( $compare, $query['week'] ) ) ) { |
|
0 | 752 |
$where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; |
5 | 753 |
} elseif ( isset( $query['w'] ) && false !== ( $value = $this->build_value( $compare, $query['w'] ) ) ) { |
0 | 754 |
$where_parts[] = _wp_mysql_week( $column ) . " $compare $value"; |
5 | 755 |
} |
9 | 756 |
if ( isset( $query['dayofyear'] ) && $value = $this->build_value( $compare, $query['dayofyear'] ) ) { |
0 | 757 |
$where_parts[] = "DAYOFYEAR( $column ) $compare $value"; |
9 | 758 |
} |
0 | 759 |
|
9 | 760 |
if ( isset( $query['day'] ) && $value = $this->build_value( $compare, $query['day'] ) ) { |
0 | 761 |
$where_parts[] = "DAYOFMONTH( $column ) $compare $value"; |
9 | 762 |
} |
0 | 763 |
|
9 | 764 |
if ( isset( $query['dayofweek'] ) && $value = $this->build_value( $compare, $query['dayofweek'] ) ) { |
0 | 765 |
$where_parts[] = "DAYOFWEEK( $column ) $compare $value"; |
9 | 766 |
} |
0 | 767 |
|
9 | 768 |
if ( isset( $query['dayofweek_iso'] ) && $value = $this->build_value( $compare, $query['dayofweek_iso'] ) ) { |
5 | 769 |
$where_parts[] = "WEEKDAY( $column ) + 1 $compare $value"; |
9 | 770 |
} |
5 | 771 |
|
0 | 772 |
if ( isset( $query['hour'] ) || isset( $query['minute'] ) || isset( $query['second'] ) ) { |
5 | 773 |
// Avoid notices. |
0 | 774 |
foreach ( array( 'hour', 'minute', 'second' ) as $unit ) { |
5 | 775 |
if ( ! isset( $query[ $unit ] ) ) { |
776 |
$query[ $unit ] = null; |
|
0 | 777 |
} |
778 |
} |
|
779 |
||
780 |
if ( $time_query = $this->build_time_query( $column, $compare, $query['hour'], $query['minute'], $query['second'] ) ) { |
|
781 |
$where_parts[] = $time_query; |
|
782 |
} |
|
783 |
} |
|
784 |
||
5 | 785 |
/* |
786 |
* Return an array of 'join' and 'where' for compatibility |
|
787 |
* with other query classes. |
|
788 |
*/ |
|
789 |
return array( |
|
790 |
'where' => $where_parts, |
|
791 |
'join' => array(), |
|
792 |
); |
|
0 | 793 |
} |
794 |
||
795 |
/** |
|
796 |
* Builds and validates a value string based on the comparison operator. |
|
797 |
* |
|
798 |
* @since 3.7.0 |
|
799 |
* |
|
800 |
* @param string $compare The compare operator to use |
|
801 |
* @param string|array $value The value |
|
5 | 802 |
* @return string|false|int The value to be used in SQL or false on error. |
0 | 803 |
*/ |
804 |
public function build_value( $compare, $value ) { |
|
9 | 805 |
if ( ! isset( $value ) ) { |
0 | 806 |
return false; |
9 | 807 |
} |
0 | 808 |
|
809 |
switch ( $compare ) { |
|
810 |
case 'IN': |
|
811 |
case 'NOT IN': |
|
5 | 812 |
$value = (array) $value; |
813 |
||
814 |
// Remove non-numeric values. |
|
815 |
$value = array_filter( $value, 'is_numeric' ); |
|
816 |
||
817 |
if ( empty( $value ) ) { |
|
818 |
return false; |
|
819 |
} |
|
820 |
||
821 |
return '(' . implode( ',', array_map( 'intval', $value ) ) . ')'; |
|
0 | 822 |
|
823 |
case 'BETWEEN': |
|
824 |
case 'NOT BETWEEN': |
|
5 | 825 |
if ( ! is_array( $value ) || 2 != count( $value ) ) { |
0 | 826 |
$value = array( $value, $value ); |
5 | 827 |
} else { |
828 |
$value = array_values( $value ); |
|
829 |
} |
|
830 |
||
831 |
// If either value is non-numeric, bail. |
|
832 |
foreach ( $value as $v ) { |
|
833 |
if ( ! is_numeric( $v ) ) { |
|
834 |
return false; |
|
835 |
} |
|
836 |
} |
|
0 | 837 |
|
838 |
$value = array_map( 'intval', $value ); |
|
839 |
||
840 |
return $value[0] . ' AND ' . $value[1]; |
|
841 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
842 |
default: |
5 | 843 |
if ( ! is_numeric( $value ) ) { |
844 |
return false; |
|
845 |
} |
|
846 |
||
0 | 847 |
return (int) $value; |
848 |
} |
|
849 |
} |
|
850 |
||
851 |
/** |
|
852 |
* Builds a MySQL format date/time based on some query parameters. |
|
853 |
* |
|
854 |
* You can pass an array of values (year, month, etc.) with missing parameter values being defaulted to |
|
855 |
* either the maximum or minimum values (controlled by the $default_to parameter). Alternatively you can |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
856 |
* pass a string that will be run through strtotime(). |
0 | 857 |
* |
858 |
* @since 3.7.0 |
|
859 |
* |
|
5 | 860 |
* @param string|array $datetime An array of parameters or a strotime() string |
861 |
* @param bool $default_to_max Whether to round up incomplete dates. Supported by values |
|
862 |
* of $datetime that are arrays, or string values that are a |
|
863 |
* subset of MySQL date format ('Y', 'Y-m', 'Y-m-d', 'Y-m-d H:i'). |
|
864 |
* Default: false. |
|
0 | 865 |
* @return string|false A MySQL format date/time or false on failure |
866 |
*/ |
|
867 |
public function build_mysql_datetime( $datetime, $default_to_max = false ) { |
|
868 |
$now = current_time( 'timestamp' ); |
|
869 |
||
870 |
if ( ! is_array( $datetime ) ) { |
|
5 | 871 |
|
872 |
/* |
|
873 |
* Try to parse some common date formats, so we can detect |
|
874 |
* the level of precision and support the 'inclusive' parameter. |
|
875 |
*/ |
|
876 |
if ( preg_match( '/^(\d{4})$/', $datetime, $matches ) ) { |
|
877 |
// Y |
|
878 |
$datetime = array( |
|
879 |
'year' => intval( $matches[1] ), |
|
880 |
); |
|
881 |
||
882 |
} elseif ( preg_match( '/^(\d{4})\-(\d{2})$/', $datetime, $matches ) ) { |
|
883 |
// Y-m |
|
884 |
$datetime = array( |
|
885 |
'year' => intval( $matches[1] ), |
|
886 |
'month' => intval( $matches[2] ), |
|
887 |
); |
|
888 |
||
889 |
} elseif ( preg_match( '/^(\d{4})\-(\d{2})\-(\d{2})$/', $datetime, $matches ) ) { |
|
890 |
// Y-m-d |
|
891 |
$datetime = array( |
|
892 |
'year' => intval( $matches[1] ), |
|
893 |
'month' => intval( $matches[2] ), |
|
894 |
'day' => intval( $matches[3] ), |
|
895 |
); |
|
896 |
||
897 |
} elseif ( preg_match( '/^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2})$/', $datetime, $matches ) ) { |
|
898 |
// Y-m-d H:i |
|
899 |
$datetime = array( |
|
900 |
'year' => intval( $matches[1] ), |
|
901 |
'month' => intval( $matches[2] ), |
|
902 |
'day' => intval( $matches[3] ), |
|
903 |
'hour' => intval( $matches[4] ), |
|
904 |
'minute' => intval( $matches[5] ), |
|
905 |
); |
|
906 |
} |
|
907 |
||
908 |
// If no match is found, we don't support default_to_max. |
|
909 |
if ( ! is_array( $datetime ) ) { |
|
910 |
// @todo Timezone issues here possibly |
|
911 |
return gmdate( 'Y-m-d H:i:s', strtotime( $datetime, $now ) ); |
|
912 |
} |
|
0 | 913 |
} |
914 |
||
915 |
$datetime = array_map( 'absint', $datetime ); |
|
916 |
||
9 | 917 |
if ( ! isset( $datetime['year'] ) ) { |
0 | 918 |
$datetime['year'] = gmdate( 'Y', $now ); |
9 | 919 |
} |
0 | 920 |
|
9 | 921 |
if ( ! isset( $datetime['month'] ) ) { |
0 | 922 |
$datetime['month'] = ( $default_to_max ) ? 12 : 1; |
9 | 923 |
} |
0 | 924 |
|
9 | 925 |
if ( ! isset( $datetime['day'] ) ) { |
0 | 926 |
$datetime['day'] = ( $default_to_max ) ? (int) date( 't', mktime( 0, 0, 0, $datetime['month'], 1, $datetime['year'] ) ) : 1; |
9 | 927 |
} |
0 | 928 |
|
9 | 929 |
if ( ! isset( $datetime['hour'] ) ) { |
0 | 930 |
$datetime['hour'] = ( $default_to_max ) ? 23 : 0; |
9 | 931 |
} |
0 | 932 |
|
9 | 933 |
if ( ! isset( $datetime['minute'] ) ) { |
0 | 934 |
$datetime['minute'] = ( $default_to_max ) ? 59 : 0; |
9 | 935 |
} |
0 | 936 |
|
9 | 937 |
if ( ! isset( $datetime['second'] ) ) { |
0 | 938 |
$datetime['second'] = ( $default_to_max ) ? 59 : 0; |
9 | 939 |
} |
0 | 940 |
|
941 |
return sprintf( '%04d-%02d-%02d %02d:%02d:%02d', $datetime['year'], $datetime['month'], $datetime['day'], $datetime['hour'], $datetime['minute'], $datetime['second'] ); |
|
942 |
} |
|
943 |
||
944 |
/** |
|
945 |
* Builds a query string for comparing time values (hour, minute, second). |
|
946 |
* |
|
947 |
* If just hour, minute, or second is set than a normal comparison will be done. |
|
948 |
* However if multiple values are passed, a pseudo-decimal time will be created |
|
949 |
* in order to be able to accurately compare against. |
|
950 |
* |
|
951 |
* @since 3.7.0 |
|
952 |
* |
|
953 |
* @param string $column The column to query against. Needs to be pre-validated! |
|
954 |
* @param string $compare The comparison operator. Needs to be pre-validated! |
|
955 |
* @param int|null $hour Optional. An hour value (0-23). |
|
956 |
* @param int|null $minute Optional. A minute value (0-59). |
|
957 |
* @param int|null $second Optional. A second value (0-59). |
|
958 |
* @return string|false A query part or false on failure. |
|
959 |
*/ |
|
960 |
public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { |
|
961 |
global $wpdb; |
|
962 |
||
963 |
// Have to have at least one |
|
9 | 964 |
if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) ) { |
0 | 965 |
return false; |
9 | 966 |
} |
0 | 967 |
|
968 |
// Complex combined queries aren't supported for multi-value queries |
|
969 |
if ( in_array( $compare, array( 'IN', 'NOT IN', 'BETWEEN', 'NOT BETWEEN' ) ) ) { |
|
970 |
$return = array(); |
|
971 |
||
9 | 972 |
if ( isset( $hour ) && false !== ( $value = $this->build_value( $compare, $hour ) ) ) { |
0 | 973 |
$return[] = "HOUR( $column ) $compare $value"; |
9 | 974 |
} |
0 | 975 |
|
9 | 976 |
if ( isset( $minute ) && false !== ( $value = $this->build_value( $compare, $minute ) ) ) { |
0 | 977 |
$return[] = "MINUTE( $column ) $compare $value"; |
9 | 978 |
} |
0 | 979 |
|
9 | 980 |
if ( isset( $second ) && false !== ( $value = $this->build_value( $compare, $second ) ) ) { |
0 | 981 |
$return[] = "SECOND( $column ) $compare $value"; |
9 | 982 |
} |
0 | 983 |
|
984 |
return implode( ' AND ', $return ); |
|
985 |
} |
|
986 |
||
987 |
// Cases where just one unit is set |
|
988 |
if ( isset( $hour ) && ! isset( $minute ) && ! isset( $second ) && false !== ( $value = $this->build_value( $compare, $hour ) ) ) { |
|
989 |
return "HOUR( $column ) $compare $value"; |
|
990 |
} elseif ( ! isset( $hour ) && isset( $minute ) && ! isset( $second ) && false !== ( $value = $this->build_value( $compare, $minute ) ) ) { |
|
991 |
return "MINUTE( $column ) $compare $value"; |
|
992 |
} elseif ( ! isset( $hour ) && ! isset( $minute ) && isset( $second ) && false !== ( $value = $this->build_value( $compare, $second ) ) ) { |
|
993 |
return "SECOND( $column ) $compare $value"; |
|
994 |
} |
|
995 |
||
996 |
// Single units were already handled. Since hour & second isn't allowed, minute must to be set. |
|
9 | 997 |
if ( ! isset( $minute ) ) { |
0 | 998 |
return false; |
9 | 999 |
} |
0 | 1000 |
|
1001 |
$format = $time = ''; |
|
1002 |
||
1003 |
// Hour |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
1004 |
if ( null !== $hour ) { |
0 | 1005 |
$format .= '%H.'; |
1006 |
$time .= sprintf( '%02d', $hour ) . '.'; |
|
1007 |
} else { |
|
1008 |
$format .= '0.'; |
|
1009 |
$time .= '0.'; |
|
1010 |
} |
|
1011 |
||
1012 |
// Minute |
|
1013 |
$format .= '%i'; |
|
1014 |
$time .= sprintf( '%02d', $minute ); |
|
1015 |
||
1016 |
if ( isset( $second ) ) { |
|
1017 |
$format .= '%s'; |
|
1018 |
$time .= sprintf( '%02d', $second ); |
|
1019 |
} |
|
1020 |
||
1021 |
return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time ); |
|
1022 |
} |
|
1023 |
} |