From 0768efe71b3f1d8b31a095ff69faa0e9ab19f31b Mon Sep 17 00:00:00 2001 From: Steve Sutton Date: Thu, 14 Dec 2017 12:51:52 -0500 Subject: [PATCH] Setup emails from the notifications. Send out notices. I have a test link in settings > notification_types that allow test sending of the notice. It's hard coded to the account with id 1 right now. --- classes/notifications.php | 70 ++++++++++++++++++++- models/admin/settings/notificationTypes.php | 10 ++- views/admin/notifications/notification.html | 10 +++ 3 files changed, 84 insertions(+), 6 deletions(-) create mode 100644 views/admin/notifications/notification.html diff --git a/classes/notifications.php b/classes/notifications.php index 09461a6..c1048da 100644 --- a/classes/notifications.php +++ b/classes/notifications.php @@ -68,18 +68,73 @@ class GlmNotifications * @access public * @return void */ - public function sendEmailNotification( $notification_type, $account ) + public function sendEmailNotification( $notification_id, $account ) { // Support Class $BillingSupport = new GlmBillingSupport( $this->wpdb, $this->config ); // get the Account $account = $BillingSupport->getAccountById( $account ); - echo '
$account: ' . print_r( $account, true ) . '
'; + if ( !$account ) { + // If there's no account then return false. + return false; + } + $to_email = $account['email']; + if ( !$to_email ) { + // If there's no email then return false. + return false; + } // get the Notification type - $notification_type = $this->getNotificationTypeById( $notification_type ); + var_dump( $notification_id ); + $notification_type = $this->getNotificationTypeById( $notification_id ); echo '
$notification_type: ' . print_r( $notification_type, true ) . '
'; + if ( !$notification_type ) { + // If there's no notification type then return false. + return false; + } + $subject = $notification_type['subject']; + if ( !$subject ) { + // If there's no subject then return false. + return false; + } + $from_header = $notification_type['from_header']; + $replyto = $notification_type['replyto']; + + // Setup the Smarty Engine + $smarty = new smartyTemplateSupport(); + $viewPath = GLM_MEMBERS_BILLING_PLUGIN_PATH . '/views'; + $smarty->template->setTemplateDir( $viewPath ); + + // Add standard parameters + require GLM_MEMBERS_PLUGIN_SETUP_PATH.'/standardTemplateParams.php'; + $viewFile = 'admin/notifications/notification.html'; + + $smarty->templateAssign( 'title', $notification_type['subject'] ); + $smarty->templateAssign( 'html_content', wpautop( $notification_type['message'] ) ); + + // Generate output from model data and view + $htmlMessage = $smarty->template->fetch( $viewFile ); + + // change the default wordpress from name when sending mail + add_filter( + 'wp_mail_from_name', + function ( $name ) { + $siteName = get_bloginfo( 'name' ); + return $siteName; + } + ); + // Send confirmation email, set the content type to allow html by using this filter + add_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); + + $message = $htmlMessage; + $header[] = 'From:' . $from_header; + $header[] = 'Reply-To:' . $replyto; + + wp_mail( $to_email, $subject, $message, $header ); + + // remove the filter to avoid conflicts + remove_filter( 'wp_mail_content_type', array( $this, 'set_content_type' ) ); } /** @@ -105,4 +160,13 @@ class GlmNotifications ); } + /** + * Set content type of the email. + * + * Used as filter for the wp_mail_content_type + */ + function set_content_type() + { + return "text/html"; + } } diff --git a/models/admin/settings/notificationTypes.php b/models/admin/settings/notificationTypes.php index b38a153..a5103bd 100644 --- a/models/admin/settings/notificationTypes.php +++ b/models/admin/settings/notificationTypes.php @@ -147,9 +147,13 @@ class GlmMembersAdmin_settings_notificationTypes extends GlmDataNotificationType echo '
$_REQUEST: ' . print_r( $_REQUEST, true ) . '
'; require_once GLM_MEMBERS_BILLING_PLUGIN_CLASS_PATH . '/notifications.php'; $Notifications = new GlmNotifications( $this->wpdb, $this->config ); - $notification_id = 5; - $account = 1; - $Notifications->sendEmailNotification( $notification_id, $account ); + $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 ); + } wp_die('Testing here'); break; case 'add': diff --git a/views/admin/notifications/notification.html b/views/admin/notifications/notification.html new file mode 100644 index 0000000..a94383d --- /dev/null +++ b/views/admin/notifications/notification.html @@ -0,0 +1,10 @@ + + + + + + +

{$title}

+ {$html_content} + + -- 2.17.1