setKey($api_key); } function checkKey() { $result = wp_remote_post( $this->url_me, $this->request(array('site' => home_url())) ); $code = wp_remote_retrieve_response_code($result); switch($code) { case 200: case 402: $body = json_decode(wp_remote_retrieve_body($result), true); break; case 401: $body = wp_remote_retrieve_body($result); break; default: $body = null; break; } return array('code' => $code, 'data' => $body); } function sendMessages($message_body) { $result = wp_remote_post( $this->url_messages, $this->request($message_body) ); if(is_wp_error($result)) { return array( 'status' => self::SENDING_STATUS_CONNECTION_ERROR, 'message' => $result->get_error_message() ); } $response_code = wp_remote_retrieve_response_code($result); if($response_code !== 201) { $response = (wp_remote_retrieve_body($result)) ? wp_remote_retrieve_body($result) : wp_remote_retrieve_response_message($result); return array( 'status' => self::SENDING_STATUS_SEND_ERROR, 'message' => $response, 'code' => $response_code ); } return array('status' => self::SENDING_STATUS_OK); } function checkBounces(array $emails) { $result = wp_remote_post( $this->url_bounces, $this->request($emails) ); if(wp_remote_retrieve_response_code($result) === 200) { return json_decode(wp_remote_retrieve_body($result), true); } return false; } function updateSubscriberCount($count) { $result = wp_remote_post( $this->url_stats, $this->request(array('subscriber_count' => (int)$count), 'PUT') ); if(wp_remote_retrieve_response_code($result) === 204) { return true; } return false; } function setKey($api_key) { $this->api_key = $api_key; } function getKey() { return $this->api_key; } private function auth() { return 'Basic ' . base64_encode('api:' . $this->api_key); } private function request($body, $method = 'POST') { return array( 'timeout' => 10, 'httpversion' => '1.0', 'method' => $method, 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => $this->auth() ), 'body' => json_encode($body) ); } }