PluginProbe
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent / 3.5.4
SupportCandy – AI Customer Support Ticket System & Live Chatbot Agent v3.5.4
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.4, at includes/class-wpsc-email-notifications.php

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