| 1 |
<?php |
| 2 |
namespace Ens\Email; |
| 3 |
|
| 4 |
use Ens\Utils\Helpers; |
| 5 |
|
| 6 |
/** |
| 7 |
* Class EmailSender |
| 8 |
* |
| 9 |
* @package Ens\Email |
| 10 |
* |
| 11 |
* @since 1.0.0 |
| 12 |
*/ |
| 13 |
class EmailSender { |
| 14 |
|
| 15 |
protected $to; |
| 16 |
protected $subject; |
| 17 |
protected $message; |
| 18 |
protected $action_data; |
| 19 |
protected $from; |
| 20 |
protected $identifier; |
| 21 |
|
| 22 |
/** |
| 23 |
* EmailSender constructor. |
| 24 |
* |
| 25 |
* Expected $args keys: |
| 26 |
* - action_name string |
| 27 |
* - receiver_type string |
| 28 |
* - to string |
| 29 |
* - from string |
| 30 |
* - subject string |
| 31 |
* - message string |
| 32 |
* - action_data array |
| 33 |
* - count int |
| 34 |
* |
| 35 |
* @since 1.0.0 |
| 36 |
* |
| 37 |
* @param array $args |
| 38 |
*/ |
| 39 |
public function __construct( $identifier, $action_name, $receiverType, $to, $from, $subject, $message, $action_data, $count ) { |
| 40 |
$this->identifier = $identifier; |
| 41 |
$this->to = $to; |
| 42 |
$this->from = $from; |
| 43 |
$this->subject = $this->replace_placeholders( $subject, $action_data ); |
| 44 |
$message_content = $this->replace_placeholders( $message, $action_data ); |
| 45 |
$this->message = apply_filters( Helpers::get_hook_name( $this->identifier, 'notification_sdk_email_message' ), $message_content, $receiverType, $action_name, $action_data, $count ); |
| 46 |
$this->action_data = $action_data; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Send an email. |
| 51 |
* |
| 52 |
* @since 1.0.0 |
| 53 |
* |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public function send() { |
| 57 |
// Temporarily set content type to HTML |
| 58 |
add_filter( 'wp_mail_content_type', function () { |
| 59 |
return 'text/html'; |
| 60 |
} ); |
| 61 |
|
| 62 |
$headers = $this->get_headers(); |
| 63 |
|
| 64 |
$this->message = apply_filters( Helpers::get_hook_name( $this->identifier, 'notification_sdk_email_header' ), $this->message ); |
| 65 |
$this->message = apply_filters( Helpers::get_hook_name( $this->identifier, 'notification_sdk_email_body' ), $this->message ); |
| 66 |
$this->message = apply_filters( Helpers::get_hook_name( $this->identifier, 'notification_sdk_email_footer' ), $this->message ); |
| 67 |
|
| 68 |
wp_mail( $this->to, $this->subject, $this->message, $headers ); |
| 69 |
|
| 70 |
// Remove the filter to avoid affecting other emails |
| 71 |
remove_filter( 'wp_mail_content_type', 'set_html_content_type' ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Get the headers for the email. |
| 76 |
* |
| 77 |
* @since 1.0.0 |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
protected function get_headers() { |
| 82 |
$mime_version = "MIME-Version: 1.0" . "\r\n"; |
| 83 |
$content_type = "Content-type:text/html;charset=UTF-8" . "\r\n"; |
| 84 |
|
| 85 |
$headers = [ |
| 86 |
$mime_version, |
| 87 |
$content_type, |
| 88 |
]; |
| 89 |
|
| 90 |
if ( ! empty( $this->from ) ) { |
| 91 |
$from = $this->from; |
| 92 |
$headers[] = "From: {$from}"; |
| 93 |
} |
| 94 |
|
| 95 |
return apply_filters( Helpers::get_hook_name( $this->identifier, 'eventin_email_headers' ), $headers ); |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Replace placeholders in a template. |
| 100 |
* |
| 101 |
* @since 1.0.0 |
| 102 |
* |
| 103 |
* @param string $template |
| 104 |
* @param array $data |
| 105 |
* |
| 106 |
* @return string |
| 107 |
*/ |
| 108 |
public function replace_placeholders( $template, $data ) { |
| 109 |
if ( ! is_array( $data ) ) { |
| 110 |
return $template; |
| 111 |
} |
| 112 |
foreach ( $data as $key => $value ) { |
| 113 |
if ( !is_array( $value ) ) { |
| 114 |
$template = str_replace( '{{' . $key . '}}', $value, $template ); |
| 115 |
$template = str_replace( '{%' . $key . '%}', $value, $template ); |
| 116 |
} |
| 117 |
} |
| 118 |
return $template; |
| 119 |
} |
| 120 |
} |
| 121 |
|