From 0f1c9ae8532e36f06bc2573d6725c00b8d7b5ccc Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 2 Sep 2016 10:59:56 -0400 Subject: [PATCH] Update for new setting and view file. Adding shortcode for the plugin. new view file. new setting for how many entries to fetch. --- controllers/Admin.php | 5 + controllers/Front.php | 27 ++++- models/GlmHubSpotBlog_Widget.php | 150 ++-------------------------- models/HubSpotFeed.php | 164 +++++++++++++++++++++++++++++++ views/mostrecentlist.php | 7 ++ 5 files changed, 209 insertions(+), 144 deletions(-) create mode 100644 models/HubSpotFeed.php create mode 100644 views/mostrecentlist.php diff --git a/controllers/Admin.php b/controllers/Admin.php index f6b9dcf..bb069d3 100644 --- a/controllers/Admin.php +++ b/controllers/Admin.php @@ -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) { diff --git a/controllers/Front.php b/controllers/Front.php index 15cfc1e..5825e84 100644 --- a/controllers/Front.php +++ b/controllers/Front.php @@ -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; } } diff --git a/models/GlmHubSpotBlog_Widget.php b/models/GlmHubSpotBlog_Widget.php index 16a4c2a..43773f6 100644 --- a/models/GlmHubSpotBlog_Widget.php +++ b/models/GlmHubSpotBlog_Widget.php @@ -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 '
$this->feed_data: ' . print_r( $this->feed_data, true ) . '
'; 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 index 0000000..ce1883c --- /dev/null +++ b/models/HubSpotFeed.php @@ -0,0 +1,164 @@ + + * @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 + * @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 index 0000000..586a878 --- /dev/null +++ b/views/mostrecentlist.php @@ -0,0 +1,7 @@ +objects as $feed ) : ?> + + -- 2.17.1