First Commit for plugin
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 4 Aug 2016 13:03:21 +0000 (09:03 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 4 Aug 2016 13:03:21 +0000 (09:03 -0400)
This plugin will be for storing a cache of the json data for the latest
hubspot blogs. Using the Hubspot API.

controllers/Admin.php [new file with mode: 0644]
controllers/Front.php [new file with mode: 0644]
css/admin/admin.css [new file with mode: 0644]
index.php [new file with mode: 0644]
js/admin/admin.js [new file with mode: 0644]
models/GlmHubSpotBlog_Widget.php [new file with mode: 0644]
models/database.php [new file with mode: 0644]
views/deniedAccess.php [new file with mode: 0644]
views/optionsPage.php [new file with mode: 0644]
views/text.php [new file with mode: 0644]
views/textArea.php [new file with mode: 0644]

diff --git a/controllers/Admin.php b/controllers/Admin.php
new file mode 100644 (file)
index 0000000..f6b9dcf
--- /dev/null
@@ -0,0 +1,214 @@
+<?php
+
+/**
+ * Admin.php
+ *
+ * PHP version 5.3
+ *
+ * @category  Toolkit
+ * @package   Package
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 (file)
index 0000000..15cfc1e
--- /dev/null
@@ -0,0 +1,60 @@
+<?php
+
+/**
+ * Front.php
+ *
+ * PHP version 5.3
+ *
+ * @category  Toolkit
+ * @package   Package
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 (file)
index 0000000..e69de29
diff --git a/index.php b/index.php
new file mode 100644 (file)
index 0000000..125fe04
--- /dev/null
+++ b/index.php
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Plugin Name: HubSpot Blog
+ * Description: Option Setting for the name address and phone numbers. Includes Widget
+ * Version: 0.0.1
+ * Author: Steve Sutton
+ * Author URI: http://www.gaslightmedia.com
+ * License: All right reserved
+ */
+define('GLM_HUBSPOT_PLUGIN_PATH_FILE', __FILE__);
+define('GLM_HUBSPOT_DB_VERSION', '0.0.1');
+define('GLM_HUBSPOT_DB_VERSION_OPTION_NAME', 'glm_hubspot_db_version');
+define('GLM_HUBSPOT_TABLE', 'glm_hubspot_feed');
+define('GLM_HUBSPOT_SETTINGS', 'glm_hubspot_settings');
+define('GLM_HUBSPOT_OPTION_GROUP', 'glm_hubspot_section');
+define('GLM_HUBSPOT_NEW_CAPABILITY', 'glm_hubspot_edit_client');
+define('GLM_HUBSPOT_BUILT_IN_CAPABILITY', 'edit_posts');
+
+register_activation_hook(__FILE__, array('GlmHubSpot_Admin_Controller', 'activate_plugin'));
+register_deactivation_hook(__FILE__, array('GlmHubSpot_Admin_Controller', 'deactivate_plugin'));
+register_uninstall_hook(__FILE__, array('GlmHubSpot_Admin_Controller', 'glm_hubspot_uninstall'));
+
+if (is_admin()) {
+    // call the Admin Controller to setup the Admin of the plugin
+    require_once 'controllers/Admin.php';
+    $adminController = new GlmHubSpot_Admin_Controller(
+        plugin_dir_path(__FILE__)
+    );
+}
+
+// call the Front Controller to setup the widget
+require_once 'controllers/Front.php';
+$frontController = new GlmHubSpot_Front_controller(
+    plugin_dir_path(__FILE__)
+);
+
+/*
+ * Lead the database class
+ */
+require_once 'models/database.php';
+$hubspot_db = new glm_hubspot_models_database($GLOBALS['wpdb']);
diff --git a/js/admin/admin.js b/js/admin/admin.js
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/models/GlmHubSpotBlog_Widget.php b/models/GlmHubSpotBlog_Widget.php
new file mode 100644 (file)
index 0000000..a21d249
--- /dev/null
@@ -0,0 +1,151 @@
+<?php
+
+/**
+ * Glmclientinfo_Widget.php
+ *
+ * PHP version 5.3
+ *
+ * @category  Toolkit
+ * @package   Package
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 '<pre>$options: ' . print_r( $options, true ) . '</pre>';
+        //$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 '<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>';
+        }
+    }
+
+    /**
+     * 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 (file)
index 0000000..8c21c5d
--- /dev/null
@@ -0,0 +1,82 @@
+<?php
+
+/**
+ * database.php
+ *
+ * PHP version 5.3
+ *
+ * @category  Toolkit
+ * @package   Package
+ * @author    Steve Sutton <steve@gaslightmedia.com>
+ * @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 <steve@gaslightmedia.com>
+ * @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 (file)
index 0000000..597bac8
--- /dev/null
@@ -0,0 +1 @@
+<div class="wrap"><p>You do not have permission!</p></div>
diff --git a/views/optionsPage.php b/views/optionsPage.php
new file mode 100644 (file)
index 0000000..ece180f
--- /dev/null
@@ -0,0 +1,14 @@
+<div class="wrap">
+    <form action="options.php" method="post">
+        <h1>HubSpot API Key</h1>
+        <div id="glm-client-info-wrapper" class="row">
+            <?php
+                settings_fields(GLM_HUBSPOT_OPTION_GROUP);
+                do_settings_sections(GLM_HUBSPOT_OPTION_GROUP);
+            ?>
+            <div style="clear: both; text-align: center;">
+                <?php submit_button();?>
+            </div>
+        </div>
+    </form>
+</div>
diff --git a/views/text.php b/views/text.php
new file mode 100644 (file)
index 0000000..9418736
--- /dev/null
@@ -0,0 +1,2 @@
+<input type="text" name="<?php echo GLM_HUBSPOT_SETTINGS;?>[<?php echo $fieldName;?>]"
+           value="<?php echo str_replace('"', '&quote;', $options[$fieldName]); ?>">
diff --git a/views/textArea.php b/views/textArea.php
new file mode 100644 (file)
index 0000000..5107f04
--- /dev/null
@@ -0,0 +1 @@
+<textarea cols="40" rows="5" name="<?php echo GLM_HUBSPOT_SETTINGS;?>[<?php echo $fieldName;?>]"><?php echo htmlspecialchars($options[$fieldName]); ?></textarea>