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 |