← All changes
|
app/Services/Integrations/MailChimp/MailChimp.php
+205
-172
3.6.41
→
6.2.15
View file →
| @@ -7,8 +7,9 @@ | ||
| 7 | 7 | * MailChimp API v3: http://developer.mailchimp.com |
| 8 | 8 | * This wrapper: https://github.com/drewm/mailchimp-api |
| 9 | 9 | * |
| 10 | 10 | * @author Drew McLellan <[email protected]> |
| 11 | + * | |
| 11 | 12 | * @version 2.4 |
| 12 | 13 | */ |
| 13 | 14 | class MailChimp |
| 14 | 15 | { |
| @@ -14,9 +15,9 @@ | ||
| 14 | 15 | { |
| 15 | 16 | private $api_key; |
| 16 | 17 | private $api_endpoint = 'https://<dc>.api.mailchimp.com/3.0'; |
| 17 | 18 | |
| 18 | - const TIMEOUT = 10; | |
| 19 | + public const TIMEOUT = 10; | |
| 19 | 20 | |
| 20 | 21 | /* SSL Verification |
| 21 | 22 | Read before disabling: |
| 22 | 23 | http://snippets.webaware.com.au/howto/stop-turning-off-curlopt_ssl_verifypeer-and-fix-your-php-config/ |
| @@ -23,33 +24,35 @@ | ||
| 23 | 24 | */ |
| 24 | 25 | public $verify_ssl = true; |
| 25 | 26 | |
| 26 | 27 | private $request_successful = false; |
| 27 | - private $last_error = ''; | |
| 28 | - private $last_response = array(); | |
| 29 | - private $last_request = array(); | |
| 28 | + private $last_error = ''; | |
| 29 | + private $last_response = []; | |
| 30 | + private $last_request = []; | |
| 30 | 31 | |
| 31 | 32 | /** |
| 32 | 33 | * Create a new instance |
| 33 | - * @param string $api_key Your MailChimp API key | |
| 34 | + * | |
| 35 | + * @param string $api_key Your MailChimp API key | |
| 34 | 36 | * @param string $api_endpoint Optional custom API endpoint |
| 37 | + * | |
| 35 | 38 | * @throws \Exception |
| 36 | 39 | */ |
| 37 | 40 | public function __construct($api_key, $api_endpoint = null) |
| 38 | 41 | { |
| 39 | 42 | $this->api_key = $api_key; |
| 40 | - | |
| 41 | - if ($api_endpoint === null) { | |
| 42 | - if (strpos($this->api_key, '-') === false) { | |
| 43 | - throw new \Exception("Invalid MailChimp API key `{$api_key}` supplied."); | |
| 43 | + | |
| 44 | + if (null === $api_endpoint) { | |
| 45 | + if (false === strpos($this->api_key, '-')) { | |
| 46 | + throw new \Exception(sprintf('Invalid Mailchimp API key `%s` supplied.', esc_html($api_key))); | |
| 44 | 47 | } |
| 45 | 48 | list(, $data_center) = explode('-', $this->api_key); |
| 46 | - $this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint); | |
| 49 | + $this->api_endpoint = str_replace('<dc>', $data_center, $this->api_endpoint); | |
| 47 | 50 | } else { |
| 48 | - $this->api_endpoint = $api_endpoint; | |
| 51 | + $this->api_endpoint = $api_endpoint; | |
| 49 | 52 | } |
| 50 | 53 | |
| 51 | - $this->last_response = array('headers' => null, 'body' => null); | |
| 54 | + $this->last_response = ['headers' => null, 'body' => null]; | |
| 52 | 55 | } |
| 53 | 56 | |
| 54 | 57 | /** |
| 55 | 58 | * @return string The url to the API endpoint |
| @@ -58,13 +61,14 @@ | ||
| 58 | 61 | { |
| 59 | 62 | return $this->api_endpoint; |
| 60 | 63 | } |
| 61 | 64 | |
| 62 | - | |
| 63 | 65 | /** |
| 64 | 66 | * Convert an email address into a 'subscriber hash' for identifying the subscriber in a method URL |
| 65 | - * @param string $email The subscriber's email address | |
| 66 | - * @return string Hashed version of the input | |
| 67 | + * | |
| 68 | + * @param string $email The subscriber's email address | |
| 69 | + * | |
| 70 | + * @return string Hashed version of the input | |
| 67 | 71 | */ |
| 68 | 72 | public function subscriberHash($email) |
| 69 | 73 | { |
| 70 | 74 | return md5(strtolower($email)); |
| @@ -71,9 +75,10 @@ | ||
| 71 | 75 | } |
| 72 | 76 | |
| 73 | 77 | /** |
| 74 | 78 | * Was the last request successful? |
| 75 | - * @return bool True for success, false for failure | |
| 79 | + * | |
| 80 | + * @return bool True for success, false for failure | |
| 76 | 81 | */ |
| 77 | 82 | public function success() |
| 78 | 83 | { |
| 79 | 84 | return $this->request_successful; |
| @@ -81,9 +86,10 @@ | ||
| 81 | 86 | |
| 82 | 87 | /** |
| 83 | 88 | * Get the last error returned by either the network transport, or by the API. |
| 84 | 89 | * If something didn't work, this should contain the string describing the problem. |
| 85 | - * @return string|false describing the error | |
| 90 | + * | |
| 91 | + * @return string|false describing the error | |
| 86 | 92 | */ |
| 87 | 93 | public function getLastError() |
| 88 | 94 | { |
| 89 | 95 | return $this->last_error ?: false; |
| @@ -90,9 +96,10 @@ | ||
| 90 | 96 | } |
| 91 | 97 | |
| 92 | 98 | /** |
| 93 | 99 | * Get an array containing the HTTP headers and the body of the API response. |
| 94 | - * @return array Assoc array with keys 'headers' and 'body' | |
| 100 | + * | |
| 101 | + * @return array Assoc array with keys 'headers' and 'body' | |
| 95 | 102 | */ |
| 96 | 103 | public function getLastResponse() |
| 97 | 104 | { |
| 98 | 105 | return $this->last_response; |
| @@ -99,9 +106,10 @@ | ||
| 99 | 106 | } |
| 100 | 107 | |
| 101 | 108 | /** |
| 102 | 109 | * Get an array containing the HTTP headers and the body of the API request. |
| 103 | - * @return array Assoc array | |
| 110 | + * | |
| 111 | + * @return array Assoc array | |
| 104 | 112 | */ |
| 105 | 113 | public function getLastRequest() |
| 106 | 114 | { |
| 107 | 115 | return $this->last_request; |
| @@ -108,14 +116,16 @@ | ||
| 108 | 116 | } |
| 109 | 117 | |
| 110 | 118 | /** |
| 111 | 119 | * Make an HTTP DELETE request - for deleting data |
| 112 | - * @param string $method URL of the API request method | |
| 113 | - * @param array $args Assoc array of arguments (if any) | |
| 114 | - * @param int $timeout Timeout limit for request in seconds | |
| 115 | - * @return array|false Assoc array of API response, decoded from JSON | |
| 120 | + * | |
| 121 | + * @param string $method URL of the API request method | |
| 122 | + * @param array $args Assoc array of arguments (if any) | |
| 123 | + * @param int $timeout Timeout limit for request in seconds | |
| 124 | + * | |
| 125 | + * @return array|false Assoc array of API response, decoded from JSON | |
| 116 | 126 | */ |
| 117 | - public function delete($method, $args = array(), $timeout = self::TIMEOUT) | |
| 127 | + public function delete($method, $args = [], $timeout = self::TIMEOUT) | |
| 118 | 128 | { |
| 119 | 129 | return $this->makeRequest('delete', $method, $args, $timeout); |
| 120 | 130 | } |
| 121 | 131 | |
| @@ -120,14 +130,16 @@ | ||
| 120 | 130 | } |
| 121 | 131 | |
| 122 | 132 | /** |
| 123 | 133 | * Make an HTTP GET request - for retrieving data |
| 124 | - * @param string $method URL of the API request method | |
| 125 | - * @param array $args Assoc array of arguments (usually your data) | |
| 126 | - * @param int $timeout Timeout limit for request in seconds | |
| 127 | - * @return array|false Assoc array of API response, decoded from JSON | |
| 134 | + * | |
| 135 | + * @param string $method URL of the API request method | |
| 136 | + * @param array $args Assoc array of arguments (usually your data) | |
| 137 | + * @param int $timeout Timeout limit for request in seconds | |
| 138 | + * | |
| 139 | + * @return array|false Assoc array of API response, decoded from JSON | |
| 128 | 140 | */ |
| 129 | - public function get($method, $args = array(), $timeout = self::TIMEOUT) | |
| 141 | + public function get($method, $args = [], $timeout = self::TIMEOUT) | |
| 130 | 142 | { |
| 131 | 143 | return $this->makeRequest('get', $method, $args, $timeout); |
| 132 | 144 | } |
| 133 | 145 | |
| @@ -132,14 +144,16 @@ | ||
| 132 | 144 | } |
| 133 | 145 | |
| 134 | 146 | /** |
| 135 | 147 | * Make an HTTP PATCH request - for performing partial updates |
| 136 | - * @param string $method URL of the API request method | |
| 137 | - * @param array $args Assoc array of arguments (usually your data) | |
| 138 | - * @param int $timeout Timeout limit for request in seconds | |
| 139 | - * @return array|false Assoc array of API response, decoded from JSON | |
| 148 | + * | |
| 149 | + * @param string $method URL of the API request method | |
| 150 | + * @param array $args Assoc array of arguments (usually your data) | |
| 151 | + * @param int $timeout Timeout limit for request in seconds | |
| 152 | + * | |
| 153 | + * @return array|false Assoc array of API response, decoded from JSON | |
| 140 | 154 | */ |
| 141 | - public function patch($method, $args = array(), $timeout = self::TIMEOUT) | |
| 155 | + public function patch($method, $args = [], $timeout = self::TIMEOUT) | |
| 142 | 156 | { |
| 143 | 157 | return $this->makeRequest('patch', $method, $args, $timeout); |
| 144 | 158 | } |
| 145 | 159 | |
| @@ -144,14 +158,16 @@ | ||
| 144 | 158 | } |
| 145 | 159 | |
| 146 | 160 | /** |
| 147 | 161 | * Make an HTTP POST request - for creating and updating items |
| 148 | - * @param string $method URL of the API request method | |
| 149 | - * @param array $args Assoc array of arguments (usually your data) | |
| 150 | - * @param int $timeout Timeout limit for request in seconds | |
| 151 | - * @return array|false Assoc array of API response, decoded from JSON | |
| 162 | + * | |
| 163 | + * @param string $method URL of the API request method | |
| 164 | + * @param array $args Assoc array of arguments (usually your data) | |
| 165 | + * @param int $timeout Timeout limit for request in seconds | |
| 166 | + * | |
| 167 | + * @return array|false Assoc array of API response, decoded from JSON | |
| 152 | 168 | */ |
| 153 | - public function post($method, $args = array(), $timeout = self::TIMEOUT) | |
| 169 | + public function post($method, $args = [], $timeout = self::TIMEOUT) | |
| 154 | 170 | { |
| 155 | 171 | return $this->makeRequest('post', $method, $args, $timeout); |
| 156 | 172 | } |
| 157 | 173 | |
| @@ -156,15 +172,18 @@ | ||
| 156 | 172 | } |
| 157 | 173 | |
| 158 | 174 | /** |
| 159 | 175 | * Make an HTTP PUT request - for creating new items |
| 160 | - * @param string $method URL of the API request method | |
| 161 | - * @param array $args Assoc array of arguments (usually your data) | |
| 162 | - * @param int $timeout Timeout limit for request in seconds | |
| 163 | - * @return array|false Assoc array of API response, decoded from JSON | |
| 176 | + * | |
| 177 | + * @param string $method URL of the API request method | |
| 178 | + * @param array $args Assoc array of arguments (usually your data) | |
| 179 | + * @param int $timeout Timeout limit for request in seconds | |
| 180 | + * | |
| 181 | + * @return array|false Assoc array of API response, decoded from JSON | |
| 182 | + * | |
| 164 | 183 | * @throws \Exception |
| 165 | 184 | */ |
| 166 | - public function put($method, $args = array(), $timeout = self::TIMEOUT) | |
| 185 | + public function put($method, $args = [], $timeout = self::TIMEOUT) | |
| 167 | 186 | { |
| 168 | 187 | return $this->makeRequest('put', $method, $args, $timeout); |
| 169 | 188 | } |
| 170 | 189 | |
| @@ -169,81 +188,88 @@ | ||
| 169 | 188 | } |
| 170 | 189 | |
| 171 | 190 | /** |
| 172 | 191 | * Performs the underlying HTTP request. Not very exciting. |
| 173 | - * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete | |
| 174 | - * @param string $method The API method to be called | |
| 175 | - * @param array $args Assoc array of parameters to be passed | |
| 176 | - * @param int $timeout | |
| 192 | + * | |
| 193 | + * @param string $http_verb The HTTP verb to use: get, post, put, patch, delete | |
| 194 | + * @param string $method The API method to be called | |
| 195 | + * @param array $args Assoc array of parameters to be passed | |
| 196 | + * @param int $timeout | |
| 197 | + * | |
| 177 | 198 | * @return array|false Assoc array of decoded result |
| 199 | + * | |
| 178 | 200 | * @throws \Exception |
| 179 | 201 | */ |
| 180 | - private function makeRequest($http_verb, $method, $args = array(), $timeout = self::TIMEOUT) | |
| 202 | + private function makeRequest($http_verb, $method, $args = [], $timeout = self::TIMEOUT) | |
| 181 | 203 | { |
| 182 | - if (!function_exists('curl_init') || !function_exists('curl_setopt')) { | |
| 183 | - throw new \Exception("cURL support is required, but can't be found."); | |
| 184 | - } | |
| 204 | + $timeout = apply_filters('fluentform/mailchimp_api_timeout', $timeout); | |
| 185 | 205 | |
| 186 | 206 | $url = $this->api_endpoint . '/' . $method; |
| 187 | 207 | |
| 188 | 208 | $response = $this->prepareStateForRequest($http_verb, $method, $url, $timeout); |
| 189 | 209 | |
| 190 | - $httpHeader = array( | |
| 191 | - 'Accept: application/vnd.api+json', | |
| 192 | - 'Content-Type: application/vnd.api+json', | |
| 193 | - 'Authorization: apikey ' . $this->api_key | |
| 194 | - ); | |
| 210 | + $headers = [ | |
| 211 | + 'Accept' => 'application/vnd.api+json', | |
| 212 | + 'Content-Type' => 'application/vnd.api+json', | |
| 213 | + 'Authorization' => 'apikey ' . $this->api_key, | |
| 214 | + 'User-Agent' => 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)', | |
| 215 | + ]; | |
| 195 | 216 | |
| 196 | - if (isset($args["language"])) { | |
| 197 | - $httpHeader[] = "Accept-Language: " . $args["language"]; | |
| 217 | + if (isset($args['language'])) { | |
| 218 | + $headers['Accept-Language'] = $args['language']; | |
| 219 | + unset($args['language']); | |
| 198 | 220 | } |
| 199 | 221 | |
| 200 | - $ch = curl_init(); | |
| 201 | - curl_setopt($ch, CURLOPT_URL, $url); | |
| 202 | - curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeader); | |
| 203 | - curl_setopt($ch, CURLOPT_USERAGENT, 'DrewM/MailChimp-API/3.0 (github.com/drewm/mailchimp-api)'); | |
| 204 | - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
| 205 | - curl_setopt($ch, CURLOPT_VERBOSE, true); | |
| 206 | - curl_setopt($ch, CURLOPT_HEADER, true); | |
| 207 | - curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); | |
| 208 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->verify_ssl); | |
| 209 | - curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0); | |
| 210 | - curl_setopt($ch, CURLOPT_ENCODING, ''); | |
| 211 | - curl_setopt($ch, CURLINFO_HEADER_OUT, true); | |
| 222 | + $request_args = [ | |
| 223 | + 'method' => strtoupper($http_verb), | |
| 224 | + 'timeout' => $timeout, | |
| 225 | + 'headers' => $headers, | |
| 226 | + 'sslverify' => $this->verify_ssl, | |
| 227 | + 'httpversion' => '1.0', | |
| 228 | + ]; | |
| 212 | 229 | |
| 213 | - switch ($http_verb) { | |
| 214 | - case 'post': | |
| 215 | - curl_setopt($ch, CURLOPT_POST, true); | |
| 216 | - $this->attachRequestPayload($ch, $args); | |
| 217 | - break; | |
| 230 | + // Handle GET requests - append query string to URL | |
| 231 | + if ('get' === $http_verb) { | |
| 232 | + if (! empty($args)) { | |
| 233 | + $query = http_build_query($args, '', '&'); | |
| 234 | + $url = $url . '?' . $query; | |
| 235 | + } | |
| 236 | + } else { | |
| 237 | + // For POST, PUT, PATCH, DELETE - encode body as JSON | |
| 238 | + $encoded = json_encode($args); | |
| 239 | + $this->last_request['body'] = $encoded; | |
| 240 | + $request_args['body'] = $encoded; | |
| 241 | + } | |
| 218 | 242 | |
| 219 | - case 'get': | |
| 220 | - $query = http_build_query($args, '', '&'); | |
| 221 | - curl_setopt($ch, CURLOPT_URL, $url . '?' . $query); | |
| 222 | - break; | |
| 243 | + $wp_response = wp_remote_request($url, $request_args); | |
| 223 | 244 | |
| 224 | - case 'delete': | |
| 225 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
| 226 | - break; | |
| 245 | + if (is_wp_error($wp_response)) { | |
| 246 | + $this->last_error = $wp_response->get_error_message(); | |
| 247 | + $response['headers'] = null; | |
| 248 | + $response['body'] = null; | |
| 249 | + $this->last_response = $response; | |
| 250 | + return false; | |
| 251 | + } | |
| 227 | 252 | |
| 228 | - case 'patch': | |
| 229 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH'); | |
| 230 | - $this->attachRequestPayload($ch, $args); | |
| 231 | - break; | |
| 253 | + $response_code = wp_remote_retrieve_response_code($wp_response); | |
| 254 | + $response_body = wp_remote_retrieve_body($wp_response); | |
| 255 | + $response_headers = wp_remote_retrieve_headers($wp_response); | |
| 232 | 256 | |
| 233 | - case 'put': | |
| 234 | - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
| 235 | - $this->attachRequestPayload($ch, $args); | |
| 236 | - break; | |
| 237 | - } | |
| 238 | - | |
| 239 | - $responseContent = curl_exec($ch); | |
| 240 | - $response['headers'] = curl_getinfo($ch); | |
| 241 | - $response = $this->setResponseState($response, $responseContent, $ch); | |
| 242 | - $formattedResponse = $this->formatResponse($response); | |
| 257 | + // Build response structure similar to cURL format for compatibility | |
| 258 | + $response['headers'] = [ | |
| 259 | + 'http_code' => $response_code, | |
| 260 | + 'content_type' => wp_remote_retrieve_header($wp_response, 'content-type'), | |
| 261 | + 'total_time' => isset($wp_response['headers']['total_time']) ? $wp_response['headers']['total_time'] : 0, | |
| 262 | + ]; | |
| 243 | 263 | |
| 244 | - curl_close($ch); | |
| 264 | + $response['httpHeaders'] = $this->convertWpHeadersToArray($response_headers); | |
| 265 | + $response['body'] = $response_body; | |
| 245 | 266 | |
| 267 | + // Store request headers for debugging | |
| 268 | + $this->last_request['headers'] = $headers; | |
| 269 | + | |
| 270 | + $formattedResponse = $this->formatResponse($response); | |
| 271 | + | |
| 246 | 272 | $this->determineSuccess($response, $formattedResponse, $timeout); |
| 247 | 273 | |
| 248 | 274 | return $formattedResponse; |
| 249 | 275 | } |
| @@ -248,32 +274,32 @@ | ||
| 248 | 274 | return $formattedResponse; |
| 249 | 275 | } |
| 250 | 276 | |
| 251 | 277 | /** |
| 252 | - * @param string $http_verb | |
| 253 | - * @param string $method | |
| 254 | - * @param string $url | |
| 255 | - * @param integer $timeout | |
| 256 | - */ | |
| 278 | + * @param string $http_verb | |
| 279 | + * @param string $method | |
| 280 | + * @param string $url | |
| 281 | + * @param integer $timeout | |
| 282 | + */ | |
| 257 | 283 | private function prepareStateForRequest($http_verb, $method, $url, $timeout) |
| 258 | 284 | { |
| 259 | 285 | $this->last_error = ''; |
| 260 | - | |
| 286 | + | |
| 261 | 287 | $this->request_successful = false; |
| 262 | 288 | |
| 263 | - $this->last_response = array( | |
| 264 | - 'headers' => null, // array of details from curl_getinfo() | |
| 289 | + $this->last_response = [ | |
| 290 | + 'headers' => null, // array of details from wp_remote_retrieve_response_code() | |
| 265 | 291 | 'httpHeaders' => null, // array of HTTP headers |
| 266 | - 'body' => null // content of the response | |
| 267 | - ); | |
| 292 | + 'body' => null, // content of the response | |
| 293 | + ]; | |
| 268 | 294 | |
| 269 | - $this->last_request = array( | |
| 295 | + $this->last_request = [ | |
| 270 | 296 | 'method' => $http_verb, |
| 271 | 297 | 'path' => $method, |
| 272 | 298 | 'url' => $url, |
| 273 | 299 | 'body' => '', |
| 274 | 300 | 'timeout' => $timeout, |
| 275 | - ); | |
| 301 | + ]; | |
| 276 | 302 | |
| 277 | 303 | return $this->last_response; |
| 278 | 304 | } |
| 279 | 305 | |
| @@ -278,39 +304,40 @@ | ||
| 278 | 304 | } |
| 279 | 305 | |
| 280 | 306 | /** |
| 281 | 307 | * Get the HTTP headers as an array of header-name => header-value pairs. |
| 282 | - * | |
| 308 | + * | |
| 283 | 309 | * The "Link" header is parsed into an associative array based on the |
| 284 | 310 | * rel names it contains. The original value is available under |
| 285 | 311 | * the "_raw" key. |
| 286 | - * | |
| 312 | + * | |
| 287 | 313 | * @param string $headersAsString |
| 314 | + * | |
| 288 | 315 | * @return array |
| 289 | 316 | */ |
| 290 | 317 | private function getHeadersAsArray($headersAsString) |
| 291 | 318 | { |
| 292 | - $headers = array(); | |
| 293 | - | |
| 319 | + $headers = []; | |
| 320 | + | |
| 294 | 321 | foreach (explode("\r\n", $headersAsString) as $i => $line) { |
| 295 | - if ($i === 0) { // HTTP code | |
| 322 | + if (0 === $i) { // HTTP code | |
| 296 | 323 | continue; |
| 297 | 324 | } |
| 298 | - | |
| 325 | + | |
| 299 | 326 | $line = trim($line); |
| 300 | 327 | if (empty($line)) { |
| 301 | 328 | continue; |
| 302 | 329 | } |
| 303 | - | |
| 330 | + | |
| 304 | 331 | list($key, $value) = explode(': ', $line); |
| 305 | - | |
| 306 | - if ($key == 'Link') { | |
| 332 | + | |
| 333 | + if ('Link' == $key) { | |
| 307 | 334 | $value = array_merge( |
| 308 | - array('_raw' => $value), | |
| 335 | + ['_raw' => $value], | |
| 309 | 336 | $this->getLinkHeaderAsArray($value) |
| 310 | 337 | ); |
| 311 | 338 | } |
| 312 | - | |
| 339 | + | |
| 313 | 340 | $headers[$key] = $value; |
| 314 | 341 | } |
| 315 | 342 | |
| 316 | 343 | return $headers; |
| @@ -317,52 +344,78 @@ | ||
| 317 | 344 | } |
| 318 | 345 | |
| 319 | 346 | /** |
| 320 | 347 | * Extract all rel => URL pairs from the provided Link header value |
| 321 | - * | |
| 348 | + * | |
| 322 | 349 | * Mailchimp only implements the URI reference and relation type from |
| 323 | 350 | * RFC 5988, so the value of the header is something like this: |
| 324 | - * | |
| 351 | + * | |
| 325 | 352 | * 'https://us13.api.mailchimp.com/schema/3.0/Lists/Instance.json; rel="describedBy", <https://us13.admin.mailchimp.com/lists/members/?id=XXXX>; rel="dashboard"' |
| 326 | - * | |
| 353 | + * | |
| 327 | 354 | * @param string $linkHeaderAsString |
| 355 | + * | |
| 328 | 356 | * @return array |
| 329 | 357 | */ |
| 330 | 358 | private function getLinkHeaderAsArray($linkHeaderAsString) |
| 331 | 359 | { |
| 332 | - $urls = array(); | |
| 333 | - | |
| 360 | + $urls = []; | |
| 361 | + | |
| 334 | 362 | if (preg_match_all('/<(.*?)>\s*;\s*rel="(.*?)"\s*/', $linkHeaderAsString, $matches)) { |
| 335 | 363 | foreach ($matches[2] as $i => $relName) { |
| 336 | 364 | $urls[$relName] = $matches[1][$i]; |
| 337 | 365 | } |
| 338 | 366 | } |
| 339 | - | |
| 367 | + | |
| 340 | 368 | return $urls; |
| 341 | 369 | } |
| 342 | 370 | |
| 343 | 371 | /** |
| 344 | - * Encode the data and attach it to the request | |
| 345 | - * @param resource $ch cURL session handle, used by reference | |
| 346 | - * @param array $data Assoc array of data to attach | |
| 372 | + * Convert WordPress headers object to array format | |
| 373 | + * | |
| 374 | + * @param \WP_HTTP_Requests_Response|\Requests_Utility_CaseInsensitiveDictionary $wp_headers WordPress headers object | |
| 375 | + * | |
| 376 | + * @return array Associative array of headers | |
| 347 | 377 | */ |
| 348 | - private function attachRequestPayload(&$ch, $data) | |
| 378 | + private function convertWpHeadersToArray($wp_headers) | |
| 349 | 379 | { |
| 350 | - $encoded = json_encode($data); | |
| 351 | - $this->last_request['body'] = $encoded; | |
| 352 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $encoded); | |
| 380 | + $headers = []; | |
| 381 | + | |
| 382 | + if (is_object($wp_headers) && method_exists($wp_headers, 'getAll')) { | |
| 383 | + $wp_headers = $wp_headers->getAll(); | |
| 384 | + } | |
| 385 | + | |
| 386 | + if (is_array($wp_headers)) { | |
| 387 | + foreach ($wp_headers as $key => $value) { | |
| 388 | + if (is_array($value)) { | |
| 389 | + $value = implode(', ', $value); | |
| 390 | + } | |
| 391 | + $headers[$key] = $value; | |
| 392 | + | |
| 393 | + // Handle Link header parsing | |
| 394 | + if ('Link' === $key || 'link' === strtolower($key)) { | |
| 395 | + $headers[$key] = array_merge( | |
| 396 | + ['_raw' => $value], | |
| 397 | + $this->getLinkHeaderAsArray($value) | |
| 398 | + ); | |
| 399 | + } | |
| 400 | + } | |
| 401 | + } | |
| 402 | + | |
| 403 | + return $headers; | |
| 353 | 404 | } |
| 354 | 405 | |
| 355 | 406 | /** |
| 356 | 407 | * Decode the response and format any error messages for debugging |
| 357 | - * @param array $response The response from the curl request | |
| 358 | - * @return array|false The JSON decoded into an array | |
| 408 | + * | |
| 409 | + * @param array $response The response from the WordPress HTTP request | |
| 410 | + * | |
| 411 | + * @return array|false The JSON decoded into an array | |
| 359 | 412 | */ |
| 360 | 413 | private function formatResponse($response) |
| 361 | 414 | { |
| 362 | 415 | $this->last_response = $response; |
| 363 | 416 | |
| 364 | - if (!empty($response['body'])) { | |
| 417 | + if (! empty($response['body'])) { | |
| 365 | 418 | return json_decode($response['body'], true); |
| 366 | 419 | } |
| 367 | 420 | |
| 368 | 421 | return false; |
| @@ -367,39 +420,17 @@ | ||
| 367 | 420 | |
| 368 | 421 | return false; |
| 369 | 422 | } |
| 370 | 423 | |
| 371 | - /** | |
| 372 | - * Do post-request formatting and setting state from the response | |
| 373 | - * @param array $response The response from the curl request | |
| 374 | - * @param string $responseContent The body of the response from the curl request | |
| 375 | - * * @return array The modified response | |
| 376 | - */ | |
| 377 | - private function setResponseState($response, $responseContent, $ch) | |
| 378 | - { | |
| 379 | - if ($responseContent === false) { | |
| 380 | - $this->last_error = curl_error($ch); | |
| 381 | - } else { | |
| 382 | - | |
| 383 | - $headerSize = $response['headers']['header_size']; | |
| 384 | - | |
| 385 | - $response['httpHeaders'] = $this->getHeadersAsArray(substr($responseContent, 0, $headerSize)); | |
| 386 | - $response['body'] = substr($responseContent, $headerSize); | |
| 387 | 424 | |
| 388 | - if (isset($response['headers']['request_header'])) { | |
| 389 | - $this->last_request['headers'] = $response['headers']['request_header']; | |
| 390 | - } | |
| 391 | - } | |
| 392 | - | |
| 393 | - return $response; | |
| 394 | - } | |
| 395 | - | |
| 396 | 425 | /** |
| 397 | 426 | * Check if the response was successful or a failure. If it failed, store the error. |
| 398 | - * @param array $response The response from the curl request | |
| 399 | - * @param array|false $formattedResponse The response body payload from the curl request | |
| 400 | - * @param int $timeout The timeout supplied to the curl request. | |
| 401 | - * @return bool If the request was successful | |
| 427 | + * | |
| 428 | + * @param array $response The response from the WordPress HTTP request | |
| 429 | + * @param array|false $formattedResponse The response body payload from the WordPress HTTP request | |
| 430 | + * @param int $timeout The timeout supplied to the WordPress HTTP request. | |
| 431 | + * | |
| 432 | + * @return bool If the request was successful | |
| 402 | 433 | */ |
| 403 | 434 | private function determineSuccess($response, $formattedResponse, $timeout) |
| 404 | 435 | { |
| 405 | 436 | $status = $this->findHTTPStatus($response, $formattedResponse); |
| @@ -413,10 +444,10 @@ | ||
| 413 | 444 | $this->last_error = sprintf('%d: %s', $formattedResponse['status'], $formattedResponse['detail']); |
| 414 | 445 | return false; |
| 415 | 446 | } |
| 416 | 447 | |
| 417 | - if( $timeout > 0 && $response['headers'] && $response['headers']['total_time'] >= $timeout ) { | |
| 418 | - $this->last_error = sprintf('Request timed out after %f seconds.', $response['headers']['total_time'] ); | |
| 448 | + if ($timeout > 0 && $response['headers'] && isset($response['headers']['total_time']) && $response['headers']['total_time'] >= $timeout) { | |
| 449 | + $this->last_error = sprintf('Request timed out after %f seconds.', $response['headers']['total_time']); | |
| 419 | 450 | return false; |
| 420 | 451 | } |
| 421 | 452 | |
| 422 | 453 | $this->last_error = 'Unknown error, call getLastResponse() to find out what happened.'; |
| @@ -424,19 +455,21 @@ | ||
| 424 | 455 | } |
| 425 | 456 | |
| 426 | 457 | /** |
| 427 | 458 | * Find the HTTP status code from the headers or API response body |
| 428 | - * @param array $response The response from the curl request | |
| 429 | - * @param array|false $formattedResponse The response body payload from the curl request | |
| 430 | - * @return int HTTP status code | |
| 459 | + * | |
| 460 | + * @param array $response The response from the WordPress HTTP request | |
| 461 | + * @param array|false $formattedResponse The response body payload from the WordPress HTTP request | |
| 462 | + * | |
| 463 | + * @return int HTTP status code | |
| 431 | 464 | */ |
| 432 | 465 | private function findHTTPStatus($response, $formattedResponse) |
| 433 | 466 | { |
| 434 | - if (!empty($response['headers']) && isset($response['headers']['http_code'])) { | |
| 467 | + if (! empty($response['headers']) && isset($response['headers']['http_code'])) { | |
| 435 | 468 | return (int) $response['headers']['http_code']; |
| 436 | 469 | } |
| 437 | 470 | |
| 438 | - if (!empty($response['body']) && isset($formattedResponse['status'])) { | |
| 471 | + if (! empty($response['body']) && isset($formattedResponse['status'])) { | |
| 439 | 472 | return (int) $formattedResponse['status']; |
| 440 | 473 | } |
| 441 | 474 | |
| 442 | 475 | return 418; |