Search now queues the messages into the email_queue table.
Starting working on the cron task.
*/
public $fields = false;
+ public $postStats = false;
+
/**
* Constructor
*
*/
public function entryPostProcessing($r, $a)
{
+ if ( $this->postStats ) {
+ // get number of queued emails for this message.
+ $queued = $this->wpdb->get_var(
+ $this->wpdb->prepare(
+ "SELECT count(*)
+ FROM " . GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX . "email_queue
+ WHERE message_id = %d",
+ $r['id']
+ )
+ );
+ $r['stats'] = $queued;
+ }
return $r;
}
define('GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX', $wpdb->prefix.'glm_membersMessages_');
define('GLM_MEMBERS_MESSAGES_PLUGIN_ACTIVE_DB_OPTION', 'glmMembersMessagesDbVersion');
+// These two defines are used to make sure the queue is processed only once.
+define( 'GLM_MEMBERS_MESSAGES_QUEUE_LOCK', 'glmMembersMessageLock' );
+define( 'GLM_MEMBERS_MESSAGES_QUEUE_LOCK_TIMEOUT', '30' );
+
// Determine which system we're running on - If not provided, assume PRODUCTION
$host = getenv('GLM_HOST_ID');
if (trim($host) == '') {
* version from this plugin.
*/
define('GLM_MEMBERS_MESSAGES_PLUGIN_VERSION', '0.0.1');
-define('GLM_MEMBERS_MESSAGES_PLUGIN_DB_VERSION', '0.0.1');
+define('GLM_MEMBERS_MESSAGES_PLUGIN_DB_VERSION', '0.0.2');
// This is the minimum version of the GLM Members DB plugin require for this plugin.
define('GLM_MEMBERS_MESSAGES_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0');
case 'search':
$view = 'search';
$searchResults = array();
+ $messages = array();
$mTypeSelected = false;
$whereParts = array(
- "T.status != " . $this->config['status_numb']['Archived']
+ "T.status != " . $this->config['status_numb']['Archived']
);
wp_enqueue_style( 'select2','https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.6-rc.0/css/select2.min.css' );
// Get a list of member_types for filtering
require_once GLM_MEMBERS_PLUGIN_CLASS_PATH . '/data/dataMemberTypes.php';
- $MemberTypes = new GlmDataMemberTypes( $this->wpdb, $this->config );
+ $MemberTypes = new GlmDataMemberTypes( $this->wpdb, $this->config );
$member_types = $MemberTypes->getList();
// Check if there is a member_type filter
$where = implode( ' AND ', $whereParts );
// echo '<pre>$where: ' . print_r( $where, true ) . '</pre>';
- $searchResults = $Members->getList( $where );
+ $searchResults = $Members->getSimpleMemberInfoList( $where );
+ // echo '<pre>$searchResults: ' . print_r( $searchResults, true ) . '</pre>';
+
+ $messages = $this->getList();
+
+ if ( $option2 === 'sendMessages' && isset( $_REQUEST['message_id'] ) && $messageId = filter_var( $_REQUEST['message_id'], FILTER_VALIDATE_INT ) ) {
+ $this->sendHtmlMessages( $searchResults, $messageId );
+ }
}
$tData = array(
+ 'messages' => $messages,
'mTypeSelected' => $mTypeSelected,
'categories' => $categories,
'member_types' => $member_types,
case 'list':
$view = 'list';
+ $this->postStats = true;
$messages = $this->getList();
+ $this->postStats = false;
$tData = array(
'messages' => $messages,
);
}
+ /**
+ * sendHtmlMessages
+ *
+ * Bulid the html email message for the each member and send it out.
+ *
+ * @param $data Member data from search
+ * @param $message Message array
+ */
+ public function sendHtmlMessages( $data, $messageId )
+ {
+ // echo '<pre>$messageId: ' . print_r( $messageId, true ) . '</pre>';
+ // echo '<pre>$data: ' . print_r( $data, true ) . '</pre>';
+ $message = $this->getEntry( $messageId );
+ // echo '<pre>$message: ' . print_r( $message, true ) . '</pre>';
+ $fromEmail = $message['from_email'];
+ $fromName = $message['from_name'];
+ $replyToEmail = $message['reply_to_email'];
+ $subject = $message['subject'];
+
+ if ( $messageId && $message && is_array( $data ) && !empty( $data ) ) {
+ foreach ( $data as $memData ) {
+ // echo '<pre>$memData: ' . print_r( $memData, true ) . '</pre>';
+ // get member contact data
+ require_once GLM_MEMBERS_CONTACTS_PLUGIN_CLASS_PATH . '/data/dataContacts.php';
+ $Contacts = new GlmDataContacts( $this->wpdb, $this->config );
+ $whereParts = array();
+ $whereParts[] = "T.ref_dest = " . $memData['member_pointer'];
+ $whereParts[] = "T.active = true";
+ $whereParts[] = "T.email like '%@%'";
+ $where = implode( " AND ", $whereParts );
+ $memberContacts = $Contacts->getSimplified( $where );
+ // echo '<pre>$memberContacts: ' . print_r( $memberContacts, true ) . '</pre>';
+ if ( $memberContacts && !empty( $memberContacts ) ) {
+ foreach ( $memberContacts as $contact ) {
+ $emailData = array(
+ 'member' => array(
+ 'name' => $memData['member'],
+ ),
+ 'contact' => array(
+ 'fname' => $contact['fname'],
+ 'lname' => $contact['lname'],
+ )
+ );
+ $messageBody = $this->generateHTML( $emailData, $message['message_body'], true );
+
+ // 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,
+ ),
+ array(
+ '%d', // message_id
+ '%s', // to_email
+ '%s', // subject
+ '%s', // from_email
+ '%s', // reply_to_email
+ '%s', // message_body
+ '%s', // queue_date
+ '%s', // processed
+ )
+ );
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * 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, $viewIsString = false )
+ {
+
+ // 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 );
+ }
+ }
+
+
+ // If is supplied as a string
+ if ( $viewIsString ) {
+
+ $out = $smarty->template->fetch( 'eval:' . $view );
+
+ // Otherwise $view is a file name
+ } else {
+
+ // Get the specified view file - check theme first
+ $viewPath = GLM_MEMBERS_PLUGIN_CURRENT_THEME_DIR . '/views';
+ $viewPath2 = GLM_MEMBERS_WORDPRESS_PLUGIN_PATH . 'views'; // Save default
+
+ // If the view is not found in the theme, fall back to views in the plugin
+ if ( !is_file( $viewPath . '/' . $view ) ) {
+
+ // Next try the plugin/add-on
+ $viewPath = GLM_MEMBERS_REGISTRATIONS_PLUGIN_PATH . "/views";
+
+ if ( !is_file( $viewPath . '/' . $view ) ) {
+
+ if ( GLM_MEMBERS_PLUGIN_FRONT_DEBUG ) {
+ trigger_error( "Bad or missing view file when generating checkout HTML: $viewPath/$view", E_USER_NOTICE );
+ }
+
+ }
+
+ }
+
+ // Update the Smarty view path
+ $smarty->template->setTemplateDir( $viewPath );
+
+ // If the view path doesn't match the default, add the default (using theme view)
+ if ( $viewPath2 != $viewPath ) {
+ $smarty->template->addTemplateDir( $viewPath2 );
+ }
+
+ // Generate output from model data and view
+ $out = $smarty->template->fetch( $view );
+
+ }
+
+ 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 )
+ {
+
+ // Set the From name using this wordpress hook.
+ add_filter(
+ 'wp_mail_from_name',
+ function ( $name ) {
+ return $this->config['settings']['reg_org_name'];
+ }
+ );
+ // 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:' . $fromAddress;
+ $header[] = 'Reply-To:' . $this->config['settings']['reg_org_from_email'];
+ 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';
+ }
}
10,
2
);
+
+/**
+ * Setup cron task request to run SendMessagesEmails
+ *
+ * Run only once per day. Everyday.
+ */
+// add_filter(
+// 'glm_associate_cron_request',
+// function( $cron_task ){
+// $new_cron = array(
+// array(
+// 'menu' => 'ajax',
+// 'action' => 'SendMessagesEmails',
+// 'daysOfWeek' => false,
+// 'times' => false,
+// 'params' => array()
+// )
+// );
+// return array_merge( $cron_task, $new_cron );
+// }
+// );
+++ /dev/null
--- Gaslight Media Members Database
--- File Created: 5/24/19
--- Database Version: 0.0.1
--- Database Creation Script - Messages Add-On
---
--- To permit each query below to be executed separately,
--- all queries must be separated by a line with four dashes
-
--- email_messages
-CREATE TABLE {prefix}email_messages (
- id INT NOT NULL AUTO_INCREMENT,
- from_email TINYTEXT NOT NULL,
- from_name TINYTEXT NULL,
- reply_to_email TINYTEXT NULL,
- subject TINYTEXT NOT NULL,
- last_updated DATETIME NOT NULL,
- message_body TEXT NOT NULL,
- PRIMARY KEY (id)
-);
-
-----
-
--- Logs
-CREATE TABLE {prefix}email_logs (
- id INT NOT NULL AUTO_INCREMENT,
- to_email TINYTEXT NOT NULL,
- subject TINYTEXT NOT NULL,
- from_email TINYTEXT NOT NULL,
- reply_to_email TINYTEXT NULL,
- message_body TEXT NOT NULL,
- send_date DATETIME NOT NULL,
- PRIMARY KEY (id)
-);
-
-----
-
--- Queue
-CREATE TABLE {prefix}email_queue (
- id INT NOT NULL AUTO_INCREMENT,
- to_email TINYTEXT NOT NULL,
- subject TINYTEXT NOT NULL,
- from_email TINYTEXT NOT NULL,
- reply_to_email TINYTEXT NULL,
- message_body TEXT NOT NULL,
- send_date DATETIME NOT NULL,
- processed BOOLEAN DEFAULT false,
- PRIMARY KEY (id)
-);
--- /dev/null
+-- Gaslight Media Members Database
+-- File Created: 5/24/19
+-- Database Version: 0.0.2
+-- Database Creation Script - Messages Add-On
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashes
+
+-- email_messages
+CREATE TABLE {prefix}email_messages (
+ id INT NOT NULL AUTO_INCREMENT,
+ from_email TINYTEXT NOT NULL,
+ from_name TINYTEXT NULL,
+ reply_to_email TINYTEXT NULL,
+ subject TINYTEXT NOT NULL,
+ last_updated DATETIME NOT NULL,
+ message_body TEXT NOT NULL,
+ PRIMARY KEY (id)
+);
+
+----
+
+-- Logs
+CREATE TABLE {prefix}email_logs (
+ id INT NOT NULL AUTO_INCREMENT,
+ message_id INT NOT NULL,
+ to_email TINYTEXT NOT NULL,
+ subject TINYTEXT NOT NULL,
+ from_email TINYTEXT NOT NULL,
+ reply_to_email TINYTEXT NULL,
+ message_body TEXT NOT NULL,
+ send_date DATETIME NOT NULL,
+ PRIMARY KEY (id)
+);
+
+----
+
+-- Queue
+CREATE TABLE {prefix}email_queue (
+ id INT NOT NULL AUTO_INCREMENT,
+ message_id INT NOT NULL,
+ to_email TINYTEXT NOT NULL,
+ subject TINYTEXT NOT NULL,
+ from_email TINYTEXT NOT NULL,
+ reply_to_email TINYTEXT NULL,
+ message_body TEXT NOT NULL,
+ queue_date DATETIME NOT NULL,
+ processed BOOLEAN DEFAULT false,
+ PRIMARY KEY (id)
+);
$glmMembersMessagesDbVersions = array(
'0.0.1' => array('version' => '0.0.1', 'tables' => 2, 'date' => '05/24/2019'),
+ '0.0.2' => array('version' => '0.0.2', 'tables' => 3, 'date' => '05/30/2019'),
);
--- /dev/null
+-- Gaslight Media Members Database - Messages Add-On
+-- File Created: 5/30/2019
+-- Database Version: 0.0.2
+-- Database Update From Previous Version Script
+--
+-- To permit each query below to be executed separately,
+-- all queries must be separated by a line with four dashses
+
+
+-- Add message_id to log table
+ALTER TABLE {prefix}email_logs ADD COLUMN message_id INT NOT NULL;
+
+----
+
+-- Queue
+CREATE TABLE {prefix}email_queue (
+ id INT NOT NULL AUTO_INCREMENT,
+ message_id INT NOT NULL,
+ to_email TINYTEXT NOT NULL,
+ subject TINYTEXT NOT NULL,
+ from_email TINYTEXT NOT NULL,
+ reply_to_email TINYTEXT NULL,
+ message_body TEXT NOT NULL,
+ queue_date DATETIME NOT NULL,
+ processed BOOLEAN DEFAULT false,
+ PRIMARY KEY (id)
+);
<th align="left">Subject</th>
<th align="left">Last Updated</th>
<th align="left">Sent</th>
- <th align="left">Stats</th>
+ <th align="left">Queued</th>
</tr>
</thead>
<tbody>
<td> <a href="{$thisUrl}?page={$thisPage}&option=editHtmlEmail&id={$message.id}">{$message.subject}</a> </td>
<td> {$message.last_updated.timestamp|date_format:"%D %r"} </td>
<td> </td>
- <td> </td>
+ <td> {$message.stats} </td>
</tr>
{/foreach}
{/if}
<td>
<select name="message_id" required>
<option value="">-- Select --</option>
- <option value="x">Message One</option>
- <option value="x">Message Two</option>
+ {foreach $messages as $message}
+ <option value="{$message.id}">{$message.subject}</option>
+ {/foreach}
</select>
</td>
</tr>