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 hs_process_settings_options() { $myhubspotsettings_notice = new WPHubspotNotice('main-settings-update'); if (!empty($_POST['hs_option_submitted'])) { $hs_settings = get_option('hs_settings'); if ($_GET['page'] == 'hubspot_activate') { if (isset($_POST['hs_portal'])) { $hs_settings['hs_portal'] = $_POST['hs_portal']; update_option("hs_settings", $hs_settings); return $hs_settings; } } if ($_GET['page'] == 'hubspot_settings' && check_admin_referer('hubspot-dashboard-update-options')) { if (isset($_POST['hs_portal'])) { $hs_settings['hs_portal'] = $_POST['hs_portal']; } // Check to be sure app domain does not have 'http' or 'www' if (isset($_POST['hs_appdomain'])) { $hs_settings['hs_appdomain'] = $_POST['hs_appdomain']; $hs_settings['hs_appdomain'] = str_replace('http://', '', $hs_settings['hs_appdomain']); $hs_settings['hs_appdomain'] = str_replace('www.', '', $hs_settings['hs_appdomain']); } // Check to be sure feedburner URL is a valid URL if (isset($_POST['hs_feedburner_url'])) { if (trim($_POST['hs_feedburner_url']) == '' || strpos($_POST['hs_feedburner_url'], 'feeds.feedburner.com') !== false) { $hs_settings['hs_feedburner_url'] = $_POST['hs_feedburner_url']; } else { $hs_settings['hs_feedburner_url'] = ''; $myhubspotsettings_notice->add_notice('Please enter a valid Feedburner URL'); } } if (isset($_POST['hs_actions_disabled'])) { $hs_settings['hs_actions_disabled'] = $_POST['hs_actions_disabled']; } else { $hs_settings['hs_actions_disabled'] = ''; } if (isset($_POST['hs_actions_stats_disabled'])) { $hs_settings['hs_actions_stats_disabled'] = $_POST['hs_actions_stats_disabled']; } else { $hs_settings['hs_actions_stats_disabled'] = ''; } if (isset($_POST['hs_config_notice'])) { $hs_settings['hs_config_notice'] = $_POST['hs_config_notice']; } else { $hs_settings['hs_config_notice'] = ''; } if (isset($_POST['hs_blog_autopublish'])) { $hs_settings['hs_blog_autopublish'] = $_POST['hs_blog_autopublish']; // Store the enabled date if ($hs_settings['hs_blog_autopublish'] == 'on') { add_option('hs_blog_autopublish_enabled', mktime() . '000'); } } else { $hs_settings['hs_blog_autopublish'] = ''; } $myhubspotsettings_notice->display_notice(3); update_option("hs_settings", $hs_settings); } } //updated if (isset($_POST['auth-revoke'])) { update_option('hs_refresh_token', ''); update_option('hs_access_token', ''); WPHubspotOauth::emptyAccessToken(); } // add/update refresh_token if (!empty($_GET['refresh_token']) && !empty($_GET['access_token']) && !empty($_GET['expires_in'])) { $access_token = array('token' => $_GET['access_token'], 'expires' => time() + $_GET['expires_in']); update_option('hs_access_token', $access_token); update_option('hs_refresh_token', $_GET['refresh_token']); WPHubspotOauth::emptyAccessToken(); $hs_tracking_setup = get_option('hs_tracking_setup'); if (!$hs_tracking_setup) { add_option('hs_tracking_setup', 'setup', '', 'yes'); } else { // check if hs_portal different $hs_settings = get_option('hs_settings'); if ($hs_tracking_setup and is_array($hs_tracking_setup) and array_key_exists('hs_portal', $hs_tracking_setup)) { if ($hs_tracking_setup['hs_portal'] != $hs_settings['hs_portal']) { update_option('hs_tracking_setup', 'setup'); } } } } $hs_settings = get_option('hs_settings'); $hs_authorized = get_option('hs_authorized'); if ($hs_authorized === 'fail') { $myhubspotwp_notice = new WPHubspotNotice('authorize-fail'); $myhubspotwp_notice->display_notice(10); } // Authenticate url if (!empty($hs_settings['hs_portal'])) { $access_token = WPHubspotOauth::getAccessToken(); if ($access_token) { $hs_settings['hs_authorize_url'] = 'OK'; } else { $blog_url = urlencode(HUBSPOT_ADMIN . 'admin.php?page=hubspot_settings'); $hs_settings['hs_authorize_url'] = 'https://app.hubspot.com/auth/authenticate' . '?client_id=' . HUBSPOT_CLIENT_ID . '&portalId=' . $hs_settings['hs_portal'] . '&scope=leads-rw+offline' . '&redirect_uri=' . $blog_url; } } else { $hs_settings['hs_authorize_url'] = false; } return $hs_settings; }
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; }