From 873ba0dfd4082b2c4cdf45d901641b13f32b3a0f Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Sat, 13 Jan 2018 08:15:21 -0500 Subject: [PATCH] WIP Setting up cron action for getting notifications. Still working on it. --- classes/billingSupport.php | 21 +++- classes/notifications.php | 45 +++++++- config/plugin.ini | 3 + defines.php | 4 + index.php | 78 ++++++++------ misc/documentation/Notifications.txt | 11 ++ models/admin/ajax/runCron.php | 113 ++++++++++++++++++++ models/admin/settings/notificationTypes.php | 12 --- setup/adminHooks.php | 18 ---- setup/commonHooks.php | 28 +++++ setup/validActions.php | 3 + 11 files changed, 270 insertions(+), 66 deletions(-) create mode 100644 misc/documentation/Notifications.txt create mode 100644 models/admin/ajax/runCron.php create mode 100644 setup/commonHooks.php diff --git a/classes/billingSupport.php b/classes/billingSupport.php index 768ffeb..dc5726f 100644 --- a/classes/billingSupport.php +++ b/classes/billingSupport.php @@ -419,7 +419,7 @@ class GlmBillingSupport * @param mixed $account Id of the account. * * @access public - * @return void + * @return array */ public function getUnPaidInvoicesByAccount( $account ) { @@ -436,6 +436,25 @@ class GlmBillingSupport ); } + /** + * getUnPaidInvoices + * + * Get All Un Paid Invoices + * + * @access public + * @return array + */ + public function getUnPaidInvoices() + { + return $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "invoices + WHERE paid <> true + ORDER BY transaction_time ASC", + ARRAY_A + ); + } + /** * getPaymentById * diff --git a/classes/notifications.php b/classes/notifications.php index afe1f12..eb3051b 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -235,14 +235,13 @@ class GlmNotifications /** * getNotificationsByType * - * @param mixed $type Type of send_action (10: Create Invoice, 20: Received Payment) + * @param mixed $type Type of send_action ('Create Invoice', 'Received Payment') * * @access public * @return void */ public function getNotificationsByType( $type ) { - echo '
$this->config: ' . print_r( $this->config['send_action_numb'], true ) . '
'; return $this->wpdb->get_results( $this->wpdb->prepare( "SELECT id @@ -255,6 +254,48 @@ class GlmNotifications ); } + /** + * getNotificationsWithSendByAction + * + * Return array of notifications with send_by_action as $type + * + * @param mixed $type Type of send_by_action ('Create Invoice', 'Payment Received') + * + * @access public + * @return array + */ + public function getNotificationsWithSendByAction( $type ) + { + return $this->wpdb->get_results( + $this->wpdb->prepare( + "SELECT id + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_types + WHERE send_by_action + AND send_action = %d", + $this->config['send_action_numb'][$type] + ), + ARRAY_A + ); + } + + /** + * getNotificationsWithSendByDate + * + * Return array of notifications with send_by_date + * + * @access public + * @return void + */ + public function getNotificationsWithSendByDate() + { + return $this->wpdb->get_results( + "SELECT * + FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_types + WHERE send_by_date", + ARRAY_A + ); + } + /** * getNotificationTypeById * diff --git a/config/plugin.ini b/config/plugin.ini index 3e92766..f3d941f 100644 --- a/config/plugin.ini +++ b/config/plugin.ini @@ -44,3 +44,6 @@ send_action_numb['Received Payment'] = 20; ; Send Date When send_date_when[10] = "Before" send_date_when[20] = "After" + +send_date_when_numb['Before'] = 10 +send_date_when_numb['After'] = 20 diff --git a/defines.php b/defines.php index 44679eb..368a2fe 100644 --- a/defines.php +++ b/defines.php @@ -23,6 +23,10 @@ if (trim($host) == '') { } define('GLM_MEMBERS_BILLING_PLUGIN_HOST', $host); +// Cron defines +define( 'GLM_MEMBERS_BILLING_PLUGIN_CRON_HOOK', 'glm_members_billing_notifications' ); +define( 'GLM_MEMBERS_BILLING_PLUGIN_CRON_RECURRANCE', 'twicedaily' ); + // Determine current http/https protocol $pageProtocol = 'http'; if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == '443') { diff --git a/index.php b/index.php index 7e5fa10..bbf9b32 100644 --- a/index.php +++ b/index.php @@ -72,8 +72,8 @@ if (!defined('ABSPATH')) { } /* -* Some initial setup and tests -*/ + * Some initial setup and tests + */ $startupNotices = ''; @@ -103,10 +103,10 @@ if (is_file(GLM_MEMBERS_BILLING_PLUGIN_PATH.'/setup/databaseScripts/dbVersions.p function glmMembersBillingPluginRequired() { echo '
-

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' add-on requires the base GLM Member DB plugin to be installed and active!

-

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.

+

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' add-on requires the base GLM Member DB plugin to be installed and active!

+

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.

- '; + '; } $plugin_name = 'glm-member-db/index.php'; $is_active = is_plugin_active($plugin_name); @@ -119,12 +119,12 @@ if ($is_active != '1') { function glmMembersBillingMinVerRequired() { echo '
-

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' requires that the main GLM Member DB plugin version be no older than ' - .GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION.'!
- '.GLM_MEMBERS_BILLING_MIN_VERSION_NOTE.'

-

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.

+

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' requires that the main GLM Member DB plugin version be no older than ' + .GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION.'!
+ '.GLM_MEMBERS_BILLING_MIN_VERSION_NOTE.'

+

The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.

- '; + '; } $glmMembersDatabasePluginVersion = get_option('glmMembersDatabasePluginVersion'); if (version_compare($glmMembersDatabasePluginVersion, GLM_MEMBERS_BILLING_PLUGIN_MIN_MEMBERS_REQUIRED_VERSION) < 0) { @@ -144,7 +144,7 @@ if (is_file(GLM_MEMBERS_BILLING_PLUGIN_DB_SCRIPTS.'/dbVersions.php')) { // Load Add-On Management Settings data /* None - Need to figure out a smooth way to do this. -*/ + */ $glmMembersBillingSettings = $wpdb->get_row( "SELECT * FROM ".GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX."settings WHERE id = 1", ARRAY_A ); unset($glmMembersBillingSettings['id']); @@ -196,35 +196,47 @@ ${GLM_MEMBERS_BILLING_PLUGIN_PREFIX."updateChecker"} = new OpenToolsPluginUpdate ${GLM_MEMBERS_BILLING_PLUGIN_PREFIX."updateChecker"}->declareCredentials(array( 'license_key' => __('License Key:') )); -*/ + */ /* - * - * Activate and Deactivate hooks - * + * + * Activate and Deactivate hooks + * */ - // Activate - function glmMembersBillingPluginActivate () - { - global $wpdb, $config; - require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/activate.php'; - new glmMembersBillingPluginActivate($wpdb, $config); - } - register_activation_hook(__FILE__, 'glmMembersBillingPluginActivate'); - - // Deactivate - function glmMembersBillingPluginDeactivate () - { - global $wpdb, $config; - require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/deactivate.php'; - $x = new glmMembersBillingPluginDeactivate($wpdb, $config); - return false; - } - register_deactivation_hook(__FILE__, 'glmMembersBillingPluginDeactivate'); +// Activate +function glmMembersBillingPluginActivate () +{ + global $wpdb, $config; + require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/activate.php'; + new glmMembersBillingPluginActivate($wpdb, $config); +} +register_activation_hook(__FILE__, 'glmMembersBillingPluginActivate'); + +// Deactivate +function glmMembersBillingPluginDeactivate () +{ + global $wpdb, $config; + require_once GLM_MEMBERS_BILLING_PLUGIN_PATH . '/deactivate.php'; + $x = new glmMembersBillingPluginDeactivate($wpdb, $config); + return false; +} +register_deactivation_hook(__FILE__, 'glmMembersBillingPluginDeactivate'); /* * Hooks for testing capabilities provided by this add-on */ require_once GLM_MEMBERS_BILLING_PLUGIN_SETUP_PATH.'/permissions.php'; +/** + * Action for the cron tasks. + * This cannot be inside the controllers. + */ +add_action( + GLM_MEMBERS_BILLING_PLUGIN_CRON_HOOK, + function(){ + trigger_error( print_r( $_REQUEST, true ) ); + }, + 10, + 1 + ); diff --git a/misc/documentation/Notifications.txt b/misc/documentation/Notifications.txt new file mode 100644 index 0000000..a6e8826 --- /dev/null +++ b/misc/documentation/Notifications.txt @@ -0,0 +1,11 @@ +We discussed how to implement the notifications. We talked about the wordpress +wp_cron stuff and haven't settled on if we'll use it. We may have to setup +some cron task outside of wordpress. However if we do that then the plugin +won't be very friendly to other developers who may purchase it. + +Two steps for setting up email notifications. +1. Generate a list of notifications and store them into a db-table queue. +2. Have a cron task (wp or otherwise) run every 5 or 10 minutes to go through +the queue and send out a number of them each time. + + diff --git a/models/admin/ajax/runCron.php b/models/admin/ajax/runCron.php new file mode 100644 index 0000000..a721efd --- /dev/null +++ b/models/admin/ajax/runCron.php @@ -0,0 +1,113 @@ + + * @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=pdfOutput&mystuff=THIS + * + * 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_runCron +{ + + /** + * 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 ) { + // Get all notifications that are done by date. + $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + $notifications = $Notifications->getNotificationsWithSendByDate(); + echo '
$notifications: ' . print_r( $notifications, true ) . '
'; + + // Get all invoices that are unpaid. + $billingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); + $unpaid_invoices = $billingSupport->getUnPaidInvoices(); + 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. + 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 ) { + // Is this before or after + switch ( $notice['send_date_when'] ) { + case $this->config['send_date_when_numb']['Before']: + echo '

Before

'; + $dueDate->modify( + '-' . $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 ) . '
'; + break; + case $this->config['send_date_when_numb']['After']: + echo '

After

'; + break; + default: + break; + } + } + } + + wp_die(); + } +} diff --git a/models/admin/settings/notificationTypes.php b/models/admin/settings/notificationTypes.php index e4a3e63..8c25dd8 100644 --- a/models/admin/settings/notificationTypes.php +++ b/models/admin/settings/notificationTypes.php @@ -153,18 +153,6 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType * Currently this is just using a hard coded account id of 1. * After the test it will call wp_die */ - // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; - // // Test - // $notices = $Notifications->getNotificationsByType( 'Create Invoice' ); - // echo '
$notices: ' . print_r( $notices, true ) . '
'; - // if ( $notices ) { - // foreach ( $notices as $notice ) { - // $Notifications->sendEmailNotification( $notice['id'], 1 ); - // } - // } - // wp_die('here'); - - $notification_id = filter_var( $_REQUEST['id'], FILTER_VALIDATE_INT ); if ( $notification_id ) { diff --git a/setup/adminHooks.php b/setup/adminHooks.php index ea860a1..fa09025 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -26,21 +26,3 @@ * Also note that parameters will be in the context of the main admin controller constructor. */ -add_action( - 'glm-member-db-billing-create-invoice', - function( $account ){ - // Need to get notification id's for all notifications that are done when invoice is created. - $notices = $this->wpdb->get_results( - $this->wpdb->prepare( - "SELECT id - FROM " . GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . "notification_types - WHERE send_action = %d", - $this->config['send_action_numb']['Create Invoice'] - ), - ARRAY_A - ); - }, - 10, - 1 -); - diff --git a/setup/commonHooks.php b/setup/commonHooks.php new file mode 100644 index 0000000..fa09025 --- /dev/null +++ b/setup/commonHooks.php @@ -0,0 +1,28 @@ + + * @license http://www.gaslightmedia.com Gaslightmedia + * @release adminHooks.php,v 1.0 2014/10/31 19:31:47 cscott Exp $ + * @link http://dev.gaslightmedia.com/ + */ + +/* + * Place Misc Hooks and Filters here. If this file exists, it will be included + * by the add-on main plugin script. + * + * Note that filter and hook callback functions must be included in-line as shown below... + * + * add_filter( 'filter_title', function( $parameter ) { + * // Function code + * }); + * + * Also note that parameters will be in the context of the main admin controller constructor. + */ + diff --git a/setup/validActions.php b/setup/validActions.php index 75e9cc2..3ec0ea8 100644 --- a/setup/validActions.php +++ b/setup/validActions.php @@ -59,6 +59,9 @@ $glmMembersBillingAddOnValidActions = array( 'adminActions' => array( + 'ajax' => array( + 'runCron' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, + ), 'management' => array( 'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG, ), -- 2.17.1