b43072e0 by Marty Penner

Make notifications translatable

1 parent a1a85c5a
......@@ -79,7 +79,9 @@ function print_user_notices($showOnlyUnread = false)
}
$system = get_post_meta($notice['notification_id'], 'system', true);
$content = $system['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) {
......@@ -214,6 +216,9 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
/** @var \wpdb $wpdb */
global $wpdb;
// LANGTODO: get this from the user's preference. As it stands, if an admin triggers this, the notification will be in whatever language the admin is viewing the page as
$lang = ICL_LANGUAGE_CODE;
/** @var \StdClass $notification */
$notification = get_notification_by_trigger($trigger);
......@@ -228,10 +233,8 @@ 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['text'] != '' || $email['html'] != '') && $email['subject'] != '')
? true
: false;
$notification->is_system = (isset($system['message']) && $system['message'] != '') ? true : false;
$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) {
......@@ -257,52 +260,59 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
// if is_email ===========================================
if ($notification->is_email) {
if ($uid == 0 && !isset($args['email'])) {
return;
} elseif ($uid == 0 && isset($args['email'])) {
$to_email = $args['email'];
$toEmail = $args['email'];
} else {
$user = new User\Account($uid);
$email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
if (empty($email_address_preference)) {
$to_email = $user->user_email;
$toEmail = $user->user_email;
} else {
$pp = strtolower($email_address_preference).'_';
$toEmail = get_user_meta($user->ID, $pp.'email', true);
$pp = strtolower($email_address_preference)."_";
$to_email = get_user_meta($user->ID, $pp.'email', true);
if (empty($to_email)) {
$to_email = $user->user_email;
if (empty($toEmail)) {
$toEmail = $user->user_email;
}
}
}
$contents = $email;
$from_email = get_bloginfo('admin_email');
$subject = strip_tags($contents['subject']);
$html = @$contents['html'];
$alttext = strip_tags($contents['text']);
$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);
$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 = [];
if (isset($contents['attachments'])) {
$attachments = $contents['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) {
......@@ -313,7 +323,7 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
$sentSuccessfully = false;
if (CBV\system_can_send_emails()) {
$response = \wpMandrill::mail($to_email, $subject, !empty($html) ? $html : $alttext, [], $attachments);
$response = \wpMandrill::mail($toEmail, $subject, !empty($html) ? $html : $altText, [], $attachments);
if (
is_array($response)
&& isset($response[0]['status'])
......@@ -328,10 +338,10 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
'wp_mail_daemon',
[
'notification_id' => $nid,
'from_email' => $from_email,
'to_email' => $to_email,
'from_email' => $fromEmail,
'to_email' => $toEmail,
'subject' => $subject,
'text' => $alttext,
'text' => $altText,
'html' => $html,
'attachment1' => $att1,
'attachment2' => $att2,
......
......@@ -9,7 +9,7 @@ use Tz\WordPress\Tools\Notifications\Settings;
<div class="wrap">
<h2>Notifications - Create New</h2>
<?php if (isset($flash) && $flash != ""): ?>
<?php if (isset($flash) && $flash != ''): ?>
<div class="post-success">
<?php echo $flash; ?>
</div>
......@@ -231,4 +231,4 @@ use Tz\WordPress\Tools\Notifications\Settings;
jQuery('.scheduled_sendto').show();
}
}
</script>
\ No newline at end of file
</script>
......