From 2ae3733553483ad603500bfc90584f81a7da6c93 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 4 Aug 2016 09:03:21 -0400 Subject: [PATCH 1/1] First Commit for plugin This plugin will be for storing a cache of the json data for the latest hubspot blogs. Using the Hubspot API. --- controllers/Admin.php | 214 +++++++++++++++++++++++++++++++ controllers/Front.php | 60 +++++++++ css/admin/admin.css | 0 index.php | 41 ++++++ js/admin/admin.js | 0 models/GlmHubSpotBlog_Widget.php | 151 ++++++++++++++++++++++ models/database.php | 82 ++++++++++++ views/deniedAccess.php | 1 + views/optionsPage.php | 14 ++ views/text.php | 2 + views/textArea.php | 1 + 11 files changed, 566 insertions(+) create mode 100644 controllers/Admin.php create mode 100644 controllers/Front.php create mode 100644 css/admin/admin.css create mode 100644 index.php create mode 100644 js/admin/admin.js create mode 100644 models/GlmHubSpotBlog_Widget.php create mode 100644 models/database.php create mode 100644 views/deniedAccess.php create mode 100644 views/optionsPage.php create mode 100644 views/text.php create mode 100644 views/textArea.php diff --git a/controllers/Admin.php b/controllers/Admin.php new file mode 100644 index 0000000..f6b9dcf --- /dev/null +++ b/controllers/Admin.php @@ -0,0 +1,214 @@ + + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @version SVN: (0.1) + * @link <> + */ + +/** + * Toolkit_Package_Admin + * + * Description of Admin + * + * @category Toolkit + * @package Package + * @author Steve Sutton + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @release Release: (0.1) + * @link <> + */ +class GlmHubSpot_Admin_Controller +{ + public $path; + + public function __construct($path) + { + $this->path = $path; + add_action('admin_menu', array($this, 'glm_hubspot_add_admin_menu')); + add_action('admin_init', array($this, 'glm_hubspot_settings_init')); + //add_action('wp_enqueue_scripts', array($this, 'load_stylesheet')); + //add_action('wp_enqueue_scripts', array($this, 'load_script')); + + } + + static public function activate_plugin() + { + GlmHubSpot_Admin_Controller::add_capability(); + } + + static public function deactivate_plugin() + { + GlmHubSpot_Admin_Controller::remove_capability(); + } + + static public function add_capability() + { + $roles = get_editable_roles(); + foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) { + if (isset($roles[$key]) && $role->has_cap(GLM_HUBSPOT_BUILT_IN_CAPABILITY)) { + $role->add_cap(GLM_HUBSPOT_NEW_CAPABILITY); + } + } + } + + static public function remove_capability() + { + $roles = get_editable_roles(); + foreach ($GLOBALS['wp_roles']->role_objects as $key => $role) { + if (isset($roles[$key]) && $role->has_cap(GLM_HUBSPOT_NEW_CAPABILITY)) { + $role->remove_cap(GLM_HUBSPOT_NEW_CAPABILITY); + } + } + } + + static public function glm_hubspot_uninstall() + { + delete_option(GLM_HUBSPOT_SETTINGS); + } + + public function glm_hubspot_add_admin_menu() + { + add_options_page( + 'HubSpot Blog', + 'Hubspot Blog', + GLM_HUBSPOT_NEW_CAPABILITY, + 'glm_hubspot', + array($this, 'glm_hubspot_options_page'), + 'dashicons-admin-network' + ); + } + + public function glm_hubspot_settings_exist() + { + if (false == get_option(GLM_HUBSPOT_SETTINGS)) { + add_option(GLM_HUBSPOT_SETTINGS); + } + } + + public function glm_hubspotAddSettingTextField($name, $label, $type) + { + switch ($type) { + case 'text': + $callback = 'glm_hubspotRenderText'; + break; + case 'textarea': + $callback = 'glm_hubspotRenderTextArea'; + break; + default: + return false; + break; + } + add_settings_field( + $name, + __($label, 'wordpress'), + array($this, $callback), + GLM_HUBSPOT_OPTION_GROUP, + 'glm_hubspot_' . GLM_HUBSPOT_OPTION_GROUP . '_section', + $name + ); + } + + public function glm_hubspot_settings_init() + { + register_setting(GLM_HUBSPOT_OPTION_GROUP, GLM_HUBSPOT_SETTINGS); + add_filter( + 'option_page_capability_' . GLM_HUBSPOT_OPTION_GROUP, + array($this, 'glm_hubspot_option_page_capability') + ); + add_settings_section( + 'glm_hubspot_' . GLM_HUBSPOT_OPTION_GROUP . '_section', + __('Edit Setting', 'wordpress'), + array($this, 'glm_hubspot_settings_section_callback'), + GLM_HUBSPOT_OPTION_GROUP + ); + + $fieldNames = array( + array( + 'name' => 'hapikey', + 'label' => 'Hubspot API Key', + 'type' => 'text' + ), + array( + 'name' => 'refresh_interval', + 'label' => 'Refresh Interval', + 'type' => 'text' + ), + array( + 'name' => 'refresh_interval_count', + 'label' => 'Refresh Interval Count', + 'type' => 'text' + ), + ); + + foreach ($fieldNames as $field) { + $this->glm_hubspotAddSettingTextField( + $field['name'], + $field['label'], + $field['type'] + ); + } + } + + public function glm_hubspotRenderText($fieldName) + { + static $options; + if (!$options) { + $options = get_option(GLM_HUBSPOT_SETTINGS); + } + include $this->path . 'views/text.php'; + } + + public function glm_hubspotRenderTextArea($fieldName) + { + static $options; + $options = get_option(GLM_HUBSPOT_SETTINGS); + include $this->path . 'views/textArea.php'; + } + + public function glm_hubspot_settings_section_callback() + { + echo __('HubSpot API Key', 'wordpress'); + } + + public function glm_hubspot_option_page_capability($capability) + { + return GLM_HUBSPOT_NEW_CAPABILITY; + } + + public function glm_hubspot_options_page() + { + if (current_user_can(GLM_HUBSPOT_NEW_CAPABILITY)) { + include $this->path . 'views/optionsPage.php'; + } else { + include $this->path . 'views/deniedAccess.php'; + } + } + + public function load_stylesheet() + { + //wp_enqueue_style('client-admin-css',plugins_url('glm-client-info/css/admin/admin.css' , $this->pluginDirName)); + } + public function load_script() + { + /* + wp_register_script( + 'client-info-admin-js', + plugins_url('glm-client-info/js/admin/admin.js' , $this->pluginDirName), + array(), + '0.0.1', + 'all' + ); + wp_enqueue_script('client-info-admin-js'); + */ + } +} diff --git a/controllers/Front.php b/controllers/Front.php new file mode 100644 index 0000000..15cfc1e --- /dev/null +++ b/controllers/Front.php @@ -0,0 +1,60 @@ + + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @version SVN: (0.1) + * @link <> + */ + +/** + * Toolkit_Package_Front + * + * Description of Front + * + * @category Toolkit + * @package Package + * @author Steve Sutton + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @release Release: (0.1) + * @link <> + */ +class GlmHubSpot_Front_controller +{ + + /** + * Plugin Path + * + * @var type String + */ + public $path; + + /** + * Initializes Front Controller class + * + * @param type $path Plugin path + */ + function __construct($path) + { + $this->path = $path; + add_action('widgets_init', array($this, 'glm_hubspot_blog_register_widget')); + } + + /** + * Register the Client Info Widget with WordPress + */ + function glm_hubspot_blog_register_widget() + { + include $this->path . 'models/GlmHubSpotBlog_Widget.php'; + register_widget('GlmHubSpotBlog_Widget'); + } + +} diff --git a/css/admin/admin.css b/css/admin/admin.css new file mode 100644 index 0000000..e69de29 diff --git a/index.php b/index.php new file mode 100644 index 0000000..125fe04 --- /dev/null +++ b/index.php @@ -0,0 +1,41 @@ + + * @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 GlmHubSpotBlog_Widget + extends WP_widget +{ + + private $pluginDirPath; + private $hapikey; + private $refresh_interval; + private $refresh_interval_count; + + /** + * Class Initializer + */ + public function __construct() + { + parent::__construct( + 'GlmHubSpotBlog_Widget', + __('HubSpot Widget', 'text_domain'), + array('description' => __('HubSpot Widget', 'text_domain')) + ); + } + + /** + * Output widget + * + * Includes the template file for the widget. + * Template file is plain old php for your fastest template engine yet! + * + * @param type $args Widget Args + * @param type $instance Widget Instance + * + * @return string + */ + public function widget($args, $instance) + { + $path = plugin_dir_path(__FILE__); + $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']; + $this->refresh_interval = $options['refresh_interval']; + $this->refresh_interval_count = $options['refresh_interval_count']; + + echo '
$options: ' . print_r( $options, true ) . '
'; + //$this->fetch_feed(); + } + + /** + * fetch_feed + * + * Fetch the hubspot json feed for the latest post. + * + * @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 ) . '
'; + } + } + + /** + * update_feed_data + * + * Store the feed data + * + * @param mixed JSON data from hubspot + * + * @access public + * @return void + */ + public function update_feed_data( $feedData ) + { + } + + /** + * 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/database.php b/models/database.php new file mode 100644 index 0000000..8c21c5d --- /dev/null +++ b/models/database.php @@ -0,0 +1,82 @@ + + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @version SVN: (0.1) + * @link <> + */ + +/** + * Toolkit_Package_database + * + * Description of database + * + * @category Toolkit + * @package Package + * @author Steve Sutton + * @copyright 2013 Gaslight Media + * @license Gaslight Media + * @release Release: (0.1) + * @link <> + */ +class glm_hubspot_models_database +{ + public $wpdb; + public $applicationTable; + public $appFormTable; + + /** + * Clas constructor for the database + * + * @param type $wpdb Wordpress db object + */ + public function __construct($wpdb) + { + $this->wpdb = $wpdb; + register_activation_hook( + GLM_HUBSPOT_PLUGIN_PATH_FILE, + array($this, 'install') + ); + add_action('plugins_loaded', array($this, 'glm_update_db_check')); + + $this->hubspot_table = $this->wpdb->prefix . GLM_HUBSPOT_TABLE; + } + + /** + * Install the database tables needed for jobs + */ + public function install() + { + $charset_collate = $this->wpdb->get_charset_collate(); + + $sql = "CREATE TABLE {$this->hubspot_table} ( + id BIGINT(20) NOT NULL AUTO_INCREMENT, + create_time DATETIME DEFAULT '0000-00-00 00:00:00' NOT NULL, + feed_data TEXT NOT NULL, + UNIQUE KEY id (id) + ) {$charset_collate}"; + include ABSPATH . 'wp-admin/includes/upgrade.php'; + dbDelta($sql); + + update_option(GLM_HUBSPOT_DB_VERSION_OPTION_NAME, GLM_HUBSPOT_DB_VERSION); + } + + /** + * DB Version update check to update the database tables + */ + public function glm_update_db_check() + { + if (GLM_HUBSPOT_DB_VERSION != get_option(GLM_HUBSPOT_DB_VERSION_OPTION_NAME)) { + $this->install(); + } + } + +} diff --git a/views/deniedAccess.php b/views/deniedAccess.php new file mode 100644 index 0000000..597bac8 --- /dev/null +++ b/views/deniedAccess.php @@ -0,0 +1 @@ +

You do not have permission!

diff --git a/views/optionsPage.php b/views/optionsPage.php new file mode 100644 index 0000000..ece180f --- /dev/null +++ b/views/optionsPage.php @@ -0,0 +1,14 @@ +
+
+

HubSpot API Key

+
+ +
+ +
+
+
+
diff --git a/views/text.php b/views/text.php new file mode 100644 index 0000000..9418736 --- /dev/null +++ b/views/text.php @@ -0,0 +1,2 @@ +"> diff --git a/views/textArea.php b/views/textArea.php new file mode 100644 index 0000000..5107f04 --- /dev/null +++ b/views/textArea.php @@ -0,0 +1 @@ + -- 2.17.1