Setup emails from the notifications.
authorSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Dec 2017 17:51:52 +0000 (12:51 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Thu, 14 Dec 2017 17:51:52 +0000 (12:51 -0500)
Send out notices.
I have a test link in settings > notification_types that allow test
sending of the notice. It's hard coded to the account with id 1 right
now.

classes/notifications.php
models/admin/settings/notificationTypes.php
views/admin/notifications/notification.html [new file with mode: 0644]

index 09461a6..c1048da 100644 (file)
@@ -68,18 +68,73 @@ class GlmNotifications
      * @access public
      * @return void
      */
-    public function sendEmailNotification( $notification_type, $account )
+    public function sendEmailNotification( $notification_id, $account )
     {
         // Support Class
         $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
         // get the Account
         $account = $BillingSupport->getAccountById( $account );
-        echo '<pre>$account: ' . print_r( $account, true ) . '</pre>';
+        if ( !$account ) {
+            // If there's no account then return false.
+            return false;
+        }
+        $to_email = $account['email'];
+        if ( !$to_email ) {
+            // If there's no email then return false.
+            return false;
+        }
 
         // get the Notification type
-        $notification_type = $this->getNotificationTypeById( $notification_type );
+        var_dump( $notification_id );
+        $notification_type = $this->getNotificationTypeById( $notification_id );
         echo '<pre>$notification_type: ' . print_r( $notification_type, true ) . '</pre>';
+        if ( !$notification_type ) {
+            // If there's no notification type then return false.
+            return false;
+        }
+        $subject  = $notification_type['subject'];
+        if ( !$subject ) {
+            // If there's no subject then return false.
+            return false;
+        }
+        $from_header = $notification_type['from_header'];
+        $replyto     = $notification_type['replyto'];
+
+        // Setup the Smarty Engine
+        $smarty   = new smartyTemplateSupport();
+        $viewPath = GLM_MEMBERS_BILLING_PLUGIN_PATH . '/views';
+        $smarty->template->setTemplateDir( $viewPath );
+
+        // Add standard parameters
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php';
+        $viewFile   = 'admin/notifications/notification.html';
+
+        $smarty->templateAssign( 'title', $notification_type['subject'] );
+        $smarty->templateAssign( 'html_content', wpautop( $notification_type['message'] ) );
+
+        // Generate output from model data and view
+        $htmlMessage = $smarty->template->fetch( $viewFile );
+
 
+        // change the default wordpress from name when sending mail
+        add_filter(
+            'wp_mail_from_name',
+            function ( $name ) {
+                $siteName = get_bloginfo( 'name' );
+                return $siteName;
+            }
+        );
+        // Send confirmation email, set the content type to allow html by using this filter
+        add_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
+
+        $message  = $htmlMessage;
+        $header[] = 'From:' . $from_header;
+        $header[] = 'Reply-To:' . $replyto;
+
+        wp_mail( $to_email, $subject, $message, $header );
+
+        // remove the filter to avoid conflicts
+        remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) );
     }
 
     /**
@@ -105,4 +160,13 @@ class GlmNotifications
         );
     }
 
+    /**
+     * Set content type of the email.
+     *
+     * Used as filter for the wp_mail_content_type
+     */
+    function set_content_type()
+    {
+        return "text/html";
+    }
 }
index b38a153..a5103bd 100644 (file)
@@ -147,9 +147,13 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType
             echo '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
             require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH .  '/notifications.php';
             $Notifications = new GlmNotifications( $this->wpdb, $this->config );
-            $notification_id = 5;
-            $account = 1;
-            $Notifications->sendEmailNotification( $notification_id, $account );
+            $notification_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT );
+            if ( $notification_id ) {
+                echo '<pre>$notification_id: ' . print_r( $notification_id, true ) . '</pre>';
+                $account = 1; // TODO: remove this. It's for testing only
+                $noticeReturned = $Notifications->sendEmailNotification( $notification_id, $account );
+                var_dump( $noticeReturned );
+            }
             wp_die('Testing here');
             break;
         case 'add':
diff --git a/views/admin/notifications/notification.html b/views/admin/notifications/notification.html
new file mode 100644 (file)
index 0000000..a94383d
--- /dev/null
@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html>
+    <head>
+        <meta http-equiv="content-type" content="text/html;charset=utf-8">
+    </head>
+    <body>
+        <h1 id="title"> {$title} </h1>
+        {$html_content}
+    </body>
+</html>