*/ class Activetrail_Api { private $credentials = null; /** * HTTP status code of the last request made through this instance. A bad/expired * token can come back as a non-2xx response with an empty or non-JSON body, so * callers that need to know "did this actually work" should check this rather * than only inspecting the response body. */ public $last_response_code = null; public function __construct($credentials = null) { if (!$credentials) { wp_die('Error Initializing ActiveTrail API Credentials'); } $this->credentials = $credentials; } public function get_contact_fields() { $url = ACTIVETRAIL_API_URL . AT_ENDPOINT_GET_CONTACT_FIELDS; $args = array( 'headers' => array( 'Authorization' => 'Basic '. $this->credentials['app_token_id'], 'Content-Type' => 'application/json' ) ); $response = wp_remote_get($url, $args); $this->last_response_code = is_wp_error($response) ? null : wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); return $body; } public function get_contact($data = array()) { $url = ACTIVETRAIL_API_URL . AT_ENDPOINT_CONTACTS; $url = sprintf("%s?%s", $url, http_build_query($data)); $args = array( 'headers' => array( 'Authorization' => 'Basic '. $this->credentials['app_token_id'], 'Content-Type' => 'application/json' ) ); $response = wp_remote_get( $url, $args ); $this->last_response_code = is_wp_error($response) ? null : wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body( $response ); return $body; } public function import_contacts($data) { $url = ACTIVETRAIL_API_URL . AT_ENDPOINT_CONTACTS_IMPORT; $data_string = ''; if ($data) { $data_string = json_encode($data); } $args = array( 'headers' => array( 'Authorization' => 'Basic '. $this->credentials['app_token_id'], 'Content-Type' => 'application/json; charset=utf-8', 'Content-Length' => strlen($data_string) ), 'method' => 'POST', 'body' => $data_string, 'timeout' => '5', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'cookies' => array() ); $response = wp_remote_post($url, $args); $body = wp_remote_retrieve_body($response); return $body; } public function import_contacts_external($data) { $url = ACTIVETRAIL_API_URL . AT_ENDPOINT_CONTACTS_IMPORT_EXTERNAL; $data_string = ''; if ($data) { $data_string = json_encode($data); } $args = array( 'headers' => array( 'Authorization' => 'Basic '. $this->credentials['app_token_id'], 'Content-Type' => 'application/json; charset=utf-8', 'Content-Length' => strlen($data_string) ), 'method' => 'POST', 'body' => $data_string, 'timeout' => '5', 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'cookies' => array() ); $response = wp_remote_post($url, $args); $body = wp_remote_retrieve_body($response); return $body; } public function get_groups($search_term = '', $page = 1, $limit = 20) { return $this->get_paged_list(AT_ENDPOINT_GROUPS, $search_term, $page, $limit); } public function get_mailing_lists($search_term = '', $page = 1, $limit = 20) { return $this->get_paged_list(AT_ENDPOINT_MAILING_LISTS, $search_term, $page, $limit); } private function get_paged_list($endpoint, $search_term, $page, $limit) { $url = ACTIVETRAIL_API_URL . $endpoint; $url = sprintf('%s?%s', $url, http_build_query(array( 'SearchTerm' => $search_term, 'Page' => $page, 'Limit' => $limit, ))); $args = array( 'headers' => array( 'Authorization' => 'Basic '. $this->credentials['app_token_id'], 'Content-Type' => 'application/json' ) ); $response = wp_remote_get($url, $args); $this->last_response_code = is_wp_error($response) ? null : wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); return $body; } }