Refactoring some common code.
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 30 Aug 2019 17:03:22 +0000 (13:03 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 30 Aug 2019 17:03:22 +0000 (13:03 -0400)
Two places were using mail functions. Placing the mail functions into a
mailer.php in classes. It has a namespace setup for it. GlmMessages.

classes/data/dataEmailMessages.php
classes/mailer.php [new file with mode: 0644]
models/admin/messages/index.php
models/admin/messages/sendMessagesEmails.php

index 2788f2b..eab54a3 100644 (file)
@@ -254,40 +254,4 @@ class GlmDataEmailMessages extends GlmDataAbstract
     }
 
 
-    public function fixImages( $content )
-    {
-        $dom           = new DOMDocument( '1.0', 'UTF-8' );
-        $dom->encoding = 'UTF-8';
-        $test          = $dom->loadHTML( '<?xml encoding="UTF-8">' . $content );
-        $images        = $dom->getElementsByTagName( 'img' );
-        foreach ( $images as $image ) {
-            $width = $height = $align = null;
-            if ( $image->hasAttribute( 'class' ) ) {
-                // Is it left or right or center?
-                if ( preg_match( '/alignleft/', $image->getAttribute('class'))) {
-                    $align = 'left';
-                }
-                if ( preg_match( '/alignright/', $image->getAttribute('class'))) {
-                    $align = 'right';
-                }
-                if ( preg_match( '/aligncenter/', $image->getAttribute('class'))) {
-                    $align = 'center';
-                }
-            }
-            switch ( $align ) {
-            case 'left':
-                $image->setAttribute( 'style', 'float: left; margin: 10px;' );
-                break;
-            case 'right':
-                $image->setAttribute( 'style', 'float: right; margin: 10px;' );
-                break;
-            case 'center':
-                $image->setAttribute( 'style', 'display: block; text-align: center; margin: 0 auto;' );
-                break;
-            }
-        }
-        $content = $dom->saveHTML();
-        return $content;
-    }
-
 }
diff --git a/classes/mailer.php b/classes/mailer.php
new file mode 100644 (file)
index 0000000..bc9ce51
--- /dev/null
@@ -0,0 +1,148 @@
+<?php
+namespace GlmMessages;
+
+/**
+ * Class mailer
+ *
+ * Deals with creating the html body and sending of emails.
+ */
+class mailer
+{
+    /**
+     * sendHtmlEmail
+     *
+     * Create html email and send using wp_mail.
+     *
+     * @param mixed $to          To email address
+     * @param mixed $subject     Subject line for the email
+     * @param mixed $htmlMessage Html message for the email
+     *
+     * @access public
+     * @return void
+     */
+    public function sendHtmlEmail( $from, $to, $subject, $htmlMessage, $fromName, $replyTo = null )
+    {
+
+        // Set the From name using this wordpress hook.
+        add_filter(
+            'wp_mail_from_name',
+            function ( $name ) use ( $fromName ) {
+                return $fromName;
+            }
+        );
+        // Send confirmation email, set the content type to allow html by using this filter
+        add_filter( 'wp_mail_content_type', function() {
+            return 'text/html';
+        } );
+
+        $message  = $htmlMessage;
+        $header[] = 'From:' . $from;
+        if ( $replyTo ) {
+            $header[] = 'Reply-To:' . $replyTo;
+        }
+
+        wp_mail( $to, $subject, $message, $header );
+
+        remove_filter( 'wp_mail_from_name' );
+        remove_filter( 'wp_mail_content_type' );
+    }
+
+    /**
+     * Merge template and data to produce HTML
+     *
+     * Checks the theme's view directories and the view directories for
+     * this plugin for a matching view file.
+     *
+     * Note that $view needs to have the proper view directory path
+     * includes. (i.e. "/views/front/registrations/summary.html")
+     *
+     * $view may also be a template as a string if $viewIsString is true.
+     *
+     * @param $data array Array of data to merge with the template
+     * @param $view string Name of view file (see above))
+     * @param $viewIsString boolean If true, $view is a string containing the view.
+     *
+     * @access public
+     * @return void
+     */
+    function generateHTML( $data, $view, $message, $process_id, $email )
+    {
+
+        // Load Smarty Template support.
+        $smarty = new \smartyTemplateSupport();
+
+        // Add standard parameters.
+        require GLM_MEMBERS_PLUGIN_SETUP_PATH . '/standardTemplateParams.php';
+
+        // Add data from model to Smarty template.
+        if ( is_array( $data ) && count( $data ) > 0 ) {
+            foreach ( $data as $k => $d ) {
+                $smarty->templateAssign( $k, $d );
+            }
+        }
+
+        $emailContent = $smarty->template->fetch( 'eval:' . $this->fixImages( $view ) );
+        $tData        = array(
+            'image'        => $message['image'],
+            'content'      => $emailContent,
+            'footer'       => $message['footer'],
+            'templateName' => $message['title'],
+            'process_id'   => $process_id,
+            'email'        => $email,
+        );
+        if ( is_array( $tData ) && count( $tData ) > 0 ) {
+            foreach ( $tData as $k => $d ) {
+                $smarty->templateAssign( $k, $d );
+            }
+        }
+
+        $viewPath = GLM_MEMBERS_MESSAGES_PLUGIN_PATH . '/views';
+        $smarty->template->setTemplateDir( $viewPath );
+        $viewFile = 'email/messages/newsletter.html';
+        $out      = $smarty->template->fetch( $viewFile );
+
+        return $out;
+
+    }
+
+    /**
+     * Deal with the images.
+     *
+     * This will update the styles for images so they float properly in the email.
+     */
+    public function fixImages( $content )
+    {
+        $dom           = new \DOMDocument( '1.0', 'UTF-8' );
+        $dom->encoding = 'UTF-8';
+        $test          = $dom->loadHTML( '<?xml encoding="UTF-8">' . $content );
+        $images        = $dom->getElementsByTagName( 'img' );
+        foreach ( $images as $image ) {
+            $width = $height = $align = null;
+            if ( $image->hasAttribute( 'class' ) ) {
+                // Is it left or right or center?
+                if ( preg_match( '/alignleft/', $image->getAttribute('class'))) {
+                    $align = 'left';
+                }
+                if ( preg_match( '/alignright/', $image->getAttribute('class'))) {
+                    $align = 'right';
+                }
+                if ( preg_match( '/aligncenter/', $image->getAttribute('class'))) {
+                    $align = 'center';
+                }
+            }
+            switch ( $align ) {
+            case 'left':
+                $image->setAttribute( 'style', 'float: left; margin: 10px;' );
+                break;
+            case 'right':
+                $image->setAttribute( 'style', 'float: right; margin: 10px;' );
+                break;
+            case 'center':
+                $image->setAttribute( 'style', 'display: block; text-align: center; margin: 0 auto;' );
+                break;
+            }
+        }
+        $content = $dom->saveHTML();
+        return $content;
+    }
+}
index 15b277a..804caa9 100644 (file)
@@ -17,6 +17,9 @@
 require_once GLM_MEMBERS_MESSAGES_PLUGIN_CLASS_PATH.'/data/dataEmailMessages.php';
 require_once GLM_MEMBERS_MESSAGES_PLUGIN_CLASS_PATH.'/data/dataEmailTemplates.php';
 require_once GLM_MEMBERS_MESSAGES_PLUGIN_CLASS_PATH.'/data/dataEmailLogs.php';
