syncHubSpot(); } add_action('admin_menu', array($this, 'adminMenu')); add_action('save_post', array($this, 'onSave')); } function onSave($post_id) { if (wp_is_post_revision($post_id)) return; $this->syncHubSpot(); } public function getBetween($left, $right, $source, $offset = 1) { $step1 = explode($left, $source); if (count($step1) < 2 + $offset - 1) { return false; } $step2 = explode($right, $step1[1 + $offset - 1]); if (isset($step2[0])) { return trim(preg_replace('/\s\s+/', ' ', $step2[0])); } return false; } public function getCurl() { $curl = curl_init(); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($curl, CURLOPT_REFERER, 'http://google.pl'); curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/6.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1'); curl_setopt($curl, CURLOPT_COOKIEFILE, dirname(__FILE__) . '/cookies.txt'); curl_setopt($curl, CURLOPT_COOKIEJAR, dirname(__FILE__) . '/cookies.txt'); curl_setopt($curl, CURLOPT_AUTOREFERER, true); curl_setopt($curl, CURLOPT_VERBOSE, false); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); return $curl; } public function social_media_message() { $rss_url = get_option('rss_url'); $posts = $this->getPosts($rss_url); $hubspot_api_key = get_option('hubspot_api_key'); $url = 'https://api.hubapi.com/broadcast/v1/broadcasts?hapikey=' . $hubspot_api_key; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); $all_messages = json_decode(curl_exec($curl)); $publishing_channel = get_option('publishing_channel'); if (empty($publishing_channel)) { return false; } foreach ($posts as $post) { $description = $post->getNode('contentencoded')->getValue(); $title = trim($post->getNode('title')->getValue()); $body = trim($description); $add = true; foreach ($all_messages as $message) { $cb = trim($message->content->originalBody); if ($cb === $body) { $add = false; } } if ($add) { foreach ($publishing_channel as $pc) { $url = 'https://api.hubapi.com/broadcast/v1/broadcasts?hapikey=' . $hubspot_api_key; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); $data = array( 'channelGuid' => $pc, 'content' => array( 'body' => $body ) ); $payload = json_encode($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); $id = json_decode(curl_exec($curl)); } $this->addToLog('The "' . $title . '" has been added as Social Message (Hubspot).'); } } } public function getPosts() { $rss_url = get_option('rss_url'); $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $rss_url); $rss = curl_exec($curl); $parser = new XmlParser($rss); $posts = $parser->getNode('channel')->getNodes('item'); return $posts; } public function syncPardot() { $use_pardot = get_option('use_pardot'); $pardot_added = get_option('pardot_added'); if(!$pardot_added) { $pardot_added = update_option('pardot_added',array()); $pardot_added = get_option('pardot_added'); } if($use_pardot!=='yes') { return true; } $rss_url = get_option('rss_url'); $posts = $this->getPosts($rss_url); foreach($posts as $post) { } } public function syncHubSpot() { $hubspot_api_key = get_option('hubspot_api_key'); $use_hubspot = get_option('use_hubspot'); if($use_hubspot!=='yes') { return true; } $rss_url = get_option('rss_url'); $post_as = get_option('post_as'); set_time_limit(3600); if (trim($hubspot_api_key) != '' && trim($rss_url) != '') { if ($post_as == 'social_media_message') { $this->social_media_message(); } $url = 'https://api.hubapi.com/content/api/v2/blogs?hapikey=' . $hubspot_api_key; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); $all_blogs = json_decode(curl_exec($curl)); $url = 'https://api.hubapi.com/blogs/v3/blog-authors?hapikey=' . $hubspot_api_key; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); $all_authors = json_decode(curl_exec($curl)); $url = 'https://api.hubapi.com/content/api/v2/blog-posts?hapikey=' . $hubspot_api_key . '&limit=200'; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); $all_posts = json_decode(curl_exec($curl)); $posts = $this->getPosts($rss_url); $new_post_status = get_option('new_post_status'); if (!empty($posts) && !empty($all_blogs->objects) && !empty($all_authors->objects)) { foreach ($posts as $post) { $description = $post->getNode('contentencoded')->getValue(); $title = trim($post->getNode('title')->getValue()); $pubDate = strtotime($post->getNode('pubDate')->getValue()); $add = true; foreach ($all_posts->objects as $p) { $t = trim(strip_tags($p->html_title)); if ($t == $title) { $add = false; } } if ($add) { $url = 'https://api.hubapi.com/content/api/v2/blog-posts?hapikey=' . $hubspot_api_key; $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POST, true); $data = array( 'post_body' => $description, 'blog_author_id' => $all_authors->objects[0]->id, 'name' => $title, 'meta_description' => $title, 'publish_date' => $pubDate * 1000, 'content_group_id' => $all_blogs->objects[0]->id ); $payload = json_encode($data); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); $id = json_decode(curl_exec($curl)); $this->addToLog('The "' . $title . '" has been added to blog (Hubspot)'); if ($new_post_status == 'publish') { /* publish the post */ $url = 'https://api.hubapi.com/content/api/v2/blog-posts/' . $id->id . '/publish-action?hapikey=' . $hubspot_api_key; $data = array( 'action' => 'schedule-publish', 'blog_post_id' => $id->id ); $payload = json_encode($data); $curl = $this->getCurl(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_POSTFIELDS, $payload); curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type:application/json')); curl_setopt($curl, CURLOPT_POST, true); $res = curl_exec($curl); $this->addToLog('The "' . $title . '" has been published (Hubspot)'); } } } } //var_dump($all_posts->objects); } } public function addToLog($line) { $l = '[' . time() . ']' . $line . "\n"; file_put_contents(dirname(__FILE__) . '/log.txt', $l, FILE_APPEND); } public function log() { $log = file_get_contents(dirname(__FILE__) . '/log.txt'); $lines = explode("\n", $log); array_reverse($lines); $lines = array_slice($lines, 0, 200); require dirname(__FILE__) . '/tmpl/log.php'; } public function adminMenu() { add_menu_page('Hubspot Plugin', 'Hubspot Plugin', 'manage_options', 'hubspot_plugin', array($this, 'settings')); add_submenu_page('hubspot_plugin', 'FAQ', 'FAQ', 'manage_options', 'faq', array($this, 'faq')); add_submenu_page('hubspot_plugin', 'Log', 'Log', 'manage_options', 'log', array($this, 'log')); add_submenu_page('hubspot_plugin', 'Support', 'Support', 'manage_options', 'support', array($this, 'support')); add_submenu_page('hubspot_plugin', 'Tracking Code', 'Tracking Code', 'manage_options', 'tracking-code', array($this, 'hubspot_plugin_options')); } public function support() { require dirname(__FILE__) . '/tmpl/support.php'; } public function faq() { require dirname(__FILE__) . '/tmpl/faq.php'; } /** * Creates settings page */ function hubspot_plugin_options () { ?>
HubSpot Tracking Code and HubSpot for WordPress are like two rival siblings - they don\'t play nice together, but don\'t panic - it\'s an easy fix. Deactivate HubSpot for WordPress and then try activating HubSpot Tracking Code for WordPress again, and everything should work fine.
' . 'By the way - make sure you replace all your form and CTA shortcodes with HubSpot embed codes
', 'hubspot-plugin-advance' ); ?>