--- /dev/null
+<?php
+
+/**
+ * Glmclientinfo_Widget.php
+ *
+ * PHP version 5.3
+ *
+ * @category Toolkit
+ * @package Package
+ * @author Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2013 Gaslight Media
+ * @license Gaslight Media
+ * @version SVN: (0.1)
+ * @link <>
+ */
+
+/**
+ * Toolkit_Package_QuickSite_Widget
+ *
+ * Description of QuickSite_Widget
+ *
+ * @category Toolkit
+ * @package Package
+ * @author Steve Sutton <steve@gaslightmedia.com>
+ * @copyright 2013 Gaslight Media
+ * @license Gaslight Media
+ * @release Release: (0.1)
+ * @link <>
+ */
+class GlmHomeAway_Widget
+ extends WP_widget
+{
+
+ private $wpdb;
+ private $pluginDirPath;
+ private $hapikey;
+ private $refresh_interval;
+ private $refresh_interval_count;
+ private $hubspot_table;
+ private $feed_data;
+
+ /**
+ * Class Initializer
+ */
+ public function __construct()
+ {
+ parent::__construct(
+ 'GlmHubSpotBlog_Widget',
+ __('HubSpot Widget', 'text_domain'),
+ array('description' => __('HubSpot Widget', 'text_domain'))
+ );
+ $this->wpdb = $GLOBALS['wpdb'];
+ $this->hubspot_table = $this->wpdb->prefix . GLM_HUBSPOT_TABLE;
+ }
+
+ /**
+ * Output widget
+ *
+ * Includes the template file for the widget.
+ * Template file is plain old php for your fastest template engine yet!
+ *
+ * @param type $args Widget Args
+ * @param type $instance Widget Instance
+ *
+ * @return string
+ */
+ public function widget($args, $instance)
+ {
+ $path = plugin_dir_path(__FILE__);
+ $this->check_for_updates();
+ $title = $this->feed_data->objects[0]->html_title;
+ $image_url = $this->feed_data->objects[0]->featured_image;
+ $content = $this->feed_data->objects[0]->post_body;
+ $url = $this->feed_data->objects[0]->published_url;
+
+// echo '<pre>$this->feed_data: ' . print_r( $this->feed_data, true ) . '</pre>';
+ include_once GLM_HUBSPOT_PLUGIN_DIR . '/views/frontPage.php';
+ }
+
+
+ /**
+ * check_for_updates
+ *
+ * Check to see if we need to refresh the feed from hubspot.
+ * If no settings then do nothing.
+ * If nothing is in the feed table then try to refresh.
+ *
+ * @access public
+ * @return void
+ */
+ public function check_for_updates()
+ {
+ $options = get_option( GLM_HUBSPOT_SETTINGS );
+ if ( !$options ) {
+ return false;
+ }
+ $this->hapikey = $options['hapikey'];
+ if ( !$this->hapikey ) {
+ return false;
+ }
+ $this->refresh_interval = $options['refresh_interval'];
+ if ( !$this->refresh_interval ) {
+ return false;
+ }
+ $this->refresh_interval_count = $options['refresh_interval_count'];
+ if ( !filter_var( $this->refresh_interval_count, FILTER_VALIDATE_INT )
+ || $this->refresh_interval_count <= 0
+ ) {
+ return false;
+ }
+ $feed = $this->fetch_feed();
+ if ( !$feed ) {
+ $feed = $this->update_feed_data();
+ }
+ $current_time = new DateTime();
+ $last_updated = new DateTime( $feed['create_time'] );
+ $last_updated->modify( '+' . $this->refresh_interval_count . ' ' . $this->refresh_interval );
+
+ if ( $current_time > $last_updated ) {
+ $feed = $this->update_feed_data();
+ }
+ $this->feed_data = json_decode( $feed['feed_data'] );
+ }
+
+ /**
+ * fetch_feed
+ *
+ * Fetch the feed data from Cache
+ *
+ * @access public
+ * @return void
+ */
+ public function fetch_feed()
+ {
+ return $this->wpdb->get_row(
+ $this->wpdb->prepare(
+ "SELECT *
+ FROM {$this->hubspot_table}
+ WHERE id = %d",
+ 1
+ ),
+ ARRAY_A
+ );
+ }
+
+ /**
+ * update_feed_data
+ *
+ * Store the feed data
+ *
+ * @param mixed JSON data from hubspot
+ *
+ * @access public
+ * @return void
+ */
+ public function update_feed_data()
+ {
+ $feed = array();
+ // https://api.hubapi.com/content/api/v2/blog-posts?hapikey=demo&limit=1&offset=0&order_by=-publish_date&state=PUBLISHED
+ $feed_url = "https://api.hubapi.com/content/api/v2/blog-posts?hapikey={$this->hapikey}"
+ . '&limit=1&offset=0&order_by=-publish_date&state=PUBLISHED';
+ $response = wp_remote_get( esc_url_raw( $feed_url ) );
+ $response_code = wp_remote_retrieve_response_code( $response );
+ if ( $response_code === 200 ) {
+ $api_response = json_decode( wp_remote_retrieve_body( $response) );
+ $this->wpdb->replace(
+ $this->hubspot_table,
+ array(
+ 'id' => 1,
+ 'create_time' => current_time( 'mysql' ),
+ 'feed_data' => json_encode( $api_response )
+ ),
+ array(
+ '%d',
+ '%s',
+ '%s'
+ )
+ );
+ $feed = $this->fetch_feed();
+ }
+ return $feed;
+ }
+
+ /**
+ * Return the quicksite option for the given key
+ *
+ * @param type $name Name of the quicksite option to return
+ *
+ * @return string Option
+ */
+ function glm_get_clientinfo_option($name)
+ {
+ $settings = get_option('glmclientinfo_settings');
+ $states = get_option('glmclientinfo_states');
+ if ($name == 'stateFull' && $settings['state']) {
+ return $states[$settings['state']];
+ }
+ if ($name == 'state2Full' && $settings['state2']) {
+ return $states[$settings['state2']];
+ }
+ return ($settings && $settings[$name])
+ ? $settings[$name]
+ : null;
+ }
+
+}