Added immediate send and display buttons for E-Mail in Service Provider Section
authorChuck Scott <cscott@gaslightmedia.com>
Tue, 6 Nov 2018 20:31:36 +0000 (15:31 -0500)
committerChuck Scott <cscott@gaslightmedia.com>
Tue, 6 Nov 2018 20:31:36 +0000 (15:31 -0500)
activate.php
classes/serverBandwidthSupport.php
models/adminServerStats.php
models/ajaxServerUsageTargetCheck.php
views/adminServerStats.html

index ca7338b..b921f35 100755 (executable)
@@ -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<P>",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.
index 2b84049..02abd78 100755 (executable)
@@ -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 = '
+                        <p>Gaslight Media:</p>
+                        <p>A Website has exceeded the agreed target traffic for '.$month.'.</p>
+                        <table>
+                            <tr><th align="left">Website:</th><td>'.$this->website.'</td>
+                            <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
+                            <tr><th align="left">Last Month:</th><td>'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes</td></tr>
+                            <tr style="color: red;"><th align="left" style="padding-right: 1em;">Percent of Target:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
+                        </table>
+                        <p>Sincerely,</p>
+                        <p>Gaslight Media</p>
+                    ';
+
+                } else {
+
+                    $mesg = '
+                        <p>Gaslight Media:</p>
+                        <p>Website traffic total for '.$month.'.</p>
+                        <table>
+                            <tr><th align="left">Website:</th><td>'.$this->website.'</td>
+                            <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
+                            <tr><th align="left">Last Month:</th><td>'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes</td></tr>
+                            <tr><th align="left" style="padding-right: 1em;">Percent of Target:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
+                        </table>
+                        <p>Sincerely,</p>
+                        <p>Gaslight Media</p>
+                    ';
+
+                }
+                $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 = '
+                        <p>Dear Gaslight Media Website Owner:</p>
+                        <p>Congradulations, your Website has exceeded the traffic target for '.$month.'.</p>
+                        <table>
+                            <tr><th align="left">Website:</th><td>'.$this->website.'</td>
+                            <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
+                            <tr><th align="left">Last Month:</th><td>'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes</td></tr>
+                            <tr style="color: red;"><th align="left" style="padding-right: 1em; color: red;">Percent of Target:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
+                        </table>
+                        <p>
+                            This is good because it means your Website is getting more traffic!
+                        </p>
+                        <p>
+                            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.
+                        </p>
+                        <p>
+                            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.
+                        </p>
+                        <p>Sincerely,</p>
+                        <p>
+                            Gaslight Media<br>
+                            120 E. Lake Street<br>
+                            Petoskey, MI 49770<br>
+                            231-487-0692
+                        </p>
+                    ';
+
+                // Otherwise use this message when stats are not exceeded
+                } else {
+
+                    $mesg = '
+                        <p>Dear Gaslight Media Website Owner:</p>
+                        <p>This is a review of your Website traffic for '.$month.'.</p>
+                        <table>
+                            <tr><th align="left">Website:</th><td>'.$this->website.'</td>
+                            <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
+                            <tr><th align="left">Last Month:</th><td>'.sprintf('%.3f', $stats['lastMonth']['data_total']).' Gigabytes</td></tr>
+                            <tr><th align="left" style="padding-right: 1em;">Percent of Target:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
+                        </table>
+                        <p>
+                            Please contact us if you have any questions about this or other concerns.
+                        </p>
+                        <p>Sincerely,</p>
+                        <p>
+                            Gaslight Media<br>
+                            120 E. Lake Street<br>
+                            Petoskey, MI 49770<br>
+                            231-487-0692
+                        </p>
+                    ';
+
+                }
+
+                $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 = "
+                <H2>Server Usage E-Mail - Test Mode</h2>
+                From: $from<br>
+                To: $to<br>
+                Reply-To: $from<br>
+                Subject: $subject<br>
+
+                $msg
+                <hr>
+                <p>&nbsp;</p>
+            ";
+            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 );
+
+    }
+
+
+
+
 }
index 3aa2b6f..1ab3f10 100755 (executable)
@@ -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;
 
