Setup the notices for admin or editor roles.
authorSteve Sutton <steve@gaslightmedia.com>
Tue, 18 Sep 2018 12:58:39 +0000 (08:58 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Tue, 18 Sep 2018 12:58:39 +0000 (08:58 -0400)
Setup the define to add in the user's wordpress id also.
This will allow the notices to show for each user until the user
dismisses them.

defines.php
setup/adminHooks.php

index 9bdc026..98175c6 100644 (file)
@@ -20,8 +20,8 @@ define('GLM_SERVERSTATS_PLUGIN_RELAY_URL', 'https://www.gaslightmedia.com/wp-adm
 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');
-
+$user_id = get_current_user_id();
+define('GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS', 'glm_info_notice_dismiss_' . $user_id );
 // Determine which system we're running on - If not provided, assume PRODUCTION
 $host = getenv('GLM_HOST_ID');
 if (trim($host) == '') {
index 4eb3bde..91f01be 100644 (file)
@@ -56,132 +56,138 @@ add_filter('glm_associate_cron_request', function($request) {
 
 // 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' );
-        // trigger_error( '$current_time: ' . $current_time . ' ' . date( 'Y-m-d H:i:s', $current_time ), E_USER_NOTICE );
-
-        $latest = get_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, false );
-        // trigger_error( '$latest: ' . $latest, E_USER_NOTICE );
-        $latest_data = unserialize( $latest );
-        // trigger_error( '$cached: ' . $latest_data['cached'] . ' ' . date( 'Y-m-d H:i:s', $latest_data['cached'] ), E_USER_NOTICE );
-
-        // check latest for time if too old then fetch latest and update time.
-        $stale_time   = strtotime( '-' . GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT );
-        // If you have a cache then check to see if that cache is older than GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT.
-        // If it is then set fetch_latest to true.
-        if ( $latest ) {
-            if ( $latest_data['cached'] <= $stale_time ) {
-                // trigger_error( 'stale time occurred', E_USER_NOTICE );
-                $fetch_latest = true;
-            }
-        } else {
-            $fetch_latest = true;
-        }
 
-        // If fetch_latest then grab the newest from Relay.
-        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);
-
-            // If we get a good response.
-            if ( $status == 200 ) {
-                $messages = json_decode( $message_data, true );
-                // trigger_error( '$messages: ' . print_r( $messages, true ), E_USER_NOTICE );
-                foreach ( $messages as $message ) {
-                    $title             = $message['title'];
-                    $dashboard_message = $message['dashboard_message'];
-                    $id                = $message['id'];
-                    $updated           = $message['updated']['timestamp'];
+$user_is_admin  = current_user_can( 'administrator' );
+$user_is_editor = current_user_can( 'editor' );
+
+if ( $user_is_admin || $user_is_editor ) {
+    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' );
+            // trigger_error( '$current_time: ' . $current_time . ' ' . date( 'Y-m-d H:i:s', $current_time ), E_USER_NOTICE );
+
+            $latest = get_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, false );
+            // trigger_error( '$latest: ' . $latest, E_USER_NOTICE );
+            $latest_data = unserialize( $latest );
+            // trigger_error( '$cached: ' . $latest_data['cached'] . ' ' . date( 'Y-m-d H:i:s', $latest_data['cached'] ), E_USER_NOTICE );
+
+            // check latest for time if too old then fetch latest and update time.
+            $stale_time   = strtotime( '-' . GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT );
+            // If you have a cache then check to see if that cache is older than GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_TIMEOUT.
+            // If it is then set fetch_latest to true.
+            if ( $latest ) {
+                if ( $latest_data['cached'] <= $stale_time ) {
+                    // trigger_error( 'stale time occurred', E_USER_NOTICE );
+                    $fetch_latest = true;
                 }
             } else {
-                // Don't do anything here yet.
-            }
-            if ( $id && $updated ) {
-                $latest_version = $id . '-' . $updated;
-            } else {
-                $latest_version = false;
-            }
-            // trigger_error( '$latest_version: ' . $latest_version, E_USER_NOTICE );
-            // Need to check the latest version with what we have cached.
-            // If our version is the same then we're ok. Don't delete the dismiss option.
-            if ( isset( $latest_data ) && isset( $latest_data['version'] ) && $latest_version == $latest_data['version'] ) {
-                // use the cached data
-            } else {
-                // If there's a new update then delete the dismiss option.
-                delete_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS );
+                $fetch_latest = true;
             }
 
-            $latest_data = array(
-                'id'                => $id,
-                'title'             => $title,
-                'dashboard_message' => $dashboard_message,
-                'cached'            => $current_time,
-                'version'           => $id . '-' . $updated
-            );
+            // If fetch_latest then grab the newest from Relay.
+            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);
+
+                // If we get a good response.
+                if ( $status == 200 ) {
+                    $messages = json_decode( $message_data, true );
+                    // trigger_error( '$messages: ' . print_r( $messages, true ), E_USER_NOTICE );
+                    foreach ( $messages as $message ) {
+                        $title             = $message['title'];
+                        $dashboard_message = $message['dashboard_message'];
+                        $id                = $message['id'];
+                        $updated           = $message['updated']['timestamp'];
+                    }
+                } else {
+                    // Don't do anything here yet.
+                }
+                if ( $id && $updated ) {
+                    $latest_version = $id . '-' . $updated;
+                } else {
+                    $latest_version = false;
+                }
+                // trigger_error( '$latest_version: ' . $latest_version, E_USER_NOTICE );
+                // Need to check the latest version with what we have cached.
+                // If our version is the same then we're ok. Don't delete the dismiss option.
+                if ( isset( $latest_data ) && isset( $latest_data['version'] ) && $latest_version == $latest_data['version'] ) {
+                    // use the cached data
+                } else {
+                    // If there's a new update then delete the dismiss option.
+                    delete_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS );
+                }
 
-            // Store the option fetched.
-            update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, serialize( $latest_data ), true );
+                $latest_data = array(
+                    'id'                => $id,
+                    'title'             => $title,
+                    'dashboard_message' => $dashboard_message,
+                    'cached'            => $current_time,
+                    'version'           => $id . '-' . $updated
+                );
+
+                // Store the option fetched.
+                update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, serialize( $latest_data ), true );
+
+                if ( $latest_version ) {
+                    // If there's $latest_version then store it.
+                    update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_VERSION, $latest_version, true );
+                }
 
