EmailNotification.php
220 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Notifications\Transporter; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Notifications\Interfaces\NotificationsInterface; |
| 7 | |
| 8 | use function WPStaging\functions\debug_log; |
| 9 | |
| 10 | class EmailNotification implements NotificationsInterface |
| 11 | { |
| 12 | /** |
| 13 | * @var string |
| 14 | */ |
| 15 | private $sender = ''; |
| 16 | |
| 17 | /** |
| 18 | * @var string |
| 19 | */ |
| 20 | private $recipient = ''; |
| 21 | |
| 22 | /** |
| 23 | * @var string |
| 24 | */ |
| 25 | private $subject = ''; |
| 26 | |
| 27 | /** |
| 28 | * @var array |
| 29 | */ |
| 30 | private $attachments = []; |
| 31 | |
| 32 | /** |
| 33 | * @var array |
| 34 | */ |
| 35 | private $headers = []; |
| 36 | |
| 37 | /** |
| 38 | * @var bool |
| 39 | */ |
| 40 | private $isUseHtml = false; |
| 41 | |
| 42 | /** |
| 43 | * @var bool |
| 44 | */ |
| 45 | private $isAddFooterMessage = true; |
| 46 | |
| 47 | /** |
| 48 | * @param string $subject |
| 49 | * @return self |
| 50 | */ |
| 51 | public function setSubject(string $subject) |
| 52 | { |
| 53 | $this->subject = $subject; |
| 54 | return $this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * @param string $sender |
| 59 | * @return self |
| 60 | */ |
| 61 | public function setSender(string $sender) |
| 62 | { |
| 63 | if (empty($sender)) { |
| 64 | $this->sender = $sender; |
| 65 | return $this; |
| 66 | } |
| 67 | |
| 68 | if (preg_match('/(.*)<(.+)>/', $sender, $matches)) { |
| 69 | $this->sender = $sender; |
| 70 | return $this; |
| 71 | } |
| 72 | |
| 73 | $senderName = strtok($sender, '@'); |
| 74 | $this->sender = $senderName . ' <' . $sender . '>'; |
| 75 | |
| 76 | return $this; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param string $recipient |
| 81 | * @return self |
| 82 | */ |
| 83 | public function setRecipient(string $recipient) |
| 84 | { |
| 85 | $this->recipient = $recipient; |
| 86 | return $this; |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * @param array $attachments |
| 91 | * @return self |
| 92 | */ |
| 93 | public function setAttachment(array $attachments) |
| 94 | { |
| 95 | $this->attachments = $attachments; |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * @param array $headers |
| 101 | * @return self |
| 102 | */ |
| 103 | public function setHeaders(array $headers) |
| 104 | { |
| 105 | $this->headers = $headers; |
| 106 | return $this; |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * @param bool $isUseHtml |
| 111 | * @return self |
| 112 | */ |
| 113 | public function setUseHtml(bool $isUseHtml = false) |
| 114 | { |
| 115 | $this->isUseHtml = $isUseHtml; |
| 116 | return $this; |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * @param bool $isAddFooterMessage |
| 121 | * @return self |
| 122 | */ |
| 123 | public function setIsAddFooterMessage(bool $isAddFooterMessage = false) |
| 124 | { |
| 125 | $this->isAddFooterMessage = $isAddFooterMessage; |
| 126 | return $this; |
| 127 | } |
| 128 | |
| 129 | /** |
| 130 | * @return void |
| 131 | */ |
| 132 | public function reset() |
| 133 | { |
| 134 | $this->sender = ''; |
| 135 | $this->recipient = ''; |
| 136 | $this->attachments = []; |
| 137 | $this->headers = []; |
| 138 | $this->isUseHtml = false; |
| 139 | $this->isAddFooterMessage = true; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * @param string $message |
| 144 | * @return string |
| 145 | */ |
| 146 | private function addFooterMessage(string $message): string |
| 147 | { |
| 148 | if (empty($message) || !$this->isAddFooterMessage) { |
| 149 | return $message; |
| 150 | } |
| 151 | |
| 152 | $siteUrl = get_site_url(); |
| 153 | |
| 154 | $message .= "\r\n\r\n" . "--"; |
| 155 | if (WPStaging::isPro()) { |
| 156 | $message .= "\r\n" . sprintf(esc_html__('This message was sent by the WP Staging plugin from the website %s', 'wp-staging'), $siteUrl); |
| 157 | } else { |
| 158 | $message .= "\r\n" . sprintf(esc_html__('This message was sent by the WP Staging free backup and staging plugin from the website %s', 'wp-staging'), $siteUrl); |
| 159 | } |
| 160 | |
| 161 | $message .= "\r\n" . sprintf(esc_html__('It was sent to the email address %s which can be set up on %s', 'wp-staging'), $this->recipient, $siteUrl . '/wp-admin/admin.php?page=wpstg-settings'); |
| 162 | |
| 163 | if (!WPStaging::isPro()) { |
| 164 | $message .= "\r\n\r\n" . sprintf(esc_html__('Get more control over your notifications by using WP Staging Pro %s.', 'wp-staging'), 'https://wp-staging.com/'); |
| 165 | } |
| 166 | |
| 167 | $message .= "\r\n\r\n" . esc_html__('Please do not reply to this email.', 'wp-staging'); |
| 168 | return $message; |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * @param string $message |
| 173 | * @return bool |
| 174 | */ |
| 175 | public function send(string $message): bool |
| 176 | { |
| 177 | if (empty($message)) { |
| 178 | return false; |
| 179 | } |
| 180 | |
| 181 | $headers = []; |
| 182 | |
| 183 | if (!empty($this->sender)) { |
| 184 | $headers[] = 'From: ' . $this->sender; |
| 185 | $headers[] = 'Reply-To: ' . $this->sender; |
| 186 | } |
| 187 | |
| 188 | if ($this->isUseHtml) { |
| 189 | $headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 190 | } |
| 191 | |
| 192 | if (!empty($this->headers)) { |
| 193 | $headers = array_merge($headers, $this->headers); |
| 194 | } |
| 195 | |
| 196 | add_action('wp_mail_failed', function ($wpError) { |
| 197 | debug_log(sprintf('[EmailNotification] %s', $wpError->get_error_message()), 'info', false); |
| 198 | }); |
| 199 | |
| 200 | $message = $this->addFooterMessage($message); |
| 201 | if (!$this->isUseHtml) { |
| 202 | $message = $this->cleanHtmlEntitiesAndTags($message); |
| 203 | } |
| 204 | |
| 205 | return wp_mail($this->recipient, $this->subject, $message, $headers, $this->attachments); |
| 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Clean HTML entities and tags from message while preserving line breaks |
| 210 | * @param string $message |
| 211 | * @return string |
| 212 | */ |
| 213 | private function cleanHtmlEntitiesAndTags(string $message): string |
| 214 | { |
| 215 | $message = html_entity_decode($message, ENT_QUOTES | ENT_HTML5, 'UTF-8'); |
| 216 | $message = wp_kses($message, []); |
| 217 | return str_replace(['>', '&'], ['>', '&'], $message); |
| 218 | } |
| 219 | } |
| 220 |