| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
global $wppmfunction, $current_user,$wpdb; |
| 6 |
$from_name = get_option('wppm_en_from_name'); |
| 7 |
$from_email = get_option('wppm_en_from_email'); |
| 8 |
$ignore_emails = get_option('wppm_en_ignore_emails'); |
| 9 |
$project_id = absint(sanitize_text_field($project_id)); |
| 10 |
$project_data = $wppmfunction->get_project($project_id); |
| 11 |
$proj_users = $project_data['users']; |
| 12 |
if(!empty($proj_users)){ |
| 13 |
$proj_users = explode(',',$proj_users); |
| 14 |
} |
| 15 |
if ( !$from_name || !$from_email ) { |
| 16 |
return; |
| 17 |
} |
| 18 |
$wppm_email_notificatins = get_option('wppm_email_notification'); |
| 19 |
$wppm_default_email_notification_to_current_user = get_option('wppm_default_email_notification_to_current_user'); |
| 20 |
foreach ($wppm_email_notificatins as $key=>$val) : |
| 21 |
if($val['type']=='change_project_assign_users'){ |
| 22 |
$subject = $wppmfunction->replace_macro(stripslashes($val['subject']), $project_id); |
| 23 |
$body = $wppmfunction->replace_macro(stripslashes($val['body']),$project_id); |
| 24 |
$recipients = $val['recipients']; |
| 25 |
$email_addresses = array(); |
| 26 |
if(!empty($recipients)){ |
| 27 |
foreach ($recipients as $recipient) { |
| 28 |
if(is_numeric($recipient)){ |
| 29 |
if($recipient == 1 && !empty($proj_users)) { |
| 30 |
foreach($proj_users as $user){ |
| 31 |
if(!empty($user)){ |
| 32 |
$user = absint($user); |
| 33 |
$project_user_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wppm_project_users WHERE proj_id = %d AND user_id = %d",$project_id,$user)); |
| 34 |
$userdata = get_userdata($user); |
| 35 |
if((!empty($project_user_data)) && $project_user_data->role_id == 1){ |
| 36 |
$email_addresses[] = $userdata->user_email; |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
} |
| 41 |
elseif($recipient == 2 && !empty($proj_users)){ |
| 42 |
foreach($proj_users as $user){ |
| 43 |
if(!empty($user)){ |
| 44 |
$user = absint($user); |
| 45 |
$project_user_data = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$wpdb->prefix}wppm_project_users WHERE proj_id = %d AND user_id = %d",$project_id,$user)); |
| 46 |
$userdata = get_userdata($user); |
| 47 |
if((!empty($project_user_data)) && $project_user_data->role_id == 2){ |
| 48 |
$email_addresses[] = $userdata->user_email; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} else { |
| 54 |
switch ($recipient) { |
| 55 |
case 'previously_assigned_user': |
| 56 |
$email_addresses = array_merge($email_addresses,$wppmfunction->get_previously_assigned_users($project_id)); |
| 57 |
break; |
| 58 |
case 'project_creator': |
| 59 |
$email_addresses = array_merge($email_addresses,$wppmfunction->get_project_creator_email($project_id)); |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
} |
| 64 |
} |
| 65 |
$email_addresses = array_unique($email_addresses); |
| 66 |
$email_addresses = array_diff($email_addresses,$ignore_emails); |
| 67 |
if(empty($wppm_default_email_notification_to_current_user)){ |
| 68 |
$email_addresses = array_diff($email_addresses,array($current_user->user_email)); |
| 69 |
} |
| 70 |
$email_addresses = apply_filters('wppm_en_project_assign_users_email_addresses',$email_addresses,$val,$project_id); |
| 71 |
$email_addresses = array_values($email_addresses); |
| 72 |
$to = isset($email_addresses[0])? $email_addresses[0] : ''; |
| 73 |
if($to){ |
| 74 |
unset($email_addresses[0]); |
| 75 |
} else { |
| 76 |
continue; // no email address found to send. So go to next foreach iteration. |
| 77 |
} |
| 78 |
$bcc = implode(',',$email_addresses); |
| 79 |
$headers = "From: {$from_name} <{$from_email}>\r\n"; |
| 80 |
$email_addresses = explode(',',$bcc); |
| 81 |
foreach ($email_addresses as $email_address) { |
| 82 |
$headers .= "BCC: {$email_address}\r\n"; |
| 83 |
} |
| 84 |
$headers .= "Content-Type: text/html; charset=UTF-8\r\n"; |
| 85 |
wp_mail($to, $subject, $body, $headers); |
| 86 |
do_action('wppm_after_project_change_users_mail',$project_id,$val); |
| 87 |
} |
| 88 |
endforeach; |
| 89 |
|
| 90 |
|