From ac9b244e177ca87cc85597159ec5806707944fb6 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 8 Jan 2019 13:44:54 -0500 Subject: [PATCH] Added Cookie Notification Feature front-end Cookie notice message that's displayed once to a user Added configuration options for the Cookie notice message Added configuraiton option to enable or disable the notification features of this plugin --- index.php | 53 +++++++++-- models/adminServerStatsConfig.php | 53 ++++++++--- models/frontCookiePopUp.php | 151 ++++++++++++++++++++++++++++++ readme.txt | 5 + setup/adminHooks.php | 6 +- setup/adminMenus.php | 74 ++++++++++----- views/adminServerStatsConfig.html | 39 +++++++- 7 files changed, 335 insertions(+), 46 deletions(-) create mode 100755 models/frontCookiePopUp.php diff --git a/index.php b/index.php index 59a8c1d..f08bd0a 100755 --- a/index.php +++ b/index.php @@ -36,23 +36,21 @@ require_once ABSPATH . 'wp-includes/pluggable.php'; // Include defines to tell if a plugin is active include_once ABSPATH . 'wp-admin/includes/plugin.php'; +require_once 'defines.php'; + +require_once GLM_SERVERSTATS_PLUGIN_LIB_PATH.'/smartyTemplateSupport.php'; + // If this is an admin request if (is_admin()) { - require_once 'defines.php'; - require_once GLM_SERVERSTATS_PLUGIN_LIB_PATH.'/smartyTemplateSupport.php'; add_action('admin_menu', 'configureMenus'); require_once GLM_SERVERSTATS_PLUGIN_PATH.'/setup/adminHooks.php'; -} else { - - // No front-end functionality - return; - } + /* * Function to configure admin menus */ @@ -71,6 +69,17 @@ add_action( 'wp_ajax_glm_server_stats', 'glmServerStatsAjax'); * Load CSS and Scripts * */ + +function glmServerJqueryScipts() +{ + // jQuery scripts + wp_enqueue_script('jquery', false, array(), false, true); + wp_enqueue_script('jquery-style', false, array(), false, true); + wp_enqueue_script('jquery-ui-core', false, array(), false, true); + wp_enqueue_script('jquery-ui-widget', false, array(), false, true); + wp_enqueue_script('jquery-ui-dialog', false, array(), false, true); +} + function glmServerStatsScripts() { @@ -150,6 +159,7 @@ function serverStatsController($model) { // Enque admin scripts and css here so that only happens when we're doing something + glmServerJqueryScipts(); glmServerStatsScripts(); // Load and execute the specified model @@ -242,5 +252,34 @@ function do_glm_serverstats_cron() { } +/* + * Cookie Notice Pop-Up + * Shows the pop-up once then sets a cookie to not show it again for some number of days. + * + * To test this pop-up add "cookie_notice_test=true" to URL. + */ +$cookieNoticeName = 'cookie_popup_displayed'; +$cookieNoticeDays = 10; + +// Get Configuration for Cookie info popup +$serverstatsConfig = get_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION); +// If this is an admin request and the pop-up hasn't been displayed within the specified time +if ($serverstatsConfig['show_cookie_popup'] && !is_admin() && (!isset($_COOKIE['cookie_popup_displayed']) || isset($_REQUEST['cookie_notice_test']))) { + // Make sure that the jQuery scripts are included + function enqueueJquery() { + glmServerJqueryScipts(); + } + add_action( 'wp_enqueue_scripts', 'enqueueJquery' ); + + // Have WordPress include the cookie pop-up code just before the footer. + function doCookiePopUp() { + include(GLM_SERVERSTATS_PLUGIN_MODEL_PATH.'/frontCookiePopUp.php'); + } + add_action( 'get_footer', 'doCookiePopUp' ); + + // Redisplay after set # of days + setcookie($cookieNoticeName, true, time()+(84600*$cookieNoticeDays), '/'); + +} diff --git a/models/adminServerStatsConfig.php b/models/adminServerStatsConfig.php index 5aa5c20..3a34102 100755 --- a/models/adminServerStatsConfig.php +++ b/models/adminServerStatsConfig.php @@ -49,8 +49,10 @@ class adminServerStatsConfig extends glmServerStatsBandwidthSupport public function model() { - $connectError = False; + $connectError = false; $connectionUpdated = false; + $forceReload = false; + $reloadUrl = false; // If an update was submitted $option = 'none'; @@ -58,23 +60,50 @@ class adminServerStatsConfig extends glmServerStatsBandwidthSupport $option = $_REQUEST['option']; } + $startingConfig = $this->getConfig(); + // If this is a connection update, store that if ($option == 'update_connection') { // Filter the input data $config = filter_input_array( INPUT_POST, - array( - 'db_name' => FILTER_SANITIZE_STRING, - 'db_host' => FILTER_SANITIZE_STRING, - 'db_user' => FILTER_SANITIZE_STRING, - 'db_pass' => FILTER_SANITIZE_STRING, - 'website' => FILTER_SANITIZE_STRING - ) + array( + 'show_usage' => FILTER_VALIDATE_BOOLEAN, + 'show_notifications' => FILTER_VALIDATE_BOOLEAN, + 'show_cookie_popup' => FILTER_VALIDATE_BOOLEAN, + 'cookie_message' => FILTER_SANITIZE_STRING, + 'cookie_opt_gdpr' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_access_logs' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_permanent' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_tracking' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_shared_tracking' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_https' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_logged_in' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_forms' => FILTER_VALIDATE_BOOLEAN, + 'cookie_opt_payment' => FILTER_VALIDATE_BOOLEAN, + 'db_name' => FILTER_SANITIZE_STRING, + 'db_host' => FILTER_SANITIZE_STRING, + 'db_user' => FILTER_SANITIZE_STRING, + 'db_pass' => FILTER_SANITIZE_STRING, + 'website' => FILTER_SANITIZE_STRING + ) ); // Update the WordPress option where we store the configuration for this plugin update_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION, $config); + $connectionUpdated = true; + + // If turning off show_usage, make sure we reload with a valid URL + if ($startingConfig['show_usage'] != $config['show_usage']) { + if ($config['show_usage']) { + $reloadUrl = '?page=glm-server-stats-configure'; + } else { + $reloadUrl = '?page=glm-server-stats-main-menu'; + } + $forceReload = true; + } + // Otherwise get existing connection configuration } else { $config = $this->getConfig(); @@ -86,21 +115,19 @@ class adminServerStatsConfig extends glmServerStatsBandwidthSupport // If we can connect to the database if ($connected) { $websiteId = $this->siteId; - if ($option == 'update_connection') { - $connectionUpdated = true; - } // If we can't, get the reason why } else { $connectError = $this->connectError; } - // Compile template data $templateData = array_merge( $config, array( - 'isProvider' => $this->isProvider(), + 'forceReload' => $forceReload, + 'reloadUrl' => $reloadUrl, + 'isProvider' => $this->isProvider(), 'connectionUpdated' => $connectionUpdated, 'connectError' => $connectError, ) diff --git a/models/frontCookiePopUp.php b/models/frontCookiePopUp.php new file mode 100755 index 0000000..5a1e19f --- /dev/null +++ b/models/frontCookiePopUp.php @@ -0,0 +1,151 @@ + + +
+

