| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $wpscfunction, $current_user,$wpdb; |
| 7 |
|
| 8 |
$from_name = get_option('wpsc_en_from_name',''); |
| 9 |
$from_email = get_option('wpsc_en_from_email',''); |
| 10 |
$reply_to = get_option('wpsc_en_reply_to',''); |
| 11 |
$ignore_emails = get_option('wpsc_en_ignore_emails',''); |
| 12 |
|
| 13 |
$wpsc_email_sending_method = get_option('wpsc_email_sending_method') ; |
| 14 |
|
| 15 |
if ( !$from_name || !$from_email ) { |
| 16 |
return; |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
$email_templates = get_terms([ |
| 21 |
'taxonomy' => 'wpsc_en', |
| 22 |
'hide_empty' => false, |
| 23 |
'orderby' => 'ID', |
| 24 |
'order' => 'ASC', |
| 25 |
'meta_query' => array( |
| 26 |
'relation' => 'AND', |
| 27 |
array( |
| 28 |
'key' => 'type', |
| 29 |
'value' => 'change_status', |
| 30 |
'compare' => '=' |
| 31 |
), |
| 32 |
), |
| 33 |
]); |
| 34 |
|
| 35 |
|
| 36 |
$email_subject = get_option('wpsc_email_notification_subject'); |
| 37 |
$email_body = get_option('wpsc_email_notification_body'); |
| 38 |
|
| 39 |
foreach ($email_templates as $email) : |
| 40 |
|
| 41 |
$conditions = get_term_meta($email->term_id,'conditions',true); |
| 42 |
if( $wpscfunction->check_ticket_conditions($conditions,$ticket_id) ) : |
| 43 |
|
| 44 |
$subject = $wpscfunction->replace_macro($email_subject['email_subject_' . $email->term_id], $ticket_id); |
| 45 |
$subject = '['.get_option('wpsc_ticket_alice','').$ticket_id.'] '.stripslashes($subject); |
| 46 |
$body = $wpscfunction->replace_macro(stripslashes($email_body['email_body_' . $email->term_id]), $ticket_id); |
| 47 |
$recipients = get_term_meta($email->term_id,'recipients',true); |
| 48 |
$extra_recipients = get_term_meta($email->term_id,'extra_recipients',true); |
| 49 |
|
| 50 |
$email_addresses = array(); |
| 51 |
foreach ($recipients as $recipient) { |
| 52 |
if(is_numeric($recipient)){ |
| 53 |
$agents = get_terms([ |
| 54 |
'taxonomy' => 'wpsc_agents', |
| 55 |
'hide_empty' => false, |
| 56 |
'meta_query' => array( |
| 57 |
'relation' => 'AND', |
| 58 |
array( |
| 59 |
'key' => 'role', |
| 60 |
'value' => $recipient, |
| 61 |
'compare' => '=' |
| 62 |
), |
| 63 |
), |
| 64 |
]); |
| 65 |
foreach ($agents as $agent) { |
| 66 |
$user_id = get_term_meta($agent->term_id,'user_id',true); |
| 67 |
if($user_id){ |
| 68 |
$user = get_user_by('id',$user_id); |
| 69 |
$email_addresses[] = $user->user_email; |
| 70 |
} |
| 71 |
} |
| 72 |
} else { |
| 73 |
switch ($recipient) { |
| 74 |
case 'customer': |
| 75 |
$customer_email = $wpscfunction->get_ticket_fields($ticket_id,'customer_email'); |
| 76 |
$email_addresses[] = $customer_email; |
| 77 |
break; |
| 78 |
case 'assigned_agent': |
| 79 |
$email_addresses = array_merge($email_addresses,$wpscfunction->get_assigned_agent_emails($ticket_id)); |
| 80 |
break; |
| 81 |
case 'previously_assigned_agent': |
| 82 |
$email_addresses = array_merge($email_addresses,$wpscfunction->get_previously_assigned_agents($ticket_id)); |
| 83 |
break; |
| 84 |
case 'extra_ticket_users' : |
| 85 |
$extra_users = $wpscfunction->get_ticket_meta($ticket_id,'extra_ticket_users'); |
| 86 |
$email_addresses = array_merge($email_addresses, $extra_users); |
| 87 |
break; |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
if(isset($extra_recipients[0]) && $extra_recipients[0] ) { |
| 92 |
$email_addresses = array_merge($email_addresses,$extra_recipients); |
| 93 |
} |
| 94 |
|
| 95 |
if(in_array('current_user', $recipients)){ |
| 96 |
$email_addresses[] = $current_user->user_email; |
| 97 |
}else{ |
| 98 |
$email_addresses = array_diff($email_addresses,array($current_user->user_email)); |
| 99 |
} |
| 100 |
$email_addresses = apply_filters('wpsc_en_change_status_email_addresses',$email_addresses,$email,$ticket_id); |
| 101 |
$email_addresses = array_values($email_addresses); |
| 102 |
|
| 103 |
if ($ignore_emails) { |
| 104 |
$ignore_emails = array_map('trim', $ignore_emails); |
| 105 |
$email_addresses = array_map('trim', $email_addresses); |
| 106 |
foreach ($ignore_emails as $ignore_email) { |
| 107 |
foreach($email_addresses as $index => $email){ |
| 108 |
if(fnmatch($ignore_email, $email)){ |
| 109 |
unset($email_addresses[$index]); |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
$email_addresses = array_unique($email_addresses); |
| 115 |
$email_addresses = array_values($email_addresses); |
| 116 |
|
| 117 |
$to = isset($email_addresses[0])? $email_addresses[0] : ''; |
| 118 |
if($to){ |
| 119 |
unset($email_addresses[0]); |
| 120 |
} else { |
| 121 |
continue; // no email address found to send. So go to next foreach iteration. |
| 122 |
} |
| 123 |
|
| 124 |
$from_email = apply_filters('wpsc_change_status_from_email_headers',$from_email,$ticket_id); |
| 125 |
$reply_to = apply_filters('wpsc_change_status_replyto_headers',$reply_to,$ticket_id); |
| 126 |
|
| 127 |
$bcc = implode(',',$email_addresses); |
| 128 |
|
| 129 |
$to = apply_filters('wpsc_change_status_to_email',$to,$email_addresses,$ticket_id); |
| 130 |
$bcc = apply_filters('wpsc_change_status_bcc_emails',$bcc,$email_addresses,$ticket_id); |
| 131 |
|
| 132 |
$args = array( |
| 133 |
'ticket_id' => $ticket_id, |
| 134 |
'from_email' => $from_email, |
| 135 |
'reply_to' => $reply_to, |
| 136 |
'email_subject' => $subject, |
| 137 |
'email_body' => $body, |
| 138 |
'to_email' => $to, |
| 139 |
'bcc_email' => $bcc, |
| 140 |
'date_created' => date("Y-m-d H:i:s"), |
| 141 |
'mail_status' => 0, |
| 142 |
'email_type' => 'change_status', |
| 143 |
|
| 144 |
); |
| 145 |
|
| 146 |
if($wpsc_email_sending_method){ |
| 147 |
|
| 148 |
$wpdb->insert( $wpdb->prefix . 'wpsc_email_notification',$args); |
| 149 |
|
| 150 |
}else{ |
| 151 |
|
| 152 |
$headers = "From: {$from_name} <{$from_email}>\r\n"; |
| 153 |
$headers .= "Reply-To: {$reply_to}\r\n"; |
| 154 |
$email_addresses = explode(',',$bcc); |
| 155 |
foreach ($email_addresses as $email_address) { |
| 156 |
if(strlen($email_address)) $headers .= "BCC: {$email_address}\r\n"; |
| 157 |
} |
| 158 |
|
| 159 |
$headers .= "Content-Type: text/html; charset=utf-8\r\n"; |
| 160 |
|
| 161 |
wp_mail($to, $subject, $body, $headers); |
| 162 |
|
| 163 |
} |
| 164 |
|
| 165 |
do_action('wpsc_after_change_status_mail',$ticket_id,$args); |
| 166 |
|
| 167 |
endif; |
| 168 |
|
| 169 |
endforeach; |
| 170 |
|