Update for new setting and view file.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 2 Sep 2016 14:59:56 +0000 (10:59 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 2 Sep 2016 14:59:56 +0000 (10:59 -0400)
Adding shortcode for the plugin.
new view file.
new setting for how many entries to fetch.

controllers/Admin.php
controllers/Front.php
models/GlmHubSpotBlog_Widget.php
models/HubSpotFeed.php [new file with mode: 0644]
views/mostrecentlist.php [new file with mode: 0644]

index f6b9dcf..bb069d3 100644 (file)
@@ -148,6 +148,11 @@ class GlmHubSpot_Admin_Controller
                 'label' => 'Refresh Interval Count',
                 'type'  => 'text'
             ),
+            array(
+                'name'  => 'number_of_entries',
+                'label' => 'Number of entries to fetch',
+                'type'  => 'text',
+            ),
         );
 
         foreach ($fieldNames as $field) {
index 15cfc1e..5825e84 100644 (file)
@@ -42,19 +42,38 @@ class GlmHubSpot_Front_controller
      *
      * @param type $path Plugin path
      */
-    function __construct($path)
+    public function __construct($path)
     {
         $this->path = $path;
-        add_action('widgets_init', array($this, 'glm_hubspot_blog_register_widget'));
+        add_action( 'widgets_init', array( $this, 'glm_hubspot_blog_register_widget' ) );
+        add_shortcode( 'glm-hubspot', array( $this, 'do_shortcode' ) );
     }
 
     /**
      * Register the Client Info Widget with WordPress
      */
-    function glm_hubspot_blog_register_widget()
+    public function glm_hubspot_blog_register_widget()
     {
         include $this->path . 'models/GlmHubSpotBlog_Widget.php';
-        register_widget('GlmHubSpotBlog_Widget');
+        register_widget( 'GlmHubSpotBlog_Widget' );
+    }
+
+    public function do_shortcode( $atts )
+    {
+        $attributes = shortcode_atts(
+            array(
+                'template' => 'frontPage',
+                'entries'  => 1,
+            ),
+            $atts
+        );
+        require_once $this->path . 'models/HubSpotFeed.php';
+        $hs_feed   = new GlmHubSpotFeed();
+        $feed      = $hs_feed->fetch_feed();
+        $feed_data = json_decode( $feed['feed_data'] );
+
+        include $this->path . 'views/' . $attributes['template'] . '.php';
+        return $text;
     }
 
 }
index 16a4c2a..43773f6 100644 (file)
@@ -13,7 +13,7 @@
  * @version   SVN: (0.1)
  * @link      <>
  */
-
+require_once 'HubSpotFeed.php';
 /**
  * Toolkit_Package_QuickSite_Widget
  *
@@ -32,12 +32,6 @@ class GlmHubSpotBlog_Widget
 {
 
     private $wpdb;
-    private $pluginDirPath;
-    private $hapikey;
-    private $refresh_interval;
-    private $refresh_interval_count;
-    private $hubspot_table;
-    private $feed_data;
 
     /**
      * Class Initializer
@@ -49,7 +43,7 @@ class GlmHubSpotBlog_Widget
             __('HubSpot Widget', 'text_domain'),
             array('description' => __('HubSpot Widget', 'text_domain'))
         );
-        $this->wpdb = $GLOBALS['wpdb'];
+        $this->wpdb          = $GLOBALS['wpdb'];
         $this->hubspot_table = $this->wpdb->prefix . GLM_HUBSPOT_TABLE;
     }
 
@@ -66,141 +60,17 @@ class GlmHubSpotBlog_Widget
      */
     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;
+        $path      = plugin_dir_path(__FILE__);
+        $hs_feed   = new GlmHubSpotFeed();
+        $feed      = $hs_feed->fetch_feed();
+        $feed_data = json_decode( $feed['feed_data'] );
+        $title     = $feed_data->objects[0]->html_title;
+        $image_url = $feed_data->objects[0]->featured_image;
+        $content   = $feed_data->objects[0]->post_body;
+        $url       = $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;
-    }
-
 }
diff --git a/models/HubSpotFeed.php b/models/HubSpotFeed.php
new file mode 100644 (file)
index 0000000..ce1883c
--- /dev/null
@@ -0,0 +1,164 @@
+<?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 GlmHubSpotFeed
+{
+
+    private $wpdb;
+    private $pluginDirPath;
+    private $hapikey;
+    private $refresh_interval;
+    private $refresh_interval_count;
+    private $hubspot_table;
+    private $feed_data;
+    private $number_of_entries;
+    private $view;
+
+    /**
+     * Class Initializer
+     */
+    public function __construct()
+    {
+        $this->wpdb          = $GLOBALS['wpdb'];
+        $this->hubspot_table = $this->wpdb->prefix . GLM_HUBSPOT_TABLE;
+        $this->check_for_updates();
+    }
+
+    /**
+     * 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;
+        }
+        $this->number_of_entries = $options['number_of_entries'];
+        if (   !filter_var( $this->number_of_entries, FILTER_VALIDATE_INT )
+            || $this->number_of_entries <= 0
+        ) {
+            return false;
+        }
+        $this->view = $options['view'];
+
+        $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();
+        $limit = ( $this->number_of_entries ) ? $this->number_of_entries : 1;
+        // 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=' . $limit . '&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;
+    }
+}
diff --git a/views/mostrecentlist.php b/views/mostrecentlist.php
new file mode 100644 (file)
index 0000000..586a878
--- /dev/null
@@ -0,0 +1,7 @@
+<?php foreach ( $feed_data->objects as $feed ) : ?>
+    <ul class="glm-hubspot-recent">
+        <li>
+            <a target="_blank" class="image-link" href="<?php echo $feed->published_url;?>"><?php echo $feed->html_title;?></a>
+        </li>
+    </ul>
+<?php endforeach;?>