diff --git a/admin/admin-init.php b/admin/admin-init.php index dd78652d34f..ef4f163b340 100644 --- a/admin/admin-init.php +++ b/admin/admin-init.php @@ -524,4 +524,33 @@ function woocommerce_add_shortcode_tinymce_plugin($plugin_array) { function woocommerce_refresh_mce($ver) { $ver += 3; return $ver; +} + +/** + * Preview Emails + **/ +add_action('admin_init', 'woocommerce_preview_emails'); + +function woocommerce_preview_emails() { + if (isset($_GET['preview_woocommerce_mail'])) : + $nonce = $_REQUEST['_wpnonce']; + if (!wp_verify_nonce($nonce, 'preview-mail') ) die('Security check'); + + global $woocommerce; + + $mailer = $woocommerce->mailer(); + + $email_heading = __('Email preview', 'woothemes'); + + $message = '
'.$key.': '.$value.'
'; + endforeach; + endif; + } + + /** + * Customer new account welcome email + **/ + function customer_new_account( $user_id, $plaintext_pass ) { + global $user_login, $user_pass, $blogname; + + if (!$user_id || !$plaintext_pass) return; + + $user = new WP_User($user_id); + + $user_login = stripslashes($user->user_login); + $user_email = stripslashes($user->user_email); + $user_pass = $plaintext_pass; + + $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); + + $subject = sprintf(__('Your account on %s', 'woothemes'), $blogname); + $email_heading = __('Your account details', 'woothemes'); + + // Buffer + ob_start(); + + // Get mail template + woocommerce_get_template('emails/customer_new_account.php', false); + + // Get contents + $message = ob_get_clean(); + + // Send the mail + $this->send( $user_email, $subject, $message ); + } + +} \ No newline at end of file diff --git a/templates/emails/customer_completed_order.php b/templates/emails/customer_completed_order.php index 22c82128c31..d131c42088d 100644 --- a/templates/emails/customer_completed_order.php +++ b/templates/emails/customer_completed_order.php @@ -1,6 +1,6 @@ - + diff --git a/templates/emails/customer_note_notification.php b/templates/emails/customer_note_notification.php index 65ce6d880f7..9a4f7b384f2 100644 --- a/templates/emails/customer_note_notification.php +++ b/templates/emails/customer_note_notification.php @@ -1,6 +1,6 @@ - + diff --git a/templates/emails/customer_pay_for_order.php b/templates/emails/customer_pay_for_order.php index e4332f2b606..8ce50f92077 100644 --- a/templates/emails/customer_pay_for_order.php +++ b/templates/emails/customer_pay_for_order.php @@ -1,6 +1,6 @@ - + diff --git a/templates/emails/customer_processing_order.php b/templates/emails/customer_processing_order.php index 3bc8a095762..a2dc0a5b451 100644 --- a/templates/emails/customer_processing_order.php +++ b/templates/emails/customer_processing_order.php @@ -1,6 +1,6 @@ - + diff --git a/woocommerce.php b/woocommerce.php index 80f3adea8dd..b3c3d0c7362 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -48,6 +48,7 @@ class woocommerce { var $payment_gateways; var $countries; var $validation; + var $woocommerce_email; /** Taxonomies ************************************************************/ @@ -98,6 +99,11 @@ class woocommerce { add_action( 'plugins_loaded', array( &$this->shipping, 'init' ), 1); // Load shipping methods - some more may be added by plugins add_action( 'plugins_loaded', array( &$this->payment_gateways, 'init' ), 1); // Load payment methods - some more may be added by plugins + // Email Actions + $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_on-hold', 'woocommerce_order_status_failed_to_processing', 'woocommerce_order_status_failed_to_completed', 'woocommerce_order_status_pending_to_processing', 'woocommerce_order_status_pending_to_on-hold', 'woocommerce_order_status_completed', 'woocommerce_new_customer_note' ); + + foreach ($email_actions as $action) add_action($action, array( &$this, 'send_transactional_email')); + // Actions for SSL if (!is_admin()) : add_action( 'wp', array( &$this, 'ssl_redirect')); @@ -151,7 +157,6 @@ class woocommerce { include( 'woocommerce_functions.php' ); // Contains core functions for the front/back end include( 'widgets/widgets-init.php' ); // Widget classes - include( 'woocommerce_emails.php' ); // Email template handlers include( 'classes/countries.class.php' ); // Defines countries and states include( 'classes/order.class.php' ); // Single order class include( 'classes/product.class.php' ); // Product class @@ -808,7 +813,7 @@ class woocommerce { /** Load Instances on demand **********************************************/ /** - * Checkout Class + * Get Checkout Class */ function checkout() { if ( !class_exists('woocommerce_checkout') ) include( 'classes/checkout.class.php' ); @@ -816,12 +821,31 @@ class woocommerce { } /** - * Logging Class + * Get Logging Class */ function logger() { if ( !class_exists('woocommerce_logger') ) include( 'classes/woocommerce_logger.class.php' ); return new woocommerce_logger(); } + + /** + * Email Class + */ + function send_transactional_email( $args = array() ) { + $this->mailer(); + + do_action( current_filter() . '_notification' , $args ); + } + + function mailer() { + // Init mail class + if ( !class_exists('woocommerce_email') ) : + include( 'classes/woocommerce_email.class.php' ); + $this->woocommerce_email = &new woocommerce_email(); + endif; + + return $this->woocommerce_email; + } /** Helper functions ******************************************************/ diff --git a/woocommerce_actions.php b/woocommerce_actions.php index 0c5aae4adc4..b586a7ecc43 100644 --- a/woocommerce_actions.php +++ b/woocommerce_actions.php @@ -486,9 +486,10 @@ function woocommerce_process_registration() { // Change role wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ; - - // send the user a confirmation and their login details - woocommerce_customer_new_account( $user_id, $password ); + + // send the user a confirmation and their login details + $mailer = $woocommerce->mailer(); + $mailer->customer_new_account( $user_id, $password ); // set the WP login cookie $secure_cookie = is_ssl() ? true : false; diff --git a/woocommerce_emails.php b/woocommerce_emails.php deleted file mode 100644 index 60813a7691f..00000000000 --- a/woocommerce_emails.php +++ /dev/null @@ -1,465 +0,0 @@ -billing_email, $subject, $message ); -} - - -/** - * Completed order notification email template - this one includes download links for downloadable products - **/ -add_action('woocommerce_order_status_completed', 'woocommerce_completed_order_customer_notification'); - -function woocommerce_completed_order_customer_notification( $id ) { - - global $order_id, $email_heading; - - $order_id = $id; - - $order = &new woocommerce_order( $order_id ); - - if ($order->has_downloadable_item()) : - $subject = __('[%s] Order Complete/Download Links', 'woothemes'); - $email_heading = __('Order Complete/Download Links', 'woothemes'); - else : - $subject = __('[%s] Order Complete', 'woothemes'); - $email_heading = __('Order Complete', 'woothemes'); - endif; - - $email_heading = apply_filters('woocommerce_completed_order_customer_notification_subject', $email_heading); - - $subject = sprintf($subject, get_bloginfo('name')); - - // Buffer - ob_start(); - - // Get mail template - woocommerce_get_template('emails/customer_completed_order.php', false); - - // Get contents - $message = ob_get_clean(); - - // Send the mail - woocommerce_mail( $order->billing_email, $subject, $message ); -} - - -/** - * Pay for order notification email template - this one includes a payment link - **/ -function woocommerce_pay_for_order_customer_notification( $the_order ) { - - global $order_id, $order, $email_heading; - - $order = $the_order; - $order_id = $order->id; - - $email_heading = sprintf(__('Invoice for Order #%s', 'woothemes'), $order_id); - - $subject = sprintf(__('[%s] Pay for Order', 'woothemes'), get_bloginfo('name')); - - // Buffer - ob_start(); - - // Get mail template - woocommerce_get_template('emails/customer_pay_for_order.php', false); - - // Get contents - $message = ob_get_clean(); - - // Send the mail - woocommerce_mail( $order->billing_email, $subject, $message ); -} - -/** - * Customer note notification - **/ -add_action('woocommerce_new_customer_note', 'woocommerce_customer_note_notification', 10, 2); - -function woocommerce_customer_note_notification( $id, $note ) { - - global $order_id, $email_heading, $customer_note; - - $order_id = $id; - $customer_note = $note; - - $order = &new woocommerce_order( $order_id ); - - if (!$customer_note) return; - - $email_heading = __('A note has been added to your order', 'woothemes'); - - $subject = sprintf(__('[%s] A note has been added to your order', 'woothemes'), get_bloginfo('name')); - - // Buffer - ob_start(); - - // Get mail template - woocommerce_get_template('emails/customer_note_notification.php', false); - - // Get contents - $message = ob_get_clean(); - - // Send the mail - woocommerce_mail( $order->billing_email, $subject, $message ); -} - - -/** - * Low stock notification email - **/ -function woocommerce_low_stock_notification( $product ) { - $_product = &new woocommerce_product($product); - - $subject = '[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'woothemes'); - - $message = woocommerce_mail_template( - __('Product low in stock', 'woothemes'), - '#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is low in stock.', 'woothemes') - ); - - // Send the mail - woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message ); -} - -/** - * No stock notification email - **/ -function woocommerce_no_stock_notification( $product ) { - $_product = &new woocommerce_product($product); - - $subject = '[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'woothemes'); - - $message = woocommerce_mail_template( - __('Product out of stock', 'woothemes'), - '#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is out of stock.', 'woothemes') - ); - - // Send the mail - woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message ); -} - - -/** - * Backorder notification email - **/ -function woocommerce_product_on_backorder_notification( $product, $amount ) { - $_product = &new woocommerce_product($product); - - $subject = '[' . get_bloginfo('name') . '] ' . __('Product Backorder', 'woothemes'); - - $message = woocommerce_mail_template( - __('Product Backorder', 'woothemes'), - $amount . __(' units of #', 'woothemes') . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('have been backordered.', 'woothemes') - ); - - // Send the mail - woocommerce_mail( get_option('woocommerce_stock_email_recipient'), $subject, $message ); -} - -/** - * Preview Emails - **/ -add_action('admin_init', 'woocommerce_preview_emails'); - -function woocommerce_preview_emails() { - if (isset($_GET['preview_woocommerce_mail'])) : - $nonce = $_REQUEST['_wpnonce']; - if (!wp_verify_nonce($nonce, 'preview-mail') ) die('Security check'); - - global $email_heading; - - $email_heading = __('Email preview', 'woothemes'); - - do_action('woocommerce_email_header'); - - echo ''.$key.': '.$value.'
'; - endforeach; - endif; -} - - -/** - * Customer new account welcome email - **/ -function woocommerce_customer_new_account( $user_id, $plaintext_pass ) { - global $email_heading, $user_login, $user_pass, $blogname; - - if ( empty($plaintext_pass) ) return; - - $user = new WP_User($user_id); - - $user_login = stripslashes($user->user_login); - $user_email = stripslashes($user->user_email); - $user_pass = $plaintext_pass; - - $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); - - $subject = sprintf(__('Your account on %s', 'woothemes'), $blogname); - $email_heading = __('Your account details', 'woothemes'); - - // Buffer - ob_start(); - - // Get mail template - woocommerce_get_template('emails/customer_new_account.php', false); - - // Get contents - $message = ob_get_clean(); - - // Send the mail - woocommerce_mail( $user_email, $subject, $message ); -} - -/** - * Hex darker/lighter/contrast functions for colours - **/ -if (!function_exists('woocommerce_hex_darker')) { - function woocommerce_hex_darker( $color, $factor = 30 ) { - $color = str_replace('#', '', $color); - - $base['R'] = hexdec($color{0}.$color{1}); - $base['G'] = hexdec($color{2}.$color{3}); - $base['B'] = hexdec($color{4}.$color{5}); - - $color = '#'; - - foreach ($base as $k => $v) : - $amount = $v / 100; - $amount = round($amount * $factor); - $new_decimal = $v - $amount; - - $new_hex_component = dechex($new_decimal); - if(strlen($new_hex_component) < 2) : - $new_hex_component = "0".$new_hex_component; - endif; - $color .= $new_hex_component; - endforeach; - - return $color; - } -} -if (!function_exists('woocommerce_hex_lighter')) { - function woocommerce_hex_lighter( $color, $factor = 30 ) { - $color = str_replace('#', '', $color); - - $base['R'] = hexdec($color{0}.$color{1}); - $base['G'] = hexdec($color{2}.$color{3}); - $base['B'] = hexdec($color{4}.$color{5}); - - $color = '#'; - - foreach ($base as $k => $v) : - $amount = 255 - $v; - $amount = $amount / 100; - $amount = round($amount * $factor); - $new_decimal = $v + $amount; - - $new_hex_component = dechex($new_decimal); - if(strlen($new_hex_component) < 2) : - $new_hex_component = "0".$new_hex_component; - endif; - $color .= $new_hex_component; - endforeach; - - return $color; - } -} -if (!function_exists('woocommerce_light_or_dark')) { - function woocommerce_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { - return (hexdec($color) > 0xffffff/2) ? $dark : $light; - } -} \ No newline at end of file diff --git a/woocommerce_functions.php b/woocommerce_functions.php index 03daa842c08..2066123909f 100644 --- a/woocommerce_functions.php +++ b/woocommerce_functions.php @@ -9,6 +9,17 @@ * @author WooThemes */ +/** + * HTML emails from WooCommerce + **/ +function woocommerce_mail( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) { + global $woocommerce; + + $mailer = $woocommerce->mailer(); + + $mailer->send( $to, $subject, $message, $headers, $attachments ); +} + /** * WooCommerce conditionals * @@ -219,6 +230,66 @@ function woocommerce_get_formatted_variation( $variation = '', $flat = false ) { endif; } +/** + * Hex darker/lighter/contrast functions for colours + **/ +if (!function_exists('woocommerce_hex_darker')) { + function woocommerce_hex_darker( $color, $factor = 30 ) { + $color = str_replace('#', '', $color); + + $base['R'] = hexdec($color{0}.$color{1}); + $base['G'] = hexdec($color{2}.$color{3}); + $base['B'] = hexdec($color{4}.$color{5}); + + $color = '#'; + + foreach ($base as $k => $v) : + $amount = $v / 100; + $amount = round($amount * $factor); + $new_decimal = $v - $amount; + + $new_hex_component = dechex($new_decimal); + if(strlen($new_hex_component) < 2) : + $new_hex_component = "0".$new_hex_component; + endif; + $color .= $new_hex_component; + endforeach; + + return $color; + } +} +if (!function_exists('woocommerce_hex_lighter')) { + function woocommerce_hex_lighter( $color, $factor = 30 ) { + $color = str_replace('#', '', $color); + + $base['R'] = hexdec($color{0}.$color{1}); + $base['G'] = hexdec($color{2}.$color{3}); + $base['B'] = hexdec($color{4}.$color{5}); + + $color = '#'; + + foreach ($base as $k => $v) : + $amount = 255 - $v; + $amount = $amount / 100; + $amount = round($amount * $factor); + $new_decimal = $v + $amount; + + $new_hex_component = dechex($new_decimal); + if(strlen($new_hex_component) < 2) : + $new_hex_component = "0".$new_hex_component; + endif; + $color .= $new_hex_component; + endforeach; + + return $color; + } +} +if (!function_exists('woocommerce_light_or_dark')) { + function woocommerce_light_or_dark( $color, $dark = '#000000', $light = '#FFFFFF' ) { + return (hexdec($color) > 0xffffff/2) ? $dark : $light; + } +} + /** * Exclude order comments from queries and RSS *