PluginProbe ʕ •ᴥ•ʔ
WP STAGING – WordPress Backups, Restore, Migration & Clone / 4.11.0
WP STAGING – WordPress Backups, Restore, Migration & Clone v4.11.0
4.11.0 4.10.0 4.9.5 4.9.4 4.9.3 4.9.2 4.9.1 4.9.0 4.8.1 trunk 3.0.0 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.0.6 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.10.0 3.2.0 3.3.1 3.3.2 3.3.3 3.4.1 3.4.3 3.5.0 3.6.0 3.7.1 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 3.8.7 3.9.0 3.9.1 3.9.2 3.9.3 3.9.4 4.0.0 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.2.0 4.2.1 4.3.0 4.3.1 4.3.2 4.4.0 4.5.0 4.6.0 4.7.0 4.7.1 4.7.2 4.7.3 4.8.0
wp-staging / Notifications / Transporter / EmailTemplateBuilder.php
wp-staging / Notifications / Transporter Last commit date
EmailNotification.php 1 day ago EmailTemplateBuilder.php 1 day ago
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
13
14 private $title;
15
16
17
18
19 private $message;
20
21
22
23
24 private $details = [];
25
26
27
28
29 private $isBasic = false;
30
31
32
33
34 private $recipient = '';
35
36
37
38
39 public function __construct(TemplateEngine $templateEngine)
40 {
41 parent::__construct($templateEngine);
42 $this->isBasic = WPStaging::isBasic();
43 }
44
45
46
47
48
49
50 public static function create(TemplateEngine $templateEngine)
51 {
52 return new self($templateEngine);
53 }
54
55
56
57
58
59
60 public function setTitle(string $title)
61 {
62 $this->title = $title;
63 return $this;
64 }
65
66
67
68
69
70
71 public function setMessage(string $message)
72 {
73 $this->message = $message;
74 return $this;
75 }
76
77
78
79
80
81
82 public function setDetails(array $details)
83 {
84 $this->details = $details;
85 return $this;
86 }
87
88
89
90
91
92
93 public function setRecipient(string $recipient = '')
94 {
95 $this->recipient = $recipient;
96 return $this;
97 }
98
99
100
101
102
103 protected function getTemplate(): string
104 {
105 return 'notifications/email-template.php';
106 }
107
108
109
110
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
128
129
130 private function processMessage(): string
131 {
132 $message = $this->beautifyJsonInMessage();
133 $message = nl2br($message);
134 return $this->convertUrlsToLinks($message);
135 }
136
137
138
139
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
153
154
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
175
176
177 public function generate(): string
178 {
179 return $this->templateEngine->render(
180 $this->getTemplate(),
181 $this->getRenderData()
182 );
183 }
184
185
186
187
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