From 8664a2c320459c98184f5c3f1ec514dfa5f0b329 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Mon, 15 Jan 2018 15:42:42 -0500 Subject: [PATCH] Setup 2 cron task style ajax models. 1 for storing id's into the queue. 2 for sending the queues. --- classes/notifications.php | 89 ++++++++++++++++++- index.php | 2 +- models/admin/ajax/runQueue.php | 84 +++++++++++++++++ .../ajax/{runCron.php => setupQueue.php} | 38 ++++++-- ..._V0.0.7.sql => create_database_V0.0.8.sql} | 10 +++ setup/databaseScripts/dbVersions.php | 1 + .../update_database_V0.0.8.sql | 14 +++ setup/validActions.php | 3 +- 8 files changed, 232 insertions(+), 9 deletions(-) create mode 100644 models/admin/ajax/runQueue.php rename models/admin/ajax/{runCron.php => setupQueue.php} (63%) rename setup/databaseScripts/{create_database_V0.0.7.sql => create_database_V0.0.8.sql} (97%) create mode 100644 setup/databaseScripts/update_database_V0.0.8.sql diff --git a/classes/notifications.php b/classes/notifications.php index eb3051b..90b8f83 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -81,6 +81,7 @@ class GlmNotifications // echo '
$account: ' . print_r( $account, true ) . '
'; // exit; $to_email = $account['email']; + // echo '
$to_email: ' . print_r( $to_email, true ) . '
'; if ( !$to_email ) { // If there's no email then return false. return false; @@ -89,7 +90,7 @@ class GlmNotifications // get the Notification type var_dump( $notification_id ); $notification_type = $this->getNotificationTypeById( $notification_id ); - // echo '
$notification_type: ' . print_r( $notification_type, true ) . '
'; + echo '
$notification_type: ' . print_r( $notification_type, true ) . '
'; if ( !$notification_type ) { // If there's no notification type then return false. return false; @@ -171,6 +172,17 @@ class GlmNotifications $this->recordNotification( $notice_data ); } + /** + * parseSmartyTemplateString + * + * + * + * @param mixed $data + * @param mixed $template_string + * + * @access public + * @return void + */ public function parseSmartyTemplateString( $data, $template_string ) { // Load Smarty Template support @@ -328,4 +340,79 @@ class GlmNotifications { return "text/html"; } + + /** + * queueNotice + * + * Queue the noticifation. + * + * @param mixed $notification_type Id for the notification type + * @param mixed $account Id for the account + * + * @access public + * @return void + */ + public function queueNotice( $notification_type, $account ) + { + // First check to see if there's already one in the queue. + $queue_id = $this->wpdb->get_var( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_queue + WHERE notification_type = %d + AND account = %d", + $notification_type, + $account + ) + ); + if ( !$queue_id ) { + $this->wpdb->insert( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notification_queue', + array( + 'notification_type' => $notification_type, + 'account' => $account + ), + array( + '%d', + '%d' + ) + ); + } + } + + /** + * getQueuedNotifications + * + * Grab all queued notifications. + * + * @access public + * @return array + */ + public function getQueuedNotifications() + { + return $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_queue", + ARRAY_A + ); + } + + /** + * deleteQueue + * + * Remove notice from the queue. + * + * @param mixed $queue_id Queue id to delete. + * + * @access public + * @return void + */ + public function deleteQueue( $queue_id ) + { + $this->wpdb->delete( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notification_queue', + array( 'id' => $queue_id ), + array( '%d' ) + ); + } } diff --git a/index.php b/index.php index bbf9b32..dd0bbab 100644 --- a/index.php +++ b/index.php @@ -38,7 +38,7 @@ * version from this plugin. */ define('GLM_MEMBERS_BILLING_PLUGIN_VERSION', '0.0.1'); -define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.7'); +define('GLM_MEMBERS_BILLING_PLUGIN_DB_VERSION', '0.0.8'); // This is the minimum version of the GLM Members DB plugin require for this plugin. define('GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION', '2.8.0'); diff --git a/models/admin/ajax/runQueue.php b/models/admin/ajax/runQueue.php new file mode 100644 index 0000000..2e4c5d4 --- /dev/null +++ b/models/admin/ajax/runQueue.php @@ -0,0 +1,84 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @version 0.1 + */ + +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; +require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; +/** + * Steve Note... + * + * You can get to this using the following URL. + * + * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=runCron + * + * 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. + * + */ + +// Load Members data abstract +// require_once GLM_MEMBERS_PLUGIN_CLASS_PATH.'/data/dataImages.php'; + +/** + * This class performs the work of handling images passed to it via + * an AJAX call that goes through the WorPress AJAX Handler. + * + */ +class GlmMembersAdmin_ajax_runQueue +{ + + /** + * WordPress Database Object + * + * @var $wpdb + * @access public + */ + public $wpdb; + /** + * Plugin Configuration Data + * + * @var $config + * @access public + */ + public $config; + + public function __construct ($wpdb, $config) + { + + // Save WordPress Database object + $this->wpdb = $wpdb; + + // Save plugin configuration object + $this->config = $config; + + } + + public function modelAction( $actionData = false ) { + $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + // Get the queue and send notices. + $queued = $Notifications->getQueuedNotifications(); + echo '
$queued: ' . print_r( $queued, true ) . '
'; + if ( isset( $queued ) && !empty( $queued ) ) { + foreach ( $queued as $queue ) { + $Notifications->sendEmailNotification( $queue['notification_type'], $queue['account'] ); + $Notifications->deleteQueue( $queue['id'] ); + } + } + wp_die(); + } +} diff --git a/models/admin/ajax/runCron.php b/models/admin/ajax/setupQueue.php similarity index 63% rename from models/admin/ajax/runCron.php rename to models/admin/ajax/setupQueue.php index a721efd..5e495ea 100644 --- a/models/admin/ajax/runCron.php +++ b/models/admin/ajax/setupQueue.php @@ -20,7 +20,7 @@ require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; * * You can get to this using the following URL. * - * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=pdfOutput&mystuff=THIS + * {host}/wp-admin/admin-ajax.php?action=glm_members_admin_ajax&glm_action=runCron * * 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 @@ -39,7 +39,7 @@ require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/billingSupport.php'; * an AJAX call that goes through the WorPress AJAX Handler. * */ -class GlmMembersAdmin_ajax_runCron +class GlmMembersAdmin_ajax_setupQueue { /** @@ -80,13 +80,15 @@ class GlmMembersAdmin_ajax_runCron echo '
$unpaid_invoices: ' . print_r( $unpaid_invoices, true ) . '
'; // Loop through the invoices and for each one see if any notices are to be sent out. + $currentDate = new DateTime(); + echo '
$currentDate: ' . print_r( $currentDate, true ) . '
'; foreach ( $unpaid_invoices as $invoice ) { $due_date = $invoice['due_date']; - $dueDate = new DateTime( $due_date ); - echo '
$dueDate 1: ' . print_r( $dueDate, true ) . '
'; // Loop through the notifications and see if this unpaid one is due. foreach ( $notifications as $notice ) { + // Need due date set in this loop each time. + $dueDate = new DateTime( $due_date ); // Is this before or after switch ( $notice['send_date_when'] ) { case $this->config['send_date_when_numb']['Before']: @@ -95,12 +97,36 @@ class GlmMembersAdmin_ajax_runCron '-' . $notice['send_date_number'] . ' '. $this->config['send_date_period'][$notice['send_date_period']] . (($notice['send_date_number'] > 1) ? 's': '') - ); - echo '
$dueDate 2: ' . print_r( $dueDate, true ) . '
'; + echo '
$dueDate: ' . print_r( $dueDate, true ) . '
'; + $diff = $dueDate->diff( $currentDate ); + // When the diff is 0 days then the notice needs to go out. + + $days_from = $diff->format( '%a' ); + echo '
$days_from: ' . print_r( $days_from, true ) . '
'; + // Check to see if this date occurs today. + if ( $days_from === '0' ) { + // Add the notice id and account id to the queue + $Notifications->queueNotice( $notice['id'], $invoice['account'] ); + } + break; case $this->config['send_date_when_numb']['After']: echo '

After

'; + $dueDate->modify( + '+' . $notice['send_date_number'] . ' '. + $this->config['send_date_period'][$notice['send_date_period']] . + (($notice['send_date_number'] > 1) ? 's': '') + ); + echo '
$dueDate: ' . print_r( $dueDate, true ) . '
'; + $diff = $dueDate->diff( $currentDate ); + $days_from = $diff->format( '%a' ); + echo '
$days_from: ' . print_r( $days_from, true ) . '
'; + // Check to see if this date occurs today. + if ( $days_from === '0' ) { + // Create the notice email + $Notifications->queueNotice( $notice['id'], $invoice['account'] ); + } break; default: break; diff --git a/setup/databaseScripts/create_database_V0.0.7.sql b/setup/databaseScripts/create_database_V0.0.8.sql similarity index 97% rename from setup/databaseScripts/create_database_V0.0.7.sql rename to setup/databaseScripts/create_database_V0.0.8.sql index 3ea2308..988d265 100644 --- a/setup/databaseScripts/create_database_V0.0.7.sql +++ b/setup/databaseScripts/create_database_V0.0.8.sql @@ -146,6 +146,16 @@ CREATE TABLE {prefix}notifications ( ---- +-- Notification Queue +CREATE TABLE {prefix}notification_queue ( + id INT NOT NULL AUTO_INCREMENT, + notification_type INT NOT NULL, -- ref to notification type + account INT NOT NULL, -- ref to account + PRIMARY KEY (id) +); + +---- + -- Gateway Settings CREATE TABLE {prefix}gateway_settings ( id INT NOT NULL AUTO_INCREMENT, diff --git a/setup/databaseScripts/dbVersions.php b/setup/databaseScripts/dbVersions.php index a96dcda..ad073e6 100644 --- a/setup/databaseScripts/dbVersions.php +++ b/setup/databaseScripts/dbVersions.php @@ -21,5 +21,6 @@ $glmMembersBillingDbVersions = array( '0.0.5' => array('version' => '0.0.5', 'tables' => 12), '0.0.6' => array('version' => '0.0.6', 'tables' => 13), '0.0.7' => array('version' => '0.0.7', 'tables' => 13), + '0.0.8' => array('version' => '0.0.8', 'tables' => 14), ); diff --git a/setup/databaseScripts/update_database_V0.0.8.sql b/setup/databaseScripts/update_database_V0.0.8.sql new file mode 100644 index 0000000..2590a71 --- /dev/null +++ b/setup/databaseScripts/update_database_V0.0.8.sql @@ -0,0 +1,14 @@ +-- Gaslight Media Billing Database +-- File Created: 1/15/2018 +-- Database Version: 0.0.8 +-- +-- To permit each query below to be executed separately, +-- all queries must be separated by a line with four dashes + +-- Notification Queue +CREATE TABLE {prefix}notification_queue ( + id INT NOT NULL AUTO_INCREMENT, + notification_type INT NOT NULL, -- ref to notification type + account INT NOT NULL, -- ref to account + PRIMARY KEY (id) +); diff --git a/setup/validActions.php b/setup/validActions.php index 3ec0ea8..c63fd14 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -60,7 +60,8 @@ $glmMembersBillingAddOnValidActions = array( 'adminActions' => array( 'ajax' => array( - 'runCron' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'setupQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + 'runQueue' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), 'management' => array( 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, -- 2.17.1