Updating html emails with pixel tracking code
authorSteve Sutton <steve@gaslightmedia.com>
Fri, 16 Aug 2019 15:01:05 +0000 (11:01 -0400)
committerSteve Sutton <steve@gaslightmedia.com>
Fri, 16 Aug 2019 15:03:11 +0000 (11:03 -0400)
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.

13 files changed:
index.php
models/admin/ajax/ajaxMessagePreview.php
models/admin/ajax/newsletter.php
models/admin/ajax/trackMessagePixel.php [new file with mode: 0644]
models/admin/messages/index.php
models/admin/messages/sendMessagesEmails.php
setup/databaseScripts/create_database_V0.0.5.sql [deleted file]
setup/databaseScripts/create_database_V0.0.6.sql [new file with mode: 0644]
setup/databaseScripts/dbVersions.php
setup/databaseScripts/update_database_V0.0.6.sql [new file with mode: 0644]
setup/validActions.php
views/admin/messages/editHtmlEmail.html
views/email/messages/newsletter.html

index a0a08be..0cb6b98 100644 (file)
--- 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');
index 4d09191..3ea28cf 100644 (file)
@@ -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
index bcdac4c..6ec8d8d 100644 (file)
@@ -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 (file)
index 0000000..ac055b8
--- /dev/null
@@ -0,0 +1,140 @@
+<?php
+
+/**
+ * Gaslight Media Members Database
+ * PDF Output by admin-ajax
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @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 '<pre>$email: ' . print_r( $email, true ) . '</pre>';
+
+            echo '<pre>$process_id: ' . print_r( $process_id, true ) . '</pre>';
+
+            $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;
+
+    }
+
+
+}
index 2806eab..0e07735 100644 (file)
@@ -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 ) {
index d48d0d3..01cc9e2 100644 (file)
@@ -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.5.sql
deleted file mode 100644 (file)
index 5150848..0000000
+++ /dev/null
@@ -1,68 +0,0 @@
--- Gaslight Media Members Database
--- File Created: 5/24/19
--- Database Version: 0.0.5
--- 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,
-    template_id INT NOT NULL,
-    title TINYTEXT NULL,
-    image TINYTEXT NULL,
-    archived BOOLEAN DEFAULT false,
-    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,
-    footer TEXT 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,
-    email_read BOOLEAN DEFAULT false,
-    read_date DATETIME 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)
-);
-
-----
-
--- Template
-CREATE TABLE {prefix}email_templates (
-    id INT NOT NULL AUTO_INCREMENT,
-    name TEXT NOT NULL,
-    image TINYTEXT NULL,
-    footer TEXT NULL,
-    PRIMARY KEY (id)
-);
diff --git a/setup/databaseScripts/create_database_V0.0.6.sql b/setup/databaseScripts/create_database_V0.0.6.sql
new file mode 100644 (file)
index 0000000..d6a932b
--- /dev/null
@@ -0,0 +1,70 @@
+-- Gaslight Media Members Database
+-- File Created: 5/24/19
+-- Database Version: 0.0.6
+-- 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,
+    template_id INT NOT NULL,
+    title TINYTEXT NULL,
+    image TINYTEXT NULL,
+    archived BOOLEAN DEFAULT false,
+    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,
+    footer TEXT 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,
+    email_read BOOLEAN DEFAULT false,
+    read_date DATETIME NULL,
+    process_id TINYTEXT 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,
+    process_id TINYTEXT NOT NULL,
+    PRIMARY KEY (id)
+);
+
+----
+
+-- Template
+CREATE TABLE {prefix}email_templates (
+    id INT NOT NULL AUTO_INCREMENT,
+    name TEXT NOT NULL,
+    image TINYTEXT NULL,
+    footer TEXT NULL,
+    PRIMARY KEY (id)
+);
index 2f804f8..6e2d823 100644 (file)
@@ -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 (file)
index 0000000..b9ade98
--- /dev/null
@@ -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;
index 78fb726..b4f3da4 100644 (file)
@@ -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,
index dd47009..d8fe17d 100644 (file)
@@ -72,7 +72,7 @@
     </fieldset>
 
     <fieldset class="fieldset cell small-12 medium-6">
-        <legend>Message Options</legend>
+        <legend>Message Configuration</legend>
 
         {* Template
         {$ui = [
index 1aa258e..d5d07d5 100644 (file)
@@ -12,7 +12,7 @@
     {/literal}
   </head>
   <body style="-moz-box-sizing:border-box;-ms-text-size-adjust:100%;-webkit-box-sizing:border-box;-webkit-text-size-adjust:100%;Margin:0;background:#f3f3f3!important;box-sizing:border-box;color:#0a0a0a;font-family:Helvetica,Arial,sans-serif;font-size:16px;font-weight:400;line-height:1.3;margin:0;min-width:100%;padding:0;text-align:left;width:100%!important">
-    <span class="preheader" style="color:#f3f3f3;display:none!important;font-size:1px;line-height:1px;max-height:0;max-width:0;mso-hide:all!important;opacity:0;overflow:hidden;visibility:hidden"></span>
+    <span class="preheader" style="color:#f3f3f3;display:none!important;font-size:1px;line-height:1px;max-height:0;max-width:0;mso-hide:all!important;opacity:0;overflow:hidden;visibility:hidden">{if !empty($process_id)}<img style="display:none!important;width:1px;height:1px;visibility:hidden;" src="{$ajaxUrl}?action=glm_members_admin_ajax&glm_action=trackMessagePixel&process_id={$process_id}&email={$email}">{/if}</span>
     <table class="body" style="Margin:0;background:#f3f3f3!important;border-collapse:collapse;border-spacing:0;color:#0a0a0a;font-family:Helvetica,Arial,sans-serif;font-size:16px;font-weight:400;height:100%;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;width:100%">
       <tr style="padding:0;text-align:left;vertical-align:top">
         <td class="center" align="center" valign="top" style="-moz-hyphens:auto;-webkit-hyphens:auto;Margin:0;border-collapse:collapse!important;color:#0a0a0a;font-family:Helvetica,Arial,sans-serif;font-size:16px;font-weight:400;hyphens:auto;line-height:1.3;margin:0;padding:0;text-align:left;vertical-align:top;word-wrap:break-word">