f3eb8d8b by Chris Boden

Merging notification changes (cron queue and mail cleaning) into live for CBV

1 parent 56376268
...@@ -188,34 +188,28 @@ function remove_notice($notification_id = -1) { ...@@ -188,34 +188,28 @@ function remove_notice($notification_id = -1) {
188 @trigger = notification unique slug name 188 @trigger = notification unique slug name
189 */ 189 */
190 function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),$send_override = false) { 190 function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),$send_override = false) {
191 191 global $wpdb;
192
193 $notification = get_notification_by_trigger($trigger); 192 $notification = get_notification_by_trigger($trigger);
194 193
195
196 if($notification) { 194 if($notification) {
197
198
199 // get the notification and notificatio details.... 195 // get the notification and notificatio details....
200 $nid = $notification->ID; 196 $nid = $notification->ID;
201 $details = get_post_meta($nid,'details',true); 197 $details = get_post_meta($nid,'details',true);
202 $email = get_post_meta($nid,'email',true); 198 $email = get_post_meta($nid,'email',true);
203 $system = get_post_meta($nid,'system',true); 199 $system = get_post_meta($nid,'system',true);
204 200
205
206 $notification->trigger = $details['trigger']; 201 $notification->trigger = $details['trigger'];
207 $notification->status = isset($details['status']) ? $details['status'] : "active"; 202 $notification->status = isset($details['status']) ? $details['status'] : "active";
208 $notification->type = $details['type']; 203 $notification->type = $details['type'];
209 $notification->sendto = $details['sendto']; 204 $notification->sendto = $details['sendto'];
210 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false; 205 $notification->is_email = (($email['text'] != "" || $email['html'] != "") && $email['subject'] != "") ? true : false;
211 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false; 206 $notification->is_system = (isset($system['message']) && $system['message'] != "") ? true : false;
212 207
213
214 // if is_system ========================================== 208 // if is_system ==========================================
215 if ($notification->is_system && $uid != 0) { 209 if ($notification->is_system && $uid != 0) {
216 get_user_notices($uid); 210 get_user_notices($uid);
217 $notices = Vars::$notices; 211 $notices = Vars::$notices;
218 212
219 $insert = array( 213 $insert = array(
220 'notification_id' => $nid 214 'notification_id' => $nid
221 , 'status' => 'unread' 215 , 'status' => 'unread'
...@@ -234,22 +228,61 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(), ...@@ -234,22 +228,61 @@ function send_triggered_notification($uid,$trigger="NO_TRIGGER",$args = array(),
234 update_user_meta($uid,'notices',$notices); 228 update_user_meta($uid,'notices',$notices);
235 } 229 }
236 230
237
238 // if is_email =========================================== 231 // if is_email ===========================================
239 if ($notification->is_email) { 232 if ($notification->is_email) {
240 send_email($uid,$email,$args, true); 233
241 } 234 $user = new User\Account($uid);
235
236 $email_address_preference = get_user_meta($user->ID, 'email_address_preference', true);
237 if (empty($email_address_preference)) {
238 $to_email = $user->user_email;
239 } else {
240
241 $pp = strtolower($email_address_preference)."_";
242 $to_email = get_user_meta($user->ID, $pp.'email', true);
243
244 if (empty($to_email)) {
245 $to_email = $user->user_email;
246 }
247 }
242 248
243 249 $contents = $email;
250
251 $from_email = get_bloginfo('admin_email');
252 $subject = strip_tags($contents['subject']);
253 $html = $contents['html'];
254 $alttext = strip_tags($contents['text']);
255
256 foreach($args as $key => $val) {
257 if ( (filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
258 $html = str_replace("{".$key."}","<a href='".$val."'>".$val."</a>",$html);
259 } else {
260 $html = str_replace("{".$key."}", $val, $html);
261 $alttext = str_replace("{".$key."}", $val, $alttext);
262 $subject = str_replace("{".$key."}", $val, $subject);
263 }
264 }
265
266 $attachments = array();
267 if (isset($contents['attachments'])) {
268 $attachments = $contents['attachments'];
269 }
270
271 $att1 = isset($attachments[0]) ? $attachments[0] : '';
272 $att2 = isset($attachments[1]) ? $attachments[1] : '';
273 $att3 = isset($attachments[2]) ? $attachments[2] : '';
274
275 $wpdb->query("INSERT INTO wp_mail_daemon (notification_id,from_email,to_email,subject,text,html,attachment1,attachment2,attachment3,sent,sent_date) VALUES ($nid,'$from_email','$to_email','$subject','$alttext','$html','$att1','$att2','$att3',0,'')");
276
277 //send_email($uid,$email,$args, true);
278 }
244 } 279 }
245 // if the system notification has set current user than get current user otherwise loop through the users needed. 280 // if the system notification has set current user than get current user otherwise loop through the users needed.
246 } 281 }
247 282
248 function send_email($uid = 0, $contents,$args, $override = false) { 283 function send_email($uid = 0, $contents,$args, $override = false) {
249
250 if ( $uid == 0 && !isset($args['email'])) { return; } 284 if ( $uid == 0 && !isset($args['email'])) { return; }
251 285
252
253 if ( isset($args['email'])) { 286 if ( isset($args['email'])) {
254 $to_email = $args['email']; 287 $to_email = $args['email'];
255 } else { 288 } else {
...@@ -272,10 +305,9 @@ function send_email($uid = 0, $contents,$args, $override = false) { ...@@ -272,10 +305,9 @@ function send_email($uid = 0, $contents,$args, $override = false) {
272 305
273 $from_address = get_bloginfo('admin_email'); 306 $from_address = get_bloginfo('admin_email');
274 307
275 308 $subject = strip_tags($contents['subject']);
276 $subject = $contents['subject']; 309 $html = $contents['html'];
277 $html = $contents['html']; 310 $alttext = $contents['text'];
278 $alttext = $contents['text'];
279 311
280 $headers = ""; 312 $headers = "";
281 313
...@@ -296,11 +328,11 @@ function send_email($uid = 0, $contents,$args, $override = false) { ...@@ -296,11 +328,11 @@ function send_email($uid = 0, $contents,$args, $override = false) {
296 if ( (filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) { 328 if ( (filter_var($val, FILTER_VALIDATE_URL) !== false) && !empty($html)) {
297 $message = str_replace("{".$key."}","<a href='".$val."'>".$val."</a>",$message); 329 $message = str_replace("{".$key."}","<a href='".$val."'>".$val."</a>",$message);
298 } else { 330 } else {
299 $message = str_replace("{".$key."}",$val,$message); 331 $message = str_replace("{".$key."}", $val, $message);
332 $subject = str_replace("{".$key."}", $val, $subject);
300 } 333 }
301
302 } 334 }
303 335
304 // Additional headers 336 // Additional headers
305 //$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n"; 337 //$headers .= 'To: '.$to_email.' <'.$to_email.'>' . "\r\n";
306 $headers .= 'From: CICBV <'.$from_address.'>' . "\r\n"; 338 $headers .= 'From: CICBV <'.$from_address.'>' . "\r\n";
......