From 9006ba30241196360202840e6b9cdc870f17aaa7 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 4 Aug 2016 09:47:02 -0400 Subject: [PATCH] Updates for the fetch of feed and check for out of date feed entry. 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 | 82 +++++++++++++++++++++++++------- 1 file changed, 65 insertions(+), 17 deletions(-) diff --git a/models/GlmHubSpotBlog_Widget.php b/models/GlmHubSpotBlog_Widget.php index a21d249..dc30763 100644 --- a/models/GlmHubSpotBlog_Widget.php +++ b/models/GlmHubSpotBlog_Widget.php @@ -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 '
$this->feed_data: ' . print_r( $this->feed_data, true ) . '
'; } /** @@ -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 '
$options: ' . print_r( $options, true ) . '
'; - //$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 '
$response: ' . print_r( $response, true ) . '
'; - $response_code = wp_remote_retrieve_response_code( $response ); - echo '
$response_code: ' . print_r( $response_code, true ) . '
'; - if ( $response_code === 200 ) { - $api_response = json_decode( wp_remote_retrieve_body( $response) ); - echo '
$api_response: ' . print_r( $api_response, true ) . '
'; - } + 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; } /** -- 2.17.1