index 5bdc424..df0c115 100755 (executable)
@@ -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 = '
-                    <p>Gaslight Media:</p>
-                    <p>A Website has exceeded the agreed target traffic for '.$month.'.</p>
-                    <table>
-                        <tr><th align="left">Website:</th><td>'.$this->website.'</td>
-                        <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
-                        <tr><th align="left">Over Target:</th><td>'.sprintf('%.3f', $stats['trafficDiff']['traffic']).' Gigabytes</td></tr>
-                        <tr><th align="left" style="padding-right: 1em;">Percent Over:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
-                    </table>
-                    <p>Sincerely,</p>
-                    <p>Gaslight Media</p>
-                ';
-
-                $this->sendHtmlEmail($this->glm_contact, 'info@gaslightmedia.com', 'Gaslight Media Server Usage Report', $mesg);
-            }
-
-            // If customer contact provided
-            if (trim($this->cust_contact) != '') {
-
-                $mesg = '
-                    <p>Dear Gaslight Media Website Owner:</p>
-                    <p>Congradulations, your Website has exceeded the traffic target for '.$month.'.</p>
-                    <table>
-                        <tr><th align="left">Website:</th><td>'.$this->website.'</td>
-                        <tr><th align="left">Target:</th><td>'.sprintf('%.3f', $this->target).' Gigabytes</td></tr>
-                        <tr><th align="left">Over Target:</th><td>'.sprintf('%.3f', $stats['trafficDiff']['traffic']).' Gigabytes</td></tr>
-                        <tr><th align="left" style="padding-right: 1em;">Percent Over:</th><td>'.sprintf('%.0f', $stats['trafficDiff']['percent']).'%</td></tr>
-                    </table>
-                    <p>
-                        This is good because it means your Website is getting more traffic!
-                    </p>
-                    <p>
-                        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.
-                    </p>
-                    <p>
-                        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.
-                    </p>
-                    <p>Sincerely,</p>
-                    <p>
-                        Gaslight Media<br>
-                        120 E. Lake Street<br>
-                        Petoskey, MI 49770<br>
-                        231-487-0692
-                    </p>
-                ';
-
-                $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 "
-                <H2>Server Usage E-Mail - Development Mode</h2>
-                From: $from<br>
-                To: $to<br>
-                Reply-To: $from<br>
-                Subject: $subject<br>
-
-                $msg
-                <hr>
-                <p>&nbsp;</p>
-            ";
-            return;
-        }
-
-        $header = [
-            'Content-type: text/html;charset=utf-8',
-            "From: $from",
-            "Reply-To: $from"
-        ];
-
-        wp_mail( $to, $subject, $msg, $header );
-
-    }
-
 }
index 7c917a6..a261420 100755 (executable)
@@ -92,6 +92,8 @@
                     <th class="glm-required">Gaslight Media Contact E-Mail:</th>
                     <td>
                         <input type="text" name="glm_contact" value="{$settings.glm_contact}" class="glm-form-text-input-medium">
+                        &nbsp;&nbsp;<a href="{$ajaxUrl}?action=glm_server_stats&glm_action=ajaxServerUsageTargetCheck&website={$selectedSite}&forceSend=true&custContact=false&glmContact=true" target="usageEmail" class="button button-secondary">Send Server Usage E-Mail Now</a>
+                        &nbsp;&nbsp;<a href="{$ajaxUrl}?action=glm_server_stats&glm_action=ajaxServerUsageTargetCheck&website={$selectedSite}&forceDisplay=true&custContact=false&glmContact=true" target="usageEmail" class="button button-secondary">Display E-Mail Now</a>
                         <br>E-mail address of Gaslight Media contact who will receive over-target E-Mail.
                     </td>
                 </tr>
                     <th class="glm-required">Customer Contact E-Mail:</th>
                     <td>
                         <input type="text" name="cust_contact" value="{$settings.cust_contact}" class="glm-form-text-input-medium">
+                        &nbsp;&nbsp;<a href="{$ajaxUrl}?action=glm_server_stats&glm_action=ajaxServerUsageTargetCheck&website={$selectedSite}&forceSend=true&custContact=true&glmContact=false" target="usageEmail" class="button button-secondary">Send Server Usage E-Mail Now</a>
+                        &nbsp;&nbsp;<a href="{$ajaxUrl}?action=glm_server_stats&glm_action=ajaxServerUsageTargetCheck&website={$selectedSite}&forceDisplay=true&custContact=true&glmContact=false" target="usageEmail" class="button button-secondary">Display E-Mail Now</a>
                         <br>E-mail address of customer contact who will receive over-target E-Mail.
                     </td>
                 </tr>