generated from 10up/plugin-scaffold
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRequest.php
308 lines (265 loc) · 6.81 KB
/
Request.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
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
<?php
/**
* Request curated content
*
* @package SophiWP
*/
namespace SophiWP\SiteAutomation;
use function SophiWP\Settings\get_sophi_settings;
/**
* Class: Request
*/
class Request {
/**
* Auth object which manages access_token.
*
* @var Auth $auth
*/
private $auth;
/**
* Site Automation API URL.
*
* @var string $api_url
*/
private $api_url;
/**
* Page name for Site Automation request.
*
* @var string $page
*/
private $page;
/**
* Widget name for Site Automation request.
*
* @var string $widget
*/
private $widget;
/**
* Status of latest Site Automation request.
*
* @var array $status
*/
private $status;
/**
* Class constructor.
*
* @param Auth $auth Authentication object.
*/
public function __construct( $auth ) {
$this->auth = $auth;
add_action(
'sophi_retry_get_curated_data',
[ $this, 'do_cron' ],
10,
2
);
}
/**
* Get (cached) curated posts.
*
* @param string $page Page name.
* @param string $widget Widget name.
* @param float $timeout The request timeout value.
*
* @return array|bool
*/
public function get( $page, $widget, $timeout = 3 ) {
$this->page = $page;
$this->widget = $widget;
$this->api_url = $this->set_api_url( $page, $widget );
$this->status = $this->get_status();
$site_automation_data = false;
/**
* Whether to bypass caching.
*
* @since 1.0.9
* @hook sophi_bypass_get_cache
*
* @param {bool} $bypass_cache True or false.
* @param {bool} $page Page name.
* @param {bool} $widget Widget name.
*
* @return {bool} Whether to bypass cache.
*/
$bypass_cache = apply_filters( 'sophi_bypass_get_cache', false, $page, $widget );
if ( ! $bypass_cache ) {
$site_automation_data = get_option( "sophi_site_automation_data_{$page}_{$widget}" );
}
if ( $site_automation_data && ! empty( $this->status['success'] ) ) {
return $site_automation_data;
}
$response = $this->request( $timeout );
if ( is_wp_error( $response ) ) {
$this->set_status(
[
'success' => false,
'message' => $response->get_error_message(),
]
);
$this->retry();
// If we have stale data, use it.
if ( $site_automation_data ) {
return $site_automation_data;
} else {
return [];
}
}
$this->set_status( [ 'success' => true ] );
return $this->process( $response, $bypass_cache );
}
/**
* Retry getting curated data if error occurred.
*/
private function retry() {
$retry_time = $this->status['retry'] < 20 ? 5 * MINUTE_IN_SECONDS : HOUR_IN_SECONDS;
if ( $this->status['retry'] > 50 ) {
return;
}
wp_schedule_single_event( time() + $retry_time, 'sophi_retry_get_curated_data', [ $this->page, $this->widget ] );
}
/**
* Cron call back.
*
* @param string $page Page name.
* @param string $widget Widget name.
*/
public function do_cron( $page, $widget ) {
$this->get( $page, $widget, 3 );
}
/**
* Get request status from database.
*
* @return array
*/
public function get_status() {
return get_transient( "sophi_site_automation_status_{$this->page}_{$this->widget}" );
}
/**
* Set request status. Setting both database option and class attribute.
*
* @param array $data Status array.
*/
private function set_status( $data ) {
$data = wp_parse_args(
$data,
[
'success' => false,
'retry' => 0,
'message' => '',
]
);
if ( empty( $data['success'] ) ) {
$data['retry'] = $this->status['retry'] + 1;
} else {
$data['retry'] = 0;
}
$this->status = $data;
set_transient( "sophi_site_automation_status_{$this->page}_{$this->widget}", $data, $this->get_cache_duration() );
}
/**
* Get curated data from Sophi Site Automation API.
*
* @param float $timeout The request timeout value.
*
* @return mixed WP_Error on failure or body request on success.
*/
private function request( $timeout ) {
$access_token = $this->auth->get_access_token();
if ( is_wp_error( $access_token ) ) {
return $access_token;
}
$args = [
'headers' => [
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $access_token,
],
];
/**
* Filters the arguments used in Sophi HTTP request.
*
* @since 1.0.14
* @hook sophi_request_args
*
* @param {array} $args HTTP request arguments.
* @param {string} $url The request URL.
*
* @return {array} HTTP request arguments.
*/
$args = apply_filters( 'sophi_request_args', $args, $this->api_url );
if ( function_exists( 'vip_safe_wp_remote_get' ) ) {
$result = vip_safe_wp_remote_get( $this->api_url, '', 3, $timeout, 20, $args );
} else {
$args['timeout'] = $timeout;
$result = wp_remote_get( $this->api_url, $args ); // phpcs:ignore
}
/**
* Filters a Sophi HTTP request immediately after the response is received.
*
* @since 1.0.14
* @hook sophi_request_result
*
* @param {array|WP_Error} $result Result of HTTP request.
* @param {array} $args HTTP request arguments.
* @param {string} $url The request URL.
*
* @return {array|WP_Error} Result of HTTP request.
*/
$result = apply_filters( 'sophi_request_result', $result, $args, $this->api_url );
if ( is_wp_error( $result ) ) {
return $result;
}
if ( wp_remote_retrieve_response_code( $result ) !== 200 ) {
return new \WP_Error( wp_remote_retrieve_response_code( $result ), $result['response']['message'] );
}
return json_decode( wp_remote_retrieve_body( $result ), true );
}
/**
* Process response from Sophi.
*
* @param array $response Response of Site Automation API.
* @param bool $bypass_cache Whether to bypass cache or not.
*
* @return array
*/
private function process( $response, $bypass_cache ) {
if ( ! $response ) {
return [];
}
if ( ! $bypass_cache ) {
update_option( "sophi_site_automation_data_{$this->page}_{$this->widget}", $response );
}
return $response;
}
/**
* Prepare Site Automation API URL
*
* @param string $page Page name.
* @param string $widget Widget name.
*
* @return string
*/
private function set_api_url( $page, $widget ) {
$site_automation_url = get_sophi_settings( 'site_automation_url' );
$site_automation_url = untrailingslashit( $site_automation_url );
$host = parse_url( get_home_url(), PHP_URL_HOST );
return sprintf( '%1$s/curatedHosts/%2$s/curator?page=%3$s&widget=%4$s', $site_automation_url, $host, $page, $widget );
}
/**
* Filterable cache duration.
*
* @return int
*/
private function get_cache_duration() {
/**
* Filter Sophi cache duration. Defaults to five minutes.
*
* @since 1.0.0
* @hook sophi_cache_duration
*
* @param {int} $cache_duration Cache duration in seconds. Default 300.
*
* @return {int} Cache duration in seconds.
*/
return apply_filters( 'sophi_cache_duration', 5 * MINUTE_IN_SECONDS );
}
}