Adding Gaslight Notification setup.
authorSteve Sutton <steve@gaslightmedia.com>
Wed, 27 Jun 2018 16:41:02 +0000 (12:41 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Wed, 27 Jun 2018 16:41:02 +0000 (12:41 -0400)
Will pull Glm Relay message from the main plugin on gaslightmedia.com
website. Will display the admin notice until it is dismissed.

defines.php
index.php
js/notice-update.js [new file with mode: 0644]
models/glmInfoNotifications.php
setup/adminHooks.php [new file with mode: 0644]
views/glmInfoNotifications.html

index c72f370..cb74a7f 100644 (file)
@@ -15,6 +15,11 @@ define('GLM_SERVERSTATS_SITE_TITLE', get_bloginfo( 'name' ));
 define('GLM_SERVERSTATS_PLUGIN_NAME', 'Gaslight Media ServerStats (serverstats)');
 define('GLM_SERVERSTATS_PLUGIN_SHORT_NAME', 'ServerStats');
 define('GLM_SERVERSTATS_PLUGIN_SLUG', 'glm-serverstats');
+define('GLM_SERVERSTATS_PLUGIN_RELAY_URL', 'https://www.gaslightmedia.com/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=relay');
+define('GLM_SERVERSTATS_PLUGIN_RELAY_LATEST', 'glm_serverstats_relay_latest');
+define('GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_VERSION', 'glm_serverstats_relay_latest_version');
+define('GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT', '20 minutes');
+define('GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS', 'glm_info_notice_dismiss');
 
 // Determine which system we're running on - If not provided, assume PRODUCTION
 $host = getenv('GLM_HOST_ID');
@@ -67,4 +72,4 @@ define('GLM_SERVERSTATS_PLUGIN_MODEL_PATH', GLM_SERVERSTATS_PLUGIN_PATH.'/models
 define('GLM_SERVERSTATS_PLUGIN_VIEW_PATH', GLM_SERVERSTATS_PLUGIN_PATH.'/views');
 
 // Update Server
-define('GLM_SERVERSTATS_PLUGIN_UPDATE_SERVER', 'http://www.gaslightmedia.com/update_server');
\ No newline at end of file
+define('GLM_SERVERSTATS_PLUGIN_UPDATE_SERVER', 'http://www.gaslightmedia.com/update_server');
index 69b18d7..2972732 100644 (file)
--- a/index.php
+++ b/index.php
@@ -44,6 +44,8 @@ if (is_admin()) {
 
     add_action('admin_menu', 'configureMenus');
 
+    require_once GLM_SERVERSTATS_PLUGIN_PATH.'/setup/adminHooks.php';
+
 } else {
 
     // No front-end functionality
diff --git a/js/notice-update.js b/js/notice-update.js
new file mode 100644 (file)
index 0000000..dbf76c2
--- /dev/null
@@ -0,0 +1,11 @@
+jQuery(document).on( 'click', '.glm-serverstatus-notice .notice-dismiss', function() {
+    console.log( 'clicked on dismissed' );
+
+    jQuery.ajax({
+        url: ajaxurl,
+        data: {
+            action: 'my_dismiss_glm_notice'
+        }
+    })
+
+})
index 4b1df54..34bccc1 100644 (file)
@@ -23,8 +23,37 @@ class glmInfoNotifications
 {
     public function model()
     {
-        $messages = false;
-        $url = 'http://develop.localhost/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=relay';
+        // Initiate Variables.
+        $messages  = false;
+        $view_type = 'main';
+        $url       = GLM_SERVERSTATS_PLUGIN_RELAY_URL;
+
+        // Setup the option.
+        $option = isset( $_REQUEST['option'] ) ? filter_var( $_REQUEST['option'] ) : 'latest';
+
+        // Add option to $url based on the current option.
+        switch ( $option ) {
+
+        case 'view':
+            $url      .= '&option=view';
+            if ( isset( $_REQUEST['id'] ) && $id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ) ) {
+                $url .= '&id=' . $id;
+            }
+            break;
+
+        case 'full':
+            $url      .= '&option=full';
+            $view_type = 'list';
+            break;
+
+        case 'latest':
+        default:
+            $url .= '&option=latest';
+            break;
+
+        }
+
+        // echo '<pre>$url: ' . print_r( $url, true ) . '</pre>';
 
         // Get the messages from the main plugin.
         $fetchData   = wp_remote_get( $url );
@@ -33,15 +62,16 @@ class glmInfoNotifications
 
         if ( $status == 200 ) {
             $messages = json_decode( $messageData, true );
+        } else {
         }
 
-        // If there's no message id then only get the first one.
-        $messages = array_slice( $messages, 0, 1 );
+        // echo '<pre>$messages: ' . print_r( $messages, true ) . '</pre>';
 
         // Compile template data
         $templateData = array(
-            'messages'    => $messages,
-            'status'      => $status,
+            'messages'  => $messages,
+            'status'    => $status,
+            'view_type' => $view_type,
         );
 
         // Return status, suggested view, and data to controller
diff --git a/setup/adminHooks.php b/setup/adminHooks.php
new file mode 100644 (file)
index 0000000..cc158f3
--- /dev/null
@@ -0,0 +1,176 @@
+<?php
+/**
+ * Gaslight Media Members Database
+ * GLM Members Misc Admin Hooks and Filters
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @license  http://www.gaslightmedia.com Gaslightmedia
+ * @release  adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $
+ * @link     http://dev.gaslightmedia.com/
+ */
+
+/*
+ * Place Misc Hooks and Filters here. If this file exists, it will be included
+ * by the add-on main plugin script.
+ *
+ * Note that filter and hook callback functions must be included in-line as shown below...
+ *
+ *  add_filter( 'filter_title', function( $parameter ) {
+ *     // Function code
+ *  });
+ *
+ *  Also note that parameters will be in the context of the main admin controller constructor.
+  */
+
+/*
+ * Add an action to GLM Associate Cron
+ *
+ * See description for "GLM Associate Cron" in main plugin index.php file.
+ */
+/* Sample add cron request for testing.
+add_filter('glm_associate_cron_request', function($request) {
+
+    // Sanity check - Must be passed an array
+    if (is_array($request)) {
+
+        $request[] = array(
+            'menu'      => GLM_MEMBERS_PLUGIN_UNDERSCORED_NAME, // Menu where action exists
+            'action'    => 'test_action',                       // Name of action to run
+            'daysOfWeek'=> false,                               // All Days
+            'times'     => false,                               // All Hours
+            'params'    => false                                // Any parameters needed by the triggered action
+        );
+
+    } else {
+        trigger_error('Cron Request filter recieved bad request array!', E_USER_WARNING);
+    }
+
+    return $request;
+
+}, 10, 1);
+*/
+
+// Need to see if the cache is old and then to see if there's a new version for the notice
+// on the server.
+add_action(
+    'admin_init',
+    function(){
+        $fetch_latest   = false;
+        $latest_version = $id = $updated = '';
+
+        // Save the default for later
+        $default_timezone = date_default_timezone_get();
+        date_default_timezone_set( 'America/Detroit' );
+
+        // Get current time
+        $current_time = strtotime( 'now' );
+
+        $latest = get_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, false );
+        $latest_data = unserialize( $latest );
+
+        // echo '<div><pre>$latest_data: ' . print_r( $latest_data, true ) . '</pre></div>';
+
+        // check latest for time if too old then fetch latest and update time.
+        $stale_time   = strtotime( '-' . GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT );
+        if ( $current_time <= $stale_time ) {
+            $fetch_latest = true;
+        }
+        if ( $latest ) {
+            if ( $latest_data['cached'] <= $stale_time ) {
+                $fetch_latest = true;
+            }
+        } else {
+            $fetch_latest = true;
+        }
+
+        if ( $fetch_latest ) {
+            // Get the messages from the main plugin.
+            $response     = wp_remote_get( GLM_SERVERSTATS_PLUGIN_RELAY_URL . '&option=latest' );
+            $message_data = wp_remote_retrieve_body( $response );
+            $status       = wp_remote_retrieve_response_code( $response);
+
+            // echo '<pre>$response: ' . print_r( $response, true ) . '</pre>';
+            if ( $status == 200 ) {
+                $messages = json_decode( $message_data, true );
+                // echo '<pre>$messages: ' . print_r( $messages, true ) . '</pre>';
+                foreach ( $messages as $message ) {
+                    $title   = $message['title'];
+                    $id      = $message['id'];
+                    $updated = $message['updated']['timestamp'];
+                }
+            } else {
+                // echo '<pre>$status: ' . print_r( $status, true ) . '</pre>';
+            }
+            if ( $id && $updated ) {
+                $latest_version = $id . '-' . $updated;
+            } else {
+                $latest_version = false;
+            }
+            // echo '<pre>$latest_version: ' . print_r( $latest_version, true ) . '</pre>';
+            if ( isset( $latest_data ) && isset( $latest_data['version'] ) && $latest_version == $latest_data['version'] ) {
+                // use the cached data
+            } else {
+                delete_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS );
+            }
+
+            $latest_data = array(
+                'id'      => $id,
+                'title'   => $title,
+                'cached'  => $current_time,
+                'version' => $id . '-' . $updated
+            );
+
+        }
+
+        // Store as option
+        update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, serialize( $latest_data ), true );
+        if ( $latest_version ) {
+            update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_VERSION, $latest_version, true );
+        }
+
+
+        // Reset the default timezone
+        date_default_timezone_set( $default_timezone );
+
+    }
+);
+
+if ( empty( get_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS ) ) ) {
+    add_action(
+        'admin_notices',
+        function(){
+            $page = isset( $_REQUEST['page'] ) ? $_REQUEST['page'] : '';
+            if ( $page === 'glm-info-main-menu' ) {
+                return false;
+            }
+            echo '<div class="glm-serverstatus-notice updated notice is-dismissible">';
+            // Grab the cached notice.
+            $latest = get_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, false );
+            $latest_data = unserialize( $latest );
+
+            echo '<p><b>Gaslight Media Notification: </b><br>';
+            echo '<p><b>'.$latest_data['title'].'</b><br>';
+            echo '<a href="' . admin_url( '/' ) . 'index.php?page=glm-info-main-menu">More Details</a></p>';
+            echo '</div>';
+        }, 10, 1
+    );
+}
+
+add_action( 'admin_enqueue_scripts', function(){
+    wp_enqueue_script(
+        'glm-serverstats-notice-update',
+        GLM_SERVERSTATS_PLUGIN_URL . '/js/notice-update.js',
+        array( 'jquery' ),
+        '1.0',
+        true
+    );
+} );
+
+add_action( 'wp_ajax_my_dismiss_glm_notice', function(){
+    // Set option for dismissing notices.
+    update_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS, 1, true );
+} );
index e6886ca..aa5e688 100644 (file)
@@ -2,16 +2,32 @@
 
     <h1>GLM Notifications</h1>
 
+    <ul>
+        <li>
+            <a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=full">Full List</a>
+        </li>
+    </ul>
+
     <div id="glm-info-container">
 
         {if $messages}
-            {foreach $messages as $m}
-                <div class="glm-info-message">
-                    <h2>{$m.title}</h2>
-                    <div>posted on {$m.updated.datetime}</div>
-                    {$m.message}
-                </div>
-            {/foreach}
+            {if $view_type == 'main'}
+                {foreach $messages as $m}
+                    <div class="glm-info-message">
+                        <h2>{$m.title}</h2>
+                        <div>posted on {$m.updated.datetime}</div>
+                        {$m.message}
+                    </div>
+                {/foreach}
+            {else}
+                {foreach $messages as $m}
+                    <ul>
+                        <li>
+                            <strong><a href="{$thisUrl}?page={$thisPage}&glm_action=index&option=view&id={$m.id}">{$m.title}</a> {$m.updated.datetime}</strong>
+                        </li>
+                    </ul>
+                {/foreach}
+            {/if}
         {/if}
 
     </div>