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
|
2012-12-03 19:19:58 +00:00
|
|
|
* @version 2.0.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
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
/**
|
|
|
|
* @var array Array of email notification classes.
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public $emails;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
/**
|
|
|
|
* @var string Stores the emailer's address.
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $_from_address;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var string Stores the emailer's name.
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $_from_name;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
/**
|
|
|
|
* @var mixed Content type for sent emails
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private $_content_type;
|
|
|
|
|
2013-09-12 13:41:02 +00:00
|
|
|
/**
|
2014-06-19 19:43:05 +00:00
|
|
|
* @var WC_Emails The single instance of the class
|
2013-09-12 13:41:02 +00:00
|
|
|
* @since 2.1
|
|
|
|
*/
|
|
|
|
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() {
|
|
|
|
if ( is_null( self::$_instance ) )
|
|
|
|
self::$_instance = new self();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
/**
|
|
|
|
* Constructor for the email class hooks in all emails that can be sent.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function __construct() {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
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 );
|
|
|
|
|
|
|
|
// 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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
2013-08-02 10:17:56 +00:00
|
|
|
$this->emails['WC_Email_New_Order'] = include( 'emails/class-wc-email-new-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_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
|
|
|
|
if ( ! class_exists( 'Emogrifier' ) ) {
|
|
|
|
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
|
|
|
* @access public
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
function get_emails() {
|
|
|
|
return $this->emails;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get from name for email.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_from_name() {
|
|
|
|
if ( ! $this->_from_name )
|
|
|
|
$this->_from_name = get_option( 'woocommerce_email_from_name' );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-03-18 13:37:07 +00:00
|
|
|
return wp_specialchars_decode( $this->_from_name );
|
2012-09-17 00:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get from email address.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_from_address() {
|
|
|
|
if ( ! $this->_from_address )
|
|
|
|
$this->_from_address = get_option( 'woocommerce_email_from_address' );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
return $this->_from_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the content type for the email.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function get_content_type() {
|
|
|
|
return $this->_content_type;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the email header.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $email_heading heading for the email
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
function wrap_message( $email_heading, $message, $plain_text = false ) {
|
|
|
|
// 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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $to
|
|
|
|
* @param mixed $subject
|
|
|
|
* @param mixed $message
|
|
|
|
* @param string $headers (default: "Content-Type: text/html\r\n")
|
|
|
|
* @param string $attachments (default: "")
|
|
|
|
* @param string $content_type (default: "text/html")
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "", $content_type = 'text/html' ) {
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
// Set content type
|
|
|
|
$this->_content_type = $content_type;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
// Send
|
2014-09-23 17:02:00 +00:00
|
|
|
$email = new WC_Email();
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function customer_invoice( $order ) {
|
|
|
|
$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.
|
|
|
|
*
|
|
|
|
* @access public
|
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
|
|
|
* @return void
|
|
|
|
*/
|
2013-06-04 15:33:05 +00:00
|
|
|
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;
|
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
|
|
|
* @access public
|
|
|
|
* @param mixed $order
|
|
|
|
* @param bool $sent_to_admin (default: false)
|
|
|
|
* @param bool $plain_text (default: false)
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function order_meta( $order, $sent_to_admin = false, $plain_text = false ) {
|
|
|
|
|
|
|
|
$meta = array();
|
2012-12-11 17:02:08 +00:00
|
|
|
$show_fields = apply_filters( 'woocommerce_email_order_meta_keys', array(), $sent_to_admin );
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
if ( $order->customer_note )
|
|
|
|
$meta[ __( 'Note', 'woocommerce' ) ] = wptexturize( $order->customer_note );
|
|
|
|
|
2012-11-27 16:22:47 +00:00
|
|
|
if ( $show_fields )
|
2013-08-22 11:25:17 +00:00
|
|
|
foreach ( $show_fields as $key => $field ) {
|
|
|
|
if ( is_numeric( $key ) )
|
|
|
|
$key = $field;
|
|
|
|
|
|
|
|
$meta[ wptexturize( $key ) ] = wptexturize( get_post_meta( $order->id, $field, true ) );
|
2012-09-17 00:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( sizeof( $meta ) > 0 ) {
|
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-04-15 15:01:20 +00:00
|
|
|
foreach ( $meta as $key => $value ) {
|
|
|
|
if ( $value ) {
|
|
|
|
echo $key . ': ' . $value . "\n";
|
|
|
|
}
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2012-09-17 00:53:17 +00:00
|
|
|
} else {
|
|
|
|
|
2014-04-15 15:01:20 +00:00
|
|
|
foreach ( $meta as $key => $value ) {
|
|
|
|
if ( $value ) {
|
|
|
|
echo '<p><strong>' . $key . ':</strong> ' . $value . '</p>';
|
|
|
|
}
|
|
|
|
}
|
2012-09-17 00:53:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Low stock notification email.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $product
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function low_stock( $product ) {
|
|
|
|
|
|
|
|
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
|
|
|
|
|
|
|
|
$subject = apply_filters( 'woocommerce_email_subject_low_stock', sprintf( '[%s] %s', $blogname, __( 'Product low in stock', 'woocommerce' ) ), $product );
|
|
|
|
|
|
|
|
$sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
|
|
|
|
|
|
|
|
if ( ! empty( $product->variation_id ) )
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
else
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
|
2012-10-16 09:45:33 +00:00
|
|
|
$message = $title . __( 'is low in stock.', 'woocommerce' );
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// CC, BCC, additional headers
|
|
|
|
$headers = apply_filters('woocommerce_email_headers', '', 'low_stock', $product);
|
|
|
|
|
|
|
|
// Attachments
|
2014-05-12 18:55:53 +00:00
|
|
|
$attachments = apply_filters('woocommerce_email_attachments', array(), 'low_stock', $product);
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// Send the mail
|
|
|
|
wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* No stock notification email.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $product
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function no_stock( $product ) {
|
|
|
|
|
|
|
|
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
|
|
|
|
|
|
|
|
$subject = apply_filters( 'woocommerce_email_subject_no_stock', sprintf( '[%s] %s', $blogname, __( 'Product out of stock', 'woocommerce' ) ), $product );
|
|
|
|
|
|
|
|
$sku = ($product->sku) ? '(' . $product->sku . ') ' : '';
|
|
|
|
|
|
|
|
if ( ! empty( $product->variation_id ) )
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . ' ' . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
else
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . ' ' . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
|
2012-10-16 09:45:33 +00:00
|
|
|
$message = $title . __( 'is out of stock.', 'woocommerce' );
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// CC, BCC, additional headers
|
|
|
|
$headers = apply_filters('woocommerce_email_headers', '', 'no_stock', $product);
|
|
|
|
|
|
|
|
// Attachments
|
2014-05-12 18:55:53 +00:00
|
|
|
$attachments = apply_filters('woocommerce_email_attachments', array(), 'no_stock', $product);
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// Send the mail
|
|
|
|
wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Backorder notification email.
|
|
|
|
*
|
|
|
|
* @access public
|
|
|
|
* @param mixed $args
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
function backorder( $args ) {
|
|
|
|
|
|
|
|
$defaults = array(
|
|
|
|
'product' => '',
|
|
|
|
'quantity' => '',
|
|
|
|
'order_id' => ''
|
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $defaults );
|
|
|
|
|
|
|
|
extract( $args );
|
|
|
|
|
|
|
|
if (!$product || !$quantity) return;
|
|
|
|
|
|
|
|
$blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
|
|
|
|
|
|
|
|
$subject = apply_filters( 'woocommerce_email_subject_backorder', sprintf( '[%s] %s', $blogname, __( 'Product Backorder', 'woocommerce' ) ), $product );
|
|
|
|
|
|
|
|
$sku = ($product->sku) ? ' (' . $product->sku . ')' : '';
|
|
|
|
|
|
|
|
if ( ! empty( $product->variation_id ) )
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Variation #%s of %s', 'woocommerce' ), $product->variation_id, get_the_title($product->id)) . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
else
|
2012-10-16 09:45:33 +00:00
|
|
|
$title = sprintf(__( 'Product #%s - %s', 'woocommerce' ), $product->id, get_the_title($product->id)) . $sku;
|
2012-09-17 00:53:17 +00:00
|
|
|
|
2014-08-15 12:29:21 +00:00
|
|
|
$order = wc_get_order( $order_id );
|
2013-05-27 14:20:01 +00:00
|
|
|
$message = sprintf(__( '%s units of %s have been backordered in order %s.', 'woocommerce' ), $quantity, $title, $order->get_order_number() );
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// CC, BCC, additional headers
|
|
|
|
$headers = apply_filters('woocommerce_email_headers', '', 'backorder', $args);
|
|
|
|
|
|
|
|
// Attachments
|
2014-05-12 18:55:53 +00:00
|
|
|
$attachments = apply_filters('woocommerce_email_attachments', array(), 'backorder', $args);
|
2012-09-17 00:53:17 +00:00
|
|
|
|
|
|
|
// Send the mail
|
|
|
|
wp_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message, $headers, $attachments );
|
|
|
|
}
|
|
|
|
|
2013-05-27 14:20:01 +00:00
|
|
|
}
|