Guest User

/wp-content/plugins/mailchimp/lib/mailchimp/mailchimp.php

a guest
Dec 2nd, 2016
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.42 KB | None | 0 0
  1. <?php
  2.  
  3. class MailChimp_API {
  4.  
  5.     public $key;
  6.     public $datacenter;
  7.  
  8.     public function __construct($api_key) {
  9.         $api_key = trim($api_key);
  10.         if(!$api_key) {
  11.             throw new Exception(__('Invalid API Key: ' . $api_key));
  12.         }
  13.  
  14.         $this->key        = $api_key;
  15.         $dc               = explode('-', $api_key);
  16.         $this->datacenter = empty($dc[1]) ? 'us1' : $dc[1];
  17.         $this->api_url    = 'https://' . $this->datacenter . '.api.mailchimp.com/3.0/';
  18.         return;
  19.     }
  20.  
  21.     public function get($endpoint, $count=10, $fields=array()) {
  22.         $url = $this->api_url . $endpoint;
  23.        
  24.         $query_params = '';
  25.        
  26.         if($count) {
  27.             $query_params = 'count=' . $count . '&';
  28.         }
  29.  
  30.         if(!empty($fields)) {
  31.             foreach($fields as $field => $value) {
  32.                 $query_params .= $field . '=' . $value . '&';
  33.             }
  34.         }
  35.  
  36.         if( !empty($query_params) ){
  37.             $url .= "?{$query_params}";
  38.         }
  39.  
  40.         $args = array(
  41.             'timeout'     => 5,
  42.             'redirection' => 5,
  43.             'httpversion' => '1.1',
  44.             'user-agent'  => 'MailChimp WordPress Plugin/' . get_bloginfo( 'url' ),
  45.             'headers'     => array("Authorization" => 'apikey ' . $this->key)
  46.         );
  47.  
  48.         $request = wp_remote_get($url, $args);
  49.  
  50.         if(is_array($request) && $request['response']['code'] == 200) {
  51.             return json_decode($request['body'], true);
  52.         } elseif(is_array($request) && $request['response']['code']) {
  53.             $error = json_decode($request['body'], true);
  54.             $error = new WP_Error('mailchimp-get-error', $error['detail']);
  55.             return $error;
  56.         } else {
  57.             return false;
  58.         }
  59.     }
  60.  
  61.     public function post($endpoint, $body, $method='POST') {
  62.         $url = $this->api_url . $endpoint;
  63.        
  64.         $args = array(
  65.             'method' => $method,
  66.             'timeout' => 5,
  67.             'redirection' => 5,
  68.             'httpversion' => '1.1',
  69.             'user-agent'  => 'MailChimp WordPress Plugin/' . get_bloginfo( 'url' ),
  70.             'headers'     => array("Authorization" => 'apikey ' . $this->key),
  71.             'body' => json_encode($body)
  72.         );
  73.         $request = wp_remote_post($url, $args);
  74.  
  75.         if(is_array($request) && $request['response']['code'] == 200) {
  76.             return json_decode($request['body'], true);
  77.         } else {
  78.             if(is_wp_error($request)) {
  79.                 return new WP_Error('mc-subscribe-error', $request->get_error_message());
  80.             }
  81.  
  82.             $body = json_decode($request['body'], true);
  83.             $merges = get_option('mc_merge_vars');
  84.             foreach ($merges as $merge) {
  85.                 if (empty($body['errors'])) {
  86.                     //Email address doesn't come back from the API, so if something's wrong, it's that.
  87.                     $field_name = 'Email Address';
  88.                     $body['errors'][0]['message'] = 'Please fill out a valid email address.';
  89.                 }
  90.                 elseif ( isset($body['errors'][0]['field']) && $merge['tag'] == $body['errors'][0]['field']) {
  91.                     $field_name = $merge['name'];
  92.                 }
  93.             }
  94.             $message = sprintf($field_name . ": " . $body['errors'][0]['message']);
  95.             return new WP_Error('mc-subscribe-error-api', $message);
  96.         }
  97.     }
  98. }
Add Comment
Please, Sign In to add comment