PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/mailchimp-for-wp/includes/api/class-api-v3-client.php

https://bitbucket.org/viruscommdev/jboivinavocat
PHP | 230 lines | 105 code | 40 blank | 85 comment | 15 complexity | 4752f88e9dab0619a1ccfd62016515fd MD5 | raw file
Possible License(s): GPL-3.0, GPL-2.0, MIT, Apache-2.0
  1. <?php
  2. class MC4WP_API_v3_Client {
  3. /**
  4. * @var string
  5. */
  6. private $api_key;
  7. /**
  8. * @var string
  9. */
  10. private $api_url = 'https://api.mailchimp.com/3.0/';
  11. /**
  12. * @var array
  13. */
  14. private $last_response;
  15. /**
  16. * @var array
  17. */
  18. private $last_request;
  19. /**
  20. * Constructor
  21. *
  22. * @param string $api_key
  23. */
  24. public function __construct( $api_key ) {
  25. $this->api_key = $api_key;
  26. $dash_position = strpos( $api_key, '-' );
  27. if( $dash_position !== false ) {
  28. $this->api_url = str_replace( '//api.', '//' . substr( $api_key, $dash_position + 1 ) . ".api.", $this->api_url );
  29. }
  30. }
  31. /**
  32. * @param string $resource
  33. * @param array $args
  34. *
  35. * @return mixed
  36. */
  37. public function get( $resource, array $args = array() ) {
  38. return $this->request( 'GET', $resource, $args );
  39. }
  40. /**
  41. * @param string $resource
  42. * @param array $data
  43. *
  44. * @return mixed
  45. */
  46. public function post( $resource, array $data ) {
  47. return $this->request( 'POST', $resource, $data );
  48. }
  49. /**
  50. * @param string $resource
  51. * @param array $data
  52. * @return mixed
  53. */
  54. public function put( $resource, array $data ) {
  55. return $this->request( 'PUT', $resource, $data );
  56. }
  57. /**
  58. * @param string $resource
  59. * @param array $data
  60. * @return mixed
  61. */
  62. public function patch( $resource, array $data ) {
  63. return $this->request( 'PATCH', $resource, $data );
  64. }
  65. /**
  66. * @param string $resource
  67. * @return mixed
  68. */
  69. public function delete( $resource ) {
  70. return $this->request( 'DELETE', $resource );
  71. }
  72. /**
  73. * @param string $method
  74. * @param string $resource
  75. * @param array $data
  76. *
  77. * @return mixed
  78. *
  79. * @throws MC4WP_API_Exception
  80. */
  81. private function request( $method, $resource, array $data = array() ) {
  82. $this->reset();
  83. // don't bother if no API key was given.
  84. if( empty( $this->api_key ) ) {
  85. throw new MC4WP_API_Exception( "Missing API key", 001 );
  86. }
  87. $url = $this->api_url . ltrim( $resource, '/' );
  88. $args = array(
  89. 'url' => $url,
  90. 'method' => $method,
  91. 'headers' => $this->get_headers(),
  92. 'timeout' => 10,
  93. 'sslverify' => apply_filters( 'mc4wp_use_sslverify', true ),
  94. );
  95. if( ! empty( $data ) ) {
  96. if( in_array( $method, array( 'GET', 'DELETE' ) ) ) {
  97. $url = add_query_arg( $data, $url );
  98. } else {
  99. $args['body'] = json_encode( $data );
  100. }
  101. }
  102. // perform request
  103. $response = wp_remote_request( $url, $args );
  104. // store request & response
  105. $this->last_request = $args;
  106. $this->last_response = $response;
  107. // parse response
  108. $data = $this->parse_response( $response );
  109. return $data;
  110. }
  111. /**
  112. * @return array
  113. */
  114. private function get_headers() {
  115. global $wp_version;
  116. $headers = array();
  117. $headers['Authorization'] = 'Basic ' . base64_encode( 'mc4wp:' . $this->api_key );
  118. $headers['Accept'] = 'application/json';
  119. $headers['Content-Type'] = 'application/json';
  120. $headers['User-Agent'] = 'mc4wp/' . MC4WP_VERSION . '; WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' );
  121. // Copy Accept-Language from browser headers
  122. if( ! empty( $_SERVER['HTTP_ACCEPT_LANGUAGE'] ) ) {
  123. $headers['Accept-Language'] = $_SERVER['HTTP_ACCEPT_LANGUAGE'];
  124. }
  125. return $headers;
  126. }
  127. /**
  128. * @param array|WP_Error $response
  129. *
  130. * @return mixed
  131. *
  132. * @throws MC4WP_API_Exception
  133. */
  134. private function parse_response( $response ) {
  135. if( $response instanceof WP_Error ) {
  136. throw new MC4WP_API_Connection_Exception( $response->get_error_message(), (int) $response->get_error_code() );
  137. }
  138. // decode response body
  139. $code = (int) wp_remote_retrieve_response_code( $response );
  140. $message = wp_remote_retrieve_response_message( $response );
  141. $body = wp_remote_retrieve_body( $response );
  142. // set body to "true" in case MailChimp returned No Content
  143. if( $code < 300 && empty( $body ) ) {
  144. $body = "true";
  145. }
  146. $data = json_decode( $body );
  147. if( $code >= 400 ) {
  148. // check for akamai errors
  149. // {"type":"akamai_error_message","title":"akamai_503","status":503,"ref_no":"Reference Number: 00.950e16c3.1498559813.1450dbe2"}
  150. if( is_object( $data ) && isset( $data->type ) && $data->type === 'akamai_error_message' ) {
  151. throw new MC4WP_API_Connection_Exception( $message, $code, $this->last_request, $this->last_response, $data );
  152. }
  153. if( $code === 404 ) {
  154. throw new MC4WP_API_Resource_Not_Found_Exception( $message, $code, $this->last_request, $this->last_response, $data );
  155. }
  156. // mailchimp returned an error..
  157. throw new MC4WP_API_Exception( $message, $code, $this->last_request, $this->last_response, $data );
  158. }
  159. if( ! is_null( $data ) ) {
  160. return $data;
  161. }
  162. // unable to decode response
  163. throw new MC4WP_API_Exception( $message, $code, $this->last_request, $this->last_response );
  164. }
  165. /**
  166. * Empties all data from previous response
  167. */
  168. private function reset() {
  169. $this->last_response = null;
  170. $this->last_request = null;
  171. }
  172. /**
  173. * @return string
  174. */
  175. public function get_last_response_body() {
  176. return wp_remote_retrieve_body( $this->last_response );
  177. }
  178. /**
  179. * @return array
  180. */
  181. public function get_last_response_headers() {
  182. return wp_remote_retrieve_headers( $this->last_response );
  183. }
  184. /**
  185. * @return array|WP_Error
  186. */
  187. public function get_last_response() {
  188. return $this->last_response;
  189. }
  190. }