This repository was archived by the owner on Nov 30, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApi.php
More file actions
144 lines (122 loc) · 3.82 KB
/
Api.php
File metadata and controls
144 lines (122 loc) · 3.82 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
<?php
/**
* Cloudflare WP API
*
* WordPress HTTP API replacement of the jamesryanbell/cloudflare package.
*
* @package Cloudflare\WP
* @author Typist Tech <cloudflare-wp-api@typist.tech>
* @copyright 2017 Typist Tech
* @license GPL-2.0+
* @see https://www.typist.tech/projects/cloudflare-wp-api
*/
declare(strict_types=1);
namespace Cloudflare;
use WP_Error;
/**
* Class Api.
*/
class Api extends BaseApi
{
/**
* API call method for sending requests via wp_remote_request.
*
* @param string $path Path of the endpoint.
* @param array|null $data Data to be sent along with the request.
* @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH').
*
* @return array|WP_Error
*/
protected function request($path, array $data = null, $method = null)
{
$authError = $this->authenticationError();
if (null !== $authError) {
return $authError;
}
$url = 'https://api.cloudflare.com/client/v4/' . $path;
$args = $this->prepareRequestArguments($data, $method);
$response = wp_remote_request($url, $args);
if (is_wp_error($response)) {
return $response;
}
return $this->decode($response);
}
/**
* Return WP Error if this object does not contain necessary info to perform API requests.
*
* @return null|WP_Error
*/
private function authenticationError()
{
if (empty($this->email) || empty($this->auth_key)) {
return new WP_Error('authentication-error', 'Authentication information must be provided');
}
if (! is_email($this->email)) {
return new WP_Error('authentication-error', 'Email is not valid');
}
return null;
}
/**
* Prepare arguments for wp_remote_request.
*
* @param array|null $data Data to be sent along with the request.
* @param string|null $method Type of method that should be used ('GET', 'POST', 'PUT', 'DELETE', 'PATCH').
*
* @return array
*/
private function prepareRequestArguments(array $data = null, string $method = null): array
{
$data = $data ?? [];
$method = $method ?? 'GET';
// Removes null entries.
$data = array_filter($data, function ($val) {
return (null !== $val);
});
$headers = [
'Content-Type' => 'application/json',
'X-Auth-Email' => $this->email,
'X-Auth-Key' => $this->auth_key,
];
$args = [
'body' => wp_json_encode($data),
'headers' => $headers,
'method' => strtoupper($method),
'timeout' => 15,
];
return $args;
}
/**
* Decode Cloudflare response.
*
* @param array $response The response from Cloudflare.
*
* @return array|WP_Error
*/
private function decode(array $response)
{
$decodedBody = json_decode($response['body'], true);
if (null === $decodedBody) {
return new WP_Error('decode-error', 'Unable to decode response body', $response);
}
if (true !== $decodedBody['success']) {
return $this->wpErrorFor($decodedBody['errors'], $response);
}
return $response;
}
/**
* Decode Cloudflare error messages to WP Error.
*
* @param array $errors The error messages from Cloudflare.
* @param array $response The full response from Cloudflare.
*
* @return WP_Error
*/
private function wpErrorFor(array $errors, array $response): WP_Error
{
$wpError = new WP_Error;
foreach ($errors as $error) {
$wpError->add($error['code'], $error['message'], $response);
}
return $wpError;
}
}