PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 2.3.1
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v2.3.1
3.5.4 3.5.3 3.5.2 3.5.1 3.4.9 3.5.0 3.4.8 3.4.7 trunk 2.3.1 3.3.6 3.3.7 3.3.8 3.3.9 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6
supportcandy / includes / actions / en_assign_agent.php

en_assign_agent.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 2.3.1, at includes/actions/en_assign_agent.php

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