+require_once GLM_MEMBERS_MESSAGES_PLUGIN_CLASS_PATH.'/mailer.php';
+
+use GlmMessages\mailer as glmMailer;
 
 /*
  * This class performs the work for the default action of the "Members" menu
@@ -658,7 +661,8 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages
      */
     public function queueHtmlMessages( $data, $messageId, $sendNow = false )
     {
-        $message = $this->getEntry( $messageId );
+        $glmMailer    = new glmMailer();
+        $message      = $this->getEntry( $messageId );
         $fromEmail    = $message['from_email'];
         $fromName     = $message['from_name'];
         $replyToEmail = $message['reply_to_email'];
@@ -691,10 +695,10 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages
                                 'mobile_phone' => $contact['mobile_phone'],
                             )
                         );
-                        $messageBody = $this->generateHTML( $emailData, wpautop( $message['message_body'] ), $message, $process_id, $contact['email'] );
+                        $messageBody = $glmMailer->generateHTML( $emailData, wpautop( $message['message_body'] ), $message, $process_id, $contact['email'] );
 
                         if ( $sendNow ) {
-                            $this->sendHtmlEmail(
+                            $glmMailer->sendHtmlEmail(
                                 $fromEmail,
                                 $contact['email'],
                                 $subject,
@@ -760,108 +764,4 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages
         }
     }
 
-    /**
-     * Merge template and data to produce HTML
-     *
-     * Checks the theme's view directories and the view directories for
-     * this plugin for a matching view file.
-     *
-     * Note that $view needs to have the proper view directory path
-     * includes. (i.e. "/views/front/registrations/summary.html")
-     *
-     * $view may also be a template as a string if $viewIsString is true.
-     *
-     * @param $data array Array of data to merge with the template
-     * @param $view string Name of view file (see above))
-     * @param $viewIsString boolean If true, $view is a string containing the view.
-     *
-     * @access public
-     * @return void
-     */
-    function generateHTML( $data, $view, $message, $process_id, $email )
-    {
-
-        // Load Smarty Template support.
-        $smarty = new smartyTemplateSupport();
-
-        // Add standard parameters.
-        require GLM_MEMBERS_PLUGIN_SETUP_PATH . '/standardTemplateParams.php';
-
-        // Add data from model to Smarty template.
-        if ( is_array( $data ) && count( $data ) > 0 ) {
-            foreach ( $data as $k => $d ) {
-                $smarty->templateAssign( $k, $d );
-            }
-        }
-
-        $emailContent = $smarty->template->fetch( 'eval:' . $this->fixImages( $view ) );
-        $tData        = array(
-            'image'        => $message['image'],
-            'content'      => $emailContent,
-            'footer'       => $message['footer'],
-            'templateName' => $message['title'],
-            'process_id'   => $process_id,
-            'email'        => $email,
-        );
-        if ( is_array( $tData ) && count( $tData ) > 0 ) {
-            foreach ( $tData as $k => $d ) {
-                $smarty->templateAssign( $k, $d );
-            }
-        }
-
-        $viewPath = GLM_MEMBERS_MESSAGES_PLUGIN_PATH . '/views';
-        $smarty->template->setTemplateDir( $viewPath );
-        $viewFile = 'email/messages/newsletter.html';
-        $out      = $smarty->template->fetch( $viewFile );
-
-        return $out;
-
-    }
-
-    /**
-     * sendHtmlEmail
-     *
-     * Create html email and send using wp_mail.
-     *
-     * @param mixed $to          To email address
-     * @param mixed $subject     Subject line for the email
-     * @param mixed $htmlMessage Html message for the email
-     *
-     * @access public
-     * @return void
-     */
-    public function sendHtmlEmail( $from, $to, $subject, $htmlMessage, $fromName, $replyTo = null )
-    {
-
-        // Set the From name using this wordpress hook.
-        add_filter(
-            'wp_mail_from_name',
-            function ( $name ) use ( $fromName ) {
-
-                return $fromName;
-            }
-        );
-        // 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;
-        if ( $replyTo ) {
-            $header[] = 'Reply-To:' . $replyTo;
-        }
-
-        wp_mail( $to, $subject, $message, $header );
-
-    }
-
-    /**
-     * Set content type of the email.
-     *
-     * Used as filter for the wp_mail_content_type
-     */
-    function set_content_type()
-    {
-        return 'text/html';
-    }
 }
