-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Expand file tree
/
Copy pathclass-wc-email.php
More file actions
1831 lines (1616 loc) · 56 KB
/
class-wc-email.php
File metadata and controls
1831 lines (1616 loc) · 56 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Class WC_Email file.
*
* @package WooCommerce\Emails
*/
use Automattic\WooCommerce\Internal\EmailEditor\BlockEmailRenderer;
use Automattic\WooCommerce\Internal\EmailEditor\TransactionalEmailPersonalizer;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
use Automattic\WooCommerce\Vendor\Pelago\Emogrifier\CssInliner;
use Automattic\WooCommerce\Vendor\Pelago\Emogrifier\HtmlProcessor\CssToAttributeConverter;
use Automattic\WooCommerce\Vendor\Pelago\Emogrifier\HtmlProcessor\HtmlPruner;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( class_exists( 'WC_Email', false ) ) {
return;
}
/**
* Email Class
*
* WooCommerce Email Class which is extended by specific email template classes to add emails to WooCommerce
*
* @class WC_Email
* @version 2.5.0
* @package WooCommerce\Classes\Emails
* @extends WC_Settings_API
*/
class WC_Email extends WC_Settings_API {
/**
* Skip-reason identifier used when the email has no recipient address.
*
* @since 10.9.0
*/
public const SKIP_REASON_NO_RECIPIENT = 'no_recipient';
/**
* Email method ID.
*
* @var string
*/
public $id;
/**
* Email method title.
*
* @var string
*/
public $title;
/**
* 'yes' if the method is enabled.
*
* @var string yes, no
*/
public $enabled;
/**
* Description for the email.
*
* @var string
*/
public $description;
/**
* Default heading.
*
* Supported for backwards compatibility but we recommend overloading the
* get_default_x methods instead so localization can be done when needed.
*
* @var string
*/
public $heading = '';
/**
* Default subject.
*
* Supported for backwards compatibility but we recommend overloading the
* get_default_x methods instead so localization can be done when needed.
*
* @var string
*/
public $subject = '';
/**
* Plain text template path.
*
* @var string
*/
public $template_plain;
/**
* HTML template path.
*
* @var string
*/
public $template_html;
/**
* Initial email block template path.
*
* @var string
*/
public $template_block;
/**
* Template path.
*
* @var string
*/
public $template_base;
/**
* Recipients for the email.
*
* @var string
*/
public $recipient;
/**
* Cc recipients for the email.
*
* @var string
*/
public $cc;
/**
* Bcc recipients for the email.
*
* @var string
*/
public $bcc;
/**
* Object this email is for, for example a customer, product, or email.
*
* @var object|bool
*/
public $object;
/**
* Mime boundary (for multipart emails).
*
* @var string
*/
public $mime_boundary;
/**
* Mime boundary header (for multipart emails).
*
* @var string
*/
public $mime_boundary_header;
/**
* True when email is being sent.
*
* @var bool
*/
public $sending;
/**
* True when the email notification is sent manually only.
*
* @var bool
*/
protected $manual = false;
/**
* True when the email notification is sent to customers.
*
* @var bool
*/
protected $customer_email = false;
/**
* Email group slug.
*
* @var string
*/
public $email_group = '';
/**
* List of preg* regular expression patterns to search for,
* used in conjunction with $plain_replace.
* https://raw.github.com/ushahidi/wp-silcc/master/class.html2text.inc
*
* @var array $plain_search
* @see $plain_replace
*/
public $plain_search = array(
"/\r/", // Non-legal carriage return.
'/&(nbsp|#0*160);/i', // Non-breaking space.
'/&(quot|rdquo|ldquo|#0*8220|#0*8221|#0*147|#0*148);/i', // Double quotes.
'/&(apos|rsquo|lsquo|#0*8216|#0*8217);/i', // Single quotes.
'/>/i', // Greater-than.
'/</i', // Less-than.
'/�*38;/i', // Ampersand.
'/&/i', // Ampersand.
'/&(copy|#0*169);/i', // Copyright.
'/&(trade|#0*8482|#0*153);/i', // Trademark.
'/&(reg|#0*174);/i', // Registered.
'/&(mdash|#0*151|#0*8212);/i', // mdash.
'/&(ndash|minus|#0*8211|#0*8722);/i', // ndash.
'/&(bull|#0*149|#0*8226);/i', // Bullet.
'/&(pound|#0*163);/i', // Pound sign.
'/&(euro|#0*8364);/i', // Euro sign.
'/&(dollar|#0*36);/i', // Dollar sign.
'/&[^&\s;]+;/i', // Unknown/unhandled entities.
'/[ ]{2,}/', // Runs of spaces, post-handling.
);
/**
* List of pattern replacements corresponding to patterns searched.
*
* @var array $plain_replace
* @see $plain_search
*/
public $plain_replace = array(
'', // Non-legal carriage return.
' ', // Non-breaking space.
'"', // Double quotes.
"'", // Single quotes.
'>', // Greater-than.
'<', // Less-than.
'&', // Ampersand.
'&', // Ampersand.
'(c)', // Copyright.
'(tm)', // Trademark.
'(R)', // Registered.
'--', // mdash.
'-', // ndash.
'*', // Bullet.
'£', // Pound sign.
'EUR', // Euro sign. € ?.
'$', // Dollar sign.
'', // Unknown/unhandled entities.
' ', // Runs of spaces, post-handling.
);
/**
* Strings to find/replace in subjects/headings.
*
* @var array
*/
public $placeholders = array();
/**
* Strings to find in subjects/headings.
*
* @deprecated 3.2.0 in favour of placeholders
* @var array
*/
public $find = array();
/**
* Strings to replace in subjects/headings.
*
* @deprecated 3.2.0 in favour of placeholders
* @var array
*/
public $replace = array();
/**
* E-mail type: plain, html or multipart.
*
* @var string
*/
public $email_type;
/**
* Whether email improvements feature is enabled.
*
* @var bool
*/
public $email_improvements_enabled;
/**
* Whether email block editor feature is enabled.
*
* @var bool
*/
public $block_email_editor_enabled;
/**
* Personalizer instance for converting Personalization tags.
*
* @var TransactionalEmailPersonalizer
*/
public $personalizer;
/**
* Block content template path.
*
* @var string
*/
public $template_block_content = 'emails/block/general-block-email.php';
/**
* Constructor.
*/
public function __construct() {
$this->email_improvements_enabled = FeaturesUtil::feature_is_enabled( 'email_improvements' );
$this->block_email_editor_enabled = FeaturesUtil::feature_is_enabled( 'block_email_editor' );
// Find/replace.
$this->placeholders = array_merge(
array(
'{site_title}' => $this->get_blogname(),
'{site_address}' => wp_parse_url( home_url(), PHP_URL_HOST ),
'{site_url}' => wp_parse_url( home_url(), PHP_URL_HOST ),
'{store_email}' => $this->get_from_address(),
),
$this->placeholders
);
// Init settings.
$this->init_form_fields();
$this->init_settings();
// Default template base if not declared in child constructor.
if ( is_null( $this->template_base ) ) {
$this->template_base = WC()->plugin_path() . '/templates/';
}
$this->email_type = $this->get_option( 'email_type' );
$this->enabled = $this->get_option( 'enabled' );
if ( FeaturesUtil::feature_is_enabled( 'email_improvements' ) ) {
$this->cc = $this->get_option( 'cc', '' );
$this->bcc = $this->get_option( 'bcc', '' );
}
if ( $this->block_email_editor_enabled ) {
$this->personalizer = wc_get_container()->get( TransactionalEmailPersonalizer::class );
}
add_action( 'phpmailer_init', array( $this, 'handle_multipart' ) );
add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
// Use priority 1 to ensure our skip classes are added before lazy loading plugins process the images.
add_filter( 'wp_get_attachment_image_attributes', array( $this, 'prevent_lazy_loading_on_attachment' ), 1, 1 );
}
/**
* Handle multipart mail.
*
* @param PHPMailer $mailer PHPMailer object.
* @return PHPMailer
*/
public function handle_multipart( $mailer ) {
if ( ! $this->sending ) {
return $mailer;
}
if ( 'multipart' === $this->get_email_type() ) {
$mailer->AltBody = wordwrap( // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
preg_replace( $this->plain_search, $this->plain_replace, wp_strip_all_tags( $this->get_content_plain() ) )
);
} else {
$mailer->AltBody = ''; // phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase
}
$this->sending = false;
return $mailer;
}
/**
* Format email string.
*
* @param mixed $string Text to replace placeholders in.
* @return string
*/
public function format_string( $string ) {
$find = array_keys( $this->placeholders );
$replace = array_values( $this->placeholders );
// If using legacy find replace, add those to our find/replace arrays first. @todo deprecate in 4.0.0.
$find = array_merge( (array) $this->find, $find );
$replace = array_merge( (array) $this->replace, $replace );
// Take care of blogname which is no longer defined as a valid placeholder.
$find[] = '{blogname}';
$replace[] = $this->get_blogname();
// If using the older style filters for find and replace, ensure the array is associative and then pass through filters. @todo deprecate in 4.0.0.
if ( has_filter( 'woocommerce_email_format_string_replace' ) || has_filter( 'woocommerce_email_format_string_find' ) ) {
$legacy_find = $this->find;
$legacy_replace = $this->replace;
foreach ( $this->placeholders as $find => $replace ) {
$legacy_key = sanitize_title( str_replace( '_', '-', trim( $find, '{}' ) ) );
$legacy_find[ $legacy_key ] = $find;
$legacy_replace[ $legacy_key ] = $replace;
}
$string = str_replace( apply_filters( 'woocommerce_email_format_string_find', $legacy_find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $legacy_replace, $this ), $string );
}
/**
* Filter for main find/replace.
*
* @since 3.2.0
*/
return apply_filters( 'woocommerce_email_format_string', str_replace( $find, $replace, $string ), $this );
}
/**
* Set the locale to the store locale for customer emails to make sure emails are in the store language.
*/
public function setup_locale() {
/**
* Filter the ability to switch email locale.
*
* @since 6.8.0
*
* @param bool $default_value The default returned value.
* @param WC_Email $email The WC_Email object.
*/
$switch_email_locale = apply_filters( 'woocommerce_allow_switching_email_locale', true, $this );
if ( $switch_email_locale && $this->is_customer_email() && apply_filters( 'woocommerce_email_setup_locale', true ) ) {
wc_switch_to_site_locale();
}
}
/**
* Restore the locale to the default locale. Use after finished with setup_locale.
*/
public function restore_locale() {
/**
* Filter the ability to restore email locale.
*
* @since 6.8.0
*
* @param bool $default_value The default returned value.
* @param WC_Email $email The WC_Email object.
*/
$restore_email_locale = apply_filters( 'woocommerce_allow_restoring_email_locale', true, $this );
if ( $restore_email_locale && $this->is_customer_email() && apply_filters( 'woocommerce_email_restore_locale', true ) ) {
wc_restore_locale();
}
}
/**
* Get available email groups with their titles.
*
* @since 10.3.0
* @return array Associative array of email group slugs => titles.
*/
public function get_email_groups() {
$email_groups = array(
'accounts' => __( 'Accounts', 'woocommerce' ),
'orders' => __( 'Orders', 'woocommerce' ),
'order-processing' => __( 'Order updates', 'woocommerce' ), // @deprecated Please use 'order-updates' instead. Will be removed in 10.5.0.
'order-updates' => __( 'Order updates', 'woocommerce' ),
'order-exceptions' => __( 'Order changes', 'woocommerce' ), // @deprecated Please use 'order-changes' instead. Will be removed in 10.5.0.
'order-changes' => __( 'Order changes', 'woocommerce' ),
'payments' => __( 'Payments', 'woocommerce' ),
);
/**
* Filter the available email groups.
*
* @since 10.3.0
* @param array $email_groups Associative array of email group slugs => titles.
*/
return apply_filters( 'woocommerce_email_groups', $email_groups );
}
/**
* Get the title for the current email group.
*
* @since 10.3.0
* @return string The email group title. Falls back to the email group slug if not found.
*/
public function get_email_group_title() {
$email_groups = $this->get_email_groups();
$title = isset( $email_groups[ $this->email_group ] ) ? $email_groups[ $this->email_group ] : $this->email_group;
/**
* Filter the email group title.
*
* @since 10.3.0
* @param string $title The email group title.
* @param string $email_group The email group slug.
* @param array $email_groups Associative array of email group slugs => titles.
*/
return (string) apply_filters( 'woocommerce_email_group_title', $title, $this->email_group, $email_groups );
}
/**
* Get email subject.
*
* @since 3.1.0
* @return string
*/
public function get_default_subject() {
return $this->subject;
}
/**
* Get email heading.
*
* @since 3.1.0
* @return string
*/
public function get_default_heading() {
return $this->heading;
}
/**
* Default content to show below main email content.
*
* @since 3.7.0
* @return string
*/
public function get_default_additional_content() {
return '';
}
/**
* Return content from the additional_content field.
*
* Displayed above the footer.
*
* @since 3.7.0
* @return string
*/
public function get_additional_content() {
/**
* Provides an opportunity to inspect and modify additional content for the email.
*
* @since 3.7.0
*
* @param string $additional_content Additional content to be added to the email.
* @param object|bool $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
return apply_filters( 'woocommerce_email_additional_content_' . $this->id, $this->format_string( $this->get_option_or_transient( 'additional_content' ) ), $this->object, $this );
}
/**
* Get email subject.
*
* @return string
*/
public function get_subject() {
/**
* Provides an opportunity to inspect and modify subject for the email.
*
* @since 2.0.0
*
* @param string $subject Subject of the email.
* @param object|bool $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
$subject = apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->get_option_or_transient( 'subject', $this->get_default_subject() ) ), $this->object, $this );
if ( $this->block_email_editor_enabled ) {
// Because the new email editor uses rich-text component for subject editing, to be ensure that the subject is always in plain text, we need to strip all tags.
$subject = wp_strip_all_tags( $this->personalizer->personalize_transactional_content( $subject, $this ) );
}
return $subject;
}
/**
* Get email preheader.
*
* @return string
*/
public function get_preheader() {
/**
* Provides an opportunity to inspect and modify preheader for the email.
*
* @since 9.9.0
*
* @param string $preheader Preheader of the email.
* @param object|bool $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
$preheader = apply_filters( 'woocommerce_email_preheader' . $this->id, $this->format_string( $this->get_option_or_transient( 'preheader', '' ) ), $this->object, $this );
if ( $this->block_email_editor_enabled ) {
$preheader = $this->personalizer->personalize_transactional_content( $preheader, $this );
}
return $preheader;
}
/**
* Get email heading.
*
* @return string
*/
public function get_heading() {
/**
* Provides an opportunity to inspect and modify heading for the email.
*
* @since 2.0.0
*
* @param string $heading Heading to be added to the email.
* @param object|bool $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->get_option_or_transient( 'heading', $this->get_default_heading() ) ), $this->object, $this );
}
/**
* Get valid recipients.
*
* @return string
*/
public function get_recipient() {
/**
* Filter the recipient for the email.
*
* @since 2.0.0
* @since 3.7.0 Added $email parameter.
* @param string $recipient Recipient.
* @param object $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
$recipient = apply_filters( 'woocommerce_email_recipient_' . $this->id, $this->recipient, $this->object, $this );
$recipients = array_map( 'trim', explode( ',', $recipient ?? '' ) );
$recipients = array_filter( $recipients, 'is_email' );
return implode( ', ', $recipients );
}
/**
* Get valid Cc recipients.
*
* @return string
*/
public function get_cc_recipient() {
/**
* Filter the Cc recipient for the email.
*
* @since 9.8.0
* @param string $cc Cc recipient.
* @param object $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
$cc = apply_filters( 'woocommerce_email_cc_recipient_' . $this->id, $this->cc, $this->object, $this );
$ccs = array_map( 'trim', explode( ',', $cc ?? '' ) );
$ccs = array_filter( $ccs, 'is_email' );
$ccs = array_map( 'sanitize_email', $ccs );
return implode( ', ', $ccs );
}
/**
* Get valid Bcc recipients.
*
* @return string
*/
public function get_bcc_recipient() {
/**
* Filter the Bcc recipient for the email.
*
* @since 9.8.0
* @param string $bcc Bcc recipient.
* @param object $object The object (ie, product or order) this email relates to, if any.
* @param WC_Email $email WC_Email instance managing the email.
*/
$bcc = apply_filters( 'woocommerce_email_bcc_recipient_' . $this->id, $this->bcc, $this->object, $this );
$bccs = array_map( 'trim', explode( ',', $bcc ?? '' ) );
$bccs = array_filter( $bccs, 'is_email' );
$bccs = array_map( 'sanitize_email', $bccs );
return implode( ', ', $bccs );
}
/**
* Get email headers.
*
* @return string
*/
public function get_headers() {
$header = 'Content-Type: ' . $this->get_content_type() . "\r\n";
// For order notification emails sent to admin, always use customer's billing email as reply-to.
if ( in_array( $this->id, array( 'new_order', 'cancelled_order', 'failed_order' ), true ) ) {
if ( $this->object && $this->object->get_billing_email() && ( $this->object->get_billing_first_name() || $this->object->get_billing_last_name() ) ) {
$header .= 'Reply-to: ' . $this->object->get_billing_first_name() . ' ' . $this->object->get_billing_last_name() . ' <' . $this->object->get_billing_email() . ">\r\n";
}
} else {
// Check if custom reply-to is enabled and configured for non-admin notification emails.
$reply_to_enabled = $this->get_reply_to_enabled();
$reply_to_address = $this->get_reply_to_address();
$reply_to_name = $this->get_reply_to_name();
if ( $reply_to_enabled && ! empty( $reply_to_address ) && is_email( $reply_to_address ) ) {
$reply_to_name = ! empty( $reply_to_name ) ? $reply_to_name : $this->get_from_name();
$header .= 'Reply-to: ' . $reply_to_name . ' <' . $reply_to_address . ">\r\n";
} elseif ( $this->get_from_address() && $this->get_from_name() ) {
$header .= 'Reply-to: ' . $this->get_from_name() . ' <' . $this->get_from_address() . ">\r\n";
}
}
if ( FeaturesUtil::feature_is_enabled( 'email_improvements' ) ) {
$cc = $this->get_cc_recipient();
if ( ! empty( $cc ) ) {
$header .= 'Cc: ' . sanitize_text_field( $cc ) . "\r\n";
}
$bcc = $this->get_bcc_recipient();
if ( ! empty( $bcc ) ) {
$header .= 'Bcc: ' . sanitize_text_field( $bcc ) . "\r\n";
}
}
return apply_filters( 'woocommerce_email_headers', $header, $this->id, $this->object, $this );
}
/**
* Get email attachments.
*
* @return array
*/
public function get_attachments() {
return apply_filters( 'woocommerce_email_attachments', array(), $this->id, $this->object, $this );
}
/**
* Return email type.
*
* @return string
*/
public function get_email_type() {
$email_type = $this->email_type;
/**
* This filter is documented in templates/emails/email-styles.php
*
* @since 9.6.0
* @param bool $is_email_preview Whether the email is being previewed.
*/
$is_email_preview = apply_filters( 'woocommerce_is_email_preview', false );
// Transient is used for live email preview without saving the settings.
if ( $is_email_preview ) {
$transient = get_transient( "woocommerce_{$this->id}_email_type" );
$email_type = $transient ? $transient : $email_type;
}
return $email_type && class_exists( 'DOMDocument' ) ? $email_type : 'plain';
}
/**
* Get block editor email template content.
*
* @return string
*/
public function get_block_editor_email_template_content() {
return wc_get_template_html(
$this->template_block_content,
array(
'order' => $this->object,
'sent_to_admin' => false,
'plain_text' => false,
'email' => $this,
)
);
}
/**
* Get email content type.
*
* @param string $default_content_type Default wp_mail() content type.
* @return string
*/
public function get_content_type( $default_content_type = '' ) {
switch ( $this->get_email_type() ) {
case 'html':
$content_type = 'text/html';
break;
case 'multipart':
$content_type = 'multipart/alternative';
break;
default:
$content_type = 'text/plain';
break;
}
return apply_filters( 'woocommerce_email_content_type', $content_type, $this, $default_content_type );
}
/**
* Return the email's title
*
* @return string
*/
public function get_title() {
return apply_filters( 'woocommerce_email_title', $this->title, $this );
}
/**
* Return the email's description
*
* @return string
*/
public function get_description() {
return apply_filters( 'woocommerce_email_description', $this->description, $this );
}
/**
* Proxy to parent's get_option and attempt to localize the result using gettext.
*
* @param string $key Option key.
* @param mixed $empty_value Value to use when option is empty.
* @return string
*/
public function get_option( $key, $empty_value = null ) {
$value = parent::get_option( $key, $empty_value );
return apply_filters( 'woocommerce_email_get_option', $value, $this, $value, $key, $empty_value );
}
/**
* Checks if this email is enabled and will be sent.
*
* @return bool
*/
public function is_enabled() {
return apply_filters( 'woocommerce_email_enabled_' . $this->id, 'yes' === $this->enabled, $this->object, $this );
}
/**
* Checks if this email is manually sent
*
* @return bool
*/
public function is_manual() {
return $this->manual;
}
/**
* Checks if this email is customer focussed.
*
* @return bool
*/
public function is_customer_email() {
return $this->customer_email;
}
/**
* Get WordPress blog name.
*
* @return string
*/
public function get_blogname() {
return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
}
/**
* Get email content.
*
* @return string
*/
public function get_content() {
$this->sending = true;
$block_email_content = $this->get_block_email_html_content();
if ( $block_email_content ) {
$this->email_type = 'plain' === $this->email_type ? 'html' : $this->email_type;
return $block_email_content;
}
if ( 'plain' === $this->get_email_type() ) {
$email_content = wordwrap( preg_replace( $this->plain_search, $this->plain_replace, wp_strip_all_tags( $this->get_content_plain() ) ), 70 );
} else {
$email_content = $this->get_content_html();
}
return $email_content;
}
/**
* Apply inline styles to dynamic content.
*
* We only inline CSS for html emails.
*
* @version 10.2.0
* @param string|null $content Content that will receive inline styles.
* @return string
*/
public function style_inline( $content ) {
if ( in_array( $this->get_content_type(), array( 'text/html', 'multipart/alternative' ), true ) ) {
/**
* Filter to allow the ability to override the email inline styling method.
*
* @since 10.2.0
*
* @param callable $style_inline_callback The default email inline styling callback.
* @param string|null $content Content that will receive inline styles.
* @param WC_Email $email The WC_Email object.
*/
$style_inline_callback = apply_filters( 'woocommerce_mail_style_inline_callback', array( $this, 'apply_inline_style' ), $content, $this );
if ( ! is_callable( $style_inline_callback ) ) {
$style_inline_callback = array( $this, 'apply_inline_style' );
}
return call_user_func( $style_inline_callback, $content );
}
return $content;
}
/**
* Apply inline styles to dynamic content using Emogrifier library (if supported).
*
* @since 10.2.0
* @param string|null $content Content that will receive inline styles.
* @return string
*/
private function apply_inline_style( $content ) {
$css = '';
$css .= $this->get_must_use_css_styles();
$css .= "\n";
ob_start();
wc_get_template( 'emails/email-styles.php' );
$css .= ob_get_clean();
/**
* Provides an opportunity to filter the CSS styles included in e-mails.
*
* @since 2.3.0
*
* @param string $css CSS code.
* @param \WC_Email $email E-mail instance.
*/
$css = apply_filters( 'woocommerce_email_styles', $css, $this );
$css_inliner_class = CssInliner::class;
if ( $this->supports_emogrifier() && class_exists( $css_inliner_class ) ) {
try {
$css_inliner = CssInliner::fromHtml( $content )->inlineCss( $css );
/**
* Action hook fired when an email content has been processed by Emogrifier CssInliner instance.
*
* @since 4.1.0
*
* @param CssInliner $css_inliner CssInliner instance.
* @param WC_Email $email WC_Email instance.
*/
do_action( 'woocommerce_emogrifier', $css_inliner, $this );
$dom_document = $css_inliner->getDomDocument();
// When the email is rendered in the block editor, we don't want to remove the elements with display: none.
// The main reason is using preview text in the email body which is hidden by default.
if ( ! $this->block_email_editor_enabled ) {
HtmlPruner::fromDomDocument( $dom_document )->removeElementsWithDisplayNone();
}
$content = CssToAttributeConverter::fromDomDocument( $dom_document )
->convertCssToVisualAttributes()
->render();
} catch ( Exception $e ) {
$logger = wc_get_logger();
$logger->error( $e->getMessage(), array( 'source' => 'emogrifier' ) );
}
} else {
$content = '<style type="text/css">' . $css . '</style>' . $content;
}
return $content;
}
/**
* Returns CSS styles that should be included with all HTML e-mails, regardless of theme specific customizations.
*
* @since 9.1.0
*
* @return string
*/
protected function get_must_use_css_styles(): string {
$css = <<<'EOF'
/*
* Temporary measure until e-mail clients more properly support the correct styles.
* See https://github.com/woocommerce/woocommerce/pull/47738.
*/
.screen-reader-text {
display: none;
}
EOF;
return $css;
}
/**
* Return if emogrifier library is supported.