+ This site uses temporary "Session Cookies" to store limited information that's required to provide + you with a consistent user experience. We don't store personally identifying or other sensitive + information in these Cookies and these Cookies are erased when you close your Web Browser. +

+

+ If you're on a public computer, be sure to close all Web Browsers when you're done! +

+'.$serverstatsConfig['cookie_message'].'

'; } ?> +

+ Show more + Got it! +

+ +
+ + + diff --git a/readme.txt b/readme.txt index 024128e..d68ae2f 100755 --- a/readme.txt +++ b/readme.txt @@ -29,6 +29,11 @@ e.g. 1. Activate the plugin through the 'Plugins' menu in WordPress == Changelog == += Pending = +* Added front-end Cookie notice message that's displayed once to a user +* Added configuration options for the Cookie notice message +* Added configuraiton option to enable or disable the notification features of this plugin + = 2.1.0 = * Early Deployment Release * Updated Smarty Templates diff --git a/setup/adminHooks.php b/setup/adminHooks.php index ca49aff..a8be7f9 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -60,7 +60,11 @@ add_filter('glm_associate_cron_request', function($request) { $user_is_admin = current_user_can( 'administrator' ); $user_is_editor = current_user_can( 'editor' ); -if ( $user_is_admin || $user_is_editor ) { +// Get Configuration +$serverstatsConfig = get_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION); + +// If this site should show notifications and is a high-level user - Check for notifications +if ( $serverstatsConfig['show_notifications'] && ($user_is_admin || $user_is_editor) ) { add_action( 'admin_init', function(){ diff --git a/setup/adminMenus.php b/setup/adminMenus.php index a2467ea..fec2eb4 100755 --- a/setup/adminMenus.php +++ b/setup/adminMenus.php @@ -12,19 +12,58 @@ * @link http://dev.gaslightmedia.com/ */ +$serverstatsConfig = get_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION); $mainServerStatsMenuSlug = 'glm-server-stats-main-menu'; + +// Have to be either an Editor or Administration to see usage if (current_user_can('editor') || current_user_can('administrator')) { - add_menu_page( - "GLM Data Usage", - 'GLM Data Usage', - 'edit_pages', - $mainServerStatsMenuSlug, - function() { - serverStatsController('adminServerStats'); - }, - GLM_SERVERSTATS_PLUGIN_ASSETS_URL.'/flame.png', - '3.100' - ); + + // If we're showing usage - Make that the menu item + if ($serverstatsConfig['show_usage']) { + + add_menu_page( + "GLM Data Usage", + 'GLM Data Usage', + 'edit_pages', + $mainServerStatsMenuSlug, + function() { + serverStatsController('adminServerStats'); + }, + GLM_SERVERSTATS_PLUGIN_ASSETS_URL.'/flame.png', + '3.100' + ); + + // If the user is an administrator they can also do config + if (current_user_can('administrator')) { + add_submenu_page( + $mainServerStatsMenuSlug, + 'Configure', + 'Configure', + 'manage_options', + 'glm-server-stats-configure', + function() { + serverStatsController('adminServerStatsConfig'); + } + ); + } + + // Otherwise, if we're an administrator, then just show configuration + } elseif (current_user_can('administrator')) { + + add_menu_page( + "GLM Data Usage", + 'GLM Data Usage', + 'edit_pages', + $mainServerStatsMenuSlug, + function() { + serverStatsController('adminServerStatsConfig'); + }, + GLM_SERVERSTATS_PLUGIN_ASSETS_URL.'/flame.png', + '3.100' + ); + + } + /* Tempoorarily Dissabled add_menu_page( 'GLM Notifications', @@ -38,18 +77,5 @@ if (current_user_can('editor') || current_user_can('administrator')) { '3.200' ); */ -} -// Add a submeu for server Stats Server Config - For admin users only - if (current_user_can('administrator')) { - add_submenu_page( - $mainServerStatsMenuSlug, - 'Configure', - 'Configure', - 'manage_options', - 'glm-server-stats-configure', - function() { - serverStatsController('adminServerStatsConfig'); - } - ); } diff --git a/views/adminServerStatsConfig.html b/views/adminServerStatsConfig.html index 43698d7..6c47f28 100755 --- a/views/adminServerStatsConfig.html +++ b/views/adminServerStatsConfig.html @@ -30,14 +30,46 @@ + {if $connectionUpdated} + {/if} + + + + + + + + + + + + @@ -86,6 +118,11 @@ jQuery(document).ready(function($) { +{if $forceReload} + alert('Redirecting due to menu change. Click "OK" to continue.'); + window.location.href = '{$adminUrl}{$reloadUrl}'; +{/if} + // Flash certain elements for a short time after display $(".glm-flash-updated").fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500).fadeIn(500).fadeOut(500); -- 2.17.1
Settings Updated
Show Data Usage: + +
Show Notifications: + +
Show Cookie Pop-Up: + +
Custom Message +
   +
Include notices regarding ... +
   GDPR +
   Access Logs +
   Permanent Cookies +
   Tracking Cookies +
   Shared Tracking Cookies +
   HTTPS +
   Logged-In Users +
   Submission Forms +
   Payment Forms +
- {if $connectError}
Error {$connectError}{/if} + {if $connectError}
Unable To Connect{/if}