index 4871ecf..1e2675a 100644 (file)
  * @link     http://dev.gaslightmedia.com/
  */
 
+require_once GLM_MEMBERS_MESSAGES_PLUGIN_CLASS_PATH.'/mailer.php';
+
+use GlmMessages\mailer as glmMailer;
+
+/**
+ * Send Messages Emails class
+ *
+ * Grab all queued emails and sends them out.
+ */
 class GlmMembersAdmin_messages_sendMessagesEmails
 {
 
@@ -80,6 +89,7 @@ class GlmMembersAdmin_messages_sendMessagesEmails
     // $notiicationTest to true to just display notification messages
     public function modelAction( $actionData = false )
     {
+        $glmMailer    = new glmMailer();
         trigger_error( 'sendMessagesEmail started', E_USER_NOTICE );
 
         // Request a Lock
@@ -103,7 +113,7 @@ class GlmMembersAdmin_messages_sendMessagesEmails
 
             if ( $emailQueues ) {
                 foreach ( $emailQueues as $email ) {
-                    $this->sendHtmlEmail(
+                    $glmMailer->sendHtmlEmail(
                         $email['from_email'],
                         $email['to_email'],
                         $email['subject'],
@@ -165,49 +175,4 @@ class GlmMembersAdmin_messages_sendMessagesEmails
 
     }
 
-    /**
-     * sendHtmlEmail
-     *
-     * Create html email and send using wp_mail.
-     *
-     * @param mixed $to          To email address
-     * @param mixed $subject     Subject line for the email
-     * @param mixed $htmlMessage Html message for the email
-     *
-     * @access public
-     * @return void
-     */
-    public function sendHtmlEmail( $from, $to, $subject, $htmlMessage, $fromName, $replyTo = null )
-    {
-
-        // Set the From name using this wordpress hook.
-        add_filter(
-            'wp_mail_from_name',
-            function ( $name ) use ( $fromName ) {
-                return $fromName;
-            }
-        );
-        // 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;
-        if ( $replyTo ) {
-            $header[] = 'Reply-To:' . $replyTo;
-        }
-
-        wp_mail( $to, $subject, $message, $header );
-
-    }
-
-    /**
-     * Set content type of the email.
-     *
-     * Used as filter for the wp_mail_content_type
-     */
-    function set_content_type()
-    {
-        return 'text/html';
-    }
 }