APIClient.php
9.54 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
namespace WPMedia\Cloudflare;
use stdClass;
use Exception;
/**
* Cloudflare API Client.
*
* @since 1.0
*/
class APIClient {
const CLOUDFLARE_API = 'https://api.cloudflare.com/client/v4/';
/**
* Email address for API authentication.
*
* @var string
*/
protected $email;
/**
* API key for API authentication.
*
* @var string
*/
protected $api_key;
/**
* Zone ID.
*
* @var string
*/
protected $zone_id;
/**
* An array of arguments for wp_remote_get.
*
* @var array
*/
protected $args = [];
/**
* HTTP headers.
*
* @var array
*/
protected $headers = [];
/**
* APIClient constructor.
*
* @since 1.0
*
* @param string $useragent The user agent for this plugin or package. For example, "wp-rocket/3.5".
*/
public function __construct( $useragent ) {
$this->args = [
'timeout' => 30, // Increase from default of 5 to give extra time for the plugin to process story for exporting.
'sslverify' => true,
'body' => [],
];
$this->headers = [
'X-Auth-Email' => '',
'X-Auth-Key' => '',
'User-Agent' => $useragent,
'Content-type' => 'application/json',
];
}
/**
* Sets up the API credentials.
*
* @since 1.0
*
* @param string $email The email associated with the Cloudflare account.
* @param string $api_key The API key for the associated Cloudflare account.
* @param string $zone_id The zone ID.
*/
public function set_api_credentials( $email, $api_key, $zone_id ) {
$this->email = $email;
$this->api_key = $api_key;
$this->zone_id = $zone_id;
$this->headers['X-Auth-Email'] = $email;
$this->headers['X-Auth-Key'] = $api_key;
}
/**
* Get zone data.
*
* @since 1.0
*
* @return stdClass Cloudflare response packet.
*/
public function get_zones() {
return $this->get( "zones/{$this->zone_id}" );
}
/**
* Get the zone's page rules.
*
* @since 1.0
*
* @return stdClass Cloudflare response packet.
*/
public function list_pagerules() {
return $this->get( "zones/{$this->zone_id}/pagerules?status=active" );
}
/**
* Purges the cache.
*
* @since 1.0
*
* @return stdClass Cloudflare response packet.
*/
public function purge() {
return $this->delete( "zones/{$this->zone_id}/purge_cache", [ 'purge_everything' => true ] );
}
/**
* Purges the given URLs.
*
* @since 1.0
*
* @param array|null $urls An array of URLs that should be removed from cache.
*
* @return stdClass Cloudflare response packet.
*/
public function purge_files( array $urls ) {
return $this->delete( "zones/{$this->zone_id}/purge_cache", [ 'files' => $urls ] );
}
/**
* Changes the zone's browser cache TTL setting.
*
* @since 1.0
*
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
public function change_browser_cache_ttl( $value ) {
return $this->change_setting( 'browser_cache_ttl', $value );
}
/**
* Changes the zone's rocket loader setting.
*
* @since 1.0
*
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
public function change_rocket_loader( $value ) {
return $this->change_setting( 'rocket_loader', $value );
}
/**
* Changes the zone's minify setting.
*
* @since 1.0
*
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
public function change_minify( $value ) {
return $this->change_setting( 'minify', $value );
}
/**
* Changes the zone's cache level.
*
* @since 1.0
*
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
public function change_cache_level( $value ) {
return $this->change_setting( 'cache_level', $value );
}
/**
* Changes the zone's development mode.
*
* @since 1.0
*
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
public function change_development_mode( $value ) {
return $this->change_setting( 'development_mode', $value );
}
/**
* Changes the given setting.
*
* @since 1.0
*
* @param string $setting Name of the setting to change.
* @param string $value New setting's value.
*
* @return stdClass Cloudflare response packet.
*/
protected function change_setting( $setting, $value ) {
return $this->patch( "zones/{$this->zone_id}/settings/{$setting}", [ 'value' => $value ] );
}
/**
* Gets all of the Cloudflare settings.
*
* @since 1.0
*
* @return stdClass Cloudflare response packet.
*/
public function get_settings() {
return $this->get( "zones/{$this->zone_id}/settings" );
}
/**
* Gets Cloudflare's IPs.
*
* @since 1.0
*
* @return stdClass Cloudflare response packet.
*/
public function get_ips() {
return $this->get( '/ips' );
}
/**
* API call method for sending requests using GET.
*
* @since 1.0
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return stdClass Cloudflare response packet.
*/
protected function get( $path, array $data = [] ) {
return $this->request( $path, $data, 'get' );
}
/**
* API call method for sending requests using DELETE.
*
* @since 1.0
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return stdClass Cloudflare response packet.
*/
protected function delete( $path, array $data = [] ) {
return $this->request( $path, $data, 'delete' );
}
/**
* API call method for sending requests using PATCH.
*
* @since 1.0
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
*
* @return stdClass Cloudflare response packet.
*/
protected function patch( $path, array $data = [] ) {
return $this->request( $path, $data, 'patch' );
}
/**
* API call method for sending requests using GET, POST, PUT, DELETE OR PATCH.
*
* @since 1.0
*
* @author James Bell <james@james-bell.co.uk> - credit for original code adapted for version 1.0.
* @author WP Media
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
* @param string $method Type of method that should be used ('GET', 'DELETE', 'PATCH').
*
* @return stdClass response object.
* @throws AuthenticationException When email or api key are not set.
* @throws UnauthorizedException When Cloudflare's API returns a 401 or 403.
*/
protected function request( $path, array $data = [], $method = 'get' ) {
if ( '/ips' !== $path && ! $this->is_authorized() ) {
throw new AuthenticationException( 'Authentication information must be provided.' );
}
$response = $this->do_remote_request( $path, $data, $method );
if ( is_wp_error( $response ) ) {
throw new Exception( $response->get_error_message() );
}
$data = wp_remote_retrieve_body( $response );
if ( empty( $data ) ) {
throw new Exception( __( 'Cloudflare did not provide any reply. Please try again later.', 'rocket' ) );
}
$data = json_decode( $data );
if ( empty( $data->success ) ) {
$errors = [];
foreach ( $data->errors as $error ) {
if ( 6003 === $error->code || 9103 === $error->code ) {
$msg = __( 'Incorrect Cloudflare email address or API key.', 'rocket' );
$msg .= ' ' . sprintf(
/* translators: %1$s = opening link; %2$s = closing link */
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
// translators: Documentation exists in EN, FR; use localized URL if applicable.
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
'</a>'
);
throw new Exception( $msg );
}
if ( 7003 === $error->code ) {
$msg = __( 'Incorrect Cloudflare Zone ID.', 'rocket' );
$msg .= ' ' . sprintf(
/* translators: %1$s = opening link; %2$s = closing link */
__( 'Read the %1$sdocumentation%2$s for further guidance.', 'rocket' ),
// translators: Documentation exists in EN, FR; use localized URL if applicable.
'<a href="' . esc_url( __( 'https://docs.wp-rocket.me/article/18-using-wp-rocket-with-cloudflare/?utm_source=wp_plugin&utm_medium=wp_rocket#add-on', 'rocket' ) ) . '" rel="noopener noreferrer" target="_blank">',
'</a>'
);
throw new Exception( $msg );
}
$errors[] = $error->message;
}
throw new Exception( wp_sprintf_l( '%l ', $errors ) );
}
return $data;
}
/**
* Checks if the email and API key for the API credentials are set.
*
* @since 1.0
*
* @return bool true if authorized; else false.
*/
private function is_authorized() {
return (
isset( $this->email, $this->api_key )
&&
false !== filter_var( $this->email, FILTER_VALIDATE_EMAIL )
);
}
/**
* Does the request remote cURL request.
*
* @since 1.0
*
* @param string $path Path of the endpoint.
* @param array $data Data to be sent along with the request.
* @param string $method Type of method that should be used ('GET', 'DELETE', 'PATCH').
*
* @return array curl response packet.
*/
private function do_remote_request( $path, array $data, $method ) {
$this->args['method'] = isset( $method ) ? strtoupper( $method ) : 'GET';
if ( '/ips' !== $path ) {
$this->args['headers'] = $this->headers;
}
$this->args['body'] = [];
if ( ! empty( $data ) ) {
$this->args['body'] = wp_json_encode( $data );
}
$response = wp_remote_request( self::CLOUDFLARE_API . $path, $this->args );
return $response;
}
}