From: Steve Sutton Date: Fri, 30 Aug 2019 12:27:43 +0000 (-0400) Subject: Add ability to sent now for emails. X-Git-Url: http://cvs2.gaslightmedia.com/gitweb/?a=commitdiff_plain;h=ccd714715c02c8f719585d56959b9050ec157a21;p=WP-Plugins%2Fglm-member-db-messages.git Add ability to sent now for emails. Now has two buttons. Queue Messages: will send the emails into a queue running on wp-cron task every hour. Send Messages Now: will send the emails out. --- diff --git a/models/admin/messages/index.php b/models/admin/messages/index.php index 9c182d1..d6204f4 100644 --- a/models/admin/messages/index.php +++ b/models/admin/messages/index.php @@ -262,6 +262,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages $whereParts = array(); $catsToUse = array(); $emailsQueued = false; + $emailsSent = false; wp_enqueue_style( 'select2','https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css' ); wp_enqueue_script( 'select2js', 'https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/js/select2.min.js' ); @@ -271,8 +272,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages $Categories = new GlmDataCategories( $this->wpdb, $this->config ); $categories = $Categories->getListSortedParentChild( false ); - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - + // Filters if ( isset( $_REQUEST['filterMemberStatus'] ) && $filterStatus = filter_var( $_REQUEST['filterMemberStatus'], FILTER_VALIDATE_INT) ) { $whereParts[] = " T.ref_dest IN ( SELECT member FROM " . GLM_MEMBERS_PLUGIN_DB_PREFIX . "member_info WHERE status = " . $filterStatus . ")"; } else { @@ -334,8 +334,8 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages } if ( $catsToUse && $catsToUse !== '' ) { - $cats = ''; - $catsSep = ''; + // Reset $cats and $catSep + $cats = $catsSep = ''; // For each selected category foreach ( $catsToUse as $c ) { @@ -350,13 +350,12 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages ".GLM_MEMBERS_PLUGIN_DB_PREFIX."category_member_info M, ".GLM_MEMBERS_PLUGIN_DB_PREFIX."categories C WHERE I.id = M.member_info - AND I.status != " . $this->config['status_numb']['Archived'] ." + AND I.status != " . $this->config['status_numb']['Archived'] . " AND ( M.category in ($cats) OR (C.parent in ($cats) AND M.category = C.id) ) )"; - // $catSelectedString = $cats; } // Get a list of member_types for filtering @@ -382,8 +381,13 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages $messages = $this->getList( $where ); if ( $option2 === 'sendMessages' && isset( $_REQUEST['message_id'] ) && $messageId = filter_var( $_REQUEST['message_id'], FILTER_VALIDATE_INT ) ) { - $this->sendHtmlMessages( $searchResults, $messageId ); - $emailsQueued = true; + $sendNow = filter_var( $_REQUEST['sendNow'], FILTER_VALIDATE_BOOLEAN ); + $this->queueHtmlMessages( $searchResults, $messageId, $sendNow ); + if ( $sendNow ) { + $emailsSent = true; + } else { + $emailsQueued = true; + } } } @@ -416,6 +420,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages 'cities' => $cities, 'counties' => $counties, 'emailsQueued' => $emailsQueued, + 'emailsSent' => $emailsSent, ); break; @@ -495,7 +500,6 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages $Templates->deleteEntry( $id, true ); } - case 'listTemplates': $view = 'listTemplates'; $Templates = new GlmDataEmailTemplates( $this->wpdb, $this->config ); @@ -645,14 +649,14 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages } /** - * sendHtmlMessages + * queueHtmlMessages * - * Bulid the html email message for the each member and send it out. + * Bulid the html email message for the each member and queue it. * * @param $data Contact data from search * @param $message Message array */ - public function sendHtmlMessages( $data, $messageId ) + public function queueHtmlMessages( $data, $messageId, $sendNow = false ) { $message = $this->getEntry( $messageId ); $fromEmail = $message['from_email']; @@ -689,32 +693,67 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages ); $messageBody = $this->generateHTML( $emailData, wpautop( $message['message_body'] ), $message, $process_id, $contact['email'] ); - // Add this to the email_queue - $this->wpdb->insert( - GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX . 'email_queue', - array( - 'message_id' => $messageId, - 'to_email' => $contact['email'], - 'subject' => $subject, - 'from_email' => $fromEmail, - 'reply_to_email' => $replyToEmail, - 'message_body' => $messageBody, - 'queue_date' => date( 'Y-m-d H:i:s' ), - 'processed' => false, - 'process_id' => $process_id, - ), - array( - '%d', // message_id - '%s', // to_email - '%s', // subject - '%s', // from_email - '%s', // reply_to_email - '%s', // message_body - '%s', // queue_date - '%s', // processed - '%s', // process_id - ) - ); + if ( $sendNow ) { + $this->sendHtmlEmail( + $fromEmail, + $contact['email'], + $subject, + $messageBody, + $fromName, + $replyToEmail + ); + // Move this entry into the email_log + $this->wpdb->insert( + GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX . 'email_logs', + array( + 'message_id' => $messageId, + 'to_email' => $contact['email'], + 'subject' => $subject, + 'from_email' => $fromEmail, + 'reply_to_email' => $replyToEmail, + 'message_body' => '', + 'send_date' => date( 'Y-m-d H:i:s' ), + 'process_id' => $process_id, + ), + array( + '%d', // message_id + '%s', // to_email + '%s', // subject + '%s', // from_email + '%s', // reply_to_email + '%s', // message_body + '%s', // send_date + '%s', // process_id + ) + ); + } else { + // Add this to the email_queue + $this->wpdb->insert( + GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX . 'email_queue', + array( + 'message_id' => $messageId, + 'to_email' => $contact['email'], + 'subject' => $subject, + 'from_email' => $fromEmail, + 'reply_to_email' => $replyToEmail, + 'message_body' => $messageBody, + 'queue_date' => date( 'Y-m-d H:i:s' ), + 'processed' => false, + 'process_id' => $process_id, + ), + array( + '%d', // message_id + '%s', // to_email + '%s', // subject + '%s', // from_email + '%s', // reply_to_email + '%s', // message_body + '%s', // queue_date + '%s', // processed + '%s', // process_id + ) + ); + } } } } @@ -779,4 +818,49 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages } + /** + * 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 ( $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'; + } } diff --git a/models/admin/messages/sendMessagesEmails.php b/models/admin/messages/sendMessagesEmails.php index 53cb28c..129f43f 100644 --- a/models/admin/messages/sendMessagesEmails.php +++ b/models/admin/messages/sendMessagesEmails.php @@ -1,7 +1,7 @@ $lock: ' . print_r( $lock, true ) . ''; + // echo '
$lock: ' . print_r( $lock, true ) . '
'; if ( $lock && !empty( $lock ) ) { // Process Emails Queue diff --git a/views/admin/messages/search.html b/views/admin/messages/search.html index a578d08..a5cba2d 100644 --- a/views/admin/messages/search.html +++ b/views/admin/messages/search.html @@ -1,8 +1,33 @@ +{* + Search Form for Contacts + + Show search results and two options with select list of messages. + Option 1 Queue messages. + Option 2 Send Messages Now +*} + +{* header *} {include file='admin/header.html'}

Search

{if $searchResults} -
+ {* Emails Queued *} + {$ui = [ + 'label' => 'Emails Queued', + 'active' => $emailsQueued, + 'type' => 'success' + ]} + {include file='ui/f6/callout.html'} + {* Emails Sent *} + {$ui = [ + 'label' => 'Emails Sent', + 'active' => $emailsSent, + 'type' => 'success' + ]} + {include file='ui/f6/callout.html'} + + + @@ -43,7 +68,7 @@ {/if}
-
+
-
-
-
- +
+ + +
@@ -167,8 +192,6 @@ {/if} -{if $emailsQueued}Emails Queued{/if} - {if $searchResults} @@ -191,6 +214,12 @@