EmailTemplateBuilder.php
203 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WPStaging\Notifications\Transporter; |
| 4 | |
| 5 | use WPStaging\Core\WPStaging; |
| 6 | use WPStaging\Framework\Component\AbstractTemplateComponent; |
| 7 | use WPStaging\Framework\TemplateEngine\TemplateEngine; |
| 8 | |
| 9 | class EmailTemplateBuilder extends AbstractTemplateComponent |
| 10 | { |
| 11 | /** |
| 12 | * @var string |
| 13 | */ |
| 14 | private $title; |
| 15 | |
| 16 | /** |
| 17 | * @var string |
| 18 | */ |
| 19 | private $message; |
| 20 | |
| 21 | /** |
| 22 | * @var array |
| 23 | */ |
| 24 | private $details = []; |
| 25 | |
| 26 | /** |
| 27 | * @var bool |
| 28 | */ |
| 29 | private $isBasic = false; |
| 30 | |
| 31 | /** |
| 32 | * @var string |
| 33 | */ |
| 34 | private $recipient = ''; |
| 35 | |
| 36 | /** |
| 37 | * @param TemplateEngine $templateEngine |
| 38 | */ |
| 39 | public function __construct(TemplateEngine $templateEngine) |
| 40 | { |
| 41 | parent::__construct($templateEngine); |
| 42 | $this->isBasic = WPStaging::isBasic(); |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Create a new email template builder |
| 47 | * @param TemplateEngine $templateEngine |
| 48 | * @return self |
| 49 | */ |
| 50 | public static function create(TemplateEngine $templateEngine) |
| 51 | { |
| 52 | return new self($templateEngine); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Set the email title |
| 57 | * @param string $title |
| 58 | * @return self |
| 59 | */ |
| 60 | public function setTitle(string $title) |
| 61 | { |
| 62 | $this->title = $title; |
| 63 | return $this; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set the main message |
| 68 | * @param string $message |
| 69 | * @return self |
| 70 | */ |
| 71 | public function setMessage(string $message) |
| 72 | { |
| 73 | $this->message = $message; |
| 74 | return $this; |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * Add details to the email template |
| 79 | * @param array $details |
| 80 | * @return self |
| 81 | */ |
| 82 | public function setDetails(array $details) |
| 83 | { |
| 84 | $this->details = $details; |
| 85 | return $this; |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | * Set the recipient email address |
| 90 | * @param string $recipient |
| 91 | * @return self |
| 92 | */ |
| 93 | public function setRecipient(string $recipient = '') |
| 94 | { |
| 95 | $this->recipient = $recipient; |
| 96 | return $this; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Get the template path |
| 101 | * @return string |
| 102 | */ |
| 103 | protected function getTemplate(): string |
| 104 | { |
| 105 | return 'notifications/email-template.php'; |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Prepare data for template rendering |
| 110 | * @return array |
| 111 | */ |
| 112 | protected function getRenderData(): array |
| 113 | { |
| 114 | return [ |
| 115 | 'encodedSvg' => $this->getEncodedLogo(), |
| 116 | 'htmlMessage' => $this->processMessage(), |
| 117 | 'details' => $this->details, |
| 118 | 'isBasic' => $this->isBasic, |
| 119 | 'recipient' => $this->recipient, |
| 120 | 'year' => date('Y'), |
| 121 | 'siteUrl' => get_site_url(), |
| 122 | 'pluginName' => $this->isBasic ? 'WP Staging free backup and staging plugin' : 'WP Staging plugin', |
| 123 | ]; |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Process the message with JSON beautification and URL conversion |
| 128 | * @return string |
| 129 | */ |
| 130 | private function processMessage(): string |
| 131 | { |
| 132 | $message = $this->beautifyJsonInMessage(); |
| 133 | $message = nl2br($message); |
| 134 | return $this->convertUrlsToLinks($message); |
| 135 | } |
| 136 | |
| 137 | /** |
| 138 | * Get encoded logo SVG |
| 139 | * @return string |
| 140 | */ |
| 141 | private function getEncodedLogo(): string |
| 142 | { |
| 143 | $logoUrl = WPSTG_PLUGIN_DIR . 'assets/svg/notification-logo.svg'; |
| 144 | if (file_exists($logoUrl)) { |
| 145 | return 'data:image/svg+xml,' . rawurlencode(file_get_contents($logoUrl)); |
| 146 | } |
| 147 | |
| 148 | return ''; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Convert URLs in text to clickable links |
| 153 | * @param string $text Text containing URLs |
| 154 | * @return string Text with clickable links |
| 155 | */ |
| 156 | private function convertUrlsToLinks(string $text): string |
| 157 | { |
| 158 | return preg_replace_callback('/\b(https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/\/=]*))/i', function ($matches) { |
| 159 | $url = rtrim($matches[1], '.,;:!?'); |
| 160 | $sanitizedUrl = filter_var($url, FILTER_SANITIZE_URL); |
| 161 | if (filter_var($sanitizedUrl, FILTER_VALIDATE_URL)) { |
| 162 | return sprintf( |
| 163 | '<a href="%s" target="_blank" rel="noopener noreferrer">%s</a>', |
| 164 | htmlspecialchars($sanitizedUrl), |
| 165 | htmlspecialchars($url) |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | return $url; |
| 170 | }, $text); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Generate the HTML email template |
| 175 | * @return string |
| 176 | */ |
| 177 | public function generate(): string |
| 178 | { |
| 179 | return $this->templateEngine->render( |
| 180 | $this->getTemplate(), |
| 181 | $this->getRenderData() |
| 182 | ); |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Beautifies JSON blocks inside the message. |
| 187 | * @return string |
| 188 | */ |
| 189 | private function beautifyJsonInMessage(): string |
| 190 | { |
| 191 | return preg_replace_callback('/\{(?:[^{}]*|(?R))*\}/s', function ($matches) { |
| 192 | $jsonString = $matches[0]; |
| 193 | $decodedJson = json_decode($jsonString, true); |
| 194 | if (json_last_error() === JSON_ERROR_NONE) { |
| 195 | $prettyJson = json_encode($decodedJson, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 196 | return '<pre style="background-color: #f4f4f4; padding: 10px; white-space: break-spaces;">' . htmlspecialchars($prettyJson) . '</pre>'; |
| 197 | } |
| 198 | |
| 199 | return nl2br($jsonString); |
| 200 | }, $this->message); |
| 201 | } |
| 202 | } |
| 203 |