-            if ( $latest_version ) {
-                // If there's $latest_version then store it.
-                update_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST_VERSION, $latest_version, true );
             }
 
-        }
+            // Reset the default timezone
+            date_default_timezone_set( $default_timezone );
 
-        // 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 '<div style="display:inline-block;clear: left;">';
+                echo '<p><b>Gaslight Media Notification: </b><br>';
+                echo '<p><b>'.$latest_data['title'].'</b><br>';
+                echo wpautop( $latest_data['dashboard_message'] );
+                echo '</p></div>';
+                echo '<p><a href="' . admin_url( 'admin.php' ) . '?page=glm-info-main-menu">More Details</a></p>';
+                echo '</div>';
+            }, 10, 1
+        );
     }
-);
 
-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 );
+    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
+        );
+    } );
 
-            echo '<div style="display:inline-block;clear: left;">';
-            echo '<p><b>Gaslight Media Notification: </b><br>';
-            echo '<p><b>'.$latest_data['title'].'</b><br>';
-            echo wpautop( $latest_data['dashboard_message'] );
-            echo '</p></div>';
-            echo '<p><a href="' . admin_url( 'admin.php' ) . '?page=glm-info-main-menu">More Details</a></p>';
-            echo '</div>';
-        }, 10, 1
-    );
+    add_action( 'wp_ajax_my_dismiss_glm_notice', function(){
+        // Set option for dismissing notices.
+        update_option( GLM_SERVERSTATS_PLUGIN_RELAY_DISMISS, 1, true );
+    } );
 }
-
-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 );
-} );