forked from woocommerce/woocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-wc-emails.php
582 lines (508 loc) · 18.8 KB
/
class-wc-emails.php
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
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* Transactional Emails Controller
*
* WooCommerce Emails Class which handles the sending on transactional emails and email templates. This class loads in available emails.
*
* @class WC_Emails
* @version 2.3.0
* @package WooCommerce/Classes/Emails
* @category Class
* @author WooThemes
*/
class WC_Emails {
/** @var array Array of email notification classes */
public $emails;
/** @var WC_Emails The single instance of the class */
protected static $_instance = null;
/**
* Background emailer class.
*/
protected static $background_emailer;
/**
* Main WC_Emails Instance.
*
* Ensures only one instance of WC_Emails is loaded or can be loaded.
*
* @since 2.1
* @static
* @return WC_Emails Main instance
*/
public static function instance() {
if ( is_null( self::$_instance ) ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Cloning is forbidden.
*
* @since 2.1
*/
public function __clone() {
wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
}
/**
* Unserializing instances of this class is forbidden.
*
* @since 2.1
*/
public function __wakeup() {
wc_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce' ), '2.1' );
}
/**
* Hook in all transactional emails.
*/
public static function init_transactional_emails() {
$email_actions = apply_filters( 'woocommerce_email_actions', array(
'woocommerce_low_stock',
'woocommerce_no_stock',
'woocommerce_product_on_backorder',
'woocommerce_order_status_pending_to_processing',
'woocommerce_order_status_pending_to_completed',
'woocommerce_order_status_processing_to_cancelled',
'woocommerce_order_status_pending_to_failed',
'woocommerce_order_status_pending_to_on-hold',
'woocommerce_order_status_failed_to_processing',
'woocommerce_order_status_failed_to_completed',
'woocommerce_order_status_failed_to_on-hold',
'woocommerce_order_status_on-hold_to_processing',
'woocommerce_order_status_on-hold_to_cancelled',
'woocommerce_order_status_on-hold_to_failed',
'woocommerce_order_status_completed',
'woocommerce_order_fully_refunded',
'woocommerce_order_partially_refunded',
'woocommerce_new_customer_note',
'woocommerce_created_customer',
) );
if ( apply_filters( 'woocommerce_defer_transactional_emails', false ) ) {
self::$background_emailer = new WC_Background_Emailer();
foreach ( $email_actions as $action ) {
add_action( $action, array( __CLASS__, 'queue_transactional_email' ), 10, 10 );
}
} else {
foreach ( $email_actions as $action ) {
add_action( $action, array( __CLASS__, 'send_transactional_email' ), 10, 10 );
}
}
}
/**
* Queues transactional email so it's not sent in current request if enabled,
* otherwise falls back to send now.
*/
public static function queue_transactional_email() {
if ( is_a( self::$background_emailer, 'WC_Background_Emailer' ) ) {
self::$background_emailer->push_to_queue( array(
'filter' => current_filter(),
'args' => func_get_args(),
) );
} else {
call_user_func_array( array( __CLASS__, 'send_transactional_email' ), func_get_args() );
}
}
/**
* Init the mailer instance and call the notifications for the current filter.
*
* @internal
*
* @param string $filter Filter name.
* @param array $args Email args (default: []).
*/
public static function send_queued_transactional_email( $filter = '', $args = array() ) {
if ( apply_filters( 'woocommerce_allow_send_queued_transactional_email', true, $filter, $args ) ) {
self::instance(); // Init self so emails exist.
// Ensure gateways are loaded in case they need to insert data into the emails.
WC()->payment_gateways();
WC()->shipping();
do_action_ref_array( $filter . '_notification', $args );
}
}
/**
* Init the mailer instance and call the notifications for the current filter.
*
* @internal
*
* @param array $args Email args (default: []).
*/
public static function send_transactional_email( $args = array() ) {
try {
$args = func_get_args();
self::instance(); // Init self so emails exist.
do_action_ref_array( current_filter() . '_notification', $args );
} catch ( Exception $e ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
trigger_error( 'Transactional email triggered fatal error for callback ' . current_filter(), E_USER_WARNING );
}
}
}
/**
* Constructor for the email class hooks in all emails that can be sent.
*
*/
public function __construct() {
$this->init();
// Email Header, Footer and content hooks
add_action( 'woocommerce_email_header', array( $this, 'email_header' ) );
add_action( 'woocommerce_email_footer', array( $this, 'email_footer' ) );
add_action( 'woocommerce_email_order_details', array( $this, 'order_downloads' ), 10, 4 );
add_action( 'woocommerce_email_order_details', array( $this, 'order_details' ), 10, 4 );
add_action( 'woocommerce_email_order_meta', array( $this, 'order_meta' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $this, 'customer_details' ), 10, 3 );
add_action( 'woocommerce_email_customer_details', array( $this, 'email_addresses' ), 20, 3 );
// Hooks for sending emails during store events
add_action( 'woocommerce_low_stock_notification', array( $this, 'low_stock' ) );
add_action( 'woocommerce_no_stock_notification', array( $this, 'no_stock' ) );
add_action( 'woocommerce_product_on_backorder_notification', array( $this, 'backorder' ) );
add_action( 'woocommerce_created_customer_notification', array( $this, 'customer_new_account' ), 10, 3 );
// Let 3rd parties unhook the above via this hook
do_action( 'woocommerce_email', $this );
}
/**
* Init email classes.
*/
public function init() {
// Include email classes
include_once( dirname( __FILE__ ) . '/emails/class-wc-email.php' );
$this->emails['WC_Email_New_Order'] = include( 'emails/class-wc-email-new-order.php' );
$this->emails['WC_Email_Cancelled_Order'] = include( 'emails/class-wc-email-cancelled-order.php' );
$this->emails['WC_Email_Failed_Order'] = include( 'emails/class-wc-email-failed-order.php' );
$this->emails['WC_Email_Customer_On_Hold_Order'] = include( 'emails/class-wc-email-customer-on-hold-order.php' );
$this->emails['WC_Email_Customer_Processing_Order'] = include( 'emails/class-wc-email-customer-processing-order.php' );
$this->emails['WC_Email_Customer_Completed_Order'] = include( 'emails/class-wc-email-customer-completed-order.php' );
$this->emails['WC_Email_Customer_Refunded_Order'] = include( 'emails/class-wc-email-customer-refunded-order.php' );
$this->emails['WC_Email_Customer_Invoice'] = include( 'emails/class-wc-email-customer-invoice.php' );
$this->emails['WC_Email_Customer_Note'] = include( 'emails/class-wc-email-customer-note.php' );
$this->emails['WC_Email_Customer_Reset_Password'] = include( 'emails/class-wc-email-customer-reset-password.php' );
$this->emails['WC_Email_Customer_New_Account'] = include( 'emails/class-wc-email-customer-new-account.php' );
$this->emails = apply_filters( 'woocommerce_email_classes', $this->emails );
// include css inliner
if ( ! class_exists( 'Emogrifier' ) && class_exists( 'DOMDocument' ) ) {
include_once( dirname( __FILE__ ) . '/libraries/class-emogrifier.php' );
}
}
/**
* Return the email classes - used in admin to load settings.
*
* @return array
*/
public function get_emails() {
return $this->emails;
}
/**
* Get from name for email.
*
* @return string
*/
public function get_from_name() {
return wp_specialchars_decode( get_option( 'woocommerce_email_from_name' ), ENT_QUOTES );
}
/**
* Get from email address.
*
* @return string
*/
public function get_from_address() {
return sanitize_email( get_option( 'woocommerce_email_from_address' ) );
}
/**
* Get the email header.
*
* @param mixed $email_heading heading for the email
*/
public function email_header( $email_heading ) {
wc_get_template( 'emails/email-header.php', array( 'email_heading' => $email_heading ) );
}
/**
* Get the email footer.
*/
public function email_footer() {
wc_get_template( 'emails/email-footer.php' );
}
/**
* Wraps a message in the woocommerce mail template.
*
* @param string $email_heading Heading text.
* @param string $message Email message.
* @param bool $plain_text Set true to send as plain text. Default to false.
*
* @return string
*/
public function wrap_message( $email_heading, $message, $plain_text = false ) {
// Buffer.
ob_start();
do_action( 'woocommerce_email_header', $email_heading, null );
echo wpautop( wptexturize( $message ) );
do_action( 'woocommerce_email_footer', null );
// Get contents.
$message = ob_get_clean();
return $message;
}
/**
* Send the email.
*
* @param mixed $to
* @param mixed $subject
* @param mixed $message
* @param string $headers (default: "Content-Type: text/html\r\n")
* @param string $attachments (default: "")
* @return bool
*/
public function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) {
// Send
$email = new WC_Email();
return $email->send( $to, $subject, $message, $headers, $attachments );
}
/**
* Prepare and send the customer invoice email on demand.
*
* @param int|WC_Order $order
*/
public function customer_invoice( $order ) {
$email = $this->emails['WC_Email_Customer_Invoice'];
if ( ! is_object( $order ) ) {
$order = wc_get_order( absint( $order ) );
}
$email->trigger( $order->get_id(), $order );
}
/**
* Customer new account welcome email.
*
* @param int $customer_id
* @param array $new_customer_data
* @param bool $password_generated
*/
public function customer_new_account( $customer_id, $new_customer_data = array(), $password_generated = false ) {
if ( ! $customer_id ) {
return;
}
$user_pass = ! empty( $new_customer_data['user_pass'] ) ? $new_customer_data['user_pass'] : '';
$email = $this->emails['WC_Email_Customer_New_Account'];
$email->trigger( $customer_id, $user_pass, $password_generated );
}
/**
* Show the order details table
*
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
* @param string $email
*/
public function order_details( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
if ( $plain_text ) {
wc_get_template( 'emails/plain/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) );
} else {
wc_get_template( 'emails/email-order-details.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email ) );
}
}
/**
* Show order downloads in a table.
*
* @since 3.2.0
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
* @param string $email
*/
public function order_downloads( $order, $sent_to_admin = false, $plain_text = false, $email = '' ) {
$show_downloads = $order->has_downloadable_item() && $order->is_download_permitted();
if ( ! $show_downloads ) {
return;
}
$downloads = $order->get_downloadable_items();
$columns = apply_filters( 'woocommerce_email_downloads_columns', array(
'download-product' => __( 'Product', 'woocommerce' ),
'download-expires' => __( 'Expires', 'woocommerce' ),
'download-file' => __( 'Download', 'woocommerce' ),
) );
if ( $plain_text ) {
wc_get_template( 'emails/plain/email-downloads.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email, 'downloads' => $downloads, 'columns' => $columns ) );
} else {
wc_get_template( 'emails/email-downloads.php', array( 'order' => $order, 'sent_to_admin' => $sent_to_admin, 'plain_text' => $plain_text, 'email' => $email, 'downloads' => $downloads, 'columns' => $columns ) );
}
}
/**
* Add order meta to email templates.
*
* @param mixed $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
*/
public function order_meta( $order, $sent_to_admin = false, $plain_text = false ) {
$fields = apply_filters( 'woocommerce_email_order_meta_fields', array(), $sent_to_admin, $order );
/**
* Deprecated woocommerce_email_order_meta_keys filter.
*
* @since 2.3.0
*/
$_fields = apply_filters( 'woocommerce_email_order_meta_keys', array(), $sent_to_admin );
if ( $_fields ) {
foreach ( $_fields as $key => $field ) {
if ( is_numeric( $key ) ) {
$key = $field;
}
$fields[ $key ] = array(
'label' => wptexturize( $key ),
'value' => wptexturize( get_post_meta( $order->get_id(), $field, true ) ),
);
}
}
if ( $fields ) {
if ( $plain_text ) {
foreach ( $fields as $field ) {
if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) {
echo $field['label'] . ': ' . $field['value'] . "\n";
}
}
} else {
foreach ( $fields as $field ) {
if ( isset( $field['label'] ) && isset( $field['value'] ) && $field['value'] ) {
echo '<p><strong>' . $field['label'] . ':</strong> ' . $field['value'] . '</p>';
}
}
}
}
}
/**
* Is customer detail field valid?
* @param array $field
* @return boolean
*/
public function customer_detail_field_is_valid( $field ) {
return isset( $field['label'] ) && ! empty( $field['value'] );
}
/**
* Allows developers to add additional customer details to templates.
*
* In versions prior to 3.2 this was used for notes, phone and email but this data has moved.
*
* @param WC_Order $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
* @return string
*/
public function customer_details( $order, $sent_to_admin = false, $plain_text = false ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
return;
}
$fields = array_filter( apply_filters( 'woocommerce_email_customer_details_fields', array(), $sent_to_admin, $order ), array( $this, 'customer_detail_field_is_valid' ) );
if ( ! empty( $fields ) ) {
if ( $plain_text ) {
wc_get_template( 'emails/plain/email-customer-details.php', array( 'fields' => $fields ) );
} else {
wc_get_template( 'emails/email-customer-details.php', array( 'fields' => $fields ) );
}
}
}
/**
* Get the email addresses.
*
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_addresses( $order, $sent_to_admin = false, $plain_text = false ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
return;
}
if ( $plain_text ) {
wc_get_template( 'emails/plain/email-addresses.php', array( 'order' => $order ) );
} else {
wc_get_template( 'emails/email-addresses.php', array( 'order' => $order ) );
}
}
/**
* Get blog name formatted for emails.
* @return string
*/
private function get_blogname() {
return wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
}
/**
* Low stock notification email.
*
* @param WC_Product $product
*/
public function low_stock( $product ) {
if ( 'no' === get_option( 'woocommerce_notify_low_stock', 'yes' ) ) {
return;
}
$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product low in stock', 'woocommerce' ) );
/* translators: 1: product name 2: items in stock */
$message = sprintf(
__( '%1$s is low in stock. There are %2$d left.', 'woocommerce' ),
html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ),
html_entity_decode( strip_tags( $product->get_stock_quantity() ) )
);
wp_mail(
apply_filters( 'woocommerce_email_recipient_low_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),
apply_filters( 'woocommerce_email_subject_low_stock', $subject, $product ),
apply_filters( 'woocommerce_email_content_low_stock', $message, $product ),
apply_filters( 'woocommerce_email_headers', '', 'low_stock', $product ),
apply_filters( 'woocommerce_email_attachments', array(), 'low_stock', $product )
);
}
/**
* No stock notification email.
*
* @param WC_Product $product
*/
public function no_stock( $product ) {
if ( 'no' === get_option( 'woocommerce_notify_no_stock', 'yes' ) ) {
return;
}
$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product out of stock', 'woocommerce' ) );
/* translators: %s: product name */
$message = sprintf( __( '%s is out of stock.', 'woocommerce' ), html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ) );
wp_mail(
apply_filters( 'woocommerce_email_recipient_no_stock', get_option( 'woocommerce_stock_email_recipient' ), $product ),
apply_filters( 'woocommerce_email_subject_no_stock', $subject, $product ),
apply_filters( 'woocommerce_email_content_no_stock', $message, $product ),
apply_filters( 'woocommerce_email_headers', '', 'no_stock', $product ),
apply_filters( 'woocommerce_email_attachments', array(), 'no_stock', $product )
);
}
/**
* Backorder notification email.
*
* @param array $args
*/
public function backorder( $args ) {
$args = wp_parse_args( $args, array(
'product' => '',
'quantity' => '',
'order_id' => '',
) );
extract( $args );
if ( ! $product || ! $quantity || ! ( $order = wc_get_order( $order_id ) ) ) {
return;
}
$subject = sprintf( '[%s] %s', $this->get_blogname(), __( 'Product backorder', 'woocommerce' ) );
$message = sprintf( __( '%1$s units of %2$s have been backordered in order #%3$s.', 'woocommerce' ), $quantity, html_entity_decode( strip_tags( $product->get_formatted_name() ), ENT_QUOTES, get_bloginfo( 'charset' ) ), $order->get_order_number() );
wp_mail(
apply_filters( 'woocommerce_email_recipient_backorder', get_option( 'woocommerce_stock_email_recipient' ), $args ),
apply_filters( 'woocommerce_email_subject_backorder', $subject, $args ),
apply_filters( 'woocommerce_email_content_backorder', $message, $args ),
apply_filters( 'woocommerce_email_headers', '', 'backorder', $args ),
apply_filters( 'woocommerce_email_attachments', array(), 'backorder', $args )
);
}
/**
* Adds Schema.org markup for order in JSON-LD format.
*
* @deprecated 3.0.0
* @see WC_Structured_Data::generate_order_data()
*
* @since 2.6.0
* @param mixed $order
* @param bool $sent_to_admin (default: false)
* @param bool $plain_text (default: false)
*/
public function order_schema_markup( $order, $sent_to_admin = false, $plain_text = false ) {
wc_deprecated_function( 'WC_Emails::order_schema_markup', '3.0', 'WC_Structured_Data::generate_order_data' );
WC()->structured_data->generate_order_data( $order, $sent_to_admin, $plain_text );
WC()->structured_data->output_structured_data();
}
}