From 84d9744dca8afe9a2e2ccccde33266087fdfe7d8 Mon Sep 17 00:00:00 2001 From: Chuck Scott Date: Tue, 6 Nov 2018 15:31:36 -0500 Subject: [PATCH] Added immediate send and display buttons for E-Mail in Service Provider Section --- activate.php | 3 +- classes/serverBandwidthSupport.php | 186 +++++++++++++++++++++++++- models/adminServerStats.php | 3 - models/ajaxServerUsageTargetCheck.php | 119 ++-------------- views/adminServerStats.html | 4 + 5 files changed, 201 insertions(+), 114 deletions(-) diff --git a/activate.php b/activate.php index ca7338b..b921f35 100755 --- a/activate.php +++ b/activate.php @@ -46,10 +46,11 @@ class glmServerStatsPluginActivate die(); } - // If no cron schedue is set, add it now to start at 30 min after the hour during the next hour. + // Get scheduled cron for sending server stats notices $cronTimestamp = wp_next_scheduled( 'glm_serverstats_cron' ); trigger_error("Next Scheduled = $cronTimestamp

",E_USER_NOTICE); + // If there's no current cron for doing this, set it now if (!$cronTimestamp) { // Create a random time during the day that's not in the first and last 30 minutes. diff --git a/classes/serverBandwidthSupport.php b/classes/serverBandwidthSupport.php index 2b84049..02abd78 100755 --- a/classes/serverBandwidthSupport.php +++ b/classes/serverBandwidthSupport.php @@ -177,7 +177,7 @@ class glmServerStatsBandwidthSupport ;"; $websiteResult = $this->bwdb->query($sql); if (!$websiteResult) { - trigger_error("Get website settings failed: " . mysqli_error($bwdb) ); + trigger_error("Get website settings failed: " . mysqli_error($this->bwdb) ); } $website = mysqli_fetch_assoc($websiteResult); if (!$website){ @@ -204,7 +204,7 @@ class glmServerStatsBandwidthSupport ;"; $websiteResult = $this->bwdb->query($sql); if (!$websiteResult) { - trigger_error("Website ID query failed: " . mysqli_error($bwdb) ); + trigger_error("Website ID query failed: " . mysqli_error($this->bwdb) ); } $row = mysqli_fetch_assoc($websiteResult); if (!$row){ @@ -777,4 +777,186 @@ class glmServerStatsBandwidthSupport return ($this->connected && (strpos(GLM_SERVERSTATS_SITE_BASE_URL, '192.168.44.80') !== false || strpos(GLM_SERVERSTATS_SITE_BASE_URL, 'www.gaslightmedia.com') !== false)); } + + /* + * Check if a site needs notifications sent based on set limits + * + * @param boolean $glmContact Send message to GLM contact + * @param boolean $custContact Send message to customer contact + * @param boolean $forceDisplay Force display of formatted message even if target not exceeded + * @param boolean $forceSend Force sending of message even if target not exceeded + * + * @return string If forse + */ + public function checkForEmailNotifications($glmContact = true, $custContact = true, $forceDisplay = false, $forceSend = false) + { + $returnMsg = ''; + + $stats = $this->getStatsAndCheckTargetExceeded(); + + // If traffic had exceeded target + if ($stats['trafficDiff']['exceeded'] || $forceDisplay || $forceSend) { + + $month = date('M Y', strtotime('now -1 month')); + + // If GLM contact provided + if (trim($this->glm_contact) != '' && $glmContact) { + + // If stats were exceeded then use this message + if ($stats['trafficDiff']['exceeded']) { + + $mesg = ' +

Gaslight Media:

+

A Website has exceeded the agreed target traffic for '.$month.'.

+ + + + + +
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Last Month:'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes
Percent of Target:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
+

Sincerely,

+

Gaslight Media

+ '; + + } else { + + $mesg = ' +

Gaslight Media:

+

Website traffic total for '.$month.'.

+ + + + + +
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Last Month:'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes
Percent of Target:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
+

Sincerely,

+

Gaslight Media

+ '; + + } + $this->sendHtmlEmail($this->glm_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $mesg, $forceDisplay); + + } + + // If customer contact provided + if (trim($this->cust_contact) != '' && $custContact) { + + // If stats were exceeded then use this message + if ($stats['trafficDiff']['exceeded']) { + + $mesg = ' +

Dear Gaslight Media Website Owner:

+

Congradulations, your Website has exceeded the traffic target for '.$month.'.

+ + + + + +
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Last Month:'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes
Percent of Target:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
+

+ This is good because it means your Website is getting more traffic! +

+

+ Unlike other hosting providers, Gaslight Media doesn\'t automatically increase your monthly hosting fees simply because your Website exceeded the + agreed-upon service level for one month. Instead, we see this as an oportunity to look at your current and future needs first. If + you\'re likely to continue exceeding this traffic target, we can talk about a change in your hosting ageement to make sure you continue to + serve your Website users effectively. +

+

+ Please contact us if you have any questions about this or other concerns. Otherwise, we\'ll contact you soon to talk about the + good performance of your Website and discuss plans for the future. +

+

Sincerely,

+

+ Gaslight Media
+ 120 E. Lake Street
+ Petoskey, MI 49770
+ 231-487-0692 +

+ '; + + // Otherwise use this message when stats are not exceeded + } else { + + $mesg = ' +

Dear Gaslight Media Website Owner:

+

This is a review of your Website traffic for '.$month.'.

+ + + + + +
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Last Month:'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes
Percent of Target:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
+

+ Please contact us if you have any questions about this or other concerns. +

+

Sincerely,

+

+ Gaslight Media
+ 120 E. Lake Street
+ Petoskey, MI 49770
+ 231-487-0692 +

+ '; + + } + + $this->sendHtmlEmail($this->cust_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $mesg, $forceDisplay); + + } + + } // If traffic stats exceeded + + } + + // Ajax processing ends here + + + /** + * sendHtmlEmail + * + * Create html email and send using wp_mail. + * + * @param string $to To email address + * @param string $from From E-Mail address + * @param string $subject Subject line for the email + * @param string $htmlMessage Html message for the email + * @param boolean $forceDisplay Force display of message rather than send + * + * @access public + * @return void + */ + public function sendHtmlEmail( $to, $from, $subject, $msg, $forceDisplay = false ) + { + + // Display rather than send if requested or on a development system + if (GLM_SERVERSTATS_PLUGIN_HOST == 'DEVELOPMENT' || $forceDisplay) { + $msg = " +

Server Usage E-Mail - Test Mode

+ From: $from
+ To: $to
+ Reply-To: $from
+ Subject: $subject
+ + $msg +
+

 

+ "; + echo $msg; + return; + } + + // Send the message + $header = [ + 'Content-type: text/html;charset=utf-8', + "From: $from", + "Reply-To: $from" + ]; + wp_mail( $to, $subject, $msg, $header ); + + } + + + + } diff --git a/models/adminServerStats.php b/models/adminServerStats.php index 3aa2b6f..1ab3f10 100755 --- a/models/adminServerStats.php +++ b/models/adminServerStats.php @@ -63,7 +63,6 @@ class adminServerStats extends glmServerStatsBandwidthSupport $thisDateTime = false; $thisMonth = false; $thisMonthTime = false; - $websiteId = false; $websites = false; $settings = false; @@ -76,7 +75,6 @@ class adminServerStats extends glmServerStatsBandwidthSupport // Check for a selected site or use website in config $config = $this->getConfig(); $selectedSite = filter_input(INPUT_GET, 'selected_site', FILTER_SANITIZE_STRING); - $selectedSiteId = filter_input(INPUT_GET, 'site_id', FILTER_SANITIZE_INT); if (!$selectedSite) { $selectedSite = $config['website']; } @@ -172,7 +170,6 @@ class adminServerStats extends glmServerStatsBandwidthSupport $thisDate = date('m/d/Y', $thisDateTime); $thisMonthTime = strtotime(date('m/01/Y')); $thisMonth = date('m/Y'); - $websiteId = $this->siteId; $haveStats = true; diff --git a/models/ajaxServerUsageTargetCheck.php b/models/ajaxServerUsageTargetCheck.php index 5bdc424..df0c115 100755 --- a/models/ajaxServerUsageTargetCheck.php +++ b/models/ajaxServerUsageTargetCheck.php @@ -67,118 +67,21 @@ class ajaxServerUsageTargetCheck extends glmServerStatsBandwidthSupport */ public function model() { - - trigger_error('GLM Usage: Cron triggered Usage Target Check', E_USER_NOTICE); - - $stats = $this->getStatsAndCheckTargetExceeded(); - - // If traffic had exceeded target - if ($stats['trafficDiff']['exceeded']) { - - $month = date('M Y', strtotime('now -1 month')); - - // If GLM contact provided - if (trim($this->glm_contact) != '') { - - $mesg = ' -

Gaslight Media:

-

A Website has exceeded the agreed target traffic for '.$month.'.

- - - - - -
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Over Target:'.sprintf('%.3f', $stats['trafficDiff']['traffic']).' Gigabytes
Percent Over:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
-

Sincerely,

-

Gaslight Media

- '; - - $this->sendHtmlEmail($this->glm_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $mesg); - } - - // If customer contact provided - if (trim($this->cust_contact) != '') { - - $mesg = ' -

Dear Gaslight Media Website Owner:

-

Congradulations, your Website has exceeded the traffic target for '.$month.'.

- - - - - -
Website:'.$this->website.'
Target:'.sprintf('%.3f', $this->target).' Gigabytes
Over Target:'.sprintf('%.3f', $stats['trafficDiff']['traffic']).' Gigabytes
Percent Over:'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%
-

- This is good because it means your Website is getting more traffic! -

-

- Unlike other hosting providers, Gaslight Media doesn\'t automatically increase your monthly hosting fees simply because your Website exceeded the - agreed-upon service level for one month. Instead, we see this as an oportunity to look at your current and future needs first. If - you\'re likely to continue exceeding this traffic target, we can talk about a change in your hosting ageement to make sure you continue to - serve your Website users effectively. -

-

- Please contact us if you have any questions about this or other concerns. Otherwise, we\'ll contact you soon to talk about the - good performance of your Website and discuss plans for the future. -

-

Sincerely,

-

- Gaslight Media
- 120 E. Lake Street
- Petoskey, MI 49770
- 231-487-0692 -

- '; - - $this->sendHtmlEmail($this->cust_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $mesg); - } - - } + $request = filter_input_array( + INPUT_GET, + array( + 'glmContact' => FILTER_VALIDATE_BOOLEAN, + 'custContact' => FILTER_VALIDATE_BOOLEAN, + 'forceDisplay' => FILTER_VALIDATE_BOOLEAN, + 'forceSend' => FILTER_VALIDATE_BOOLEAN + ) + ); + + $this->checkForEmailNotifications($request['glmContact'], $request['custContact'], $request['forceDisplay'], $request['forceSend']); // Ajax processing ends here wp_die(); } - /** - * sendHtmlEmail - * - * Create html email and send using wp_mail. - * - * @param string $to To email address - * @param string $from From E-Mail address - * @param string $subject Subject line for the email - * @param string $htmlMessage Html message for the email - * - * @access public - * @return void - */ - public function sendHtmlEmail( $to, $from, $subject, $msg ) - { - - if (GLM_SERVERSTATS_PLUGIN_HOST == 'DEVELOPMENT') { - echo " -

Server Usage E-Mail - Development Mode

- From: $from
- To: $to
- Reply-To: $from
- Subject: $subject
- - $msg -
-

 

- "; - return; - } - - $header = [ - 'Content-type: text/html;charset=utf-8', - "From: $from", - "Reply-To: $from" - ]; - - wp_mail( $to, $subject, $msg, $header ); - - } - } diff --git a/views/adminServerStats.html b/views/adminServerStats.html index 7c917a6..a261420 100755 --- a/views/adminServerStats.html +++ b/views/adminServerStats.html @@ -92,6 +92,8 @@ Gaslight Media Contact E-Mail: +   Send Server Usage E-Mail Now +   Display E-Mail Now
E-mail address of Gaslight Media contact who will receive over-target E-Mail. @@ -99,6 +101,8 @@ Customer Contact E-Mail: +   Send Server Usage E-Mail Now +   Display E-Mail Now
E-mail address of customer contact who will receive over-target E-Mail. -- 2.17.1