From 645917e00ee8d1c60aae2897a9daa468a8b610db Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Thu, 15 Aug 2019 15:36:43 -0400 Subject: [PATCH] Final Testing Completed Added Send Notice Enable feature to global settings. Reviewed Notice generation cron code. Added Notice generation debug information. Other minor changes --- classes/serverBandwidthSupport.php | 23 ++++++++++++-- defines.php | 1 + index.php | 50 +++++++++++++++++++++--------- models/adminServerStats.php | 14 ++++++++- models/ajaxSendUsageReport.php | 21 +++++++++++-- views/adminServerStats.html | 17 +++++++++- views/ui/f6/form-start.html | 2 +- 7 files changed, 105 insertions(+), 23 deletions(-) diff --git a/classes/serverBandwidthSupport.php b/classes/serverBandwidthSupport.php index f616da5..9075b08 100755 --- a/classes/serverBandwidthSupport.php +++ b/classes/serverBandwidthSupport.php @@ -50,6 +50,13 @@ class glmServerStatsBandwidthSupport * @access public */ public $threshold = -1; + /** + * Reporting Send Enabled + * + * @var $send_enabled + * @access public + */ + public $send_enabled = false; /** * Default Website ID * @@ -135,6 +142,8 @@ class glmServerStatsBandwidthSupport $this->connected = false; $this->threshold = get_option(GLM_SERVERSTATS_PLUGIN_THRESHOLD_OPTION); + $this->send_enabled = get_option(GLM_SERVERSTATS_PLUGIN_SEND_OPTION); + // Initialize MySQLi $this->bwdb = mysqli_init(); @@ -868,6 +877,8 @@ class glmServerStatsBandwidthSupport * @param boolean $forceDisplay Flag to display rather than send * If this is a development system all messages will only be displayed, not sent. * + * @return integer Number of sites notified + * */ public function checkEmailNotifications($sendToGlmContact = false, $sendToCustContact = false, $sendAll = false, $site = false, $forceDisplay = false) { @@ -907,12 +918,10 @@ class glmServerStatsBandwidthSupport // For each website retrieved foreach ($websites as $site) { - $mesg = ''; // If theshold is not -1 and at least one of usage or disk % of target equals or exceeds reporting threshold if ($this->threshold != -1 && ($sendAll || $site['target_percent'] >= $this->threshold || $site['disk_target_percent'] >= $this->threshold)) { -echo "Threshold = ".$this->threshold."

"; $numbReporting++; @@ -1034,7 +1043,15 @@ echo "Threshold = ".$this->threshold."

"; $this->sendHtmlEmail($this->glm_billing_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $glmMesg, $forceDisplay); } - return; + $res = [ + 'numbSites' => $numbSites, + 'numbReporting' => $numbReporting, + 'numbReportingWithContactEmail' => $numbReportingWithContactEmail + ]; + + trigger_error("Usage E-Mail Notifications Summary - Sites: $numbSites, Sites Reporting: $numbReporting, Reporting with Contact E-Mail: $numbReportingWithContactEmail", E_USER_NOTICE ); + + return $res; } diff --git a/defines.php b/defines.php index a5a4cb0..b3d87f5 100755 --- a/defines.php +++ b/defines.php @@ -90,4 +90,5 @@ define('GLM_SERVERSTATS_PLUGIN_UPDATE_SERVER', 'http://www.gaslightmedia.com/upd // Other define('GLM_SERVERSTATS_PLUGIN_THRESHOLD_OPTION', 'glmServerStatsThreshold'); +define('GLM_SERVERSTATS_PLUGIN_SEND_OPTION', 'glmServerStatsSend'); define('GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION', 'glmServerStatsConfigData'); \ No newline at end of file diff --git a/index.php b/index.php index 668ea3e..aee5035 100755 --- a/index.php +++ b/index.php @@ -274,12 +274,12 @@ function glmServerStatsPluginDeactivate () register_deactivation_hook(__FILE__, 'glmServerStatsPluginDeactivate'); /* - * Check is we should run the target check E-mail process + * Check if we should run the target check E-mail process * - * This only runs on the Bandwidth Management Site! + * This only runs on the Bandwidth Management Site! (see defines.php) * * A WordPress scheduled event is created to run once per hour and call - * doGlmServerstatsCron(). That checkeds to see if the target checks + * doGlmServerstatsCron(). That checks to see if the target checks * have been run in the current month and if not to run them now. * * The target check will look for sites that are near or above their @@ -289,22 +289,33 @@ register_deactivation_hook(__FILE__, 'glmServerStatsPluginDeactivate'); */ // Called by WordPress scheduler as set below -//add_action('glm_serverstats_notify_cron', 'doGlmServerstatsCron'); -function doGlmServerstatsCron() { +function doGlmServerUsageCron() { trigger_error('GLM Serverstats Cron Called', E_USER_NOTICE); + $enabled = get_option(GLM_SERVERSTATS_PLUGIN_SEND_OPTION); + if (!$enabled) { + trigger_error('GLM Serverstats E-Mail Notifications not enabled!', E_USER_NOTICE); + return; + } + // Test if already run this month. $targetCheckMonth = date('m/Y'); $targetCheckLast = get_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION.'_LAST_CHECK'); - // *** FOR TESTING ONLY - Incriment number to enable another test *** - $targetCheckMonth .= '/43'; + /* *** FOR TESTING ONLY *** + * Adds a suffix to the date to enable another E-Mail check. + * This value can be incremented to re-try the E-Mail usage check for additional tests. + * If this is left here and not updated, this section will resume running the E-Mail + * usage check once per month anyway. + */ + $targetCheckMonth .= '/53'; -trigger_error("BLOCKING TARGET CHECK FOR NOW FOR TEST", E_USER_NOTICE); - if ( false && $targetCheckMonth != $targetCheckLast) { + trigger_error("GLM Serverstats Cron Check: If $targetCheckMonth equals $targetCheckLast this has already been run.", E_USER_NOTICE); - trigger_error('Running Target Check Process for Month = '.$targetCheckMonth, E_USER_NOTICE); + if ( $targetCheckMonth != $targetCheckLast) { + + trigger_error('Running Usage E-Mail Notification Process for Month = '.$targetCheckMonth, E_USER_NOTICE); update_option(GLM_SERVERSTATS_PLUGIN_CONFIG_OPTION.'_LAST_CHECK', $targetCheckMonth); @@ -315,8 +326,13 @@ trigger_error("BLOCKING TARGET CHECK FOR NOW FOR TEST", E_USER_NOTICE); } } +add_action('glm_serverstats_notify_cron', 'doGlmServerUsageCron'); -// ***** Setup a one minute schedule for TESTING ONLY +/* + * This code sets up a once per minute cron schedule to use for testing. + * Normally this would use the "hourly" schedule. This may be commented out if not in use. + * To use this schedule for testing, see the commented out code in the next block of code. + */ function cron_add_minute( $schedules ) { // Adds once weekly to the existing schedules. $schedules['minute'] = array( @@ -326,10 +342,16 @@ function cron_add_minute( $schedules ) { return $schedules; } -// If this running on the Bandwidth Management site, setup scheduler to call abovie once per hour. + +/* + * If this running on the Bandwidth Management site (see defines.php), sets scheduler to call + * doGlmServerstatsCron + * once per . + * + */ if (apply_filters('glm-serverstats-is-provider', false) || true) { - // ****** Add a 1 minute schedule option for testing only + // Add a 1 minute schedule option for testing only - For testing only add_filter( 'cron_schedules', 'cron_add_minute' ); // If there's no current cron for doing this, set it now @@ -337,7 +359,7 @@ if (apply_filters('glm-serverstats-is-provider', false) || true) { if (!$cronTimestamp) { wp_schedule_event( time(), 'hourly', 'glm_serverstats_notify_cron' ); -// for testing wp_schedule_event( time(), 'minute', 'glm_serverstats_notify_cron' ); + // wp_schedule_event( time(), 'minute', 'glm_serverstats_notify_cron' ); // For testing only } diff --git a/models/adminServerStats.php b/models/adminServerStats.php index 4638dfc..b340620 100755 --- a/models/adminServerStats.php +++ b/models/adminServerStats.php @@ -86,10 +86,19 @@ class adminServerStats extends glmServerStatsBandwidthSupport $option = $_REQUEST['option']; } - // If a threshold value is submitted + // If a threshold value is submitted, the form has been submitted if (isset($_REQUEST['threshold'])) { + $this->threshold = $_REQUEST['threshold'] - 0; update_option(GLM_SERVERSTATS_PLUGIN_THRESHOLD_OPTION, $this->threshold); + + // If a send_enabled is submitted + $this->send_enabled = false; + if (isset($_REQUEST['send_enabled']) && $_REQUEST['send_enabled'] == 'on') { + $this->send_enabled = true; + } + update_option(GLM_SERVERSTATS_PLUGIN_SEND_OPTION, $this->send_enabled); + } // Check for a selected site or use website in config @@ -217,6 +226,7 @@ class adminServerStats extends glmServerStatsBandwidthSupport 'target' => $this->target, 'diskTarget' => $this->disk_target, 'threshold' => $this->threshold, + 'send_enabled' => $this->send_enabled, 'websites' => $websites, 'isProvider' => apply_filters('glm-serverstats-is-provider', false), 'site_sort' => $site_sort, @@ -224,6 +234,8 @@ class adminServerStats extends glmServerStatsBandwidthSupport 'settings' => $settings ); + //echo "

".print_r($templateData,1)."
"; + // Return status, suggested view, and data to controller return array( 'view' => 'adminServerStats', diff --git a/models/ajaxSendUsageReport.php b/models/ajaxSendUsageReport.php index ae9edc8..18b3297 100755 --- a/models/ajaxSendUsageReport.php +++ b/models/ajaxSendUsageReport.php @@ -54,7 +54,7 @@ class ajaxSendUsageReport extends glmServerStatsBandwidthSupport public function model() { - // Check input + // Check input $site = filter_input( INPUT_GET, 'selected_site', FILTER_SANITIZE_STRING); $display = filter_input( INPUT_GET, 'display_only', FILTER_SANITIZE_STRING); @@ -63,6 +63,13 @@ class ajaxSendUsageReport extends glmServerStatsBandwidthSupport $displayOnly = true; } + // If Display is not requested, must be a real send so check if sending is enabled. + $send_enabled = get_option(GLM_SERVERSTATS_PLUGIN_SEND_OPTION); + if (!$displayOnly && !$send_enabled) { + echo "

Sending of notification E-Mail is dissabled!

"; + wp_die(); + }; + // If we have a Website selected - Do that notice if (!empty($site)) { @@ -76,7 +83,7 @@ class ajaxSendUsageReport extends glmServerStatsBandwidthSupport echo "
"; - $this->checkEmailNotifications(true, true, true, $site, $displayOnly); + $res = $this->checkEmailNotifications(true, true, true, $site, $displayOnly); } else { @@ -91,10 +98,18 @@ class ajaxSendUsageReport extends glmServerStatsBandwidthSupport wp_die(); } - $this->checkEmailNotifications(true, true, false, false, $displayOnly); + $res = $this->checkEmailNotifications(true, true, false, false, $displayOnly); } + echo " +

+ ".$res['numbSites']." Total sites
+ ".$res['numbReporting']." Number of Notifications Generated
+ ".$res['numbReportingWithContactEmail']." Number of Notfications for which there is a contact E-Mail address +

+ "; + // Ajax processing ends here wp_die(); diff --git a/views/adminServerStats.html b/views/adminServerStats.html index ed99767..cea305a 100755 --- a/views/adminServerStats.html +++ b/views/adminServerStats.html @@ -229,6 +229,8 @@

Global Settings

Monthly Reporting Threshold:
At or above {$threshold|string_format:"%.0f"}% of site's target levels
+
Send Notices Enabled:
+
{if !empty($send_enabled)}YES{else}NO{/if}
@@ -298,10 +300,23 @@ 'helpText' => ' Percentage above which target percentage should be highlighted in red and site contacts will receive E-Mail reports. Set Reporting Threshold to -1% to block all automatic monthly reporting. - ' + ', + 'errorText' => 'Please enter or select a number between -1 and 1000.' ]} {include file="ui/f6/number.html"} + {* Send Enabled *} + {$ui = [ + 'field' => 'send_enabled', + 'value' => {$send_enabled}, + 'label' => 'Send Notices', + 'helpText' => ' + Turn this on to have monthly notices set to site contacts for sites + above the reporting threashold. + ' + ]} + {include file="ui/f6/checkbox.html"} + {include file="ui/f6/section-end.html"} {* Submit Button *} diff --git a/views/ui/f6/form-start.html b/views/ui/f6/form-start.html index e94cb20..8318aef 100644 --- a/views/ui/f6/form-start.html +++ b/views/ui/f6/form-start.html @@ -28,7 +28,7 @@ {if !empty($ui.validate)}