1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
|
<?php
namespace AutomateWoo;
defined( 'ABSPATH' ) || exit;
use Pelago\Emogrifier; use Pelago\Emogrifier\CssInliner;
/** * Mailer class for HTML emails that use a template. */ class Mailer extends Mailer_Abstract {
/** @var string */ public $template = 'default';
/** @var string */ public $heading;
/** @var string */ public $preheader;
/** @var string */ public $extra_footer_text;
/** @var string */ public $tracking_pixel_url;
/** @var callable - use to replace URLs in content e.g. for click tracking */ public $replace_content_urls_callback;
/** @var bool */ public $include_automatewoo_styles = true;
/** * Mailer constructor. * * All params are deprecated, use setter methods instead. * * @todo remove params, no longer in use after Refer A Friend 2.3 * * @param string|false $subject * @param string|false $email * @param string|false $content * @param string $template */ public function __construct( $subject = false, $email = false, $content = false, $template = 'default' ) {
// deprecated $this->email = $email; $this->subject = $subject; $this->content = $content; $this->template = $template;
do_action( 'automatewoo/mailer/init' ); }
/** * @param string $heading */ public function set_heading( $heading ) { $this->heading = $heading; }
/** * @param string $preheader */ public function set_preheader( $preheader ) { $this->preheader = $preheader; }
/** * @param string $template */ public function set_template( $template ) { $this->template = $template;
// Must reset from props after template is changed. $this->from_email = null; $this->from_name = null; }
/** * @param bool $include */ public function set_include_automatewoo_styles( $include ) { $this->include_automatewoo_styles = $include; }
/** * Get email sender email address. * * @return string */ public function get_from_email() { if ( ! isset( $this->from_email ) ) { $this->from_email = Emails::get_from_address( $this->template ); } return $this->from_email; }
/** * Get email sender name. * * @return string */ public function get_from_name() { if ( ! isset( $this->from_name ) ) { $this->from_name = Emails::get_from_name( $this->template ); } return $this->from_name; }
/** * Returns email body, can be HTML or plain text. * * @since 4.4.0 * * @return string */ public function get_email_body() { $html = $this->get_content_wrapped_in_template(); return apply_filters( 'woocommerce_mail_content', $this->prepare_html( $html ) ); }
/** * @return string */ public function get_content_wrapped_in_template() { $content = $this->content;
add_filter( 'woocommerce_email_footer_text', [ $this, 'add_extra_footer_text' ] );
/** * @hooked wpautop() */ $content = apply_filters( 'automatewoo_email_content', $content );
// Buffer ob_start();
$this->get_template_part( 'email-header.php', [ 'email_heading' => $this->heading, ] );
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $content;
$this->get_template_part( 'email-footer.php' );
$html = ob_get_clean();
remove_filter( 'woocommerce_email_footer_text', [ $this, 'add_extra_footer_text' ] );
return $html; }
/** * Prepare HTML before it's sent. * Should be run after dynamic content like variables have been inserted. * * - Maybe injects preheader * - Processes email variables like {{ unsubscribe_url }} * - Fixes links with double http * - Maybe convert URLs to trackable URLs * - Replaces unsupported HTML tags * - Runs wptexturize() to convert quotes * - Fix container ID for MailPoet compatibility if required * - HTML encodes emojis * - Injects tracking pixel * - Inlines CSS * * @since 4.3.0 * * @param string $html * @return string */ public function prepare_html( $html ) { if ( $this->preheader ) { $html = $this->inject_preheader( $html ); }
$html = $this->process_email_variables( $html ); $html = $this->fix_links_with_double_http( $html ); $html = $this->replace_urls_in_content( $html ); $html = wptexturize( $html );
// If MailPoet is active and customizing WooCommerce emails then the container ID needs to be updated for compatibility if ( Integrations::is_mailpoet_overriding_styles() ) { $html = $this->fix_wrapper_for_mailpoet( $html ); }
$html = $this->style_inline( $html ); $html = Clean::encode_emoji( $html ); // encoding emojis before CSS inline seems to decode them again
if ( $this->tracking_pixel_url ) { $html = $this->inject_tracking_pixel( $html ); // add tracking pixel after CSS inline }
return $html; }
/** * Replace default email wrapper ID with one required for MailPoet inline styling * * @param string $html The contents of the email * @return string */ public function fix_wrapper_for_mailpoet( $html ) { return str_replace( 'id="wrapper"', 'id="mailpoet_woocommerce_container"', $html ); }
/** * Fix any duplicate http in links, can happen due to variables * * @param string $content * @return string */ public function fix_links_with_double_http( $content ) { $content = str_replace( '"http://http://', '"http://', $content ); $content = str_replace( '"https://https://', '"https://', $content ); $content = str_replace( '"http://https://', '"https://', $content ); $content = str_replace( '"https://http://', '"http://', $content ); return $content; }
/** * Apply inline styles to dynamic content. * * @param string|null $content * @return string */ public function style_inline( $content ) { ob_start();
if ( $this->include_automatewoo_styles ) { aw_get_template( 'email/styles.php' ); }
$this->get_template_part( 'email-styles.php' ); $css = apply_filters( 'woocommerce_email_styles', ob_get_clean(), new \WC_Email() ); $css = apply_filters( 'automatewoo/mailer/styles', $css, $this );
return $this->emogrify( $content, $css ); }
/** * @param string $text * @return string */ public function add_extra_footer_text( $text ) {
if ( ! $this->extra_footer_text ) { return $text; }
// add separator if there is footer text if ( trim( $text ) ) { $text .= apply_filters( 'automatewoo_email_footer_separator', ' - ' ); }
$text .= $this->extra_footer_text;
return $text; }
/** * Get a template part. * * @param string $file_name The name of the template file. * @param array $variables Array of variables for use in the template file. */ public function get_template_part( $file_name, $variables = [] ) { switch ( $this->template ) {
// default is the woocommerce template case 'default': $template_name = 'emails/' . $file_name; $template_path = ''; break;
// 'plain' doesn't mean the email is plain text case 'plain': aw_get_template( 'email/plain/' . $file_name, $variables ); return;
// Custom template default: $template_data = Emails::get_template( $this->template ); $template_name = $file_name;
// Check if this template has a custom path if ( is_array( $template_data ) && isset( $template_data['path'] ) ) { $template_path = untrailingslashit( $template_data['path'] ); } else { $template_path = 'automatewoo/custom-email-templates/' . $this->template; } break; }
if ( aw_str_starts_with( $template_path, '/' ) ) { // Path is absolute $located = $template_path . '/' . $template_name; } else { // Locate the relative path template $located = wc_locate_template( $template_name, $template_path ); }
// If using the woo default template, apply filters to support email customizer plugins if ( 'default' === $this->template ) { $located = apply_filters( 'wc_get_template', $located, $template_name, $variables, $template_path, '' ); do_action( 'woocommerce_before_template_part', $template_name, $template_path, $located, $variables ); }
$this->load_template_part( $located, $variables );
if ( 'default' === $this->template ) { do_action( 'woocommerce_after_template_part', $template_name, $template_path, $located, $variables ); } }
/** * Load a template part if it's found. * * Prefix params with '_' to prevent clashes when using extract on $_variables. * * @since 4.8.0 * * @param string $_template_file * @param array $_variables */ public function load_template_part( $_template_file, $_variables ) { if ( is_array( $_variables ) ) { // phpcs:ignore WordPress.PHP.DontExtract.extract_extract extract( $_variables, EXTR_SKIP ); }
if ( $_template_file && file_exists( $_template_file ) ) { include $_template_file; // nosemgrep This has not reached user input. Is used in Mailer.php to load the templates } }
/** * Maybe replace URLs with trackable URLs * * @param string $content * @return string */ public function replace_urls_in_content( $content ) { if ( ! $this->replace_content_urls_callback ) { return $content; }
$replacer = new Replace_Helper( $content, $this->replace_content_urls_callback, 'href_urls' ); return $replacer->process(); }
/** * Injects preheader HTML after opening <body> tag * * @param string $html * @return string */ public function inject_preheader( $html ) { return preg_replace_callback( '/<body[^>]*>/', function ( $matches ) { $preheader = '<div class="automatewoo-email-preheader" style="display: none !important; font-size: 1px;">' . esc_html( $this->preheader ) . '</div>'; return $matches[0] . $preheader; }, $html, 1 ); }
/** * Injects tracking pixel before closing </body> tag * * @param string $html * @return string */ public function inject_tracking_pixel( $html ) { return preg_replace_callback( '/<\/body[^>]*>/', function ( $matches ) { return $this->get_tracking_pixel_img() . $matches[0]; }, $html, 1 ); }
/** * @return string */ public function get_tracking_pixel_img() { return '<img src="' . esc_url( $this->tracking_pixel_url ) . '" height="1" width="1" alt="" style="display:inline">'; }
/** * Add inline CSS to HTML with the Emogrifier library. * * If Emogrifier can't be used the unmodified HTML will be returned. * * @since 4.4.2 * * @param string $html The HTML. * @param string $css The CSS to be inlined. * @param bool $parse_html_style_blocks Should CSS in HTML style blocks also be inlined? * * @return string */ public function emogrify( $html, $css, $parse_html_style_blocks = false ) { if ( ! class_exists( 'DOMDocument' ) ) { return $html; }
// First check if CssInliner can be used since the Emogrifier class is deprecated. if ( class_exists( CssInliner::class ) ) { $emogrifier = CssInliner::fromHtml( $html ); } elseif ( class_exists( Emogrifier::class ) ) { $emogrifier = new Emogrifier( $html, $css ); } else { return $html; }
try { if ( ! $parse_html_style_blocks ) { $emogrifier->disableStyleBlocksParsing(); }
/* * The disableInvisibleNodeRemoval() method was removed with version 3+ of Emogrifier, * which was included in WC 4.0+. Disabling the removal of invisible nodes is now * default behavior, so we don't need to do anything differently if the method * cannot be found. */ if ( method_exists( $emogrifier, 'disableInvisibleNodeRemoval' ) ) { $emogrifier->disableInvisibleNodeRemoval(); }
if ( $emogrifier instanceof CssInliner ) { $html = $emogrifier->inlineCss( $css )->render(); } else { $html = $emogrifier->emogrify(); } } catch ( \Exception $e ) { Logger::error( 'emogrifier', $e->getMessage() ); }
return $html; } }
|