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' => 15,
|
||||
'sslverify' => apply_filters('mc4wp_use_sslverify', true),
|
||||
);
|
||||
|
||||
if (! empty($data)) {
|
||||
if (in_array($method, array('GET', 'DELETE'))) {
|
||||
$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();
|
||||
$headers['Authorization'] = 'Basic ' . base64_encode('mc4wp:' . $this->api_key);
|
||||
$headers['User-Agent'] = 'mc4wp/' . MC4WP_VERSION . '; WordPress/' . $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);
|
||||
}
|
||||
|
||||
// unable to decode response
|
||||
if (is_null($data)) {
|
||||
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;
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,601 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Takes care of requests to the Mailchimp API (version 2.0, deprecated)
|
||||
*
|
||||
* @access public
|
||||
* @uses WP_HTTP
|
||||
* @since 1.0
|
||||
* @deprecated 4.0
|
||||
*/
|
||||
class MC4WP_API
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string The URL to the Mailchimp API
|
||||
*/
|
||||
protected $api_url = 'https://api.mailchimp.com/2.0/';
|
||||
|
||||
/**
|
||||
* @var string The API key to use
|
||||
*/
|
||||
protected $api_key = '';
|
||||
|
||||
/**
|
||||
* @var string The error message of the latest API request (if any)
|
||||
*/
|
||||
protected $error_message = '';
|
||||
|
||||
/**
|
||||
* @var int The error code of the last API request (if any)
|
||||
*/
|
||||
protected $error_code = 0;
|
||||
|
||||
/**
|
||||
* @var boolean Boolean indicating whether the user is connected with Mailchimp
|
||||
*/
|
||||
protected $connected;
|
||||
|
||||
/**
|
||||
* @var object The full response object of the latest API call
|
||||
*/
|
||||
protected $last_response;
|
||||
|
||||
/**
|
||||
* 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 = 'https://' . substr($api_key, $dash_position + 1) . '.api.mailchimp.com/2.0/';
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show an error message to administrators
|
||||
*
|
||||
* @param string $message
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function show_error($message)
|
||||
{
|
||||
if (! is_admin() || ! current_user_can('manage_options')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (! function_exists('add_settings_error')) {
|
||||
return false;
|
||||
}
|
||||
|
||||
add_settings_error('mc4wp-api', 'mc4wp-api-error', $message, 'error');
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $message
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function show_connection_error($message)
|
||||
{
|
||||
$message .= '<br /><br />' . sprintf('<a href="%s">' . __('Read more about common connectivity issues.', 'mailchimp-for-wp') . '</a>', 'https://kb.mc4wp.com/solving-connectivity-issues/#utm_source=wp-plugin&utm_medium=mailchimp-for-wp&utm_campaign=settings-notice');
|
||||
return $this->show_error($message);
|
||||
}
|
||||
|
||||
/**
|
||||
* Pings the Mailchimp API to see if we're connected
|
||||
*
|
||||
* The result is cached to ensure a maximum of 1 API call per page load
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function is_connected()
|
||||
{
|
||||
if (is_bool($this->connected)) {
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
$result = $this->call('helper/ping');
|
||||
$this->connected = false;
|
||||
|
||||
if (is_object($result)) {
|
||||
|
||||
// Msg key set? All good then!
|
||||
if (! empty($result->msg)) {
|
||||
$this->connected = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
// Uh oh. We got an error back.
|
||||
if (isset($result->error)) {
|
||||
$this->show_error('Mailchimp Error: ' . $result->error);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->connected;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a subscription request to the Mailchimp API
|
||||
*
|
||||
* @param string $list_id The list id to subscribe to
|
||||
* @param string $email The email address to subscribe
|
||||
* @param array $merge_vars Array of extra merge variables
|
||||
* @param string $email_type The email type to send to this email address. Possible values are `html` and `text`.
|
||||
* @param boolean $double_optin Should this email be confirmed via double opt-in?
|
||||
* @param boolean $update_existing Update information if this email is already on list?
|
||||
* @param boolean $replace_interests Replace interest groupings, only if update_existing is true.
|
||||
* @param boolean $send_welcome Send a welcome e-mail, only if double_optin is false.
|
||||
*
|
||||
* @return boolean|string True if success, 'error' if error
|
||||
*/
|
||||
public function subscribe($list_id, $email, array $merge_vars = array(), $email_type = 'html', $double_optin = true, $update_existing = false, $replace_interests = true, $send_welcome = false)
|
||||
{
|
||||
$data = array(
|
||||
'id' => $list_id,
|
||||
'email' => array( 'email' => $email ),
|
||||
'merge_vars' => $merge_vars,
|
||||
'email_type' => $email_type,
|
||||
'double_optin' => $double_optin,
|
||||
'update_existing' => $update_existing,
|
||||
'replace_interests' => $replace_interests,
|
||||
'send_welcome' => $send_welcome
|
||||
);
|
||||
|
||||
$response = $this->call('lists/subscribe', $data);
|
||||
|
||||
if (is_object($response) && isset($response->email)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the Groupings for a given List
|
||||
* @param int $list_id
|
||||
* @return array|boolean
|
||||
*/
|
||||
public function get_list_groupings($list_id)
|
||||
{
|
||||
$result = $this->call('lists/interest-groupings', array( 'id' => $list_id ));
|
||||
if (is_array($result)) {
|
||||
return $result;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $list_ids Array of ID's of the lists to fetch. (optional)
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function get_lists($list_ids = array())
|
||||
{
|
||||
$args = array(
|
||||
'limit' => 100,
|
||||
'sort_field' => 'web',
|
||||
'sort_dir' => 'ASC',
|
||||
);
|
||||
|
||||
// set filter if the $list_ids parameter was set
|
||||
if (count($list_ids) > 0) {
|
||||
$args['filters'] = array(
|
||||
'list_id' => implode(',', $list_ids)
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->call('lists/list', $args);
|
||||
|
||||
if (is_object($result) && isset($result->data)) {
|
||||
return $result->data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the lists an email address is subscribed to
|
||||
*
|
||||
* @param array|string $email
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_lists_for_email($email)
|
||||
{
|
||||
if (is_string($email)) {
|
||||
$email = array(
|
||||
'email' => $email,
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->call('helper/lists-for-email', array( 'email' => $email ));
|
||||
if (! is_array($result)) {
|
||||
return array();
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get lists with their merge_vars for a given array of list id's
|
||||
* @param array $list_ids
|
||||
* @return array|bool
|
||||
*/
|
||||
public function get_lists_with_merge_vars($list_ids)
|
||||
{
|
||||
$result = $this->call('lists/merge-vars', array('id' => $list_ids ));
|
||||
|
||||
if (is_object($result) && isset($result->data)) {
|
||||
return $result->data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the member info for one or multiple emails on a list
|
||||
*
|
||||
* @param string $list_id
|
||||
* @param array $emails
|
||||
* @return array|bool
|
||||
*/
|
||||
public function get_subscriber_info($list_id, $emails)
|
||||
{
|
||||
if (is_string($emails)) {
|
||||
$emails = array( $emails );
|
||||
}
|
||||
|
||||
$result = $this->call(
|
||||
'lists/member-info',
|
||||
array(
|
||||
'id' => $list_id,
|
||||
'emails' => $emails
|
||||
)
|
||||
);
|
||||
|
||||
if (is_object($result) && isset($result->data)) {
|
||||
return $result->data;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an email address is on a given list
|
||||
*
|
||||
* @param string $list_id
|
||||
* @param string $email
|
||||
* @return bool
|
||||
*/
|
||||
public function list_has_subscriber($list_id, $email)
|
||||
{
|
||||
$member_info = $this->get_subscriber_info($list_id, array( array( 'email' => $email ) ));
|
||||
|
||||
if (is_array($member_info) && isset($member_info[0])) {
|
||||
return ($member_info[0]->status === 'subscribed');
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $list_id
|
||||
* @param array|string $email
|
||||
* @param array $merge_vars
|
||||
* @param string $email_type
|
||||
* @param bool $replace_interests
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function update_subscriber($list_id, $email, $merge_vars = array(), $email_type = 'html', $replace_interests = false)
|
||||
{
|
||||
|
||||
// default to using email for updating
|
||||
if (is_string($email)) {
|
||||
$email = array(
|
||||
'email' => $email
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->call(
|
||||
'lists/update-member',
|
||||
array(
|
||||
'id' => $list_id,
|
||||
'email' => $email,
|
||||
'merge_vars' => $merge_vars,
|
||||
'email_type' => $email_type,
|
||||
'replace_interests' => $replace_interests
|
||||
)
|
||||
);
|
||||
|
||||
if (is_object($result)) {
|
||||
if (isset($result->error)) {
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Unsubscribes the given email or luid from the given Mailchimp list
|
||||
*
|
||||
* @param string $list_id
|
||||
* @param array|string $struct
|
||||
* @param bool $delete_member
|
||||
* @param bool $send_goodbye
|
||||
* @param bool $send_notification
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function unsubscribe($list_id, $struct, $send_goodbye = true, $send_notification = false, $delete_member = false)
|
||||
{
|
||||
if (! is_array($struct)) {
|
||||
// assume $struct is an email
|
||||
$struct = array(
|
||||
'email' => $struct
|
||||
);
|
||||
}
|
||||
|
||||
$response = $this->call(
|
||||
'lists/unsubscribe',
|
||||
array(
|
||||
'id' => $list_id,
|
||||
'email' => $struct,
|
||||
'delete_member' => $delete_member,
|
||||
'send_goodbye' => $send_goodbye,
|
||||
'send_notify' => $send_notification
|
||||
)
|
||||
);
|
||||
|
||||
if (is_object($response)) {
|
||||
if (isset($response->complete) && $response->complete) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://apidocs.mailchimp.com/api/2.0/ecomm/order-add.php
|
||||
*
|
||||
* @param array $order_data
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function add_ecommerce_order(array $order_data)
|
||||
{
|
||||
$response = $this->call('ecomm/order-add', array( 'order' => $order_data ));
|
||||
|
||||
if (is_object($response)) {
|
||||
|
||||
// complete means success
|
||||
if (isset($response->complete) && $response->complete) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// if order was already added: great
|
||||
if (isset($response->code, $response->error) && $response->code == 330 && strpos($response->error, 'already been recorded') !== false) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://apidocs.mailchimp.com/api/2.0/ecomm/order-del.php
|
||||
*
|
||||
* @param string $store_id
|
||||
* @param string $order_id
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function delete_ecommerce_order($store_id, $order_id)
|
||||
{
|
||||
$data = array(
|
||||
'store_id' => $store_id,
|
||||
'order_id' => $order_id
|
||||
);
|
||||
|
||||
$response = $this->call('ecomm/order-del', $data);
|
||||
|
||||
if (is_object($response)) {
|
||||
if (isset($response->complete) && $response->complete) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Invalid order (order not existing). Good!
|
||||
if (isset($response->code) && $response->code == 330) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Calls the Mailchimp API
|
||||
*
|
||||
* @uses WP_HTTP
|
||||
*
|
||||
* @param string $method
|
||||
* @param array $data
|
||||
*
|
||||
* @return object|bool
|
||||
*/
|
||||
public function call($method, array $data = array())
|
||||
{
|
||||
$this->empty_last_response();
|
||||
|
||||
// do not make request when no api key was provided.
|
||||
if (empty($this->api_key)) {
|
||||
$this->error_message = "Missing Mailchimp API key.";
|
||||
$this->error_code = 001;
|
||||
return false;
|
||||
}
|
||||
|
||||
// do not make request if helper/ping failed already
|
||||
if ($this->connected === false) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$data['apikey'] = $this->api_key;
|
||||
|
||||
$url = $this->api_url . $method . '.json';
|
||||
$request_args = array(
|
||||
'body' => $data,
|
||||
'timeout' => 10,
|
||||
'headers' => $this->get_headers(),
|
||||
'sslverify' => apply_filters('mc4wp_use_sslverify', true),
|
||||
);
|
||||
|
||||
$response = wp_remote_post($url, $request_args);
|
||||
|
||||
try {
|
||||
$response = $this->parse_response($response);
|
||||
} catch (Exception $e) {
|
||||
$this->error_code = $e->getCode();
|
||||
$this->error_message = $e->getMessage();
|
||||
$this->show_connection_error($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
|
||||
// store response
|
||||
$this->last_response = $response;
|
||||
|
||||
// store error (if any)
|
||||
if (is_object($response)) {
|
||||
if (! empty($response->error)) {
|
||||
$this->error_message = $response->error;
|
||||
}
|
||||
|
||||
// store error code (if any)
|
||||
if (! empty($response->code)) {
|
||||
$this->error_code = (int) $response->code;
|
||||
}
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an error occured in the most recent request
|
||||
* @return boolean
|
||||
*/
|
||||
public function has_error()
|
||||
{
|
||||
return (! empty($this->error_message));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the most recent error message
|
||||
* @return string
|
||||
*/
|
||||
public function get_error_message()
|
||||
{
|
||||
return $this->error_message;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the most recent error code
|
||||
*
|
||||
* @return int
|
||||
*/
|
||||
public function get_error_code()
|
||||
{
|
||||
return $this->error_code;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the most recent response object
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function get_last_response()
|
||||
{
|
||||
return $this->last_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Empties all data from previous response
|
||||
*/
|
||||
private function empty_last_response()
|
||||
{
|
||||
$this->last_response = null;
|
||||
$this->error_code = 0;
|
||||
$this->error_message = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the request headers to send to the Mailchimp API
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function get_headers()
|
||||
{
|
||||
global $wp_version;
|
||||
|
||||
$headers = array(
|
||||
'Accept' => 'application/json',
|
||||
'User-Agent' => 'mc4wp/' . MC4WP_VERSION . '; WordPress/' . $wp_version . '; ' . get_bloginfo('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 object
|
||||
* @throws Exception
|
||||
*/
|
||||
private function parse_response($response)
|
||||
{
|
||||
if (is_wp_error($response)) {
|
||||
throw new Exception('Error connecting to Mailchimp. ' . $response->get_error_message(), (int) $response->get_error_code());
|
||||
}
|
||||
|
||||
// decode response body
|
||||
$body = wp_remote_retrieve_body($response);
|
||||
$data = json_decode($body);
|
||||
if (! is_null($data)) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
$code = (int) wp_remote_retrieve_response_code($response);
|
||||
$message = wp_remote_retrieve_response_message($response);
|
||||
|
||||
if ($code !== 200) {
|
||||
$message = sprintf('The Mailchimp API server returned the following response: <em>%s %s</em>.', $code, $message);
|
||||
|
||||
// check for Akamai firewall response
|
||||
if ($code === 403) {
|
||||
preg_match('/Reference (.*)/i', $body, $matches);
|
||||
|
||||
if (! empty($matches[1])) {
|
||||
$message .= '</strong><br /><br />' . sprintf('This usually means that your server is blacklisted by Mailchimp\'s firewall. Please contact Mailchimp support with the following reference number: %s </strong>', $matches[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Exception($message, $code);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
<?php
|
||||
|
||||
class MC4WP_API_Connection_Exception extends MC4WP_API_Exception
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
<?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' ))) {
|
||||
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" . sprintf('Request: %s %s', $this->request['method'], $this->request['url']);
|
||||
|
||||
if (! empty($this->request['body'])) {
|
||||
$string .= sprintf(' - %s', $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" . sprintf('Response: %d %s - %s', $response_code, $response_message, $response_body);
|
||||
}
|
||||
|
||||
return $string;
|
||||
}
|
||||
}
|
||||
+6
@@ -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