| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
die( 'You are not allowed to call this page directly.' ); |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* @since 2.03.04 |
| 8 |
*/ |
| 9 |
class FrmEmail { |
| 10 |
|
| 11 |
/** |
| 12 |
* @var string |
| 13 |
*/ |
| 14 |
private $email_key = ''; |
| 15 |
|
| 16 |
/** |
| 17 |
* @var array |
| 18 |
*/ |
| 19 |
private $to = array(); |
| 20 |
|
| 21 |
/** |
| 22 |
* @var array |
| 23 |
*/ |
| 24 |
private $cc = array(); |
| 25 |
|
| 26 |
/** |
| 27 |
* @var array |
| 28 |
*/ |
| 29 |
private $bcc = array(); |
| 30 |
|
| 31 |
/** |
| 32 |
* @var string |
| 33 |
*/ |
| 34 |
private $from = ''; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var string |
| 38 |
*/ |
| 39 |
private $reply_to = ''; |
| 40 |
|
| 41 |
/** |
| 42 |
* @var string |
| 43 |
*/ |
| 44 |
private $subject = ''; |
| 45 |
|
| 46 |
/** |
| 47 |
* @var string |
| 48 |
*/ |
| 49 |
private $message = ''; |
| 50 |
|
| 51 |
/** |
| 52 |
* @var array |
| 53 |
*/ |
| 54 |
private $attachments = array(); |
| 55 |
|
| 56 |
/** |
| 57 |
* @var bool |
| 58 |
*/ |
| 59 |
private $is_plain_text = false; |
| 60 |
|
| 61 |
/** |
| 62 |
* @var bool |
| 63 |
*/ |
| 64 |
private $is_single_recipient = false; |
| 65 |
|
| 66 |
/** |
| 67 |
* @var bool |
| 68 |
*/ |
| 69 |
private $include_user_info = false; |
| 70 |
|
| 71 |
/** |
| 72 |
* @var string |
| 73 |
*/ |
| 74 |
private $charset = ''; |
| 75 |
|
| 76 |
/** |
| 77 |
* @var string |
| 78 |
*/ |
| 79 |
private $content_type = 'text/html'; |
| 80 |
|
| 81 |
/** |
| 82 |
* @var array |
| 83 |
*/ |
| 84 |
private $settings = array(); |
| 85 |
|
| 86 |
/** |
| 87 |
* @var stdClass |
| 88 |
*/ |
| 89 |
private $entry; |
| 90 |
|
| 91 |
/** |
| 92 |
* @var stdClass |
| 93 |
*/ |
| 94 |
private $form; |
| 95 |
|
| 96 |
/** |
| 97 |
* @var int |
| 98 |
*/ |
| 99 |
private $action_id = 0; |
| 100 |
|
| 101 |
/** |
| 102 |
* FrmEmail constructor |
| 103 |
* |
| 104 |
* @param object $action |
| 105 |
* @param object $entry |
| 106 |
* @param object $form |
| 107 |
*/ |
| 108 |
public function __construct( $action, $entry, $form ) { |
| 109 |
$this->set_email_key( $action ); |
| 110 |
$this->entry = $entry; |
| 111 |
$this->form = $form; |
| 112 |
$this->settings = $action->post_content; |
| 113 |
$this->action_id = (int) $action->ID; |
| 114 |
|
| 115 |
$user_id_args = self::get_user_id_args( $form->id ); |
| 116 |
$this->set_to( $user_id_args ); |
| 117 |
$this->set_cc( $user_id_args ); |
| 118 |
$this->set_bcc( $user_id_args ); |
| 119 |
|
| 120 |
if ( ! $this->has_recipients() ) { |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
$this->set_from( $user_id_args ); |
| 125 |
$this->set_reply_to( $user_id_args ); |
| 126 |
|
| 127 |
$this->set_include_user_info(); |
| 128 |
$this->set_is_plain_text(); |
| 129 |
$this->set_is_single_recipient( $action ); |
| 130 |
|
| 131 |
$this->set_charset(); |
| 132 |
$this->set_content_type(); |
| 133 |
|
| 134 |
$this->set_subject(); |
| 135 |
$this->set_message(); |
| 136 |
$this->set_attachments(); |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Set the email key property |
| 141 |
* |
| 142 |
* @since 2.03.04 |
| 143 |
* |
| 144 |
* @param object $action |
| 145 |
* |
| 146 |
* @return void |
| 147 |
*/ |
| 148 |
private function set_email_key( $action ) { |
| 149 |
$this->email_key = $action->ID; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set the to addresses |
| 154 |
* |
| 155 |
* @since 2.03.04 |
| 156 |
* |
| 157 |
* @param array $user_id_args |
| 158 |
* |
| 159 |
* @return void |
| 160 |
*/ |
| 161 |
private function set_to( $user_id_args ) { |
| 162 |
$to = $this->prepare_email_setting( $this->settings['email_to'], $user_id_args ); |
| 163 |
$to = $this->explode_emails( $to ); |
| 164 |
|
| 165 |
$where = array( |
| 166 |
'it.field_id !' => 0, |
| 167 |
'it.item_id' => $this->entry->id, |
| 168 |
); |
| 169 |
$values = FrmEntryMeta::getAll( $where, ' ORDER BY fi.field_order' ); |
| 170 |
$args = array( |
| 171 |
'email_key' => $this->email_key, |
| 172 |
'entry' => $this->entry, |
| 173 |
'form' => $this->form, |
| 174 |
); |
| 175 |
$to = apply_filters( 'frm_to_email', $to, $values, $this->form->id, $args ); |
| 176 |
|
| 177 |
$this->to = array_unique( (array) $to ); |
| 178 |
|
| 179 |
if ( ! $this->to ) { |
| 180 |
return; |
| 181 |
} |
| 182 |
|
| 183 |
$this->handle_phone_numbers(); |
| 184 |
|
| 185 |
$this->to = $this->format_recipients( $this->to ); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Set the CC addresses |
| 190 |
* |
| 191 |
* @since 2.03.04 |
| 192 |
* |
| 193 |
* @param array $user_id_args |
| 194 |
* |
| 195 |
* @return void |
| 196 |
*/ |
| 197 |
private function set_cc( $user_id_args ) { |
| 198 |
$this->cc = $this->prepare_additional_recipients( $this->settings['cc'], $user_id_args ); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Set the BCC addresses |
| 203 |
* |
| 204 |
* @since 2.03.04 |
| 205 |
* |
| 206 |
* @param array $user_id_args |
| 207 |
* |
| 208 |
* @return void |
| 209 |
*/ |
| 210 |
private function set_bcc( $user_id_args ) { |
| 211 |
$this->bcc = $this->prepare_additional_recipients( $this->settings['bcc'], $user_id_args ); |
| 212 |
} |
| 213 |
|
| 214 |
/** |
| 215 |
* Prepare CC and BCC recipients |
| 216 |
* |
| 217 |
* @since 2.03.04 |
| 218 |
* |
| 219 |
* @param string $recipients |
| 220 |
* @param array $user_id_args |
| 221 |
* |
| 222 |
* @return array |
| 223 |
*/ |
| 224 |
private function prepare_additional_recipients( $recipients, $user_id_args ) { |
| 225 |
$recipients = $this->prepare_email_setting( $recipients, $user_id_args ); |
| 226 |
$recipients = $this->explode_emails( $recipients ); |
| 227 |
$recipients = array_unique( (array) $recipients ); |
| 228 |
|
| 229 |
return $this->format_recipients( $recipients ); |
| 230 |
} |
| 231 |
|
| 232 |
/** |
| 233 |
* Set the From addresses |
| 234 |
* |
| 235 |
* @since 2.03.04 |
| 236 |
* |
| 237 |
* @param array $user_id_args |
| 238 |
* |
| 239 |
* @return void |
| 240 |
*/ |
| 241 |
private function set_from( $user_id_args ) { |
| 242 |
if ( empty( $this->settings['from'] ) ) { |
| 243 |
$from = FrmEmailHelper::get_default_from_email(); |
| 244 |
} else { |
| 245 |
$from = $this->prepare_email_setting( $this->settings['from'], $user_id_args ); |
| 246 |
} |
| 247 |
|
| 248 |
$this->from = $this->format_from( $from ); |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Set the Reply To addresses |
| 253 |
* |
| 254 |
* @since 2.03.04 |
| 255 |
* |
| 256 |
* @param array $user_id_args |
| 257 |
* |
| 258 |
* @return void |
| 259 |
*/ |
| 260 |
private function set_reply_to( $user_id_args ) { |
| 261 |
$this->reply_to = trim( $this->settings['reply_to'] ); |
| 262 |
|
| 263 |
if ( $this->reply_to ) { |
| 264 |
$this->reply_to = $this->prepare_email_setting( $this->settings['reply_to'], $user_id_args ); |
| 265 |
} |
| 266 |
|
| 267 |
if ( ! $this->reply_to ) { |
| 268 |
$this->reply_to = $this->get_email_from_name( $this->from ); |
| 269 |
} |
| 270 |
|
| 271 |
$this->reply_to = $this->format_reply_to( $this->reply_to ); |
| 272 |
} |
| 273 |
|
| 274 |
/** |
| 275 |
* Set the is_plain_text property |
| 276 |
* This should be set before the message |
| 277 |
* |
| 278 |
* @since 2.03.04 |
| 279 |
* |
| 280 |
* @return void |
| 281 |
*/ |
| 282 |
private function set_is_plain_text() { |
| 283 |
if ( empty( $this->settings['email_style'] ) ) { |
| 284 |
// If `email_style` isn't set, use the `plain_text` checkbox. |
| 285 |
$this->is_plain_text = ! empty( $this->settings['plain_text'] ); |
| 286 |
return; |
| 287 |
} |
| 288 |
|
| 289 |
$this->is_plain_text = 'plain' === $this->settings['email_style']; |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Set the include_user_info property |
| 294 |
* This should be set before the message |
| 295 |
* |
| 296 |
* @since 2.03.04 |
| 297 |
* |
| 298 |
* @return void |
| 299 |
*/ |
| 300 |
private function set_include_user_info() { |
| 301 |
if ( isset( $this->settings['inc_user_info'] ) ) { |
| 302 |
$this->include_user_info = $this->settings['inc_user_info']; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Set the is_single_recipient property |
| 308 |
* |
| 309 |
* @since 2.03.04 |
| 310 |
* |
| 311 |
* @param object $action |
| 312 |
* |
| 313 |
* @return void |
| 314 |
*/ |
| 315 |
private function set_is_single_recipient( $action ) { |
| 316 |
$args = array( |
| 317 |
'form' => $this->form, |
| 318 |
'entry' => $this->entry, |
| 319 |
'action' => $action, |
| 320 |
); |
| 321 |
|
| 322 |
/** |
| 323 |
* Send a separate email for email address in the "to" section |
| 324 |
* |
| 325 |
* @since 2.2.13 |
| 326 |
*/ |
| 327 |
$this->is_single_recipient = apply_filters( 'frm_send_separate_emails', false, $args ); |
| 328 |
} |
| 329 |
|
| 330 |
/** |
| 331 |
* Set the charset |
| 332 |
* |
| 333 |
* @since 2.03.04 |
| 334 |
* |
| 335 |
* @return void |
| 336 |
*/ |
| 337 |
private function set_charset() { |
| 338 |
$this->charset = get_option( 'blog_charset' ); |
| 339 |
} |
| 340 |
|
| 341 |
/** |
| 342 |
* Set the content type |
| 343 |
* |
| 344 |
* @since 2.03.04 |
| 345 |
* |
| 346 |
* @return void |
| 347 |
*/ |
| 348 |
private function set_content_type() { |
| 349 |
if ( $this->is_plain_text ) { |
| 350 |
$this->content_type = 'text/plain'; |
| 351 |
} |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Set the subject |
| 356 |
* |
| 357 |
* @since 2.03.04 |
| 358 |
* |
| 359 |
* @return void |
| 360 |
*/ |
| 361 |
private function set_subject() { |
| 362 |
if ( empty( $this->settings['email_subject'] ) ) { |
| 363 |
/* translators: %1$s: Form name, %2$s: Site name */ |
| 364 |
$this->subject = sprintf( __( '%1$s Form submitted on %2$s', 'formidable' ), $this->form->name, '[sitename]' ); |
| 365 |
} else { |
| 366 |
$this->subject = $this->settings['email_subject']; |
| 367 |
} |
| 368 |
|
| 369 |
// This also replaces [sitename] shortcode in default. |
| 370 |
$this->subject = FrmFieldsHelper::basic_replace_shortcodes( $this->subject, $this->form, $this->entry ); |
| 371 |
|
| 372 |
$args = array( |
| 373 |
'form' => $this->form, |
| 374 |
'entry' => $this->entry, |
| 375 |
'email_key' => $this->email_key, |
| 376 |
); |
| 377 |
$this->subject = apply_filters( 'frm_email_subject', $this->subject, $args ); |
| 378 |
$this->subject = wp_specialchars_decode( strip_tags( stripslashes( $this->subject ) ), ENT_QUOTES ); |
| 379 |
} |
| 380 |
|
| 381 |
/** |
| 382 |
* Set the email message |
| 383 |
* |
| 384 |
* @since 2.03.04 |
| 385 |
* |
| 386 |
* @return void |
| 387 |
*/ |
| 388 |
private function set_message() { |
| 389 |
$this->message = $this->settings['email_message']; |
| 390 |
|
| 391 |
if ( ! $this->is_plain_text ) { |
| 392 |
// The decode is to support [default-html] shortcodes. |
| 393 |
$this->message = html_entity_decode( $this->message ); |
| 394 |
} |
| 395 |
|
| 396 |
$this->message = FrmFieldsHelper::basic_replace_shortcodes( $this->message, $this->form, $this->entry ); |
| 397 |
$prev_mail_body = $this->message; |
| 398 |
|
| 399 |
// Make a copy to prevent changes by reference. |
| 400 |
$pass_entry = clone $this->entry; |
| 401 |
$mail_body = FrmEntriesHelper::replace_default_message( |
| 402 |
$prev_mail_body, |
| 403 |
array( |
| 404 |
'id' => $this->entry->id, |
| 405 |
'entry' => $pass_entry, |
| 406 |
'plain_text' => $this->is_plain_text, |
| 407 |
'user_info' => $this->include_user_info, |
| 408 |
'table_style' => $this->settings['email_style'], |
| 409 |
) |
| 410 |
); |
| 411 |
|
| 412 |
// Add the user info if it isn't already included |
| 413 |
if ( $this->include_user_info && $prev_mail_body === $mail_body ) { |
| 414 |
$data = $this->entry->description; |
| 415 |
$mail_body .= "\r\n\r\n" . __( 'User Information', 'formidable' ) . "\r\n"; |
| 416 |
$this->maybe_add_ip( $mail_body ); |
| 417 |
$mail_body .= __( 'User-Agent (Browser/OS)', 'formidable' ) . ': ' . FrmEntriesHelper::get_browser( $data['browser'] ) . "\r\n"; |
| 418 |
$mail_body .= __( 'Referrer', 'formidable' ) . ': ' . $data['referrer'] . "\r\n"; |
| 419 |
} |
| 420 |
|
| 421 |
$this->message = $mail_body; |
| 422 |
|
| 423 |
if ( $this->is_plain_text ) { |
| 424 |
$this->message = wp_specialchars_decode( strip_tags( $this->message ), ENT_QUOTES ); |
| 425 |
$this->message = str_replace( ' ', '', $this->message ); |
| 426 |
} else { |
| 427 |
$this->add_autop(); |
| 428 |
} |
| 429 |
|
| 430 |
$this->message = apply_filters( 'frm_email_message', $this->message, $this->package_atts() ); |
| 431 |
} |
| 432 |
|
| 433 |
/** |
| 434 |
* Runs message through autop, extracting the content inside body tag if it has <body>. |
| 435 |
* |
| 436 |
* @return void |
| 437 |
*/ |
| 438 |
private function add_autop() { |
| 439 |
$message = $this->message; |
| 440 |
preg_match( '/<body[^>]*>([\s\S]*?)<\/body>/', $message, $match ); |
| 441 |
|
| 442 |
if ( ! empty( $match[1] ) ) { |
| 443 |
$this->message = str_replace( $match[1], trim( wpautop( $match[1] ) ), $message ); |
| 444 |
} else { |
| 445 |
$this->message = trim( wpautop( $message ) ); |
| 446 |
} |
| 447 |
} |
| 448 |
|
| 449 |
/** |
| 450 |
* @param string $mail_body |
| 451 |
* |
| 452 |
* @return void |
| 453 |
*/ |
| 454 |
private function maybe_add_ip( &$mail_body ) { |
| 455 |
if ( ! empty( $this->entry->ip ) ) { |
| 456 |
$mail_body .= __( 'IP Address', 'formidable' ) . ': ' . $this->entry->ip . "\r\n"; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
/** |
| 461 |
* Set the attachments for an email message |
| 462 |
* |
| 463 |
* @since 2.03.04 |
| 464 |
* @since 5.0.16 added new action_id key to $args. |
| 465 |
* |
| 466 |
* @return void |
| 467 |
*/ |
| 468 |
private function set_attachments() { |
| 469 |
$args = array( |
| 470 |
'entry' => $this->entry, |
| 471 |
'email_key' => $this->email_key, |
| 472 |
'settings' => $this->settings, |
| 473 |
'action_id' => $this->action_id, |
| 474 |
); |
| 475 |
|
| 476 |
$this->attachments = apply_filters( 'frm_notification_attachment', array(), $this->form, $args ); |
| 477 |
} |
| 478 |
|
| 479 |
/** |
| 480 |
* Check if an email should send |
| 481 |
* |
| 482 |
* @since 2.03.04 |
| 483 |
* |
| 484 |
* @return bool|mixed|void |
| 485 |
*/ |
| 486 |
public function should_send() { |
| 487 |
if ( ! $this->has_recipients() ) { |
| 488 |
return false; |
| 489 |
} |
| 490 |
|
| 491 |
$filter_args = array( |
| 492 |
'message' => $this->message, |
| 493 |
'subject' => $this->subject, |
| 494 |
'recipient' => $this->to, |
| 495 |
'header' => $this->package_header(), |
| 496 |
); |
| 497 |
/** |
| 498 |
* Stop an email based on the message, subject, recipient, |
| 499 |
* or any information included in the email header |
| 500 |
* |
| 501 |
* @since 2.2.8 |
| 502 |
*/ |
| 503 |
return apply_filters( 'frm_send_email', true, $filter_args ); |
| 504 |
} |
| 505 |
|
| 506 |
/** |
| 507 |
* Check if an email has any recipients |
| 508 |
* |
| 509 |
* @since 2.03.04 |
| 510 |
* |
| 511 |
* @return bool |
| 512 |
*/ |
| 513 |
private function has_recipients() { |
| 514 |
return $this->to || $this->cc || $this->bcc; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Send an email |
| 519 |
* |
| 520 |
* @since 2.03.04 |
| 521 |
* |
| 522 |
* @return bool |
| 523 |
*/ |
| 524 |
public function send() { |
| 525 |
$this->remove_buddypress_filters(); |
| 526 |
$this->add_mandrill_filter(); |
| 527 |
|
| 528 |
$sent = false; |
| 529 |
|
| 530 |
if ( count( $this->to ) > 1 && $this->is_single_recipient ) { |
| 531 |
foreach ( $this->to as $recipient ) { |
| 532 |
$sent = $this->send_single( $recipient ); |
| 533 |
} |
| 534 |
} else { |
| 535 |
$sent = $this->send_single( $this->to ); |
| 536 |
} |
| 537 |
|
| 538 |
$this->remove_mandrill_filter(); |
| 539 |
|
| 540 |
return $sent; |
| 541 |
} |
| 542 |
|
| 543 |
/** |
| 544 |
* Send a single email |
| 545 |
* |
| 546 |
* @since 2.03.04 |
| 547 |
* |
| 548 |
* @param array|string $recipient |
| 549 |
* |
| 550 |
* @return bool |
| 551 |
*/ |
| 552 |
private function send_single( $recipient ) { |
| 553 |
$header = apply_filters( |
| 554 |
'frm_email_header', |
| 555 |
$this->package_header(), |
| 556 |
array( |
| 557 |
'to_email' => $recipient, |
| 558 |
'subject' => $this->subject, |
| 559 |
) |
| 560 |
); |
| 561 |
|
| 562 |
$subject = $this->encode_subject( $this->subject ); |
| 563 |
$sent = wp_mail( $recipient, $subject, $this->message, $header, $this->attachments ); |
| 564 |
|
| 565 |
if ( ! $sent ) { |
| 566 |
if ( is_array( $header ) ) { |
| 567 |
$header = implode( "\r\n", $header ); |
| 568 |
} |
| 569 |
|
| 570 |
$recipient = implode( ',', (array) $recipient ); |
| 571 |
$sent = mail( $recipient, $subject, $this->message, $header ); |
| 572 |
} |
| 573 |
|
| 574 |
do_action( 'frm_notification', $recipient, $subject, $this->message ); |
| 575 |
|
| 576 |
return $sent; |
| 577 |
} |
| 578 |
|
| 579 |
/** |
| 580 |
* Package the email header |
| 581 |
* |
| 582 |
* @since 2.03.04 |
| 583 |
* |
| 584 |
* @return array |
| 585 |
*/ |
| 586 |
private function package_header() { |
| 587 |
$header = array(); |
| 588 |
|
| 589 |
if ( $this->cc ) { |
| 590 |
$header[] = 'CC: ' . implode( ',', $this->cc ); |
| 591 |
} |
| 592 |
|
| 593 |
if ( $this->bcc ) { |
| 594 |
$header[] = 'BCC: ' . implode( ',', $this->bcc ); |
| 595 |
} |
| 596 |
|
| 597 |
$header[] = 'From: ' . $this->from; |
| 598 |
$header[] = 'Reply-To: ' . $this->reply_to; |
| 599 |
$header[] = 'Content-Type: ' . $this->content_type . '; charset="' . esc_attr( $this->charset ) . '"'; |
| 600 |
|
| 601 |
return $header; |
| 602 |
} |
| 603 |
|
| 604 |
/** |
| 605 |
* Get the userID field ID and key for email settings |
| 606 |
* |
| 607 |
* @since 2.03.04 |
| 608 |
* |
| 609 |
* @param int $form_id |
| 610 |
* |
| 611 |
* @return array |
| 612 |
*/ |
| 613 |
private function get_user_id_args( $form_id ) { |
| 614 |
$user_id_args = array( |
| 615 |
'field_id' => '', |
| 616 |
'field_key' => '', |
| 617 |
); |
| 618 |
|
| 619 |
$user_id_args['field_id'] = FrmEmailHelper::get_user_id_field_for_form( $form_id ); |
| 620 |
|
| 621 |
if ( $user_id_args['field_id'] ) { |
| 622 |
$user_id_args['field_key'] = FrmField::get_key_by_id( $user_id_args['field_id'] ); |
| 623 |
} |
| 624 |
|
| 625 |
return $user_id_args; |
| 626 |
} |
| 627 |
|
| 628 |
/** |
| 629 |
* Prepare the to, cc, bcc, reply_to, and from setting |
| 630 |
* |
| 631 |
* @since 2.03.04 |
| 632 |
* |
| 633 |
* @param string $value |
| 634 |
* @param array $user_id_args |
| 635 |
* |
| 636 |
* @return string |
| 637 |
*/ |
| 638 |
private function prepare_email_setting( $value, $user_id_args ) { |
| 639 |
if ( str_contains( $value, '[' . $user_id_args['field_id'] . ']' ) ) { |
| 640 |
$value = str_replace( '[' . $user_id_args['field_id'] . ']', '[' . $user_id_args['field_id'] . ' show="user_email"]', $value ); |
| 641 |
} elseif ( str_contains( $value, '[' . $user_id_args['field_key'] . ']' ) ) { |
| 642 |
$value = str_replace( '[' . $user_id_args['field_key'] . ']', '[' . $user_id_args['field_key'] . ' show="user_email"]', $value ); |
| 643 |
} |
| 644 |
|
| 645 |
$value = FrmFieldsHelper::basic_replace_shortcodes( $value, $this->form, $this->entry ); |
| 646 |
|
| 647 |
// Remove brackets and add a space in case there isn't one |
| 648 |
$value = str_replace( '<', ' ', $value ); |
| 649 |
|
| 650 |
return str_replace( array( '"', '>' ), '', $value ); |
| 651 |
} |
| 652 |
|
| 653 |
/** |
| 654 |
* Extract the emails from cc and bcc. Allow separation by , or ;. |
| 655 |
* Trim the emails here as well |
| 656 |
* |
| 657 |
* @since 2.03.04 |
| 658 |
* |
| 659 |
* @param string $emails |
| 660 |
* |
| 661 |
* @return array|string Emails. |
| 662 |
*/ |
| 663 |
private function explode_emails( $emails ) { |
| 664 |
$emails = $emails ? preg_split( '/(,|;)/', $emails ) : ''; |
| 665 |
return is_array( $emails ) ? array_map( 'trim', $emails ) : trim( $emails ); |
| 666 |
} |
| 667 |
|
| 668 |
/** |
| 669 |
* Format the recipients( to, cc, bcc) |
| 670 |
* |
| 671 |
* @param array $recipients |
| 672 |
* |
| 673 |
* @return array |
| 674 |
*/ |
| 675 |
private function format_recipients( $recipients ) { |
| 676 |
if ( ! $recipients ) { |
| 677 |
return $recipients; |
| 678 |
} |
| 679 |
|
| 680 |
foreach ( $recipients as $key => $val ) { |
| 681 |
$val = trim( $val ); |
| 682 |
|
| 683 |
if ( is_email( $val ) ) { |
| 684 |
// If a plain email is used, no formatting is needed |
| 685 |
continue; |
| 686 |
} |
| 687 |
|
| 688 |
$parts = explode( ' ', $val ); |
| 689 |
$email = end( $parts ); |
| 690 |
|
| 691 |
if ( ! is_email( $email ) ) { |
| 692 |
// If user enters a name without an email |
| 693 |
unset( $recipients[ $key ] ); |
| 694 |
continue; |
| 695 |
} |
| 696 |
|
| 697 |
// If user enters a name and email |
| 698 |
$name = trim( str_replace( $email, '', $val ) ); |
| 699 |
|
| 700 |
$recipients[ $key ] = $this->format_from_email( $name, $email ); |
| 701 |
}//end foreach |
| 702 |
|
| 703 |
return $recipients; |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Format the From header |
| 708 |
* |
| 709 |
* @param string $from |
| 710 |
* |
| 711 |
* @return string |
| 712 |
*/ |
| 713 |
private function format_from( $from ) { |
| 714 |
$from = trim( $from ); |
| 715 |
|
| 716 |
if ( is_email( $from ) ) { |
| 717 |
// If a plain email is used, add the site name so "WordPress" doesn't get added |
| 718 |
$from_name = wp_specialchars_decode( FrmAppHelper::site_name(), ENT_QUOTES ); |
| 719 |
$from_email = $from; |
| 720 |
} else { |
| 721 |
list( $from_name, $from_email ) = $this->get_name_and_email_for_sender( $from ); |
| 722 |
} |
| 723 |
|
| 724 |
// If sending the email from a yahoo address, change it to the WordPress default |
| 725 |
if ( str_contains( $from_email, '@yahoo.com' ) ) { |
| 726 |
// Get the site domain and get rid of www. |
| 727 |
$sitename = strtolower( FrmAppHelper::get_server_value( 'SERVER_NAME' ) ); |
| 728 |
|
| 729 |
if ( str_starts_with( $sitename, 'www.' ) ) { |
| 730 |
$sitename = substr( $sitename, 4 ); |
| 731 |
} |
| 732 |
|
| 733 |
$from_email = 'wordpress@' . $sitename; |
| 734 |
} |
| 735 |
|
| 736 |
return $this->format_from_email( $from_name, $from_email ); |
| 737 |
} |
| 738 |
|
| 739 |
/** |
| 740 |
* Format the Reply To property |
| 741 |
* |
| 742 |
* @since 2.03.04 |
| 743 |
* |
| 744 |
* @param string $reply_to |
| 745 |
* |
| 746 |
* @return string |
| 747 |
*/ |
| 748 |
private function format_reply_to( $reply_to ) { |
| 749 |
$reply_to = trim( $reply_to ); |
| 750 |
|
| 751 |
if ( ! is_email( $reply_to ) ) { |
| 752 |
list( $name, $email ) = $this->get_name_and_email_for_sender( $reply_to ); |
| 753 |
$reply_to = $this->format_from_email( $name, $email ); |
| 754 |
} |
| 755 |
|
| 756 |
return $reply_to; |
| 757 |
} |
| 758 |
|
| 759 |
/** |
| 760 |
* Get only the email if the name and email have been combined |
| 761 |
* |
| 762 |
* @since 3.0.06 |
| 763 |
* |
| 764 |
* @param string $name |
| 765 |
* |
| 766 |
* @return string |
| 767 |
*/ |
| 768 |
private function get_email_from_name( $name ) { |
| 769 |
$email = trim( trim( $name, '>' ), '<' ); |
| 770 |
|
| 771 |
if ( str_contains( $email, '<' ) ) { |
| 772 |
$parts = explode( '<', $email ); |
| 773 |
$email = trim( $parts[1], '>' ); |
| 774 |
} |
| 775 |
|
| 776 |
return $email; |
| 777 |
} |
| 778 |
|
| 779 |
/** |
| 780 |
* Get the name and email for the From or Reply To header |
| 781 |
* |
| 782 |
* @since 2.03.04 |
| 783 |
* |
| 784 |
* @param string $sender |
| 785 |
* |
| 786 |
* @return array |
| 787 |
*/ |
| 788 |
private function get_name_and_email_for_sender( $sender ) { |
| 789 |
$parts = explode( ' ', $sender ); |
| 790 |
$end = end( $parts ); |
| 791 |
|
| 792 |
if ( is_email( $end ) ) { |
| 793 |
$name = trim( str_replace( $end, '', $sender ) ); |
| 794 |
} else { |
| 795 |
// Only a name was entered in the From or Reply To field |
| 796 |
$name = $sender; |
| 797 |
$end = get_option( 'admin_email' ); |
| 798 |
} |
| 799 |
|
| 800 |
return array( $name, $end ); |
| 801 |
} |
| 802 |
|
| 803 |
/** |
| 804 |
* @since 3.0.06 |
| 805 |
* |
| 806 |
* @param string $name |
| 807 |
* @param string $email |
| 808 |
* |
| 809 |
* @return string |
| 810 |
*/ |
| 811 |
private function format_from_email( $name, $email ) { |
| 812 |
return '' !== $name ? $name . ' <' . $email . '>' : $email; |
| 813 |
} |
| 814 |
|
| 815 |
/** |
| 816 |
* Remove phone numbers from To addresses |
| 817 |
* Send the phone numbers to the frm_send_to_not_email hook |
| 818 |
* |
| 819 |
* @since 2.03.04 |
| 820 |
* |
| 821 |
* @return void |
| 822 |
*/ |
| 823 |
private function handle_phone_numbers() { |
| 824 |
foreach ( $this->to as $key => $recipient ) { |
| 825 |
if ( '[admin_email]' === $recipient || is_email( $recipient ) ) { |
| 826 |
continue; |
| 827 |
} |
| 828 |
|
| 829 |
$recipient = explode( ' ', $recipient ); |
| 830 |
|
| 831 |
if ( is_email( end( $recipient ) ) ) { |
| 832 |
continue; |
| 833 |
} |
| 834 |
|
| 835 |
do_action( |
| 836 |
'frm_send_to_not_email', |
| 837 |
array( |
| 838 |
'e' => $recipient, |
| 839 |
'subject' => $this->subject, |
| 840 |
'mail_body' => $this->message, |
| 841 |
'reply_to' => $this->reply_to, |
| 842 |
'from' => $this->from, |
| 843 |
'plain_text' => $this->is_plain_text, |
| 844 |
'attachments' => $this->attachments, |
| 845 |
'form' => $this->form, |
| 846 |
'email_key' => $key, |
| 847 |
) |
| 848 |
); |
| 849 |
|
| 850 |
// Remove phone number from to addresses |
| 851 |
unset( $this->to[ $key ] ); |
| 852 |
// end if |
| 853 |
}//end foreach |
| 854 |
} |
| 855 |
|
| 856 |
/** |
| 857 |
* Package an array of FrmEmail properties |
| 858 |
* |
| 859 |
* @since 2.03.04 |
| 860 |
* |
| 861 |
* @return array |
| 862 |
*/ |
| 863 |
public function package_atts() { |
| 864 |
return array( |
| 865 |
'to_email' => $this->to, |
| 866 |
'cc' => $this->cc, |
| 867 |
'bcc' => $this->bcc, |
| 868 |
'from' => $this->from, |
| 869 |
'reply_to' => $this->reply_to, |
| 870 |
'subject' => $this->subject, |
| 871 |
'message' => $this->message, |
| 872 |
'attachments' => $this->attachments, |
| 873 |
'plain_text' => $this->is_plain_text, |
| 874 |
'email_key' => $this->email_key, |
| 875 |
'form' => $this->form, |
| 876 |
'entry' => $this->entry, |
| 877 |
'email_style' => $this->settings['email_style'], |
| 878 |
); |
| 879 |
} |
| 880 |
|
| 881 |
/** |
| 882 |
* Remove the Buddypress email filters |
| 883 |
* |
| 884 |
* @since 2.03.04 |
| 885 |
* |
| 886 |
* @return void |
| 887 |
*/ |
| 888 |
private function remove_buddypress_filters() { |
| 889 |
remove_filter( 'wp_mail_from', 'bp_core_email_from_address_filter' ); |
| 890 |
remove_filter( 'wp_mail_from_name', 'bp_core_email_from_name_filter' ); |
| 891 |
} |
| 892 |
|
| 893 |
/** |
| 894 |
* Add Mandrill line break filter |
| 895 |
* Remove line breaks in HTML emails to prevent conflicts with Mandrill |
| 896 |
* |
| 897 |
* @since 2.03.04 |
| 898 |
* |
| 899 |
* @return void |
| 900 |
*/ |
| 901 |
private function add_mandrill_filter() { |
| 902 |
if ( ! $this->is_plain_text ) { |
| 903 |
add_filter( 'mandrill_nl2br', 'FrmEmailHelper::remove_mandrill_br' ); |
| 904 |
} |
| 905 |
} |
| 906 |
|
| 907 |
/** |
| 908 |
* Remove Mandrill line break filter |
| 909 |
* |
| 910 |
* @since 2.03.04 |
| 911 |
* |
| 912 |
* @return void |
| 913 |
*/ |
| 914 |
private function remove_mandrill_filter() { |
| 915 |
remove_filter( 'mandrill_nl2br', 'FrmEmailHelper::remove_mandrill_br' ); |
| 916 |
} |
| 917 |
|
| 918 |
/** |
| 919 |
* Encode the email subject |
| 920 |
* |
| 921 |
* @param string $subject |
| 922 |
* |
| 923 |
* @return string |
| 924 |
*/ |
| 925 |
private function encode_subject( $subject ) { |
| 926 |
if ( apply_filters( 'frm_encode_subject', false, $subject ) ) { |
| 927 |
return '=?' . $this->charset . '?B?' . base64_encode( $subject ) . '?='; |
| 928 |
} |
| 929 |
|
| 930 |
return $subject; |
| 931 |
} |
| 932 |
} |
| 933 |
|