WIP Setting up cron action for getting notifications.
authorSteve Sutton <steve@gaslightmedia.com>
Sat, 13 Jan 2018 13:15:21 +0000 (08:15 -0500)
committerSteve Sutton <steve@gaslightmedia.com>
Sat, 13 Jan 2018 13:15:21 +0000 (08:15 -0500)
Still working on it.

classes/billingSupport.php
classes/notifications.php
config/plugin.ini
defines.php
index.php
misc/documentation/Notifications.txt [new file with mode: 0644]
models/admin/ajax/runCron.php [new file with mode: 0644]
models/admin/settings/notificationTypes.php
setup/adminHooks.php
setup/commonHooks.php [new file with mode: 0644]
setup/validActions.php

index 768ffeb..dc5726f 100644 (file)
@@ -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
      *
index afe1f12..eb3051b 100644 (file)
@@ -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 '<pre>$this->config: ' . print_r( $this->config['send_action_numb'], true ) . '</pre>';
         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
      *
index 3e92766..f3d941f 100644 (file)
@@ -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
index 44679eb..368a2fe 100644 (file)
@@ -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') {
index 7e5fa10..bbf9b32 100644 (file)
--- 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 '
         <div class="error">
-            <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' add-on requires the base GLM Member DB plugin to be installed and active!</p>
-            <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.</p>
+        <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' add-on requires the base GLM Member DB plugin to be installed and active!</p>
+        <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.</p>
         </div>
-    ';
+        ';
 }
 $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 '
         <div class="error">
-            <p>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.'!<br>
-                    '.GLM_MEMBERS_BILLING_MIN_VERSION_NOTE.'</p>
-            <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.</p>
+        <p>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.'!<br>
+        '.GLM_MEMBERS_BILLING_MIN_VERSION_NOTE.'</p>
+        <p>The '.GLM_MEMBERS_BILLING_PLUGIN_NAME.' plugin has been de-activated.</p>
         </div>
-    ';
+        ';
 }
 $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 (file)
index 0000000..a6e8826
--- /dev/null
@@ -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 (file)
index 0000000..a721efd
--- /dev/null
@@ -0,0 +1,113 @@
+<?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
+ */
+
+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 '<pre>$notifications: ' . print_r( $notifications, true ) . '</pre>';
+
+        // Get all invoices that are unpaid.
+        $billingSupport = new GlmBillingSupport( $this->wpdb, $this->config );
+        $unpaid_invoices = $billingSupport->getUnPaidInvoices();
+        echo '<pre>$unpaid_invoices: ' . print_r( $unpaid_invoices, true ) . '</pre>';
+
+        // 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 '<pre>$dueDate 1: ' . print_r( $dueDate, true ) . '</pre>';
+
+            // 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 '<p>Before</p>';
+                    $dueDate->modify(
+                        '-' . $notice['send_date_number'] . ' '.
+                        $this->config['send_date_period'][$notice['send_date_period']] .
+                        (($notice['send_date_number'] > 1) ? 's': '')
+
+                    );
+                    echo '<pre>$dueDate 2: ' . print_r( $dueDate, true ) . '</pre>';
+                    break;
+                case $this->config['send_date_when_numb']['After']:
+                    echo '<p>After</p>';
+                    break;
+                default:
+                    break;
+                }
+            }
+        }
+
+        wp_die();
+    }
+}
index e4a3e63..8c25dd8 100644 (file)
@@ -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 '<pre>$_REQUEST: ' . print_r( $_REQUEST, true ) . '</pre>';
-            // // Test
-            // $notices = $Notifications->getNotificationsByType( 'Create Invoice' );
-            // echo '<pre>$notices: ' . print_r( $notices, true ) . '</pre>';
-            // 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 ) {
index ea860a1..fa09025 100644 (file)
  *  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 (file)
index 0000000..fa09025
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+/**
+ * GLM Members Billing
+ * GLM Members Misc Admin Hooks and Filters
+ *
+ * PHP version 5.5
+ *
+ * @category glmWordPressPlugin
+ * @package  glmMembersDatabase
+ * @author   Chuck Scott <cscott@gaslightmedia.com>
+ * @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.
+  */
+
index 75e9cc2..3ec0ea8 100644 (file)
@@ -59,6 +59,9 @@
 
 $glmMembersBillingAddOnValidActions = array(
     'adminActions' => array(
+        'ajax' => array(
+            'runCron' => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
+        ),
         'management' => array(
             'billing' => GLM_MEMBERS_BILLING_PLUGIN_SLUG,
         ),