-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBaseReportsController.php
151 lines (136 loc) · 4.77 KB
/
BaseReportsController.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
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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\RESTServer;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WP;
use DateTime;
use Psr\Container\ContainerInterface;
use WP_REST_Request as Request;
defined( 'ABSPATH' ) || exit;
/**
* Class BaseReportsController
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers
*/
abstract class BaseReportsController extends BaseController {
/**
* @var ContainerInterface
*/
protected $container;
/**
* BaseReportsController constructor.
*
* @param ContainerInterface $container
*/
public function __construct( ContainerInterface $container ) {
$this->container = $container;
parent::__construct( $container->get( RESTServer::class ) );
}
/**
* Get the query params for collections.
*
* @return array
*/
public function get_collection_params(): array {
return [
'context' => $this->get_context_param( [ 'default' => 'view' ] ),
'after' => [
'description' => __( 'Limit response to data after a given ISO8601 compliant date.', 'google-listings-and-ads' ),
'type' => 'string',
'format' => 'date',
'default' => '-7 days',
'validate_callback' => 'rest_validate_request_arg',
],
'before' => [
'description' => __( 'Limit response to data before a given ISO8601 compliant date.', 'google-listings-and-ads' ),
'type' => 'string',
'format' => 'date',
'default' => 'now',
'validate_callback' => 'rest_validate_request_arg',
],
'ids' => [
'description' => __( 'Limit result to items with specified ids.', 'google-listings-and-ads' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => [
'type' => 'string',
],
],
'fields' => [
'description' => __( 'Limit totals to a set of fields.', 'google-listings-and-ads' ),
'type' => 'array',
'sanitize_callback' => 'wp_parse_slug_list',
'validate_callback' => 'rest_validate_request_arg',
'items' => [
'type' => 'string',
],
],
'order' => [
'description' => __( 'Order sort attribute ascending or descending.', 'google-listings-and-ads' ),
'type' => 'string',
'default' => 'desc',
'enum' => [ 'asc', 'desc' ],
'validate_callback' => 'rest_validate_request_arg',
],
'orderby' => [
'description' => __( 'Sort collection by attribute.', 'google-listings-and-ads' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
'per_page' => [
'description' => __( 'Maximum number of rows to be returned in result data.', 'google-listings-and-ads' ),
'type' => 'integer',
'default' => 200,
'minimum' => 1,
'maximum' => 1000,
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
],
'next_page' => [
'description' => __( 'Token to retrieve the next page.', 'google-listings-and-ads' ),
'type' => 'string',
'validate_callback' => 'rest_validate_request_arg',
],
];
}
/**
* Maps query arguments from the REST request.
*
* @param Request $request REST Request.
* @return array
*/
protected function prepare_query_arguments( Request $request ): array {
$args = wp_parse_args(
array_intersect_key(
$request->get_query_params(),
$this->get_collection_params()
),
$request->get_default_params()
);
$this->normalize_timezones( $args );
return $args;
}
/**
* Converts input datetime parameters to local timezone.
*
* @param array $query_args Array of query arguments.
*/
protected function normalize_timezones( &$query_args ) {
/** @var WP $wp */
$wp = $this->container->get( WP::class );
$local_tz = $wp->wp_timezone();
foreach ( [ 'before', 'after' ] as $query_arg_key ) {
if ( isset( $query_args[ $query_arg_key ] ) && is_string( $query_args[ $query_arg_key ] ) ) {
// Assume that unspecified timezone is a local timezone.
$datetime = new DateTime( $query_args[ $query_arg_key ], $local_tz );
// In case timezone was forced by using +HH:MM, convert to local timezone.
$datetime->setTimezone( $local_tz );
$query_args[ $query_arg_key ] = $datetime;
} elseif ( isset( $query_args[ $query_arg_key ] ) && $query_args[ $query_arg_key ] instanceof DateTime ) {
// In case timezone is in other timezone, convert to local timezone.
$query_args[ $query_arg_key ]->setTimezone( $local_tz );
}
}
}
}