-
Notifications
You must be signed in to change notification settings - Fork 1
/
class-post-date.php
67 lines (61 loc) · 1.43 KB
/
class-post-date.php
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
<?php
/**
* Post date facet type.
*
* @package Elasticsearch_Extensions
*/
namespace Elasticsearch_Extensions\Facets;
use Elasticsearch_Extensions\DSL;
/**
* Post date facet type. Responsible for building
* the DSL and requests for post date facets.
*/
class Post_Date extends Facet_Type {
/**
* The query var this facet should use.
*
* @var string
*/
protected string $query_var = 'post_date';
/**
* Build the facet request.
*
* @return array
*/
public function request(): array {
return [
'post_date' => [
'date_histogram' => [
'field' => $this->controller->map_field( 'post_date' ),
'interval' => 'month',
'format' => 'yyyy-MM',
'min_doc_count' => 2,
'order' => [
'_key' => 'desc',
],
],
],
];
}
/**
* Get the request filter DSL clause.
*
* @param array $values Values to pass to filter.
* @return array
*/
public function filter( array $values ): array {
$should = [];
foreach ( $values as $date ) {
$gte = date( 'Y-m-d H:i:s', $date ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$lt = date( 'Y-m-d H:i:s', strtotime( date( 'Y-m-d', $date ) . ' + 1 month' ) ); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
$should[] = DSL::range(
'post_date',
[
'gte' => $gte,
'lt' => $lt,
]
);
}
return [ 'bool' => [ 'should' => $should ] ];
}
}