From cb79790b35e304aa99d51e12af30d0f1f7e88398 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 27 Jun 2018 12:41:02 -0400 Subject: [PATCH] Adding Gaslight Notification setup. Will pull Glm Relay message from the main plugin on gaslightmedia.com website. Will display the admin notice until it is dismissed. --- defines.php | 7 +- index.php | 2 + js/notice-update.js | 11 ++ models/glmInfoNotifications.php | 42 ++++++-- setup/adminHooks.php | 176 ++++++++++++++++++++++++++++++++ views/glmInfoNotifications.html | 30 ++++-- 6 files changed, 254 insertions(+), 14 deletions(-) create mode 100644 js/notice-update.js create mode 100644 setup/adminHooks.php diff --git a/defines.php b/defines.php index c72f370..cb74a7f 100644 --- a/defines.php +++ b/defines.php @@ -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'); diff --git a/index.php b/index.php index 69b18d7..2972732 100644 --- 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 index 0000000..dbf76c2 --- /dev/null +++ b/js/notice-update.js @@ -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' + } + }) + +}) diff --git a/models/glmInfoNotifications.php b/models/glmInfoNotifications.php index 4b1df54..34bccc1 100644 --- a/models/glmInfoNotifications.php +++ b/models/glmInfoNotifications.php @@ -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 '
$url: ' . print_r( $url, true ) . '
'; // 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 '
$messages: ' . print_r( $messages, true ) . '
'; // 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 index 0000000..cc158f3 --- /dev/null +++ b/setup/adminHooks.php @@ -0,0 +1,176 @@ + + * @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 '
$latest_data: ' . print_r( $latest_data, true ) . '
'; + + // 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 '
$response: ' . print_r( $response, true ) . '
'; + if ( $status == 200 ) { + $messages = json_decode( $message_data, true ); + // echo '
$messages: ' . print_r( $messages, true ) . '
'; + foreach ( $messages as $message ) { + $title = $message['title']; + $id = $message['id']; + $updated = $message['updated']['timestamp']; + } + } else { + // echo '
$status: ' . print_r( $status, true ) . '
'; + } + if ( $id && $updated ) { + $latest_version = $id . '-' . $updated; + } else { + $latest_version = false; + } + // echo '
$latest_version: ' . print_r( $latest_version, true ) . '
'; + 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 '
'; + // Grab the cached notice. + $latest = get_option( GLM_SERVERSTATS_PLUGIN_RELAY_LATEST, false ); + $latest_data = unserialize( $latest ); + + echo '

Gaslight Media Notification:
'; + echo '

'.$latest_data['title'].'
'; + echo 'More Details

'; + echo '
'; + }, 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 ); +} ); diff --git a/views/glmInfoNotifications.html b/views/glmInfoNotifications.html index e6886ca..aa5e688 100644 --- a/views/glmInfoNotifications.html +++ b/views/glmInfoNotifications.html @@ -2,16 +2,32 @@

GLM Notifications

+ +
{if $messages} - {foreach $messages as $m} -
-

{$m.title}

-
posted on {$m.updated.datetime}
- {$m.message} -
- {/foreach} + {if $view_type == 'main'} + {foreach $messages as $m} +
+

{$m.title}

+
posted on {$m.updated.datetime}
+ {$m.message} +
+ {/foreach} + {else} + {foreach $messages as $m} + + {/foreach} + {/if} {/if}
-- 2.17.1