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