2012-09-17 00:53:17 +00:00
< ? php
/**
* 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
2014-11-25 23:21:00 +00:00
* @ version 2.3 . 0
2012-09-17 00:53:17 +00:00
* @ package WooCommerce / Classes / Emails
2013-02-20 17:14:46 +00:00
* @ category Class
2012-09-17 00:53:17 +00:00
* @ author WooThemes
*/
class WC_Emails {
2012-11-27 16:22:47 +00:00
2014-11-25 23:21:00 +00:00
/** @var array Array of email notification classes */
2012-09-17 00:53:17 +00:00
public $emails ;
2012-11-27 16:22:47 +00:00
2014-11-25 23:21:00 +00:00
/** @var WC_Emails The single instance of the class */
2013-09-12 13:41:02 +00:00
protected static $_instance = null ;
/**
2014-06-19 19:43:05 +00:00
* Main WC_Emails Instance
2013-09-12 13:41:02 +00:00
*
2014-06-19 19:43:05 +00:00
* Ensures only one instance of WC_Emails is loaded or can be loaded .
2013-09-12 13:41:02 +00:00
*
* @ since 2.1
* @ static
2014-06-19 19:43:05 +00:00
* @ return WC_Emails Main instance
2013-09-12 13:41:02 +00:00
*/
public static function instance () {
2014-10-20 15:59:02 +00:00
if ( is_null ( self :: $_instance ) ) {
2013-09-12 13:41:02 +00:00
self :: $_instance = new self ();
2014-10-20 15:59:02 +00:00
}
2013-09-12 13:41:02 +00:00
return self :: $_instance ;
}
/**
* Cloning is forbidden .
*
* @ since 2.1
*/
public function __clone () {
2014-02-17 13:14:41 +00:00
_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' , 'woocommerce' ), '2.1' );
2013-09-12 13:41:02 +00:00
}
/**
* Unserializing instances of this class is forbidden .
*
* @ since 2.1
*/
public function __wakeup () {
2014-02-17 13:14:41 +00:00
_doing_it_wrong ( __FUNCTION__ , __ ( 'Cheatin’ huh?' , 'woocommerce' ), '2.1' );
2013-09-12 13:41:02 +00:00
}
2014-11-26 00:02:41 +00:00
/**
* 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_pending_to_cancelled' ,
'woocommerce_order_status_pending_to_on-hold' ,
'woocommerce_order_status_failed_to_processing' ,
'woocommerce_order_status_failed_to_completed' ,
'woocommerce_order_status_on-hold_to_processing' ,
'woocommerce_order_status_on-hold_to_cancelled' ,
'woocommerce_order_status_completed' ,
2015-08-12 18:01:05 +00:00
'woocommerce_order_fully_refunded' ,
2015-06-17 14:35:54 +00:00
'woocommerce_order_partially_refunded' ,
2014-11-26 00:02:41 +00:00
'woocommerce_new_customer_note' ,
'woocommerce_created_customer'
) );
foreach ( $email_actions as $action ) {
add_action ( $action , array ( __CLASS__ , 'send_transactional_email' ), 10 , 10 );
}
}
/**
* Init the mailer instance and call the notifications for the current filter .
* @ internal param array $args ( default : array ())
*/
public static function send_transactional_email () {
self :: instance ();
2015-02-03 04:06:50 +00:00
$args = func_get_args ();
do_action_ref_array ( current_filter () . '_notification' , $args );
2014-11-26 00:02:41 +00:00
}
2012-09-17 00:53:17 +00:00
/**
* Constructor for the email class hooks in all emails that can be sent .
*
*/
2014-10-20 15:59:02 +00:00
public function __construct () {
2013-07-26 14:36:28 +00:00
$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_meta' , array ( $this , 'order_meta' ), 10 , 3 );
2014-10-08 21:47:50 +00:00
add_action ( 'woocommerce_email_customer_details' , array ( $this , 'customer_details' ), 10 , 3 );
2015-05-06 12:10:06 +00:00
add_action ( 'woocommerce_email_customer_details' , array ( $this , 'email_addresses' ), 20 , 3 );
2013-07-26 14:36:28 +00:00
// 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
*/
2014-10-20 15:59:02 +00:00
public function init () {
2012-09-17 00:53:17 +00:00
// Include email classes
2014-09-23 16:12:43 +00:00
include_once ( 'emails/class-wc-email.php' );
2012-11-27 16:22:47 +00:00
2015-06-17 14:35:54 +00:00
$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_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' );
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
$this -> emails = apply_filters ( 'woocommerce_email_classes' , $this -> emails );
2014-10-08 21:34:31 +00:00
// include css inliner
2015-02-13 14:16:58 +00:00
if ( ! class_exists ( 'Emogrifier' ) && class_exists ( 'DOMDocument' ) ) {
2014-10-08 21:34:31 +00:00
include_once ( 'libraries/class-emogrifier.php' );
}
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* Return the email classes - used in admin to load settings .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ return array
*/
2014-10-20 15:59:02 +00:00
public function get_emails () {
2012-09-17 00:53:17 +00:00
return $this -> emails ;
}
/**
* Get from name for email .
*
* @ return string
*/
2014-10-20 15:59:02 +00:00
public function get_from_name () {
2015-02-12 13:20:23 +00:00
return wp_specialchars_decode ( get_option ( 'woocommerce_email_from_name' ) );
2012-09-17 00:53:17 +00:00
}
/**
* Get from email address .
*
* @ return string
*/
2014-10-20 15:59:02 +00:00
public function get_from_address () {
2014-11-25 23:21:00 +00:00
return get_option ( 'woocommerce_email_from_address' );
2012-09-17 00:53:17 +00:00
}
/**
* Get the email header .
*
* @ param mixed $email_heading heading for the email
*/
2014-10-20 15:59:02 +00:00
public function email_header ( $email_heading ) {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'emails/email-header.php' , array ( 'email_heading' => $email_heading ) );
2012-09-17 00:53:17 +00:00
}
/**
* Get the email footer .
*/
2014-10-20 15:59:02 +00:00
public function email_footer () {
2013-11-25 12:45:04 +00:00
wc_get_template ( 'emails/email-footer.php' );
2012-09-17 00:53:17 +00:00
}
/**
* Wraps a message in the woocommerce mail template .
*
* @ param mixed $email_heading
2014-09-07 23:37:55 +00:00
* @ param string $message
2012-09-17 00:53:17 +00:00
* @ return string
*/
2014-10-20 15:59:02 +00:00
public function wrap_message ( $email_heading , $message , $plain_text = false ) {
2012-09-17 00:53:17 +00:00
// Buffer
ob_start ();
do_action ( 'woocommerce_email_header' , $email_heading );
echo wpautop ( wptexturize ( $message ) );
do_action ( 'woocommerce_email_footer' );
// 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 : " " )
2015-07-16 15:56:34 +00:00
* @ return bool
2012-09-17 00:53:17 +00:00
*/
2014-11-25 23:21:00 +00:00
public function send ( $to , $subject , $message , $headers = " Content-Type: text/html \r \n " , $attachments = " " ) {
2012-09-17 00:53:17 +00:00
// Send
2014-09-23 17:02:00 +00:00
$email = new WC_Email ();
2015-07-16 15:56:34 +00:00
return $email -> send ( $to , $subject , $message , $headers , $attachments );
2012-09-17 00:53:17 +00:00
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* Prepare and send the customer invoice email on demand .
*/
2014-10-20 15:59:02 +00:00
public function customer_invoice ( $order ) {
2012-09-17 00:53:17 +00:00
$email = $this -> emails [ 'WC_Email_Customer_Invoice' ];
$email -> trigger ( $order );
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
/**
* Customer new account welcome email .
*
2013-06-04 15:33:05 +00:00
* @ param int $customer_id
* @ param array $new_customer_data
2012-09-17 00:53:17 +00:00
*/
2014-10-20 15:59:02 +00:00
public function customer_new_account ( $customer_id , $new_customer_data = array (), $password_generated = false ) {
if ( ! $customer_id ) {
2012-09-17 00:53:17 +00:00
return ;
2014-10-20 15:59:02 +00:00
}
2012-11-27 16:22:47 +00:00
2013-06-04 15:33:05 +00:00
$user_pass = ! empty ( $new_customer_data [ 'user_pass' ] ) ? $new_customer_data [ 'user_pass' ] : '' ;
2012-09-17 00:53:17 +00:00
$email = $this -> emails [ 'WC_Email_Customer_New_Account' ];
2013-06-04 15:33:05 +00:00
$email -> trigger ( $customer_id , $user_pass , $password_generated );
2012-11-27 16:22:47 +00:00
}
2012-09-17 00:53:17 +00:00
/**
* Add order meta to email templates .
2012-11-27 16:22:47 +00:00
*
2012-09-17 00:53:17 +00:00
* @ param mixed $order
* @ param bool $sent_to_admin ( default : false )
* @ param bool $plain_text ( default : false )
2014-11-13 00:53:36 +00:00
* @ return string
2012-09-17 00:53:17 +00:00
*/
2014-10-20 15:59:02 +00:00
public function order_meta ( $order , $sent_to_admin = false , $plain_text = false ) {
2014-11-13 00:53:36 +00:00
$fields = array ();
2012-09-17 00:53:17 +00:00
2014-10-20 15:59:02 +00:00
if ( $order -> customer_note ) {
2014-11-13 00:53:36 +00:00
$fields [ 'customer_note' ] = array (
'label' => __ ( 'Note' , 'woocommerce' ),
'value' => wptexturize ( $order -> customer_note )
2014-11-13 14:02:06 +00:00
);
2014-10-20 15:59:02 +00:00
}
2012-09-17 00:53:17 +00:00
2014-11-13 00:53:36 +00:00
$fields = apply_filters ( 'woocommerce_email_order_meta_fields' , $fields , $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 ) ) {
2013-08-22 11:25:17 +00:00
$key = $field ;
2014-11-13 00:53:36 +00:00
}
2013-08-22 11:25:17 +00:00
2014-11-13 00:53:36 +00:00
$fields [ $key ] = array (
'label' => wptexturize ( $key ),
'value' => wptexturize ( get_post_meta ( $order -> id , $field , true ) )
);
2012-09-17 00:53:17 +00:00
}
2014-10-20 15:59:02 +00:00
}
2012-09-17 00:53:17 +00:00
2014-11-13 00:53:36 +00:00
if ( $fields ) {
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
if ( $plain_text ) {
2012-11-27 16:22:47 +00:00
2014-11-13 00:53:36 +00:00
foreach ( $fields as $field ) {
if ( isset ( $field [ 'label' ] ) && isset ( $field [ 'value' ] ) && $field [ 'value' ] ) {
echo $field [ 'label' ] . ': ' . $field [ 'value' ] . " \n " ;
2014-04-15 15:01:20 +00:00
}
}
2012-11-27 16:22:47 +00:00
2012-09-17 00:53:17 +00:00
} else {
2014-11-13 00:53:36 +00:00
foreach ( $fields as $field ) {
if ( isset ( $field [ 'label' ] ) && isset ( $field [ 'value' ] ) && $field [ 'value' ] ) {
echo '<p><strong>' . $field [ 'label' ] . ':</strong> ' . $field [ 'value' ] . '</p>' ;
2014-04-15 15:01:20 +00:00
}
}
2012-09-17 00:53:17 +00:00
}
}
}
2014-10-08 21:47:50 +00:00
/**
* Add customer details to email templates .
*
* @ param mixed $order
* @ param bool $sent_to_admin ( default : false )
* @ param bool $plain_text ( default : false )
2014-11-13 00:53:36 +00:00
* @ return string
2014-10-08 21:47:50 +00:00
*/
2014-11-26 00:02:41 +00:00
public function customer_details ( $order , $sent_to_admin = false , $plain_text = false ) {
2014-11-13 00:53:36 +00:00
$fields = array ();
2014-10-08 21:47:50 +00:00
2014-11-13 00:33:47 +00:00
if ( $order -> billing_email ) {
2014-11-13 00:53:36 +00:00
$fields [ 'billing_email' ] = array (
2014-11-13 00:33:47 +00:00
'label' => __ ( 'Email' , 'woocommerce' ),
2014-11-12 19:44:11 +00:00
'value' => wptexturize ( $order -> billing_email )
);
2014-11-13 00:33:47 +00:00
}
2014-11-12 19:44:11 +00:00
if ( $order -> billing_phone ) {
2014-11-13 00:53:36 +00:00
$fields [ 'billing_phone' ] = array (
2014-11-13 00:33:47 +00:00
'label' => __ ( 'Tel' , 'woocommerce' ),
2014-11-12 19:44:11 +00:00
'value' => wptexturize ( $order -> billing_phone )
);
}
2014-10-08 21:47:50 +00:00
2014-11-13 00:53:36 +00:00
$fields = apply_filters ( 'woocommerce_email_customer_details_fields' , $fields , $sent_to_admin , $order );
2014-10-08 21:47:50 +00:00
2014-11-13 00:53:36 +00:00
if ( $fields ) {
2014-10-08 21:47:50 +00:00
$heading = $sent_to_admin ? __ ( 'Customer details' , 'woocommerce' ) : __ ( 'Your details' , 'woocommerce' );
2014-10-08 21:58:13 +00:00
$heading = apply_filters ( 'woocommerce_email_custom_details_header' , $heading , $sent_to_admin , $order );
2014-10-08 21:47:50 +00:00
if ( $plain_text ) {
2015-02-19 13:57:20 +00:00
echo strtoupper ( $heading ) . " \n \n " ;
2014-10-08 21:47:50 +00:00
2014-11-13 00:53:36 +00:00
foreach ( $fields as $field ) {
2014-11-12 19:44:11 +00:00
if ( isset ( $field [ 'label' ] ) && isset ( $field [ 'value' ] ) && $field [ 'value' ] ) {
echo $field [ 'label' ] . ': ' . $field [ 'value' ] . " \n " ;
2014-10-08 21:47:50 +00:00
}
}
} else {
echo '<h2>' . $heading . '</h2>' ;
2014-11-13 00:53:36 +00:00
foreach ( $fields as $field ) {
2014-11-12 19:44:11 +00:00
if ( isset ( $field [ 'label' ] ) && isset ( $field [ 'value' ] ) && $field [ 'value' ] ) {
2015-06-18 13:46:25 +00:00
echo '<p><strong>' . $field [ 'label' ] . ':</strong> <span class="text">' . $field [ 'value' ] . '</span></p>' ;
2014-10-08 21:47:50 +00:00
}
}
}
}
}
/**
* Get the email addresses .
*/
2014-11-26 00:02:41 +00:00
public function email_addresses ( $order , $sent_to_admin = false , $plain_text = false ) {
2015-02-19 13:57:20 +00:00
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 ) );
}
2014-11-13 00:33:47 +00:00
}
2014-11-12 23:32:15 +00:00
2014-10-20 15:59:02 +00:00
/**
* Get blog name formatted for emails
* @ return string
*/
private function get_blogname () {
return wp_specialchars_decode ( get_option ( 'blogname' ), ENT_QUOTES );
}
2012-09-17 00:53:17 +00:00
/**
* Low stock notification email .
*
2014-10-20 15:59:02 +00:00
* @ param WC_Product $product
2012-09-17 00:53:17 +00:00
*/
2014-10-20 15:59:02 +00:00
public function low_stock ( $product ) {
$subject = sprintf ( '[%s] %s' , $this -> get_blogname (), __ ( 'Product low in stock' , 'woocommerce' ) );
2015-08-31 11:29:34 +00:00
$message = sprintf ( __ ( '%s is low in stock.' , 'woocommerce' ), html_entity_decode ( strip_tags ( $product -> get_formatted_name () ), ENT_QUOTES , get_bloginfo ( 'charset' ) ) ) . ' ' . sprintf ( __ ( 'There are %d left' , 'woocommerce' ), html_entity_decode ( strip_tags ( $product -> get_total_stock () ) ) );
2014-10-20 15:59:02 +00:00
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 )
);
2012-09-17 00:53:17 +00:00
}
/**
* No stock notification email .
*
2014-10-20 15:59:02 +00:00
* @ param WC_Product $product
2012-09-17 00:53:17 +00:00
*/
2014-10-20 15:59:02 +00:00
public function no_stock ( $product ) {
$subject = sprintf ( '[%s] %s' , $this -> get_blogname (), __ ( 'Product out of stock' , 'woocommerce' ) );
2015-08-31 11:29:34 +00:00
$message = sprintf ( __ ( '%s is out of stock.' , 'woocommerce' ), html_entity_decode ( strip_tags ( $product -> get_formatted_name () ), ENT_QUOTES , get_bloginfo ( 'charset' ) ) );
2014-10-20 15:59:02 +00:00
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 )
);
2012-09-17 00:53:17 +00:00
}
/**
* Backorder notification email .
*
2014-10-20 15:59:02 +00:00
* @ param array $args
2012-09-17 00:53:17 +00:00
*/
2014-10-20 15:59:02 +00:00
public function backorder ( $args ) {
$args = wp_parse_args ( $args , array (
'product' => '' ,
2012-09-17 00:53:17 +00:00
'quantity' => '' ,
'order_id' => ''
2014-10-20 15:59:02 +00:00
) );
2012-09-17 00:53:17 +00:00
extract ( $args );
2014-10-20 15:59:02 +00:00
if ( ! $product || ! $quantity || ! ( $order = wc_get_order ( $order_id ) ) ) {
return ;
}
2012-09-17 00:53:17 +00:00
2014-10-20 15:59:02 +00:00
$subject = sprintf ( '[%s] %s' , $this -> get_blogname (), __ ( 'Product Backorder' , 'woocommerce' ) );
2015-08-31 11:29:34 +00:00
$message = sprintf ( __ ( '%s units of %s have been backordered in order #%s.' , 'woocommerce' ), $quantity , html_entity_decode ( strip_tags ( $product -> get_formatted_name () ), ENT_QUOTES , get_bloginfo ( 'charset' ) ), $order -> get_order_number () );
2012-09-17 00:53:17 +00:00
2014-10-20 15:59:02 +00:00
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 )
);
2012-09-17 00:53:17 +00:00
}
2013-05-27 14:20:01 +00:00
}