-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathIncentivesController.php
More file actions
184 lines (168 loc) · 4.68 KB
/
IncentivesController.php
File metadata and controls
184 lines (168 loc) · 4.68 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
<?php
declare( strict_types=1 );
namespace Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers\Ads;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Google\Ads;
use Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers\BaseController;
use Automattic\WooCommerce\GoogleListingsAndAds\API\TransportMethods;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\RESTServer;
use Automattic\WooCommerce\GoogleListingsAndAds\Proxies\WC;
use WP_REST_Request as Request;
defined( 'ABSPATH' ) || exit;
/**
* Class IncentivesController
*
* Handles fetching Choose-Your-Own (CYO) incentive offers from the Google Ads API.
*
* @since 3.3.0
*
* @package Automattic\WooCommerce\GoogleListingsAndAds\API\Site\Controllers\Ads
*/
class IncentivesController extends BaseController {
/**
* @var Ads
*/
protected $ads;
/**
* @var WC
*/
protected $wc;
/**
* IncentivesController constructor.
*
* @param RESTServer $rest_server
* @param Ads $ads
* @param WC $wc
*/
public function __construct( RESTServer $rest_server, Ads $ads, WC $wc ) {
parent::__construct( $rest_server );
$this->ads = $ads;
$this->wc = $wc;
}
/**
* Register rest routes with WordPress.
*/
public function register_routes(): void {
$this->register_route(
'ads/incentives',
[
[
'methods' => TransportMethods::READABLE,
'callback' => $this->get_incentives_callback(),
'permission_callback' => $this->get_permission_callback(),
],
'schema' => $this->get_api_response_schema_callback(),
]
);
}
/**
* @return callable
*/
protected function get_incentives_callback(): callable {
return function ( Request $request ) {
$country_code = $this->wc->get_base_country();
$language_code = $this->get_language_code();
$incentives = $this->ads->fetch_incentives( $country_code, $language_code );
return $this->prepare_item_for_response( $incentives, $request );
};
}
/**
* Get the ISO 639-1 language code from the WordPress locale.
*
* @return string
*/
protected function get_language_code(): string {
$locale = get_locale();
if ( empty( $locale ) ) {
return 'en';
}
return strtolower( substr( $locale, 0, 2 ) );
}
/**
* Get the item schema properties for the controller.
*
* @return array
*/
protected function get_schema_properties(): array {
return [
'type' => [
'type' => 'string',
'description' => __( 'The offer type.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'termsAndConditionsUrl' => [
'type' => 'string',
'description' => __( 'The consolidated terms and conditions URL.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
],
'incentives' => [
'type' => 'array',
'description' => __( 'The available incentive offers.', 'google-listings-and-ads' ),
'context' => [ 'view' ],
'items' => [
'type' => 'object',
'properties' => [
'id' => [
'type' => 'string',
'description' => __( 'The incentive ID.', 'google-listings-and-ads' ),
],
'type' => [
'type' => 'string',
'description' => __( 'The incentive type.', 'google-listings-and-ads' ),
],
'offer' => [
'type' => 'string',
'enum' => [ 'low', 'medium', 'high' ],
'description' => __( 'The offer level.', 'google-listings-and-ads' ),
],
'termsAndConditionsUrl' => [
'type' => 'string',
'description' => __( 'The terms and conditions URL for this incentive.', 'google-listings-and-ads' ),
],
'requirement' => [
'type' => 'object',
'properties' => [
'spend' => [
'type' => 'object',
'properties' => [
'awardAmount' => [
'type' => 'object',
'properties' => [
'currencyCode' => [
'type' => 'string',
],
'units' => [
'type' => 'string',
],
],
],
'requiredAmount' => [
'type' => 'object',
'properties' => [
'currencyCode' => [
'type' => 'string',
],
'units' => [
'type' => 'string',
],
],
],
],
],
],
],
],
],
],
];
}
/**
* Get the item schema name for the controller.
*
* Used for building the API response schema.
*
* @return string
*/
protected function get_schema_title(): string {
return 'incentives';
}
}