From 6ecbfe415b17db4d32fb5cd3d5a2ad8e000e6769 Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Wed, 27 Dec 2017 16:33:10 -0500 Subject: [PATCH] Working on setup for sending emails when creating invoices. --- classes/notifications.php | 72 ++++++++++++++++++++- config/plugin.ini | 3 + models/admin/settings/notificationTypes.php | 22 +++++-- setup/adminHooks.php | 19 ++++++ 4 files changed, 109 insertions(+), 7 deletions(-) diff --git a/classes/notifications.php b/classes/notifications.php index c1048da..7baacb6 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -68,12 +68,12 @@ class GlmNotifications * @access public * @return void */ - public function sendEmailNotification( $notification_id, $account ) + public function sendEmailNotification( $notification_id, $account_id ) { // Support Class $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); // get the Account - $account = $BillingSupport->getAccountById( $account ); + $account = $BillingSupport->getAccountById( $account_id ); if ( !$account ) { // If there's no account then return false. return false; @@ -135,6 +135,74 @@ class GlmNotifications // remove the filter to avoid conflicts remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); + + // Send notification to recordNotification + $notice_data = array( + 'notification_type' => $notification_id, + 'account' => $account_id, + 'from_replyto' => $from_header, + 'subject' => $subject, + 'message' => $message, + 'email_sent' => $to_email, + ); + $this->recordNotification( $notice_data ); + } + + /** + * recordNotification + * + * Record the notification. + * + * @param mixed $notification Array of data for the notification + i + * @access public + * @return void + */ + public function recordNotification( $notification ) + { + if ( !$notification['notification_type'] || !$notification['account'] + || !$notification['from_replyto'] || !$notification['subject'] + || !$notification['message'] || !$notification['email_sent'] + ) { + return false; + } + $this->wpdb->insert( + GLM_MEMBERS_BILLING_PLUGIN_DB_PREFIX . 'notifications', + array( + 'notification_type' => $notification['notification_type'], + 'account' => $notification['account'], + 'from_replyto' => $notification['from_replyto'], + 'subject' => $notification['subject'], + 'message' => $notification['message'], + 'date_sent' => date('Y-m-d H:i;s'), + 'email_sent' => $notification['email_sent'], + ), + array( + '%d', // notification_type + '%d', // account + '%s', // from_replyto + '%s', // subject + '%s', // message + '%s', // date_sent + '%s', // email_sent + ) + ); + return true; + } + + public function getNotificationByType( $type ) + { + echo '
$this->config: ' . print_r( $this->config['send_action_numb'], true ) . '
'; + 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 + ); } /** diff --git a/config/plugin.ini b/config/plugin.ini index eae86b3..3e92766 100644 --- a/config/plugin.ini +++ b/config/plugin.ini @@ -38,6 +38,9 @@ send_date_period[40] = "Year" send_action[10] = "Create Invoice" send_action[20] = "Received Payment" +send_action_numb['Create Invoice'] = 10; +send_action_numb['Received Payment'] = 20; + ; Send Date When send_date_when[10] = "Before" send_date_when[20] = "After" diff --git a/models/admin/settings/notificationTypes.php b/models/admin/settings/notificationTypes.php index bd362b3..2032b58 100644 --- a/models/admin/settings/notificationTypes.php +++ b/models/admin/settings/notificationTypes.php @@ -151,15 +151,27 @@ 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 ) . '
'; - require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; - $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + // echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; + // require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; + // $Notifications = new GlmNotifications( $this->wpdb, $this->config ); + // // Test + // $notices = $Notifications->getNotificationByType( '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 ) { echo '
$notification_id: ' . print_r( $notification_id, true ) . '
'; $account = 1; // TODO: remove this. It's for testing only $noticeReturned = $Notifications->sendEmailNotification( $notification_id, $account ); - var_dump( $noticeReturned ); + // var_dump( $noticeReturned ); } wp_die('Testing here'); break; @@ -168,7 +180,7 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType /** * add: * - * This calls the dataabstract newEntry method and sets up the + * This calls the dataAbstract newEntry method and sets up the * edit for for creating a new notification_type. */ $notification = $this->newEntry(); diff --git a/setup/adminHooks.php b/setup/adminHooks.php index ab234d1..ea860a1 100644 --- a/setup/adminHooks.php +++ b/setup/adminHooks.php @@ -25,3 +25,22 @@ * * 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 +); + -- 2.17.1