Updates for the fetch of feed and check for out of date feed entry.
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 4 Aug 2016 13:47:02 +0000 (09:47 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 4 Aug 2016 13:47:02 +0000 (09:47 -0400)
The process for the fetching of the feed data is now completed like the
Blog feed from wordpress that was done for MichiganTrailMaps and
GaylordGolfMecca websites.

models/GlmHubSpotBlog_Widget.php

index a21d249..dc30763 100644 (file)
@@ -31,10 +31,13 @@ class GlmHubSpotBlog_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
@@ -46,6 +49,8 @@ class 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;
     }
 
     /**
@@ -63,6 +68,7 @@ class GlmHubSpotBlog_Widget
     {
         $path = plugin_dir_path(__FILE__);
         $this->check_for_updates();
+        //echo '<pre>$this->feed_data: ' . print_r( $this->feed_data, true ) . '</pre>';
     }
 
     /**
@@ -81,35 +87,53 @@ class GlmHubSpotBlog_Widget
         if ( !$options ) {
             return false;
         }
-        $this->hapikey                = $options['hapikey'];
-        $this->refresh_interval       = $options['refresh_interval'];
+        $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 );
 
-        echo '<pre>$options: ' . print_r( $options, true ) . '</pre>';
-        //$this->fetch_feed();
+        if ( $current_time > $last_updated ) {
+            $feed = $this->update_feed_data();
+        }
+        $this->feed_data = json_decode( $feed['feed_data'] );
     }
 
     /**
      * fetch_feed
      *
-     * Fetch the hubspot json feed for the latest post.
+     * Fetch the feed data from Cache
      *
      * @access public
      * @return void
      */
     public function fetch_feed()
     {
-        // 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 ) );
-        //echo '<pre>$response: ' . print_r( $response, true ) . '</pre>';
-        $response_code = wp_remote_retrieve_response_code( $response );
-        echo '<pre>$response_code: ' . print_r( $response_code, true ) . '</pre>';
-        if ( $response_code === 200 ) {
-            $api_response = json_decode( wp_remote_retrieve_body( $response) );
-            echo '<pre>$api_response: ' . print_r( $api_response, true ) . '</pre>';
-        }
+        return $this->wpdb->get_row(
+            $this->wpdb->prepare(
+                "SELECT *
+                  FROM {$this->hubspot_table}
+                 WHERE id = %d",
+                1
+            ),
+            ARRAY_A
+        );
     }
 
     /**
@@ -122,8 +146,32 @@ class GlmHubSpotBlog_Widget
      * @access public
      * @return void
      */
-    public function update_feed_data( $feedData )
+    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 = $api_response;
+        }
+        return $feed;
     }
 
     /**