ae4cf76a by Jeff Balicki

dd

1 parent de5d89fb
<?php
namespace Tz\WordPress\Tools\Notifications;
use Tz\Common;
use Tz\WordPress\CBV;
use Tz\WordPress\CBV\User;
use Tz\WordPress\Tools;
use Tz\WordPress\Tools\Sequencer;
const OPTION_NAME = 'notif_options';
const NOTIFICATION_STATUS_READ = 'read';
const OPTION_NAME = 'notif_options';
const NOTIFICATION_STATUS_READ = 'read';
const NOTIFICATION_STATUS_UNREAD = 'unread';
call_user_func(
function () {
Tools\add_actions(__NAMESPACE__.'\Actions');
Tools\add_actions(__NAMESPACE__ . '\Actions');
Vars::$options = new Tools\WP_Option(OPTION_NAME);
Vars::$notices = [];
if (is_admin()) {
require_once(__DIR__.'/Validation.php');
require_once(__DIR__.'/Admin.php');
require_once (__DIR__ . '/Validation.php');
require_once (__DIR__ . '/Admin.php');
}
}
);
/**
* @param $a
* @param $subkey
......@@ -56,7 +55,7 @@ function get_user_notices($uid)
{
$notices = get_user_meta($uid, 'notices', true);
if (!empty($notices)) {
$notices = subval_sort($notices, 'sent_date', 'arsort');
$notices = subval_sort($notices, 'sent_date', 'arsort');
Vars::$notices = $notices;
}
}
......@@ -72,33 +71,33 @@ function print_user_notices($showOnlyUnread = false)
get_user_notices($user->ID);
$notices = Vars::$notices;
$rows = '';
$rows = '';
foreach ($notices as $index => $notice) {
if ($showOnlyUnread && $notice['status'] != 'unread') {
continue;
}
$triggered = get_post_meta($notice['notification_id'], 'trigger', true);
$triggered = get_post_meta($notice['notification_id'], 'trigger', true);
$enableTriggers = [
'MEMBERSHIP_FEES_DUE',
'STUDENT_FEES_DUE'
'STUDENT_FEES_DUE',
];
$system = get_post_meta($notice['notification_id'], 'system', true);
$system = get_post_meta($notice['notification_id'], 'system', true);
$content = '';
if(!isset($notice['message']) && in_array($triggered, $enableTriggers)){
if (!isset($notice['message']) && in_array($triggered, $enableTriggers)) {
$emailAddressPreference = get_user_meta($user->ID, 'email_address_preference', true);
if (empty($emailAddressPreference)) {
$userData = get_userdata($user->ID);
$toEmail = $userData->user_email;
$toEmail = $userData->user_email;
} else {
$pp = strtolower($emailAddressPreference) . '_';
$pp = strtolower($emailAddressPreference) . '_';
$toEmail = get_user_meta($user->ID, $pp . 'email', true);
if (empty($toEmail)) {
$userData = get_userdata($user->ID);
$toEmail = $userData->user_email;
$toEmail = $userData->user_email;
}
}
......@@ -119,7 +118,7 @@ function print_user_notices($showOnlyUnread = false)
foreach ($results as $result) {
if ($toEmail == $result->to_email) {
$stringArray = (preg_split("/\r\n|\n|\r/", $result->text, -1, PREG_SPLIT_NO_EMPTY));
$message = implode("\n", [$stringArray[0], $stringArray[1]]);
$message = implode("\n", [$stringArray[0], $stringArray[1]]);
break;
}
}
......@@ -131,49 +130,47 @@ function print_user_notices($showOnlyUnread = false)
}
} else {
if(isset($notice['message'])) {
if (isset($notice['message'])) {
$content = $notice['message'];
} else {
$content = isset($system[ICL_LANGUAGE_CODE.'_message'])
? $system[ICL_LANGUAGE_CODE.'_message']
: $system['en_message'];
$content = isset($system[ICL_LANGUAGE_CODE . '_message'])
? $system[ICL_LANGUAGE_CODE . '_message']
: $system['en_message'];
}
}
if (isset($notice['args']) && count($notice['args']) > 0) {
foreach ($notice['args'] as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
$content = str_replace(
'{'.$key.'}',
'{' . $key . '}',
"<a href='"
.$val
."'>"
.__('Click here', 'CBV_DOMAIN')
.'</a>',
. $val
. "'>"
. __('Click here', 'CBV_DOMAIN')
. '</a>',
$content
);
} else {
$content = str_replace('{'.$key.'}', $val, $content);
$content = str_replace('{' . $key . '}', $val, $content);
}
}
}
$rows .= '<tr class="notice-row" id="'.$index.'">';
$rows .= '<td width="12" style="padding: 0 0 0 10px; vertical-align: middle;"><a href="javascript:;" class="notice '
.(($notice['status'] == 'read') ? 'read' : 'unread')
.'"><img src="/wp-content/themes/cbv/assets/images/blank.gif" style="width: 12px; height: 12px;"></a></td>';
$rows .= '<tr style="margin-bottom:10px;" class="notice-row" id="' . $index . '">';
$rows .= '<td style="padding: 0px 0 px 20px; vertical-align: top; width:5%;"><a href="javascript:;" class="notice '
. '"><img src="/wp-content/themes/cbv/assets/images/new/Icon_Archive.svg" style="width: 25px; height: 25px;"></a></td>';
if ($system['system_message_type'] == 'none') {
$system['system_message_type'] = 'notice';
}
$rows .= '<td width="80" style="vertical-align: middle;"><span>'
.ucfirst(str_replace('_', '&nbsp;', $system['system_message_type']))
.'</span></td>';
$rows .= '<td style="vertical-align: middle;">'.nl2br($content).'<br /><span class="legal">'
.__('Sent:', 'CBV_DOMAIN')
.CBV\tz_display('notice_sent_date', $notice['sent_date']).'</span></td>';
$rows .= '<td style="vertical-align: middle; width:55%;">' . nl2br($content) . '</td><td style="vertical-align: middle; width:26%;">'
. CBV\tz_display('notice_sent_date', $notice['sent_date']) . '</span></td>';
$rows .= '<td style="vertical-align: middle; width:10%;padding: 5px 0px 5px 0px;" style="vertical-align: middle;"><span>'
. ucfirst(str_replace('_', '&nbsp;', $system['system_message_type']))
. '</span></td>';
$rows .= '</tr>';
}
......@@ -192,10 +189,10 @@ function print_user_notices($showOnlyUnread = false)
function get_notification_by_trigger($trigger)
{
$args = [
'post_type' => 'notifications',
'post_type' => 'notifications',
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'desc'
'orderby' => 'modified',
'order' => 'desc',
];
$my_notif = false;
......@@ -222,7 +219,7 @@ function get_num_notices()
$user = new User\CurrentAccount();
get_user_notices($user->ID);
$notices = Vars::$notices;
$notices = Vars::$notices;
$notifications = [];
// return $num;
if (empty($notices)) {
......@@ -282,18 +279,18 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
if ($notification) {
// get the notification and notificatio details....
$nid = $notification->ID;
$nid = $notification->ID;
$details = get_post_meta($nid, 'details', true);
$email = get_post_meta($nid, 'email', true);
$system = get_post_meta($nid, 'system', true);
$push = get_post_meta($nid, 'push', true);
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : 'active';
$notification->type = $details['type'];
$notification->sendto = @$details['sendto'];
$notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != '');
$notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != '');
$notification->is_push = (isset($push[$lang.'_message']) && $push[$lang.'_message'] != '');
$email = get_post_meta($nid, 'email', true);
$system = get_post_meta($nid, 'system', true);
$push = get_post_meta($nid, 'push', true);
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : 'active';
$notification->type = $details['type'];
$notification->sendto = @$details['sendto'];
$notification->is_email = (($email[$lang . '_text'] != '' || $email[$lang . '_html'] != '') && $email[$lang . '_subject'] != '');
$notification->is_system = (isset($system[$lang . '_message']) && $system[$lang . '_message'] != '');
$notification->is_push = (isset($push[$lang . '_message']) && $push[$lang . '_message'] != '');
// if is_system ==========================================
if ($notification->is_system && $uid != 0) {
get_user_notices($uid);
......@@ -302,10 +299,10 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
// Add a system message into the usermeta as well to track the system notification.
$insert = [
'notification_id' => $nid,
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang.'_message'],
'args' => $args
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang . '_message'],
'args' => $args,
];
// Convert the args to actual message
......@@ -314,16 +311,16 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
foreach ($insert['args'] as $key => $val) {
if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
$content = str_replace(
'{'.$key.'}',
'{' . $key . '}',
"<a href='"
.$val
."'>"
.__('Click here', 'CBV_DOMAIN')
.'</a>',
. $val
. "'>"
. __('Click here', 'CBV_DOMAIN')
. '</a>',
$content
);
} else {
$content = str_replace('{'.$key.'}', $val, $content);
$content = str_replace('{' . $key . '}', $val, $content);
}
}
$insert['message'] = $content;
......@@ -341,24 +338,22 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
}
if ($notification->is_push) {
$preferred_language = get_user_meta($uid, 'preferred_language', true);
if(empty($preferred_language)) {
$preferred_language = 'en';
}
$time = time();
$title = $push[$preferred_language . '_title'];
$message = $push[$preferred_language . '_message'];
$link = $push[$preferred_language . '_link'];
$status = $push[$preferred_language . '_status'];
foreach ($args as $key => $val) {
$title = str_replace('{'.$key.'}', $val, $title);
$message = str_replace('{'.$key.'}', $val, $message);
}
$preferred_language = get_user_meta($uid, 'preferred_language', true);
if (empty($preferred_language)) {
$preferred_language = 'en';
}
$time = time();
$title = $push[$preferred_language . '_title'];
$message = $push[$preferred_language . '_message'];
$link = $push[$preferred_language . '_link'];
$status = $push[$preferred_language . '_status'];
foreach ($args as $key => $val) {
$title = str_replace('{' . $key . '}', $val, $title);
$message = str_replace('{' . $key . '}', $val, $message);
}
// Do argument template again
// Set arguments for email template
......@@ -399,25 +394,23 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'preferred_email',
'member_id',
'user_id',
'course_note_listing'
'course_note_listing',
];
$user_data = array_flip($user_data);
foreach ($user_data as $field => $value) {
$user_data[$field] = User\clean_string(get_user_meta($uid, $field, true));
}
foreach ($user_data as $key => $val) {
$title = str_replace('{'.$key.'}', $val, $title );
$message = str_replace('{'.$key.'}', $val, $message);
}
$title = str_replace('{' . $key . '}', $val, $title);
$message = str_replace('{' . $key . '}', $val, $message);
}
$wpdb->query(
"INSERT INTO wp_push_daemon (post_id,user_id,time,title,message,link,status) VALUES (0, $uid,'$time','$title','$message','$link','$status')"
);
$wpdb->query(
"INSERT INTO wp_push_daemon (post_id,user_id,time,title,message,link,status) VALUES (0, $uid,'$time','$title','$message','$link','$status')"
);
}
......@@ -432,8 +425,8 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
if (empty($emailAddressPreference)) {
$toEmail = $user->user_email;
} else {
$pp = strtolower($emailAddressPreference).'_';
$toEmail = get_user_meta($user->ID, $pp.'email', true);
$pp = strtolower($emailAddressPreference) . '_';
$toEmail = get_user_meta($user->ID, $pp . 'email', true);
if (empty($toEmail)) {
$toEmail = $user->user_email;
......@@ -444,27 +437,27 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$contents = $email;
$fromEmail = get_bloginfo('admin_email');
$subject = strip_tags(
isset($contents[$lang.'_subject']) && !empty($contents[$lang.'_subject'])
? $contents[$lang.'_subject']
: $contents['en_subject']
$subject = strip_tags(
isset($contents[$lang . '_subject']) && !empty($contents[$lang . '_subject'])
? $contents[$lang . '_subject']
: $contents['en_subject']
);
$html = isset($contents[$lang.'_html']) && !empty($contents[$lang.'_html'])
? $contents[$lang.'_html']
: @$contents['en_html'];
$altText = strip_tags(
isset($contents[$lang.'_text']) && !empty($contents[$lang.'_text'])
? $contents[$lang.'_text']
: $contents['en_text']
$html = isset($contents[$lang . '_html']) && !empty($contents[$lang . '_html'])
? $contents[$lang . '_html']
: @$contents['en_html'];
$altText = strip_tags(
isset($contents[$lang . '_text']) && !empty($contents[$lang . '_text'])
? $contents[$lang . '_text']
: $contents['en_text']
);
foreach ($args as $key => $val) {
if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$html = str_replace('{'.$key.'}', '<a href="'.$val.'">'.$val.'</a>', $html);
$html = str_replace('{' . $key . '}', '<a href="' . $val . '">' . $val . '</a>', $html);
} else {
$html = str_replace('{'.$key.'}', $val, $html);
$altText = str_replace('{'.$key.'}', $val, $altText);
$subject = str_replace('{'.$key.'}', $val, $subject);
$html = str_replace('{' . $key . '}', $val, $html);
$altText = str_replace('{' . $key . '}', $val, $altText);
$subject = str_replace('{' . $key . '}', $val, $subject);
}
}
......@@ -507,7 +500,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'preferred_email',
'member_id',
'user_id',
'course_note_listing'
'course_note_listing',
];
$user_data = array_flip($user_data);
......@@ -516,36 +509,36 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$user_data[$field] = User\clean_string(get_user_meta($uid, $field, true));
// Choose user's preferred email address
if($field == 'preferred_email') {
if ($field == 'preferred_email') {
$user_data['preferred_email'] = $user_data['email_address_preference'] == 'Home' ? $user_data['home_email'] : $user_data['work_email'];
}
// Set course note listing page
if($field == 'course_note_listing' && empty($args['course_note_listing'])) {
if ($field == 'course_note_listing' && empty($args['course_note_listing'])) {
$user_data['course_note_listing'] = '<a href="' . get_bloginfo('url') . CBV\PAGE_COURSENOTES . '">Course Note Listing page</a>';
}
}
foreach ($user_data as $key => $val) {
if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$html = str_replace('{'.$key.'}', '<a href="'.$val.'">'.$val.'</a>', $html);
$html = str_replace('{' . $key . '}', '<a href="' . $val . '">' . $val . '</a>', $html);
} else {
$html = str_replace('{'.$key.'}', $val, $html);
$altText = str_replace('{'.$key.'}', $val, $altText);
$subject = str_replace('{'.$key.'}', $val, $subject);
$html = str_replace('{' . $key . '}', $val, $html);
$altText = str_replace('{' . $key . '}', $val, $altText);
$subject = str_replace('{' . $key . '}', $val, $subject);
}
}
$attachments = isset($contents[$lang.'_attachments'])
? $contents[$lang.'_attachments']
: $contents['en_attachments'];
$att1 = isset($attachments[0]) ? $attachments[0] : '';
$att2 = isset($attachments[1]) ? $attachments[1] : '';
$att3 = isset($attachments[2]) ? $attachments[2] : '';
$attachments = isset($contents[$lang . '_attachments'])
? $contents[$lang . '_attachments']
: $contents['en_attachments'];
$att1 = isset($attachments[0]) ? $attachments[0] : '';
$att2 = isset($attachments[1]) ? $attachments[1] : '';
$att3 = isset($attachments[2]) ? $attachments[2] : '';
$attachments = array_map(
function ($attachmentPath) {
return realpath(__DIR__.'/../../../uploads/notifications/'.$attachmentPath);
return realpath(__DIR__ . '/../../../uploads/notifications/' . $attachmentPath);
},
$attachments
);
......@@ -567,16 +560,16 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'wp_mail_daemon',
[
'notification_id' => $nid,
'from_email' => $fromEmail,
'to_email' => $toEmail,
'subject' => $subject,
'text' => $altText,
'html' => $html,
'attachment1' => $att1,
'attachment2' => $att2,
'attachment3' => $att3,
'sent' => (int)$sentSuccessfully,
'sent_date' => CBV\tz_date('Y-m-d H:i:s')
'from_email' => $fromEmail,
'to_email' => $toEmail,
'subject' => $subject,
'text' => $altText,
'html' => $html,
'attachment1' => $att1,
'attachment2' => $att2,
'attachment3' => $att3,
'sent' => (int) $sentSuccessfully,
'sent_date' => CBV\tz_date('Y-m-d H:i:s'),
],
[
'%d',
......@@ -589,7 +582,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'%s',
'%s',
'%d',
'%s'
'%s',
]
);
}
......@@ -598,7 +591,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $args = [])
{
$lang = 'en';
/** @var \StdClass $notification */
......@@ -606,17 +599,17 @@ function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $a
if ($notification) {
// get the notification and notificatio details....
$nid = $notification->ID;
$nid = $notification->ID;
$details = get_post_meta($nid, 'details', true);
$email = get_post_meta($nid, 'email', true);
$system = get_post_meta($nid, 'system', true);
$email = get_post_meta($nid, 'email', true);
$system = get_post_meta($nid, 'system', true);
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : 'active';
$notification->type = $details['type'];
$notification->sendto = @$details['sendto'];
$notification->is_email = (($email[$lang.'_text'] != '' || $email[$lang.'_html'] != '') && $email[$lang.'_subject'] != '');
$notification->is_system = (isset($system[$lang.'_message']) && $system[$lang.'_message'] != '');
$notification->trigger = $details['trigger'];
$notification->status = isset($details['status']) ? $details['status'] : 'active';
$notification->type = $details['type'];
$notification->sendto = @$details['sendto'];
$notification->is_email = (($email[$lang . '_text'] != '' || $email[$lang . '_html'] != '') && $email[$lang . '_subject'] != '');
$notification->is_system = (isset($system[$lang . '_message']) && $system[$lang . '_message'] != '');
// if is_system ==========================================
if ($notification->is_system && $uid != 0) {
......@@ -626,65 +619,57 @@ function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $a
// Add a system message into the usermeta as well to track the system notification.
$insert = [
'notification_id' => $nid,
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang.'_message'],
'args' => $args
];
}
}
$contents = $email;
$fromEmail = get_bloginfo('admin_email');
$subject = strip_tags(
isset($contents[$lang.'_subject']) && !empty($contents[$lang.'_subject'])
? $contents[$lang.'_subject']
: $contents['en_subject']
);
$html = isset($contents[$lang.'_html']) && !empty($contents[$lang.'_html'])
? $contents[$lang.'_html']
: @$contents['en_html'];
$altText = strip_tags(
isset($contents[$lang.'_text']) && !empty($contents[$lang.'_text'])
? $contents[$lang.'_text']
: $contents['en_text']
);
foreach ($args as $key => $val) {
if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$html = str_replace('{'.$key.'}', '<a href="'.$val.'">'.$val.'</a>', $html);
} else {
$html = str_replace('{'.$key.'}', $val, $html);
$altText = str_replace('{'.$key.'}', $val, $altText);
$subject = str_replace('{'.$key.'}', $val, $subject);
}
}
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang . '_message'],
'args' => $args,
];
}
}
$sentSuccessfully = false;
if (CBV\system_can_send_emails_using_wpdb()) {
$response = \wpMandrill::mail($toEmail, $subject, !empty($html) ? $html : $altText, [], $attachments);
if (
is_array($response)
&& isset($response[0]['status'])
&& isset($response[0]['status'])
&& !in_array($response['status'], ['rejected', 'invalid'])
) {
$sentSuccessfully = true;
}
}
$contents = $email;
$fromEmail = get_bloginfo('admin_email');
$subject = strip_tags(
isset($contents[$lang . '_subject']) && !empty($contents[$lang . '_subject'])
? $contents[$lang . '_subject']
: $contents['en_subject']
);
$html = isset($contents[$lang . '_html']) && !empty($contents[$lang . '_html'])
? $contents[$lang . '_html']
: @$contents['en_html'];
$altText = strip_tags(
isset($contents[$lang . '_text']) && !empty($contents[$lang . '_text'])
? $contents[$lang . '_text']
: $contents['en_text']
);
foreach ($args as $key => $val) {
if ((filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
$html = str_replace('{' . $key . '}', '<a href="' . $val . '">' . $val . '</a>', $html);
} else {
$html = str_replace('{' . $key . '}', $val, $html);
$altText = str_replace('{' . $key . '}', $val, $altText);
$subject = str_replace('{' . $key . '}', $val, $subject);
}
}
$sentSuccessfully = false;
if (CBV\system_can_send_emails_using_wpdb()) {
$response = \wpMandrill::mail($toEmail, $subject, !empty($html) ? $html : $altText, [], $attachments);
if (
is_array($response)
&& isset($response[0]['status'])
&& isset($response[0]['status'])
&& !in_array($response['status'], ['rejected', 'invalid'])
) {
$sentSuccessfully = true;
}
}
}
/**
* @param int $grpID
*
......@@ -737,9 +722,9 @@ function current_url()
}
$pageURL .= '://';
if ($_SERVER['SERVER_PORT'] != '80') {
$pageURL .= $_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['REQUEST_URI'];
$pageURL .= $_SERVER['SERVER_NAME'] . ':' . $_SERVER['SERVER_PORT'] . $_SERVER['REQUEST_URI'];
} else {
$pageURL .= $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$pageURL .= $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
}
return $pageURL;
......