first commit
This commit is contained in:
@@ -0,0 +1,253 @@
|
||||
<?php
|
||||
|
||||
class MC4WP_API_V3_Client
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $api_key;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $api_url = 'https://api.mailchimp.com/3.0/';
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $last_response;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $last_request;
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param string $api_key
|
||||
*/
|
||||
public function __construct($api_key)
|
||||
{
|
||||
$this->api_key = $api_key;
|
||||
|
||||
$dash_position = strpos($api_key, '-');
|
||||
if ($dash_position !== false) {
|
||||
$this->api_url = str_replace('//api.', '//' . substr($api_key, $dash_position + 1) . '.api.', $this->api_url);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @param array $args
|
||||
*
|
||||
* @return mixed
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
public function get($resource, array $args = array())
|
||||
{
|
||||
return $this->request('GET', $resource, $args);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @param array $data
|
||||
*
|
||||
* @return mixed
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
public function post($resource, array $data)
|
||||
{
|
||||
return $this->request('POST', $resource, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
public function put($resource, array $data)
|
||||
{
|
||||
return $this->request('PUT', $resource, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @param array $data
|
||||
* @return mixed
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
public function patch($resource, array $data)
|
||||
{
|
||||
return $this->request('PATCH', $resource, $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $resource
|
||||
* @return mixed
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
public function delete($resource)
|
||||
{
|
||||
return $this->request('DELETE', $resource);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
* @param string $resource
|
||||
* @param array $data
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws MC4WP_API_Exception
|
||||
*/
|
||||
private function request($method, $resource, array $data = array())
|
||||
{
|
||||
$this->reset();
|
||||
|
||||
// don't bother if no API key was given.
|
||||
if (empty($this->api_key)) {
|
||||
throw new MC4WP_API_Exception('Missing API key', 001);
|
||||
}
|
||||
|
||||
$method = strtoupper(trim($method));
|
||||
$url = $this->api_url . ltrim($resource, '/');
|
||||
$args = array(
|
||||
'method' => $method,
|
||||
'headers' => $this->get_headers(),
|
||||
'timeout' => 20,
|
||||
'sslverify' => apply_filters('mc4wp_use_sslverify', true),
|
||||
);
|
||||
|
||||
if (! empty($data)) {
|
||||
if (in_array($method, array( 'GET', 'DELETE' ), true)) {
|
||||
$url = add_query_arg($data, $url);
|
||||
} else {
|
||||
$args['headers']['Content-Type'] = 'application/json';
|
||||
$args['body'] = json_encode($data);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the request arguments for all requests generated by this class
|
||||
*
|
||||
* @param array $args
|
||||
*/
|
||||
$args = apply_filters('mc4wp_http_request_args', $args, $url);
|
||||
|
||||
// perform request
|
||||
$response = wp_remote_request($url, $args);
|
||||
|
||||
// store request & response
|
||||
$args['url'] = $url;
|
||||
$this->last_request = $args;
|
||||
$this->last_response = $response;
|
||||
|
||||
// parse response
|
||||
$data = $this->parse_response($response);
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
private function get_headers()
|
||||
{
|
||||
global $wp_version;
|
||||
|
||||
$headers = array(
|
||||
'Authorization' => sprintf('Basic %s', base64_encode('mc4wp:' . $this->api_key)),
|
||||
'User-Agent' => sprintf('mc4wp/%s; WordPress/%s; %s', MC4WP_VERSION, $wp_version, home_url()),
|
||||
);
|
||||
|
||||
// Copy Accept-Language from browser headers
|
||||
if (! empty($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
|
||||
$headers['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
|
||||
}
|
||||
|
||||
return $headers;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array|WP_Error $response
|
||||
*
|
||||
* @return mixed
|
||||
*
|
||||
* @throws MC4WP_API_Connection_Exception|MC4WP_API_Resource_Not_Found_Exception|MC4WP_API_Exception
|
||||
*/
|
||||
private function parse_response($response)
|
||||
{
|
||||
if ($response instanceof WP_Error) {
|
||||
throw new MC4WP_API_Connection_Exception($response->get_error_message(), (int) $response->get_error_code(), $this->last_request);
|
||||
}
|
||||
|
||||
// decode response body
|
||||
$code = (int) wp_remote_retrieve_response_code($response);
|
||||
$message = wp_remote_retrieve_response_message($response);
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
|
||||
// set body to "true" in case Mailchimp returned No Content
|
||||
if ($code < 300 && empty($body)) {
|
||||
$body = 'true';
|
||||
}
|
||||
|
||||
$data = json_decode($body);
|
||||
if ($code >= 400) {
|
||||
// check for akamai errors
|
||||
// {"type":"akamai_error_message","title":"akamai_503","status":503,"ref_no":"Reference Number: 00.950e16c3.1498559813.1450dbe2"}
|
||||
if (is_object($data) && isset($data->type) && $data->type === 'akamai_error_message') {
|
||||
throw new MC4WP_API_Connection_Exception($message, $code, $this->last_request, $this->last_response, $data);
|
||||
}
|
||||
|
||||
if ($code === 404) {
|
||||
throw new MC4WP_API_Resource_Not_Found_Exception($message, $code, $this->last_request, $this->last_response, $data);
|
||||
}
|
||||
|
||||
// mailchimp returned an error..
|
||||
throw new MC4WP_API_Exception($message, $code, $this->last_request, $this->last_response, $data);
|
||||
}
|
||||
|
||||
// throw exception if unable to decode response
|
||||
if ($data === null) {
|
||||
throw new MC4WP_API_Exception($message, $code, $this->last_request, $this->last_response);
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empties all data from previous response
|
||||
*/
|
||||
private function reset()
|
||||
{
|
||||
$this->last_response = null;
|
||||
$this->last_request = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function get_last_response_body()
|
||||
{
|
||||
return wp_remote_retrieve_body($this->last_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function get_last_response_headers()
|
||||
{
|
||||
return wp_remote_retrieve_headers($this->last_response);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array|WP_Error
|
||||
*/
|
||||
public function get_last_response()
|
||||
{
|
||||
return $this->last_response;
|
||||
}
|
||||
}
|
||||
1376
wp-content/plugins/mailchimp-for-wp/includes/api/class-api-v3.php
Normal file
1376
wp-content/plugins/mailchimp-for-wp/includes/api/class-api-v3.php
Normal file
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class MC4WP_API_Connection_Exception extends MC4WP_API_Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Class MC4WP_API_Exception
|
||||
*
|
||||
* @property string $title
|
||||
* @property string $detail
|
||||
* @property array $errors
|
||||
*/
|
||||
class MC4WP_API_Exception extends Exception
|
||||
{
|
||||
/**
|
||||
* @var object
|
||||
*/
|
||||
public $response = array();
|
||||
|
||||
/**
|
||||
* @var object
|
||||
*/
|
||||
public $request = array();
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public $response_data = array();
|
||||
|
||||
/**
|
||||
* MC4WP_API_Exception constructor.
|
||||
*
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param array $request
|
||||
* @param array $response
|
||||
* @param object $data
|
||||
*/
|
||||
public function __construct($message, $code, $request = null, $response = null, $data = null)
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
|
||||
$this->request = $request;
|
||||
$this->response = $response;
|
||||
|
||||
$this->response_data = $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Backwards compatibility for direct property access.
|
||||
* @param string $property
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get($property)
|
||||
{
|
||||
if (in_array($property, array( 'title', 'detail', 'errors' ), true)) {
|
||||
if (! empty($this->response_data) && isset($this->response_data->{$property})) {
|
||||
return $this->response_data->{$property};
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function __toString()
|
||||
{
|
||||
$string = $this->message . '.';
|
||||
|
||||
// add errors from response data returned by Mailchimp
|
||||
if (! empty($this->response_data)) {
|
||||
if (! empty($this->response_data->title) && $this->response_data->title !== $this->getMessage()) {
|
||||
$string .= ' ' . $this->response_data->title . '.';
|
||||
}
|
||||
|
||||
// add detail message
|
||||
if (! empty($this->response_data->detail)) {
|
||||
$string .= ' ' . $this->response_data->detail;
|
||||
}
|
||||
|
||||
// add field specific errors
|
||||
if (! empty($this->response_data->errors) && isset($this->response_data->errors[0]->field)) {
|
||||
// strip off obsolete msg
|
||||
$string = str_replace('For field-specific details, see the \'errors\' array.', '', $string);
|
||||
|
||||
// generate list of field errors
|
||||
$field_errors = array();
|
||||
foreach ($this->response_data->errors as $error) {
|
||||
if (! empty($error->field)) {
|
||||
$field_errors[] = sprintf('- %s : %s', $error->field, $error->message);
|
||||
} else {
|
||||
$field_errors[] = sprintf('- %s', $error->message);
|
||||
}
|
||||
}
|
||||
|
||||
$string .= " \n" . join("\n", $field_errors);
|
||||
}
|
||||
}
|
||||
|
||||
// Add request data
|
||||
if (! empty($this->request) && is_array($this->request)) {
|
||||
$string .= "\n\n" . sprintf("Request: \n%s %s\n", $this->request['method'], $this->request['url']);
|
||||
|
||||
// foreach ( $this->request['headers'] as $key => $value ) {
|
||||
// $string .= sprintf( "%s: %s\n", $key, $value );
|
||||
// }
|
||||
|
||||
if (! empty($this->request['body'])) {
|
||||
$string .= "\n" . $this->request['body'];
|
||||
}
|
||||
}
|
||||
|
||||
// Add response data
|
||||
if (! empty($this->response) && is_array($this->response)) {
|
||||
$response_code = wp_remote_retrieve_response_code($this->response);
|
||||
$response_message = wp_remote_retrieve_response_message($this->response);
|
||||
$response_body = wp_remote_retrieve_body($this->response);
|
||||
$string .= "\n\n" . sprintf("Response: \n%d %s\n%s", $response_code, $response_message, $response_body);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
<?php
|
||||
|
||||
class MC4WP_API_Resource_Not_Found_Exception extends MC4WP_API_Exception
|
||||
{
|
||||
// Thrown when a requested resource does not exist in Mailchimp
|
||||
}
|
||||
Reference in New Issue
Block a user