| 1 |
<?php |
| 2 |
|
| 3 |
use MyParcelNL\Sdk\src\Support\Arr; |
| 4 |
|
| 5 |
if (! defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} // Exit if accessed directly |
| 8 |
|
| 9 |
if (class_exists('WCPN_Rest')) { |
| 10 |
return; |
| 11 |
} |
| 12 |
|
| 13 |
/** |
| 14 |
* A simple JSON REST request abstraction layer |
| 15 |
*/ |
| 16 |
class WCPN_Rest |
| 17 |
{ |
| 18 |
/** |
| 19 |
* Handle for the current cURL session |
| 20 |
* |
| 21 |
* @var CurlHandle|resource |
| 22 |
*/ |
| 23 |
private $curl = null; |
| 24 |
|
| 25 |
/** |
| 26 |
* Default cURL settings |
| 27 |
* |
| 28 |
* @var |
| 29 |
*/ |
| 30 |
protected $curlDefaults = [ |
| 31 |
// BOOLEANS |
| 32 |
CURLOPT_AUTOREFERER => true, // Update referer on redirects |
| 33 |
CURLOPT_FAILONERROR => false, // Return false on HTTP code > 400 |
| 34 |
CURLOPT_FOLLOWLOCATION => false, // DON'T Follow redirects |
| 35 |
CURLOPT_RETURNTRANSFER => true, |
| 36 |
CURLOPT_FRESH_CONNECT => true, // Don't use cached connection |
| 37 |
CURLOPT_FORBID_REUSE => true, // Close connection |
| 38 |
|
| 39 |
// INTEGERS |
| 40 |
CURLOPT_TIMEOUT => 10, // cURL timeout |
| 41 |
CURLOPT_CONNECTTIMEOUT => 10, // Connection timeout |
| 42 |
|
| 43 |
// STRINGS |
| 44 |
CURLOPT_ENCODING => "", // "identity", "deflate", and "gzip" |
| 45 |
CURLOPT_SSL_VERIFYPEER => false, // if all else fails :) |
| 46 |
]; |
| 47 |
|
| 48 |
/** |
| 49 |
* Basic constructor |
| 50 |
* Checks for cURL and initialize options |
| 51 |
* |
| 52 |
* @return void |
| 53 |
* @throws Exception |
| 54 |
*/ |
| 55 |
public function __construct() |
| 56 |
{ |
| 57 |
if (! function_exists("curl_init")) { |
| 58 |
throw new Exception("cURL is not installed on this system"); |
| 59 |
} |
| 60 |
|
| 61 |
$this->curl = curl_init(); |
| 62 |
if ((! is_resource($this->curl) && ! is_a($this->curl, 'CurlHandle')) || ! isset($this->curl)) { |
| 63 |
throw new Exception("Unable to create cURL session"); |
| 64 |
} |
| 65 |
|
| 66 |
$options = $this->curlDefaults; |
| 67 |
$options[CURLOPT_CAINFO] = |
| 68 |
dirname(__FILE__) . 'lib/ca-bundle.pem'; // Use bundled PEM file to avoid issues with Windows servers |
| 69 |
|
| 70 |
if ((ini_get('open_basedir') == '') AND (! ini_get('safe_mode'))) { |
| 71 |
$options[CURLOPT_FOLLOWLOCATION] = true; |
| 72 |
} |
| 73 |
|
| 74 |
$success = curl_setopt_array($this->curl, $options); |
| 75 |
if ($success !== true) { |
| 76 |
throw new Exception("cURL Error: " . curl_error($this->curl)); |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Closes the current cURL connection |
| 82 |
*/ |
| 83 |
public function close() |
| 84 |
{ |
| 85 |
curl_close($this->curl); |
| 86 |
unset($this->curl); |
| 87 |
} |
| 88 |
|
| 89 |
public function __destruct() |
| 90 |
{ |
| 91 |
$this->close(); |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Returns last error message |
| 96 |
* |
| 97 |
* @return string Error message |
| 98 |
*/ |
| 99 |
public function error() |
| 100 |
{ |
| 101 |
return curl_error($this->curl); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Returns last error code |
| 106 |
* |
| 107 |
* @return int |
| 108 |
*/ |
| 109 |
public function errno() |
| 110 |
{ |
| 111 |
return curl_errno($this->curl); |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* @param $url |
| 116 |
* @param array $headers |
| 117 |
* @param bool $raw |
| 118 |
* |
| 119 |
* @return array |
| 120 |
* @throws Exception |
| 121 |
*/ |
| 122 |
public function get($url, $headers = [], $raw = false) |
| 123 |
{ |
| 124 |
return $this->request($url, "GET", $headers, null, null, $raw); |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* @param $url |
| 129 |
* @param $post |
| 130 |
* @param array $headers |
| 131 |
* @param bool $raw |
| 132 |
* |
| 133 |
* @return array |
| 134 |
* @throws Exception |
| 135 |
*/ |
| 136 |
public function post($url, $post, $headers = [], $raw = false) |
| 137 |
{ |
| 138 |
return $this->request($url, "POST", $headers, $post, null, $raw); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* @param $url |
| 143 |
* @param $body |
| 144 |
* @param array $headers |
| 145 |
* @param bool $raw |
| 146 |
* |
| 147 |
* @return array |
| 148 |
* @throws Exception |
| 149 |
*/ |
| 150 |
public function put($url, $body, $headers = [], $raw = false) |
| 151 |
{ |
| 152 |
return $this->request($url, "PUT", $headers, null, $body, $raw); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @param $url |
| 157 |
* @param array $headers |
| 158 |
* @param bool $raw |
| 159 |
* |
| 160 |
* @return array |
| 161 |
* @throws Exception |
| 162 |
*/ |
| 163 |
public function delete($url, $headers = [], $raw = false) |
| 164 |
{ |
| 165 |
return $this->request($url, "GET", $headers, null, null, $raw); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* @param $url |
| 170 |
* @param string $method |
| 171 |
* @param array $headers |
| 172 |
* @param $post |
| 173 |
* @param null $body |
| 174 |
* @param bool $raw |
| 175 |
* |
| 176 |
* @return array |
| 177 |
* @throws Exception |
| 178 |
*/ |
| 179 |
public function request($url, $method = "GET", $headers = [], $post = '', $body = null, $raw = false) |
| 180 |
{ |
| 181 |
// Set the method and related options |
| 182 |
switch ($method) { |
| 183 |
case "PUT": |
| 184 |
throw new Exception('Can not put PostNL shipment', 500); |
| 185 |
break; |
| 186 |
|
| 187 |
case "POST": |
| 188 |
$response = wp_remote_post($url, ['body' => $post, 'headers' => $headers]); |
| 189 |
break; |
| 190 |
|
| 191 |
case "DELETE": |
| 192 |
throw new Exception('Can not delete PostNL shipment', 500); |
| 193 |
break; |
| 194 |
|
| 195 |
case "GET": |
| 196 |
default: |
| 197 |
$response = wp_remote_get($url, $headers); |
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
// Close any open resource handle |
| 202 |
if (isset($f) && is_resource($f)) { |
| 203 |
@fclose($f); |
| 204 |
} |
| 205 |
|
| 206 |
$status = Arr::get($response, "response.code"); |
| 207 |
$body = Arr::get($response, "body"); |
| 208 |
|
| 209 |
if ($raw !== true) { |
| 210 |
$body = json_decode($body, true); // The second parameter set to true returns objects as associative arrays |
| 211 |
} |
| 212 |
|
| 213 |
if ($status > 400) { |
| 214 |
if ($raw === true) { |
| 215 |
$body = json_decode($body, true); |
| 216 |
} |
| 217 |
|
| 218 |
if (! empty($body["errors"])) { |
| 219 |
$error = $this->parse_errors($body); |
| 220 |
} elseif (! empty($body["message"])) { |
| 221 |
$error = $body["message"]; |
| 222 |
} else { |
| 223 |
$error = "Unknown error"; |
| 224 |
} |
| 225 |
throw new Exception($error, $status); |
| 226 |
} |
| 227 |
|
| 228 |
return ["code" => $status, "body" => $body, "headers" => Arr::get($response, "headers")]; |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* @param $body |
| 233 |
* |
| 234 |
* @return mixed|string |
| 235 |
*/ |
| 236 |
public function parse_errors($body) |
| 237 |
{ |
| 238 |
$errors = $body['errors']; |
| 239 |
$message = isset($body['message']) ? $body['message'] : ''; |
| 240 |
|
| 241 |
$parsed_errors = []; |
| 242 |
foreach ($errors as $error) { |
| 243 |
$code = isset($error['code']) ? $error['code'] : ''; |
| 244 |
|
| 245 |
if (isset($error['human']) && is_array($error['human'])) { |
| 246 |
foreach ($error['human'] as $key => $human_error) { |
| 247 |
$parsed_errors[$code] = "{$human_error} (<strong>Code {$code}</strong>)"; |
| 248 |
} |
| 249 |
} elseif (isset($error['message'])) { |
| 250 |
$parsed_errors[$code] = "{$error['message']} (<strong>Code {$code}</strong>)"; |
| 251 |
} else { |
| 252 |
$parsed_errors[$code] = "{$message} (<strong>Code {$code}</strong>)"; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
if (count($parsed_errors) == 1) { |
| 257 |
$html = array_shift($parsed_errors); |
| 258 |
} else { |
| 259 |
foreach ($parsed_errors as &$parsed_error) { |
| 260 |
$parsed_error = "<li>{$parsed_error}</li>"; |
| 261 |
} |
| 262 |
$html = sprintf("<ul>%s</ul>", implode("\n", $parsed_errors)); |
| 263 |
} |
| 264 |
|
| 265 |
return $html; |
| 266 |
} |
| 267 |
} |
| 268 |
|