Diff: STRATO-apps/wordpress_03/app/wp-content/plugins/wp-rocket/inc/Engine/CDN/RocketCDN/APIClient.php
Keine Baseline-Datei – Diff nur gegen leer.
1
-
1
+
<?php
2
+
namespace WP_Rocket\Engine\CDN\RocketCDN;
3
+
4
+
use WP_Error;
5
+
6
+
/**
7
+
* Class to Interact with the RocketCDN API
8
+
*/
9
+
class APIClient {
10
+
const ROCKETCDN_API = 'https://rocketcdn.me/api/';
11
+
12
+
/**
13
+
* Gets current RocketCDN subscription data from cache if it exists
14
+
*
15
+
* Else do a request to the API to get fresh data
16
+
*
17
+
* @since 3.5
18
+
*
19
+
* @return array
20
+
*/
21
+
public function get_subscription_data() {
22
+
$status = get_transient( 'rocketcdn_status' );
23
+
24
+
if ( false !== $status ) {
25
+
return $status;
26
+
}
27
+
28
+
return $this->get_remote_subscription_data();
29
+
}
30
+
31
+
/**
32
+
* Gets fresh RocketCDN subscription data from the API
33
+
*
34
+
* @since 3.5
35
+
*
36
+
* @return array
37
+
*/
38
+
private function get_remote_subscription_data() {
39
+
$default = [
40
+
'id' => 0,
41
+
'is_active' => false,
42
+
'cdn_url' => '',
43
+
'subscription_next_date_update' => 0,
44
+
'subscription_status' => 'cancelled',
45
+
];
46
+
47
+
$token = get_option( 'rocketcdn_user_token' );
48
+
49
+
if ( empty( $token ) ) {
50
+
return $default;
51
+
}
52
+
53
+
$args = [
54
+
'headers' => [
55
+
'Authorization' => 'Token ' . $token,
56
+
],
57
+
];
58
+
59
+
$response = wp_remote_get(
60
+
self::ROCKETCDN_API . 'website/search/?url=' . home_url(),
61
+
$args
62
+
);
63
+
64
+
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
65
+
$this->set_status_transient( $default, 3 * MINUTE_IN_SECONDS );
66
+
67
+
return $default;
68
+
}
69
+
70
+
$data = wp_remote_retrieve_body( $response );
71
+
72
+
if ( empty( $data ) ) {
73
+
$this->set_status_transient( $default, 3 * MINUTE_IN_SECONDS );
74
+
75
+
return $default;
76
+
}
77
+
78
+
$data = json_decode( $data, true );
79
+
$data = array_intersect_key( (array) $data, $default );
80
+
$data = array_merge( $default, $data );
81
+
82
+
$this->set_status_transient( $data, WEEK_IN_SECONDS );
83
+
84
+
return $data;
85
+
}
86
+
87
+
/**
88
+
* Sets the RocketCDN status transient with the provided value
89
+
*
90
+
* @since 3.5
91
+
*
92
+
* @param array $value Transient value.
93
+
* @param int $duration Transient duration.
94
+
* @return void
95
+
*/
96
+
private function set_status_transient( $value, $duration ) {
97
+
set_transient( 'rocketcdn_status', $value, $duration );
98
+
}
99
+
100
+
/**
101
+
* Gets pricing & promotion data for RocketCDN from cache if it exists
102
+
*
103
+
* Else do a request to the API to get fresh data
104
+
*
105
+
* @since 3.5
106
+
*
107
+
* @return array|WP_Error
108
+
*/
109
+
public function get_pricing_data() {
110
+
$pricing = get_transient( 'rocketcdn_pricing' );
111
+
112
+
if ( false !== $pricing ) {
113
+
return $pricing;
114
+
}
115
+
116
+
return $this->get_remote_pricing_data();
117
+
}
118
+
119
+
/**
120
+
* Gets fresh pricing & promotion data for RocketCDN
121
+
*
122
+
* @since 3.5
123
+
*
124
+
* @return array|WP_Error
125
+
*/
126
+
private function get_remote_pricing_data() {
127
+
$response = wp_remote_get( self::ROCKETCDN_API . 'pricing' );
128
+
129
+
if ( is_wp_error( $response ) ) {
130
+
return $response;
131
+
}
132
+
133
+
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
134
+
return $this->get_wp_error( __( 'We could not fetch the current price because RocketCDN API returned an unexpected error code.', 'rocket' ) );
135
+
}
136
+
137
+
$data = wp_remote_retrieve_body( $response );
138
+
139
+
if ( empty( $data ) ) {
140
+
return $this->get_wp_error( __( 'RocketCDN is not available at the moment. Please retry later.', 'rocket' ) );
141
+
}
142
+
143
+
$data = json_decode( $data, true );
144
+
145
+
set_transient( 'rocketcdn_pricing', $data, 6 * HOUR_IN_SECONDS );
146
+
147
+
return $data;
148
+
}
149
+
150
+
/**
151
+
* Gets a new WP_Error instance
152
+
*
153
+
* @since 3.5
154
+
*
155
+
* @param string $message Error message.
156
+
*
157
+
* @return WP_Error
158
+
*/
159
+
private function get_wp_error( string $message ) {
160
+
return new WP_Error( 'rocketcdn_error', $message );
161
+
}
162
+
163
+
/**
164
+
* Sends a request to the API to purge the CDN cache
165
+
*
166
+
* @since 3.5
167
+
*
168
+
* @return array
169
+
*/
170
+
public function purge_cache_request() {
171
+
$subscription = $this->get_subscription_data();
172
+
$status = 'error';
173
+
174
+
if ( ! isset( $subscription['id'] ) || 0 === $subscription['id'] ) {
175
+
return [
176
+
'status' => $status,
177
+
'message' => __( 'RocketCDN cache purge failed: Missing identifier parameter.', 'rocket' ),
178
+
];
179
+
}
180
+
181
+
$token = get_option( 'rocketcdn_user_token' );
182
+
183
+
if ( empty( $token ) ) {
184
+
return [
185
+
'status' => $status,
186
+
'message' => __( 'RocketCDN cache purge failed: Missing user token.', 'rocket' ),
187
+
];
188
+
}
189
+
190
+
$args = [
191
+
'method' => 'DELETE',
192
+
'headers' => [
193
+
'Authorization' => 'Token ' . $token,
194
+
],
195
+
];
196
+
197
+
$response = wp_remote_request(
198
+
self::ROCKETCDN_API . 'website/' . $subscription['id'] . '/purge/',
199
+
$args
200
+
);
201
+
202
+
if ( is_wp_error( $response ) ) {
203
+
return [
204
+
'status' => $status,
205
+
'message' => $response->get_error_message(),
206
+
];
207
+
}
208
+
209
+
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
210
+
return [
211
+
'status' => $status,
212
+
'message' => __( 'RocketCDN cache purge failed: The API returned an unexpected response code.', 'rocket' ),
213
+
];
214
+
}
215
+
216
+
$data = wp_remote_retrieve_body( $response );
217
+
218
+
if ( empty( $data ) ) {
219
+
return [
220
+
'status' => $status,
221
+
'message' => __( 'RocketCDN cache purge failed: The API returned an empty response.', 'rocket' ),
222
+
];
223
+
}
224
+
225
+
$data = json_decode( $data );
226
+
227
+
if ( ! isset( $data->success ) ) {
228
+
return [
229
+
'status' => $status,
230
+
'message' => __( 'RocketCDN cache purge failed: The API returned an unexpected response.', 'rocket' ),
231
+
];
232
+
}
233
+
234
+
if ( ! $data->success ) {
235
+
return [
236
+
'status' => $status,
237
+
'message' => sprintf(
238
+
// translators: %s = message returned by the API.
239
+
__( 'RocketCDN cache purge failed: %s.', 'rocket' ),
240
+
isset( $data->message ) ? $data->message : ''
241
+
),
242
+
];
243
+
}
244
+
245
+
return [
246
+
'status' => 'success',
247
+
'message' => __( 'RocketCDN cache purge successful.', 'rocket' ),
248
+
];
249
+
}
250
+
251
+
/**
252
+
* Filter the arguments used in an HTTP request, to make sure our user token has not been overwritten
253
+
* by some other plugin.
254
+
*
255
+
* @since 3.5
256
+
*
257
+
* @param array $args An array of HTTP request arguments.
258
+
* @param string $url The request URL.
259
+
* @return array
260
+
*/
261
+
public function preserve_authorization_token( $args, $url ) {
262
+
if ( strpos( $url, self::ROCKETCDN_API ) === false ) {
263
+
return $args;
264
+
}
265
+
266
+
if ( empty( $args['headers']['Authorization'] ) && self::ROCKETCDN_API . 'pricing' === $url ) {
267
+
return $args;
268
+
}
269
+
270
+
$token = get_option( 'rocketcdn_user_token' );
271
+
272
+
if ( empty( $token ) ) {
273
+
return $args;
274
+
}
275
+
276
+
$value = 'token ' . $token;
277
+
278
+
if ( isset( $args['headers']['Authorization'] ) && $value === $args['headers']['Authorization'] ) {
279
+
return $args;
280
+
}
281
+
282
+
$args['headers']['Authorization'] = $value;
283
+
284
+
return $args;
285
+
}
286
+
}
287
+