PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 1.4.0
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v1.4.0
4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / app / src / Request / RequestService.php
surecart / app / src / Request Last commit date
RequestCacheService.php 3 years ago RequestService.php 3 years ago RequestServiceProvider.php 3 years ago
RequestService.php
395 lines
1 <?php
2
3 namespace SureCart\Request;
4
5 use SureCart\Support\Errors\ErrorsService;
6
7 /**
8 * Provide api request functionality.
9 */
10 class RequestService {
11 /**
12 * Has this been cached yet for the request?
13 *
14 * @var boolean
15 */
16 protected static $cached = false;
17
18 /**
19 * Undocumented variable
20 *
21 * @var string
22 */
23 protected $token = '';
24
25 /**
26 * Request URL
27 *
28 * @var string
29 */
30 protected $base_url = '';
31
32 /**
33 * The base path for the request.
34 *
35 * @var string
36 */
37 protected $base_path;
38
39 /**
40 * Errors service container
41 *
42 * @var \SureCart\Support\Errors\ErrorsService;
43 */
44 protected $errors_service;
45
46 /**
47 * What type of cached request is this.
48 *
49 * @var string|null
50 */
51 protected $cache_status = 'none';
52
53 /**
54 * Constructor.
55 *
56 * @param string $token The rest api base path.
57 */
58 public function __construct( $token = '', $base_path = '/v1', $errors_service = null ) {
59 // error handing service.
60 $this->errors_service = $errors_service ? $errors_service : new ErrorsService();
61 // set the token.
62 $this->token = $token;
63 // set the base path and url.
64 $this->base_path = $base_path;
65 $this->base_url = $this->getBaseUrl();
66 }
67
68 /**
69 * Set the API token on the fly.
70 *
71 * @param string $token API token.
72 *
73 * @return $this
74 */
75 public function setToken( $token ) {
76 $this->token = $token;
77 return $this;
78 }
79
80 /**
81 * Get the base url.
82 */
83 public function getBaseUrl() {
84 return untrailingslashit( SURECART_APP_URL ) . trailingslashit( $this->base_path );
85 }
86
87 /**
88 * Should we get a cached request?
89 *
90 * @return boolean
91 */
92 public function shouldFindCache( $cachable, $cache_key, $args = [] ) {
93 // only for fetch requests.
94 if ( isset( $args['method'] ) && 'GET' !== $args['method'] ) {
95 return false;
96 }
97
98 // if the args are set, then do what they say.
99 if ( isset( $args['query']['cached'] ) ) {
100 return (bool) $args['query']['cached'];
101 }
102
103 // don't cache edit context.
104 if ( isset( $args['query']['context'] ) && 'edit' === $args['query']['context'] ) {
105 return false;
106 }
107
108 return (bool) $cachable && $cache_key;
109 }
110
111 /**
112 * Respond to the request.
113 *
114 * @param array $response Reponse data.
115 * @param array $args Request arguments.
116 * @param string $endpoint The endpoint to request.
117 *
118 * @return array Response data.
119 */
120 public function respond( $response, $args, $endpoint ) {
121 if ( is_array( $response ) ) {
122 foreach ( $response as $item ) {
123 $item->cache_status = $this->cache_status;
124 }
125 }
126
127 if ( is_object( $response ) ) {
128 $response->cache_status = $this->cache_status;
129 }
130
131 return apply_filters( 'surecart/request/response', $response, $args, $endpoint );
132 }
133
134 /**
135 * Set the response cache status.
136 *
137 * @param object $response Response object.
138 * @param string $status The response status.
139 *
140 * @return void
141 */
142 public function setResponseCacheStatus( $response, $status ) {
143 if ( is_array( $response ) ) {
144 foreach ( $response as $item ) {
145 $item->cached = $status;
146 }
147 } elseif ( is_object( $response ) ) {
148 $response->cached = $status;
149 }
150
151 return $response;
152 }
153
154 /**
155 * Make the request
156 *
157 * @param string $endpoint Endpoint to request.
158 * @param array $args Arguments for request.
159 * @param boolean $cachable Should this request be cached.
160 * @param string $cache_key The cache key to use.
161 *
162 * @return mixed
163 */
164 public function makeRequest( $endpoint, $args = [], $cachable = false, $cache_key = '' ) {
165 // use the cache service for this request.
166 $cache = new RequestCacheService( $endpoint, $args, $cache_key );
167
168 // check if we should get a cached version of this.
169 if ( $this->shouldFindCache( $cachable, $cache_key, $args ) ) {
170 // get from cache.
171 $response_body = $cache->getTransientCache();
172 // we have a cached response.
173 if ( false !== $response_body ) {
174 $this->cache_status = 'transient';
175 return $this->respond( $response_body, $args, $endpoint );
176 }
177 }
178
179 // make the uncached request.
180 $response_body = $this->makeUncachedRequest( $endpoint, $args );
181
182 if ( is_wp_error( $response_body ) ) {
183 return $response_body;
184 }
185
186 // set in object cache.
187 $cache->setCache( $response_body, 'object' );
188 if ( (bool) $cachable && $cache_key ) {
189 $cache->setCache( $response_body, 'transient' );
190 }
191
192 // return response.
193 return $this->respond( $response_body, $args, $endpoint );
194 }
195
196 /**
197 * Make the uncached request.
198 *
199 * @param string $endpoint Endpoint to request.
200 * @param array $args Arguments for request.
201 *
202 * @return mixed
203 */
204 public function makeUncachedRequest( $endpoint, $args = [] ) {
205 // make sure we send json.
206 if ( empty( $args['headers']['Content-Type'] ) ) {
207 $args['headers']['Content-Type'] = 'application/json';
208 }
209
210 // add auth.
211 if ( empty( $args['headers']['Authorization'] ) ) {
212 $args['headers']['Authorization'] = "Bearer $this->token";
213 }
214
215 // add version header.
216 $args['headers']['X-SURECART-WP-PLUGIN-VERSION'] = \SureCart::plugin()->version();
217
218 // add referer header.
219 if ( isset( $_SERVER['HTTP_REFERER'] ) ) {
220 $args['headers']['X-SURECART-REFERRER'] = esc_url_raw( $_SERVER['HTTP_REFERER'] );
221 }
222
223 // parse args.
224 $args = wp_parse_args(
225 $args,
226 [
227 'timeout' => 20,
228 'sslverify' => true,
229 ]
230 );
231
232 // filter args and endpoint.
233 $args = apply_filters( 'surecart/request/args', $args, $endpoint );
234 $endpoint = apply_filters( 'surecart/request/endpoint', $endpoint, $args );
235
236 // make url.
237 $url = trailingslashit( $this->base_url ) . untrailingslashit( $endpoint );
238
239 // add query args.
240 if ( ! empty( $args['query'] ) ) {
241 $url = add_query_arg( $this->parseArgs( $args['query'] ), $url );
242 $url = preg_replace( '/%5B[0-9]+%5D/', '%5B%5D', $url );
243 unset( $args['query'] );
244 }
245
246 // json encode body.
247 if ( ! empty( $args['body'] ) ) {
248 if ( 'application/json' === $args['headers']['Content-Type'] ) {
249 $args['body'] = wp_json_encode( $this->parseArgs( $args['body'] ) );
250 }
251 }
252
253 // make request.
254 $response = $this->remoteRequest( $url, $args );
255
256 // bail early if it's a wp_error.
257 if ( is_wp_error( $response ) ) {
258 return $response;
259 }
260
261 $response_code = wp_remote_retrieve_response_code( $response );
262 $response_body = wp_remote_retrieve_body( $response );
263 $admin_notice = (array) wp_remote_retrieve_header( $response, 'X-SURECART-WP-ADMIN-NOTICE' );
264
265 if ( $admin_notice ) {
266 // we don't care if this fails.
267 try {
268 \SureCart::notices()->showResponseNotice( $admin_notice );
269 } catch ( \Exception $e ) {
270 error_log( $e->getMessage() );
271 }
272 }
273
274 // check for errors.
275 if ( ! in_array( $response_code, [ 200, 201 ], true ) ) {
276 error_log( print_r( $response_body, 1 ) );
277 error_log( print_r( $url, 1 ) );
278 error_log( print_r( $args, 1 ) );
279 $body = json_decode( $response_body, true );
280 if ( is_string( $body ) ) {
281 return new \WP_Error( 'error', $response_body );
282 }
283 return $this->errors_service->translate( $body, $response_code );
284 }
285
286 return json_decode( $response_body );
287 }
288
289 /**
290 * Make the remote request.
291 *
292 * @param string $url The url to request.
293 * @param array $args The args to pass to the request.
294 *
295 * @return mixed
296 */
297 public function remoteRequest( $url, $args = [] ) {
298 return wp_remote_request( esc_url_raw( $url ), $args );
299 }
300
301 /**
302 * Make a get request
303 *
304 * @param string $endpoint Endpoint for the request.
305 * @param array $args Request arguments.
306 *
307 * @return mixed
308 */
309 public function get( $endpoint, $args = [] ) {
310 $args['method'] = 'GET';
311 return $this->makeRequest( $endpoint, $args );
312 }
313
314 /**
315 * Make a post request
316 *
317 * @param string $endpoint Endpoint for the request.
318 * @param array $args Request arguments.
319 *
320 * @return mixed
321 */
322 public function post( $endpoint, $args = [] ) {
323 $args['method'] = 'POST';
324 return $this->makeRequest( $endpoint, $args );
325 }
326
327 /**
328 * Make a put request
329 *
330 * @param string $endpoint Endpoint for the request.
331 * @param array $args Request arguments.
332 *
333 * @return mixed
334 */
335 public function put( $endpoint, $args = [] ) {
336 $args['method'] = 'PUT';
337 return $this->makeRequest( $endpoint, $args );
338 }
339
340 /**
341 * Make a patch request
342 *
343 * @param string $endpoint Endpoint for the request.
344 * @param array $args Request arguments.
345 *
346 * @return mixed
347 */
348 public function patch( $endpoint, $args = [] ) {
349 $args['method'] = 'PATCH';
350 return $this->makeRequest( $endpoint, $args );
351 }
352
353 /**
354 * Make a delete request
355 *
356 * @param string $endpoint Endpoint for the request.
357 * @param array $args Request arguments.
358 *
359 * @return mixed
360 */
361 public function delete( $endpoint, $args = [] ) {
362 $args['method'] = 'DELETE';
363 return $this->makeRequest( $endpoint, $args );
364 }
365
366 /**
367 * Removes empty args
368 *
369 * @param array $args Array of arguments.
370 */
371 protected function parseArgs( $args = [] ) {
372 if ( ! is_array( $args ) ) {
373 return $args;
374 }
375 foreach ( $args as $key => $arg ) {
376 // unset null.
377 if ( null === $arg ) {
378 unset( $args[ $key ] );
379 }
380
381 // filter out wp params.
382 if ( in_array( $key, [ 'locale', 'rest_route' ], true ) ) {
383 unset( $args[ $key ] );
384 }
385
386 // convert bool to int to prevent getting unset.
387 if ( is_bool( $arg ) ) {
388 $args[ $key ] = $arg ? 1 : 0;
389 }
390 }
391
392 return $args;
393 }
394 }
395