private function refreshAccessToken() { $base_url = "https://api.hubapi.com/auth/v1/refresh"; $refresh_token = get_option('hs_refresh_token'); $params = array('method' => 'POST', 'body' => array('refresh_token' => $refresh_token, 'client_id' => self::$clientId, 'grant_type' => 'refresh_token')); $result = wp_remote_post($base_url, $params); if (is_wp_error($result)) { $error_string = $result->get_error_message(); WPHubspotLogging::log('hsWordpress: error refreshing access token ' . $error_string); update_option('hs_authorized', 'fail'); } elseif ($result and $result['response']['code'] == 200) { WPHubspotLogging::log('hsWordpress: success refreshing access token '); $body = json_decode($result['body']); $this->_accessToken = $body->access_token; $access_token = array('token' => $body->access_token, 'expires' => time() + $body->expires_in); update_option('hs_access_token', $access_token); update_option('hs_refresh_token', $body->refresh_token); update_option('hs_authorized', 'ok'); return true; } elseif ($result and $result['response']['code'] == 401) { WPHubspotLogging::log('hsWordpress: 401 code from auth service while refreshing '); update_option('hs_authorized', 'fail'); } return false; }
function hs_process_batch_request() { $accessToken = WPHubspotOauth::getAccessToken(); $url = 'https://api.hubapi.com/externalblogs/v1/blog-posts?access_token=' . $accessToken; $body = 'blogUrl=' . urlencode(site_url()) . '&platform=' . urlencode('WordPress Plugin v' . HUBSPOT_PLUGIN_VERSION); foreach ($this->posts_to_send as $post) { foreach ($post as $key => $value) { $body .= "&{$key}=" . urlencode($value); } } $args = array('body' => $body); $result = wp_remote_post($url, $args); if (is_wp_error($result)) { $error_message = $result->get_error_message(); WPHubspotLogging::log("hsWordpress: error sending blog metadata" . $error_message); } $response_code = wp_remote_retrieve_response_code($result); if ($response_code == '') { WPHubspotLogging::log("hsWordpress: error sending blog metadata, response code was blank"); $this->posts_to_send = array(); return false; } if ($response_code == 401) { $this->posts_to_send = array(); WPHubspotOauth::emptyAccessToken(); WPHubspotLogging::log("hsWordpress: error sending blog metadata. Response code was 401, clearing access token"); return false; } if ($response_code == 400 or $response_code == 404 or $response_code == 500) { $this->posts_to_send = array(); WPHubspotLogging::log("hsWordpress: error sending blog metadata. Response code" . $response_code); return false; } $this->posts_to_send = array(); return true; }
function publish_broadcast($post) { // Prepare post data $access_token = WPHubspotOauth::getAccessToken(); $post_id = $post->ID; $params = array('url' => get_permalink($post_id), 'title' => $post->post_title, 'blogPostId' => $post_id, 'access_token' => $access_token); $query = http_build_query($params); $url = $this->api_endpoint . '?' . $query; // Make the request $result = wp_remote_post($url); // Catch error with WordPress if (is_wp_error($result)) { $error_message = $result->get_error_message(); WPHubspotLogging::log("[hsWordpress] Error publishing social media post to HubSpot: " . $error_message); } // Catch errors from server $response_code = wp_remote_retrieve_response_code($result); if ($response_code == '') { WPHubspotLogging::log("[hsWordpress] Error publishing social media post to HubSpot: response code was blank"); return false; } if ($response_code == 401) { WPHubspotOauth::emptyAccessToken(); WPHubspotLogging::log("[hsWordpress] Error publishing social media post to HubSpot: Response code was 401, clearing access token."); return false; } if ($response_code == 400 or $response_code == 404 or $response_code == 500) { WPHubspotLogging::log("[hsWordpress] Error publishing social media post to HubSpot: Response code was " . $response_code); return false; } // If no errors, assume it was successful and log the event $this->log_publish_event($post); return true; }