From e22d40a761cc63fed3166dc3407d306b1b2d10ee Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Fri, 16 Aug 2019 11:01:05 -0400 Subject: [PATCH] Updating html emails with pixel tracking code Generate a unique process_id for each sending of emails. Only use if process_id is given to setup a 1x1 tracking pixel to record if email was opened. --- index.php | 2 +- models/admin/ajax/ajaxMessagePreview.php | 2 +- models/admin/ajax/newsletter.php | 2 + models/admin/ajax/trackMessagePixel.php | 140 ++++++++++++++++++ models/admin/messages/index.php | 9 +- models/admin/messages/sendMessagesEmails.php | 2 + ..._V0.0.5.sql => create_database_V0.0.6.sql} | 4 +- setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.6.sql | 15 ++ setup/validActions.php | 1 + views/admin/messages/editHtmlEmail.html | 2 +- views/email/messages/newsletter.html | 2 +- 12 files changed, 175 insertions(+), 7 deletions(-) create mode 100644 models/admin/ajax/trackMessagePixel.php rename setup/databaseScripts/{create_database_V0.0.5.sql => create_database_V0.0.6.sql} (94%) create mode 100644 setup/databaseScripts/update_database_V0.0.6.sql diff --git a/index.php b/index.php index a0a08be..0cb6b98 100644 --- a/index.php +++ b/index.php @@ -46,7 +46,7 @@ if (!defined('ABSPATH')) { define('GLM_MEMBERS_MESSAGES_PLUGIN_VERSION', '0.0.1'); // DB Version -define('GLM_MEMBERS_MESSAGES_PLUGIN_DB_VERSION', '0.0.5'); +define('GLM_MEMBERS_MESSAGES_PLUGIN_DB_VERSION', '0.0.6'); // 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'); diff --git a/models/admin/ajax/ajaxMessagePreview.php b/models/admin/ajax/ajaxMessagePreview.php index 4d09191..3ea28cf 100644 --- a/models/admin/ajax/ajaxMessagePreview.php +++ b/models/admin/ajax/ajaxMessagePreview.php @@ -61,7 +61,7 @@ class GlmMembersAdmin_ajax_ajaxMessagePreview extends GlmMembersAdmin_messages_i public $ajaxSide = true; - public function __construct ($wpdb, $config) + public function __construct ( $wpdb, $config ) { // Save WordPress Database object diff --git a/models/admin/ajax/newsletter.php b/models/admin/ajax/newsletter.php index bcdac4c..6ec8d8d 100644 --- a/models/admin/ajax/newsletter.php +++ b/models/admin/ajax/newsletter.php @@ -115,6 +115,8 @@ class GlmMembersAdmin_ajax_newsletter extends GlmDataEmailMessages 'content' => $emailContent, 'footer' => $message['footer'], 'templateName' => $message['title'], + 'process_id' => false, + 'email' => false, ); // Load Smarty Template support diff --git a/models/admin/ajax/trackMessagePixel.php b/models/admin/ajax/trackMessagePixel.php new file mode 100644 index 0000000..ac055b8 --- /dev/null +++ b/models/admin/ajax/trackMessagePixel.php @@ -0,0 +1,140 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +/** + * Steve Note + * + * You can get to this using the following URL. + * + * + {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=runQueue + * + * You should be able to do this as POST or GET and should be able to add and read additional parameters. + * I added a "mystuff" parameter to the URL above and it does output from the code in the + * modelAction() function below. + * + * To add another model under models/admin/ajax all you need to do is create it and add it to the + * setup/validActions.php file. + * + */ + +/** + * This class handles the work of creating new invoices based on. + * 1) Member Type of member matching a paid invoiceType + * 2) Member renewal date past + * 3) Member has Billing Account + * 4) Member has no active Invoice + * 5) Renewal date is within the next 30 Days + * + */ +class GlmMembersAdmin_ajax_trackMessagePixel +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public $ajaxSide = true; + + public function __construct ( $wpdb, $config ) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + // parent::__construct( $this->wpdb, $this->config ); + + } + + /** + * Perform Model Action + * + * This method does the work for this model and returns any resulting data + * + * @return array Status and data array + * + * 'status' + * + * True if successfull and false if there was a fatal failure. + * + * 'menuItemRedirect' + * + * If not false, provides a menu item the controller should + * execute after this one. Normally if this is used, there would also be a + * modelRedirect value supplied as well. + * + * 'modelRedirect' + * + * If not false, provides an action the controller should execute after + * this one. + * + * 'view' + * + * A suggested view name that the contoller should use instead of the + * default view for this model or false to indicate that the default view + * should be used. + * + * 'data' + * + * Data that the model is returning for use in merging with the view to + * produce output. + * + */ + public function modelAction ( $actionData = false ) + { + + if ( isset( $_REQUEST['process_id'] ) && isset( $_REQUEST['email'] ) ) { + $email = filter_var( $_REQUEST['email'], FILTER_VALIDATE_EMAIL ); + $process_id = filter_var( $_REQUEST['process_id'], FILTER_SANITIZE_STRING ); + + echo '
$email: ' . print_r( $email, true ) . '
'; + + echo '
$process_id: ' . print_r( $process_id, true ) . '
'; + + $this->wpdb->update( + GLM_MEMBERS_MESSAGES_PLUGIN_DB_PREFIX . 'email_logs', + array( 'email_read' => true, 'read_date' => date( 'Y-m-d H:i:s' ) ), + array( 'to_email' => $email, 'process_id' => $process_id ), + array( '%s', '%s' ), + array( '%s', '%s' ) + ); + } + + $return = array( + true + ); + + // echo json_encode( $return, true ); + echo true; + return; + + } + + +} diff --git a/models/admin/messages/index.php b/models/admin/messages/index.php index 2806eab..0e07735 100644 --- a/models/admin/messages/index.php +++ b/models/admin/messages/index.php @@ -505,6 +505,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages $fromName = $message['from_name']; $replyToEmail = $message['reply_to_email']; $subject = $message['subject']; + $process_id = sha1( $messageId . '-' . date( 'Y-m-d H:i:s' ) ); if ( $messageId && $message && is_array( $data ) && !empty( $data ) ) { foreach ( $data as $memData ) { @@ -532,7 +533,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages 'mobile_phone' => $contact['mobile_phone'], ) ); - $messageBody = $this->generateHTML( $emailData, wpautop( $message['message_body'] ), $message ); + $messageBody = $this->generateHTML( $emailData, wpautop( $message['message_body'] ), $message, $process_id, $contact['email'] ); // Add this to the email_queue $this->wpdb->insert( @@ -546,6 +547,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages 'message_body' => $messageBody, 'queue_date' => date( 'Y-m-d H:i:s' ), 'processed' => false, + 'process_id' => $process_id, ), array( '%d', // message_id @@ -556,6 +558,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages '%s', // message_body '%s', // queue_date '%s', // processed + '%s', // process_id ) ); } @@ -582,7 +585,7 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages * @access public * @return void */ - function generateHTML( $data, $view, $message ) + function generateHTML( $data, $view, $message, $process_id, $email ) { // Load Smarty Template support. @@ -604,6 +607,8 @@ class GlmMembersAdmin_messages_index extends GlmDataEmailMessages '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 ) { diff --git a/models/admin/messages/sendMessagesEmails.php b/models/admin/messages/sendMessagesEmails.php index d48d0d3..01cc9e2 100644 --- a/models/admin/messages/sendMessagesEmails.php +++ b/models/admin/messages/sendMessagesEmails.php @@ -135,6 +135,7 @@ class GlmMembersAdmin_messages_sendMessagesEmails //extends GlmDataRegistrations 'reply_to_email' => $email['reply_to_email'], 'message_body' => '', 'send_date' => date( 'Y-m-d H:i:s' ), + 'process_id' => $email['process_id'], ), array( '%d', @@ -144,6 +145,7 @@ class GlmMembersAdmin_messages_sendMessagesEmails //extends GlmDataRegistrations '%s', '%s', '%s', + '%s', ) ); } diff --git a/setup/databaseScripts/create_database_V0.0.5.sql b/setup/databaseScripts/create_database_V0.0.6.sql similarity index 94% rename from setup/databaseScripts/create_database_V0.0.5.sql rename to setup/databaseScripts/create_database_V0.0.6.sql index 5150848..d6a932b 100644 --- a/setup/databaseScripts/create_database_V0.0.5.sql +++ b/setup/databaseScripts/create_database_V0.0.6.sql @@ -1,6 +1,6 @@ -- Gaslight Media Members Database -- File Created: 5/24/19 --- Database Version: 0.0.5 +-- Database Version: 0.0.6 -- Database Creation Script - Messages Add-On -- -- To permit each query below to be executed separately, @@ -37,6 +37,7 @@ CREATE TABLE {prefix}email_logs ( send_date DATETIME NOT NULL, email_read BOOLEAN DEFAULT false, read_date DATETIME NULL, + process_id TINYTEXT NOT NULL, PRIMARY KEY (id) ); @@ -53,6 +54,7 @@ CREATE TABLE {prefix}email_queue ( message_body TEXT NOT NULL, queue_date DATETIME NOT NULL, processed BOOLEAN DEFAULT false, + process_id TINYTEXT NOT NULL, PRIMARY KEY (id) ); diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index 2f804f8..6e2d823 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -19,5 +19,6 @@ $glmMembersMessagesDbVersions = array( '0.0.3' => array('version' => '0.0.3', 'tables' => 4, 'date' => '06/04/2019'), '0.0.4' => array('version' => '0.0.4', 'tables' => 4, 'date' => '06/07/2019'), '0.0.5' => array('version' => '0.0.5', 'tables' => 4, 'date' => '08/14/2019'), + '0.0.6' => array('version' => '0.0.6', 'tables' => 4, 'date' => '08/16/2019'), ); diff --git a/setup/databaseScripts/update_database_V0.0.6.sql b/setup/databaseScripts/update_database_V0.0.6.sql new file mode 100644 index 0000000..b9ade98 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.6.sql @@ -0,0 +1,15 @@ +-- Gaslight Media Members Database - Messages Add-On +-- File Created: 8/16/2019 +-- Database Version: 0.0.6 +-- 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 dashes + +-- Add process_id to email_queue +ALTER TABLE {prefix}email_queue ADD COLUMN process_id TINYTEXT NOT NULL; + +---- + +-- Add process_id to email_logs +ALTER TABLE {prefix}email_logs ADD COLUMN process_id TINYTEXT NOT NULL; diff --git a/setup/validActions.php b/setup/validActions.php index 78fb726..b4f3da4 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -62,6 +62,7 @@ $glmMembersMessagesAddOnValidActions = array( 'ajax' => array( 'newsletter' => GLM_MEMBERS_MESSAGES_PLUGIN_SLUG, 'ajaxMessagePreview' => GLM_MEMBERS_MESSAGES_PLUGIN_SLUG, + 'trackMessagePixel' => GLM_MEMBERS_MESSAGES_PLUGIN_SLUG, ), 'messages' => array( 'index' => GLM_MEMBERS_MESSAGES_PLUGIN_SLUG, diff --git a/views/admin/messages/editHtmlEmail.html b/views/admin/messages/editHtmlEmail.html index dd47009..d8fe17d 100644 --- a/views/admin/messages/editHtmlEmail.html +++ b/views/admin/messages/editHtmlEmail.html @@ -72,7 +72,7 @@
- Message Options + Message Configuration {* Template {$ui = [ diff --git a/views/email/messages/newsletter.html b/views/email/messages/newsletter.html index 1aa258e..d5d07d5 100644 --- a/views/email/messages/newsletter.html +++ b/views/email/messages/newsletter.html @@ -12,7 +12,7 @@ {/literal} - +
-- 2.17.1