f0aa165b by Insu Mun

Add a notification message for individual notification of user. Also display use…

…r's notification message not the message saved in the content.
1 parent 2a58ec97
...@@ -66,22 +66,80 @@ function get_user_notices($uid) ...@@ -66,22 +66,80 @@ function get_user_notices($uid)
66 */ 66 */
67 function print_user_notices($showOnlyUnread = false) 67 function print_user_notices($showOnlyUnread = false)
68 { 68 {
69 global $wpdb;
69 $user = new User\CurrentAccount(); 70 $user = new User\CurrentAccount();
70 71
71 get_user_notices($user->ID); 72 get_user_notices($user->ID);
72 73
73 $notices = Vars::$notices; 74 $notices = Vars::$notices;
74 $rows = ''; 75 $rows = '';
75
76 foreach ($notices as $index => $notice) { 76 foreach ($notices as $index => $notice) {
77 if ($showOnlyUnread && $notice['status'] != 'unread') { 77 if ($showOnlyUnread && $notice['status'] != 'unread') {
78 continue; 78 continue;
79 } 79 }
80 80
81 $triggered = get_post_meta($notice['notification_id'], 'trigger', true);
82
83 $enableTriggers = [
84 'MEMBERSHIP_FEES_DUE',
85 'STUDENT_FEES_DUE'
86 ];
87
81 $system = get_post_meta($notice['notification_id'], 'system', true); 88 $system = get_post_meta($notice['notification_id'], 'system', true);
82 $content = isset($system[ICL_LANGUAGE_CODE.'_message']) 89 $content = '';
83 ? $system[ICL_LANGUAGE_CODE.'_message'] 90 if(!isset($notice['message']) && in_array($triggered, $enableTriggers)){
84 : $system['en_message']; 91 $emailAddressPreference = get_user_meta($user->ID, 'email_address_preference', true);
92 if (empty($emailAddressPreference)) {
93 $userData = get_userdata($user->ID);
94 $toEmail = $userData->user_email;
95 } else {
96 $pp = strtolower($emailAddressPreference) . '_';
97 $toEmail = get_user_meta($user->ID, $pp . 'email', true);
98
99 if (empty($toEmail)) {
100 $userData = get_userdata($user->ID);
101 $toEmail = $userData->user_email;
102 }
103 }
104
105 $message = '';
106 if (in_array($triggered, $enableTriggers)) {
107 // Get old content from notification
108 $results = $wpdb->get_results(
109 "
110 SELECT `text`, `to_email`
111 FROM `wp_mail_daemon`
112 WHERE `notification_id` = " . $notice['notification_id'] . ";
113 "
114 );
115
116 if (count($results) <= 0) {
117 continue;
118 }
119 foreach ($results as $result) {
120 if ($toEmail == $result->to_email) {
121 $stringArray = (preg_split("/\r\n|\n|\r/", $result->text, -1, PREG_SPLIT_NO_EMPTY));
122 $message = implode("\n", [$stringArray[0], $stringArray[1]]);
123 break;
124 }
125 }
126
127 $notices[$index]['message'] = $message;
128 update_user_meta($user->ID, 'notices', $notices);
129
130 $content = $message;
131
132 }
133 } else {
134 if(isset($notice['message'])) {
135 $content = $notice['message'];
136 } else {
137 $content = isset($system[ICL_LANGUAGE_CODE.'_message'])
138 ? $system[ICL_LANGUAGE_CODE.'_message']
139 : $system['en_message'];
140 }
141 }
142
85 143
86 if (isset($notice['args']) && count($notice['args']) > 0) { 144 if (isset($notice['args']) && count($notice['args']) > 0) {
87 foreach ($notice['args'] as $key => $val) { 145 foreach ($notice['args'] as $key => $val) {
...@@ -241,12 +299,36 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args = ...@@ -241,12 +299,36 @@ function send_triggered_notification($uid = 0, $trigger = 'NO_TRIGGER', $args =
241 get_user_notices($uid); 299 get_user_notices($uid);
242 $notices = Vars::$notices; 300 $notices = Vars::$notices;
243 301
302 // Add a system message into the usermeta as well to track the system notification.
244 $insert = [ 303 $insert = [
245 'notification_id' => $nid, 304 'notification_id' => $nid,
246 'status' => 'unread', 305 'status' => 'unread',
247 'sent_date' => current_time('timestamp'), 306 'sent_date' => current_time('timestamp'),
307 'message' => $system[$lang.'_message'],
248 'args' => $args 308 'args' => $args
249 ]; 309 ];
310
311 // Convert the args to actual message
312 $content = $insert['message'];
313 if (isset($insert['args']) && count($insert['args']) > 0) {
314 foreach ($insert['args'] as $key => $val) {
315 if (filter_var($val, FILTER_VALIDATE_URL) !== false) {
316 $content = str_replace(
317 '{'.$key.'}',
318 "<a href='"
319 .$val
320 ."'>"
321 .__('Click here', CBV_DOMAIN)
322 .'</a>',
323 $content
324 );
325 } else {
326 $content = str_replace('{'.$key.'}', $val, $content);
327 }
328 }
329 $insert['message'] = $content;
330 }
331
250 if (empty($notices)) { 332 if (empty($notices)) {
251 $notices = []; 333 $notices = [];
252 } 334 }
......