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;
......@@ -13,13 +12,13 @@ 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');
}
}
);
......@@ -82,12 +81,12 @@ function print_user_notices($showOnlyUnread = false)
$enableTriggers = [
'MEMBERSHIP_FEES_DUE',
'STUDENT_FEES_DUE'
'STUDENT_FEES_DUE',
];
$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);
......@@ -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']
$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>';
}
......@@ -195,7 +192,7 @@ function get_notification_by_trigger($trigger)
'post_type' => 'notifications',
'numberposts' => -1,
'orderby' => 'modified',
'order' => 'desc'
'order' => 'desc',
];
$my_notif = false;
......@@ -291,9 +288,9 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$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'] != '');
$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);
......@@ -304,8 +301,8 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'notification_id' => $nid,
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang.'_message'],
'args' => $args
'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;
......@@ -342,7 +339,7 @@ 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)) {
if (empty($preferred_language)) {
$preferred_language = 'en';
}
......@@ -353,13 +350,11 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$status = $push[$preferred_language . '_status'];
foreach ($args 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);
}
// Do argument template again
// Set arguments for email template
$user_data = [
......@@ -399,22 +394,20 @@ 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')"
);
......@@ -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;
......@@ -445,26 +438,26 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$fromEmail = get_bloginfo('admin_email');
$subject = strip_tags(
isset($contents[$lang.'_subject']) && !empty($contents[$lang.'_subject'])
? $contents[$lang.'_subject']
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']
$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']
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,28 +509,28 @@ 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']
$attachments = isset($contents[$lang . '_attachments'])
? $contents[$lang . '_attachments']
: $contents['en_attachments'];
$att1 = isset($attachments[0]) ? $attachments[0] : '';
$att2 = isset($attachments[1]) ? $attachments[1] : '';
......@@ -545,7 +538,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$attachments = array_map(
function ($attachmentPath) {
return realpath(__DIR__.'/../../../uploads/notifications/'.$attachmentPath);
return realpath(__DIR__ . '/../../../uploads/notifications/' . $attachmentPath);
},
$attachments
);
......@@ -575,8 +568,8 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'attachment1' => $att1,
'attachment2' => $att2,
'attachment3' => $att3,
'sent' => (int)$sentSuccessfully,
'sent_date' => CBV\tz_date('Y-m-d H:i:s')
'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',
]
);
}
......@@ -615,8 +608,8 @@ function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $a
$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_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) {
......@@ -628,47 +621,40 @@ function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $a
'notification_id' => $nid,
'status' => 'unread',
'sent_date' => current_time('timestamp'),
'message' => $system[$lang.'_message'],
'args' => $args
'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']
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']
$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']
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);
}
}
$sentSuccessfully = false;
if (CBV\system_can_send_emails_using_wpdb()) {
$response = \wpMandrill::mail($toEmail, $subject, !empty($html) ? $html : $altText, [], $attachments);
......@@ -682,7 +668,6 @@ function send_admin_triggered_notification($toEmail, $trigger = 'NO_TRIGGER', $a
}
}
}
/**
......@@ -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;
......