Timestamp.php
1.79 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
<?php
namespace ACP\Search\Comparison\Meta\DateTime;
use ACP\Search\Comparison\Meta;
use ACP\Search\Labels;
use ACP\Search\Operators;
use ACP\Search\Value;
class Timestamp extends Meta {
public function __construct( $meta_key, $type ) {
$operators = new Operators( [
Operators::EQ,
Operators::GT,
Operators::LT,
Operators::BETWEEN,
Operators::TODAY,
Operators::PAST,
Operators::FUTURE,
Operators::IS_EMPTY,
Operators::NOT_IS_EMPTY,
], false );
parent::__construct( $operators, $meta_key, $type, Value::DATE, new Labels\Date() );
}
/**
* @param Value $value
* @param $operator
*
* @return Value
*/
protected function map_value( Value $value, $operator ) {
switch ( $operator ) {
case Operators::EQ:
$time = $this->get_timestamp_value( $value );
return new Value(
[
$time,
$time + DAY_IN_SECONDS - 1,
],
Value::INT
);
case Operators::GT_DAYS_AGO:
case Operators::LT_DAYS_AGO:
case Operators::WITHIN_DAYS:
return new Value( $value->get_value(), Value::INT );
default:
return new Value( $this->get_timestamp_value( $value ), Value::INT );
}
}
/**
* @param Value $value
*
* @return array|int
*/
private function get_timestamp_value( Value $value ) {
return is_array( $value->get_value() )
? array_map( [ $this, 'to_time' ], $value->get_value() )
: $this->to_time( $value->get_value() );
}
protected function get_meta_query( $operator, Value $value ) {
$value = $this->map_value( $value, $operator );
switch ( $operator ) {
case Operators::EQ:
$operator = Operators::BETWEEN;
}
return parent::get_meta_query(
$operator,
$value
);
}
/**
* @param string $value
*
* @return int
*/
private function to_time( $value ) {
return (int) strtotime( $value );
}
}