extends WP_widget
{
+ private $wpdb;
private $pluginDirPath;
private $hapikey;
private $refresh_interval;
private $refresh_interval_count;
+ private $hubspot_table;
+ private $feed_data;
/**
* Class Initializer
__('HubSpot Widget', 'text_domain'),
array('description' => __('HubSpot Widget', 'text_domain'))
);
+ $this->wpdb = $GLOBALS['wpdb'];
+ $this->hubspot_table = $this->wpdb->prefix . GLM_HUBSPOT_TABLE;
}
/**
{
$path = plugin_dir_path(__FILE__);
$this->check_for_updates();
+ //echo '<pre>$this->feed_data: ' . print_r( $this->feed_data, true ) . '</pre>';
}
/**
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
+ );
}
/**
* @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;
}
/**