PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.2
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.2
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 / class-wpsc-email-notifications.php

class-wpsc-email-notifications.php in SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent 3.5.2, at includes/class-wpsc-email-notifications.php

579 lines 14.3 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 if ( ! class_exists( 'WPSC_Email_Notifications' ) ) :
7
8 class WPSC_Email_Notifications {
9
10 /**
11 * From name
12 *
13 * @var string
14 */
15 public $from_name;
16
17 /**
18 * From email address
19 *
20 * @var string
21 */
22 public $from_email;
23
24 /**
25 * Reply to email address
26 *
27 * @var string
28 */
29 public $reply_to;
30
31 /**
32 * Recipient "to" addresses for email notification
33 *
34 * @var array
35 */
36 public $to = array();
37
38 /**
39 * Recipient "cc" addresses for email notification
40 *
41 * @var array
42 */
43 public $cc = array();
44
45 /**
46 * Recipient "bcc" addresses for email notification
47 *
48 * @var array
49 */
50 public $bcc = array();
51
52 /**
53 * Subject for email notification
54 *
55 * @var string
56 */
57 public $subject;
58
59 /**
60 * Body for email notification
61 *
62 * @var string
63 */
64 public $body;
65
66 /**
67 * Email notification template
68 *
69 * @var Array
70 */
71 public $template;
72
73 /**
74 * Email notification template key
75 *
76 * @var Array
77 */
78 public $template_key;
79
80 /**
81 * Attachments of email notification
82 *
83 * @var Array
84 */
85 public $attachments = array();
86
87 /**
88 * Blocked email array
89 *
90 * @var Array
91 */
92 public static $block_emails = array();
93
94 /**
95 * Initialize this class
96 */
97 public static function init() {
98
99 add_action( 'init', array( __CLASS__, 'block_emails_list' ), 11 );
100 add_action( 'wpsc_cron_five_minute', array( __CLASS__, 'send_background_emails' ) );
101 add_action( 'wpsc_run_ajax_background_process', array( __CLASS__, 'send_background_emails' ) );
102 }
103
104 /**
105 * Block emails
106 *
107 * @return void
108 */
109 public static function block_emails_list() {
110
111 $gs = get_option( 'wpsc-en-general' );
112 self::$block_emails = is_array( $gs['blocked-emails'] ) && ! empty( $gs['blocked-emails'] ) ? $gs['blocked-emails'] : array();
113 self::$block_emails = array_unique( array_merge( self::$block_emails, array( '[email protected]' ) ) );
114 self::$block_emails = apply_filters( 'wpsc_en_blocked_emails', self::$block_emails );
115 }
116
117 /**
118 * Send background emails
119 *
120 * @return void
121 */
122 public static function send_background_emails() {
123
124 if ( get_transient( 'wpsc_email_cron_lock' ) ) {
125 return;
126 }
127
128 $gs = get_option( 'wpsc-en-general' );
129 $ttl = max( 30, (int) $gs['cron-email-count'] * 2 );
130 set_transient( 'wpsc_email_cron_lock', time(), $ttl );
131
132 try {
133 self::process_queue();
134 } finally {
135 delete_transient( 'wpsc_email_cron_lock' );
136 }
137 }
138
139 /**
140 * Process email queue
141 *
142 * @return void
143 */
144 public static function process_queue() {
145
146 $gs = get_option( 'wpsc-en-general' );
147 $count = $gs['cron-email-count'];
148
149 $emails = WPSC_Background_Email::find(
150 array(
151 'items_per_page' => $count,
152 'orderby' => 'priority',
153 'order' => 'ASC',
154 )
155 )['results'];
156
157 foreach ( $emails as $email ) {
158
159 $en = new self();
160 $en->from_name = $email->from_name;
161 $en->from_email = $email->from_email;
162 $en->reply_to = $email->reply_to;
163 $en->to = $email->to_email;
164 $en->cc = $email->cc_email;
165 $en->bcc = $email->bcc_email;
166 $en->subject = $email->subject;
167 $en->body = $email->body;
168 $en->attachments = $email->attachments;
169 $en = apply_filters( 'wpsc_en_send_data', $en, $email );
170 $en->send();
171
172 // delete db record.
173 WPSC_Background_Email::destroy( $email->id );
174 }
175 }
176
177 /**
178 * Send email for this object
179 *
180 * @return string
181 */
182 public function send() {
183
184 $en = get_option( 'wpsc-en-general' );
185 // headers.
186 $headers = '';
187 if ( $this->from_name && $this->from_email ) {
188 $headers = "From: {$this->from_name} <{$this->from_email}>\r\n";
189 }
190 if ( $this->reply_to ) {
191 $headers .= "Reply-To: {$this->reply_to}\r\n";
192 }
193 foreach ( $this->cc as $email ) {
194 $headers .= "CC: {$email}\r\n";
195 }
196 foreach ( $this->bcc as $email ) {
197 $headers .= "BCC: {$email}\r\n";
198 }
199
200 // attachments.
201 $attachments = array();
202 if ( $en['attachments-in-notification'] == 'actual-files' ) {
203
204 $upload_dir = wp_upload_dir();
205 foreach ( $this->attachments as $attachment ) :
206 if ( ! $attachment->id ) {
207 continue;
208 }
209 $filepath = $upload_dir['basedir'] . '/wpsc/temp/' . $attachment->id;
210 if ( ! file_exists( $filepath ) ) {
211 mkdir( $filepath, 0777, true );
212 }
213 $filepath .= '/' . $attachment->name;
214 copy( $upload_dir['basedir'] . $attachment->file_path, $filepath );
215 $attachments[] = $filepath;
216 endforeach;
217 }
218
219 if ( is_rtl() ) {
220 $this->body = '<div style="direction: rtl;">' . $this->body . '</div>';
221 }
222 $headers .= "Content-Type: text/html; charset=utf-8\r\n";
223 $headers = apply_filters( 'wpsc_en_headers', $headers, $this );
224
225 // send.
226 $flag = wp_mail( $this->to, $this->subject, $this->body, $headers, $attachments );
227
228 if ( $en['attachments-in-notification'] == 'actual-files' ) {
229
230 // Remove temp files & directories.
231 foreach ( $attachments as $filepath ) {
232 if ( file_exists( $filepath ) ) {
233 wp_delete_file( $filepath );
234 }
235 }
236 foreach ( $this->attachments as $attachment ) {
237 if ( is_dir( $upload_dir['basedir'] . '/wpsc/temp/' . $attachment->id ) ) {
238 rmdir( $upload_dir['basedir'] . '/wpsc/temp/' . $attachment->id );
239 }
240 }
241 }
242 return $flag;
243 }
244
245 /**
246 * Check whether conditions matches (if any) and if settings are correct
247 *
248 * @return boolean
249 */
250 public function is_valid() {
251
252 // check if is_enable.
253 if ( ! $this->template['is_enable'] ) {
254 return false;
255 }
256
257 // check block notification flag in misc to skip notifications if it is set for the ticket.
258 $misc_array = $this->ticket->misc ? $this->ticket->misc : array();
259 if ( ! empty( $misc_array['block_notifications'] ) ) {
260 return false;
261 }
262
263 // from name & email.
264 $en_general = get_option( 'wpsc-en-general' );
265
266 $this->from_name = $en_general['from-name'] ? $en_general['from-name'] : '';
267 $this->from_email = $en_general['from-email'] ? $en_general['from-email'] : '';
268 $this->reply_to = $en_general['reply-to'] ? $en_general['reply-to'] : $this->from_email;
269
270 // subject.
271 $this->set_subject();
272
273 // body.
274 $body = WPSC_Translations::get( 'wpsc-en-tn-body-' . $this->template_key, stripslashes( $this->template['body']['text'] ) );
275 $this->body = WPSC_Macros::replace( $body, $this->ticket, 'email-notification' );
276
277 // to addreses.
278 $this->set_to_addresses();
279
280 // cc addreses.
281 $this->set_cc_addresses();
282
283 // bcc addreses.
284 $this->set_bcc_addresses();
285
286 if ( ! $this->to ) {
287
288 $other_emails = array_merge( $this->cc, $this->bcc );
289 if ( $other_emails ) {
290 $this->to[] = $other_emails[0];
291 } else {
292 return false;
293 }
294 }
295 // add to email address to cc (spam issue).
296 if ( count( $this->to ) > 5 ) {
297 $other_to_emails = array_slice( $this->to, 5 );
298 $this->to = array_diff( $this->to, $other_to_emails );
299 $this->cc = array_merge( $this->cc, $other_to_emails );
300 }
301
302 // check for conditions.
303 if ( ! WPSC_Ticket_Conditions::is_valid( $this->template['conditions'], $this->ticket ) ) {
304 return false;
305 }
306
307 return true;
308 }
309
310 /**
311 * Set subject for this email
312 *
313 * @return void
314 */
315 public function set_subject() {
316
317 $general_settings = get_option( 'wpsc-gs-general' );
318 $ticket_alice = $general_settings['ticket-alice'];
319
320 $subject = WPSC_Translations::get( 'wpsc-en-tn-subject-' . $this->template_key, stripslashes( $this->template['subject'] ) );
321 $this->subject = '[' . $ticket_alice . $this->ticket->id . '] ' . WPSC_Macros::replace( $subject, $this->ticket );
322 }
323
324 /**
325 * Get "to" email addresses
326 *
327 * @return void
328 */
329 public function set_to_addresses() {
330
331 $current_user = WPSC_Current_User::$current_user;
332 $et = $this->template;
333 $to = array();
334 $gs = get_option( 'wpsc-en-general' );
335
336 // general recipients.
337 $general_recipients = $et['to']['general-recipients'];
338 foreach ( $general_recipients as $recipient ) {
339
340 switch ( $recipient ) {
341
342 case 'customer':
343 $to[] = $this->ticket->customer->email;
344 break;
345
346 case 'assignee':
347 $assinee = $this->ticket->assigned_agent;
348 foreach ( $assinee as $agent ) {
349 if (
350 $agent->is_agentgroup ||
351 ! $agent->is_active
352 ) {
353 continue;
354 }
355 $to[] = $agent->customer->email;
356 }
357 break;
358
359 case 'add-recipients':
360 $additional_recipients = $this->ticket->add_recipients ? array_map( 'trim', $this->ticket->add_recipients ) : array();
361 foreach ( $additional_recipients as $email ) {
362 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
363 continue;
364 }
365 $to[] = $email;
366 }
367 break;
368
369 case 'current-user':
370 $to[] = $current_user->customer->email;
371 break;
372
373 default:
374 $to = apply_filters( 'wpsc_en_get_to_addresses', $to, $recipient, $this );
375 }
376 }
377
378 // agent roles.
379 $agent_roles = $et['to']['agent-roles'];
380 foreach ( $agent_roles as $role_id ) {
381 $agents = WPSC_Agent::get_by_role( $role_id );
382 foreach ( $agents as $agent ) {
383 if ( ! $agent->is_active ) {
384 continue;
385 }
386 $to[] = $agent->customer->email;
387 }
388 }
389
390 // custom.
391 $custom = $et['to']['custom'] ? array_map( 'trim', $et['to']['custom'] ) : array();
392 foreach ( $custom as $email ) {
393 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
394 continue;
395 }
396 $to[] = $email;
397 }
398
399 if ( ! in_array( $et['event'], WPSC_EN_Settings_TN::$ignore_current_user ) && ! in_array( 'current-user', $general_recipients ) ) {
400 $to = array_diff( $to, array( $current_user->customer->email ) );
401 }
402
403 $to = apply_filters( 'wpsc_en_to_addresses', $to, $this );
404 $to = array_diff( $to, self::$block_emails );
405 $this->to = array_unique( $to );
406 }
407
408 /**
409 * Get "cc" email addresses
410 *
411 * @return void
412 */
413 public function set_cc_addresses() {
414
415 $current_user = WPSC_Current_User::$current_user;
416 $et = $this->template;
417 $cc = array();
418 $gs = get_option( 'wpsc-en-general' );
419
420 // general recipients.
421 $general_recipients = $et['cc']['general-recipients'];
422 foreach ( $general_recipients as $recipient ) {
423
424 switch ( $recipient ) {
425
426 case 'customer':
427 $cc[] = $this->ticket->customer->email;
428 break;
429
430 case 'assignee':
431 $assinee = $this->ticket->assigned_agent;
432 foreach ( $assinee as $agent ) {
433 if (
434 $agent->is_agentgroup ||
435 ! $agent->is_active
436 ) {
437 continue;
438 }
439 $cc[] = $agent->customer->email;
440 }
441 break;
442
443 case 'add-recipients':
444 $additional_recipients = $this->ticket->add_recipients ? array_map( 'trim', $this->ticket->add_recipients ) : array();
445 foreach ( $additional_recipients as $email ) {
446 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
447 continue;
448 }
449 $cc[] = $email;
450 }
451 break;
452
453 case 'current-user':
454 $cc[] = $current_user->customer->email;
455 break;
456
457 default:
458 $cc = apply_filters( 'wpsc_en_get_cc_addresses', $cc, $recipient, $this );
459 }
460 }
461
462 // agent roles.
463 $agent_roles = $et['cc']['agent-roles'];
464 foreach ( $agent_roles as $role_id ) {
465 $agents = WPSC_Agent::get_by_role( $role_id );
466 foreach ( $agents as $agent ) {
467 if ( ! $agent->is_active ) {
468 continue;
469 }
470 $cc[] = $agent->customer->email;
471 }
472 }
473
474 // custom.
475 $custom = $et['cc']['custom'] ? array_map( 'trim', $et['cc']['custom'] ) : array();
476 foreach ( $custom as $email ) {
477 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
478 continue;
479 }
480 $cc[] = $email;
481 }
482
483 if ( ! in_array( $et['event'], WPSC_EN_Settings_TN::$ignore_current_user ) && ! in_array( 'current-user', $general_recipients ) ) {
484 $cc = array_diff( $cc, array( $current_user->customer->email ) );
485 }
486
487 $cc = apply_filters( 'wpsc_en_cc_addresses', $cc, $this );
488 $cc = array_diff( $cc, self::$block_emails );
489 $this->cc = array_unique( $cc );
490 }
491
492 /**
493 * Get "bcc" email addresses
494 *
495 * @return void
496 */
497 public function set_bcc_addresses() {
498
499 $current_user = WPSC_Current_User::$current_user;
500 $et = $this->template;
501 $bcc = array();
502 $gs = get_option( 'wpsc-en-general' );
503
504 // general recipients.
505 $general_recipients = $et['bcc']['general-recipients'];
506 foreach ( $general_recipients as $recipient ) {
507
508 switch ( $recipient ) {
509
510 case 'customer':
511 $bcc[] = $this->ticket->customer->email;
512 break;
513
514 case 'assignee':
515 $assinee = $this->ticket->assigned_agent;
516 foreach ( $assinee as $agent ) {
517 if (
518 $agent->is_agentgroup ||
519 ! $agent->is_active
520 ) {
521 continue;
522 }
523 $bcc[] = $agent->customer->email;
524 }
525 break;
526
527 case 'add-recipients':
528 $additional_recipients = $this->ticket->add_recipients ? array_map( 'trim', $this->ticket->add_recipients ) : array();
529 foreach ( $additional_recipients as $email ) {
530 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
531 continue;
532 }
533 $bcc[] = $email;
534 }
535 break;
536
537 case 'current-user':
538 $bcc[] = $current_user->customer->email;
539 break;
540
541 default:
542 $bcc = apply_filters( 'wpsc_en_get_bcc_addresses', $bcc, $recipient, $this );
543 }
544 }
545
546 // agent roles.
547 $agent_roles = $et['bcc']['agent-roles'];
548 foreach ( $agent_roles as $role_id ) {
549 $agents = WPSC_Agent::get_by_role( $role_id );
550 foreach ( $agents as $agent ) {
551 if ( ! $agent->is_active ) {
552 continue;
553 }
554 $bcc[] = $agent->customer->email;
555 }
556 }
557
558 // custom.
559 $custom = $et['bcc']['custom'] ? array_map( 'trim', $et['bcc']['custom'] ) : array();
560 foreach ( $custom as $email ) {
561 if ( ! filter_var( $email, FILTER_VALIDATE_EMAIL ) ) {
562 continue;
563 }
564 $bcc[] = $email;
565 }
566
567 if ( ! in_array( $et['event'], WPSC_EN_Settings_TN::$ignore_current_user ) && ! in_array( 'current-user', $general_recipients ) ) {
568 $bcc = array_diff( $bcc, array( $current_user->customer->email ) );
569 }
570
571 $bcc = apply_filters( 'wpsc_en_bcc_addresses', $bcc, $this );
572 $bcc = array_diff( $bcc, self::$block_emails );
573 $this->bcc = array_unique( $bcc );
574 }
575 }
576 endif;
577
578 WPSC_Email_Notifications::init();
579