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 = '

WooCommerce sit amet

'; + + $message.= wpautop('Ut ut est qui euismod parum. Dolor veniam tation nihil assum mazim. Possim fiant habent decima et claritatem. Erat me usus gothica laoreet consequat. Clari facer litterarum aliquam insitam dolor. + +Gothica minim lectores demonstraverunt ut soluta. Sequitur quam exerci veniam aliquip litterarum. Lius videntur nisl facilisis claritatem nunc. Praesent in iusto me tincidunt iusto. Dolore lectores sed putamus exerci est. '); + + echo $mailer->wrap_message( $email_heading, $message ); + + exit; + + endif; } \ No newline at end of file diff --git a/admin/writepanels/writepanel-order_data.php b/admin/writepanels/writepanel-order_data.php index 88e4e0d176d..b43d57b01e2 100644 --- a/admin/writepanels/writepanel-order_data.php +++ b/admin/writepanels/writepanel-order_data.php @@ -548,14 +548,14 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) { $order->add_order_note( sprintf( __('Item #%s stock reduced from %s to %s.', 'woothemes'), $order_item['id'], $old_stock, $new_quantity) ); if ($new_quantity<0) : - do_action('woocommerce_product_on_backorder_notification', $order_item['id'], $values['quantity']); + do_action('woocommerce_product_on_backorder', array( 'product' => $order_item['id'], 'quantity' => $values['quantity'])); endif; // stock status notifications if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount')>=$new_quantity) : - do_action('woocommerce_no_stock_notification', $order_item['id']); + do_action('woocommerce_no_stock', $order_item['id']); elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount')>=$new_quantity) : - do_action('woocommerce_low_stock_notification', $order_item['id']); + do_action('woocommerce_low_stock', $order_item['id']); endif; endif; @@ -603,7 +603,9 @@ function woocommerce_process_shop_order_meta( $post_id, $post ) { elseif (isset($_POST['invoice']) && $_POST['invoice']) : // Mail link to customer - woocommerce_pay_for_order_customer_notification( $order ); + global $woocommerce; + $mailer = $woocommerce->mailer(); + $mailer->pay_for_order_customer_notification( $order ); endif; diff --git a/admin/writepanels/writepanels-init.php b/admin/writepanels/writepanels-init.php index c480c668c24..9deaa65af25 100644 --- a/admin/writepanels/writepanels-init.php +++ b/admin/writepanels/writepanels-init.php @@ -72,6 +72,8 @@ function woocommerce_meta_boxes_save( $post_id, $post ) { global $wpdb; if ( !$_POST ) return $post_id; + if ( is_int( wp_is_post_revision( $post_id ) ) ) return; + if( is_int( wp_is_post_autosave( $post_id ) ) ) return; if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return $post_id; if ( !isset($_POST['woocommerce_meta_nonce']) || (isset($_POST['woocommerce_meta_nonce']) && !wp_verify_nonce( $_POST['woocommerce_meta_nonce'], 'woocommerce_save_data' ))) return $post_id; if ( !current_user_can( 'edit_post', $post_id )) return $post_id; diff --git a/classes/checkout.class.php b/classes/checkout.class.php index d3dea448c43..5213e559c0a 100644 --- a/classes/checkout.class.php +++ b/classes/checkout.class.php @@ -567,8 +567,8 @@ class woocommerce_checkout { wp_update_user( array ('ID' => $user_id, 'role' => 'customer') ) ; // send the user a confirmation and their login details - woocommerce_customer_new_account( $user_id, $user_pass ); - //wp_new_user_notification( $user_id, $user_pass ); + $mailer = $woocommerce->mailer(); + $mailer->customer_new_account( $user_id, $password ); // set the WP login cookie $secure_cookie = is_ssl() ? true : false; diff --git a/classes/order.class.php b/classes/order.class.php index dd1c0364fce..28cf57fe8fc 100644 --- a/classes/order.class.php +++ b/classes/order.class.php @@ -469,7 +469,7 @@ class woocommerce_order { add_comment_meta($comment_id, 'is_customer_note', $is_customer_note); if ($is_customer_note) : - do_action( 'woocommerce_new_customer_note', $this->id, $note ); + do_action( 'woocommerce_new_customer_note', array( 'order_id' => $this->id, 'customer_note' => $note ) ); endif; return $comment_id; @@ -620,14 +620,14 @@ class woocommerce_order { $this->add_order_note( sprintf( __('Item #%s stock reduced from %s to %s.', 'woothemes'), $item['id'], $old_stock, $new_quantity) ); if ($new_quantity<0) : - do_action('woocommerce_product_on_backorder_notification', $item['id'], $item['qty']); + do_action('woocommerce_product_on_backorder', array( 'product' => $order_item['id'], 'quantity' => $values['quantity'])); endif; // stock status notifications if (get_option('woocommerce_notify_no_stock_amount') && get_option('woocommerce_notify_no_stock_amount')>=$new_quantity) : - do_action('woocommerce_no_stock_notification', $item['id']); + do_action('woocommerce_no_stock', $item['id']); elseif (get_option('woocommerce_notify_low_stock_amount') && get_option('woocommerce_notify_low_stock_amount')>=$new_quantity) : - do_action('woocommerce_low_stock_notification', $item['id']); + do_action('woocommerce_low_stock', $item['id']); endif; endif; diff --git a/classes/woocommerce_email.class.php b/classes/woocommerce_email.class.php new file mode 100644 index 00000000000..0393b24ff05 --- /dev/null +++ b/classes/woocommerce_email.class.php @@ -0,0 +1,373 @@ +_from_name = get_option('woocommerce_email_from_name'); + $this->_from_address = get_option('woocommerce_email_from_address'); + + /** + * Email Header + Footer + **/ + add_action('woocommerce_email_header', array(&$this, 'email_header')); + add_action('woocommerce_email_footer', array(&$this, 'email_footer')); + + /** + * Add order meta to email templates + **/ + add_action('woocommerce_email_after_order_table', array(&$this, 'order_meta'), 10, 2); + + /** + * 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_order_status_pending_to_processing_notification', array(&$this, 'new_order')); + add_action('woocommerce_order_status_pending_to_completed_notification', array(&$this, 'new_order')); + add_action('woocommerce_order_status_pending_to_on-hold_notification', array(&$this, 'new_order')); + add_action('woocommerce_order_status_failed_to_processing_notification', array(&$this, 'new_order')); + add_action('woocommerce_order_status_failed_to_completed_notification', array(&$this, 'new_order')); + + add_action('woocommerce_order_status_pending_to_processing_notification', array(&$this, 'customer_processing_order')); + add_action('woocommerce_order_status_pending_to_on-hold_notification', array(&$this, 'customer_processing_order')); + + add_action('woocommerce_order_status_completed_notification', array(&$this, 'customer_completed_order')); + + add_action('woocommerce_new_customer_note_notification', array(&$this, 'customer_note')); + + // Let 3rd parties unhook the above via this hook + do_action( 'woocommerce_email', $this ); + } + + function get_from_name() { + return $this->_from_name; + } + + function get_from_address() { + return $this->_from_address; + } + + function get_content_type() { + return 'text/html'; + } + + function email_header() { + woocommerce_get_template('emails/email_header.php', false); + } + + function email_footer() { + woocommerce_get_template('emails/email_footer.php', false); + } + + /** + * Wraps a message in the woocommerce mail template + **/ + function wrap_message( $email_heading, $message ) { + // Buffer + ob_start(); + + do_action('woocommerce_email_header'); + + echo wpautop(wptexturize( $message )); + + do_action('woocommerce_email_footer'); + + // Get contents + $message = ob_get_clean(); + + return $message; + } + + function send( $to, $subject, $message, $headers = "Content-Type: text/html\r\n", $attachments = "" ) { + add_filter( 'wp_mail_from', array(&$this, 'get_from_address') ); + add_filter( 'wp_mail_from_name', array(&$this, 'get_from_name') ); + add_filter( 'wp_mail_content_type', array(&$this, 'get_content_type') ); + + // Send the mail + wp_mail( $to, $subject, $message, $headers, $attachments ); + + // Unhook + remove_filter( 'wp_mail_from', array(&$this, 'get_from_address') ); + remove_filter( 'wp_mail_from_name', array(&$this, 'get_from_name') ); + remove_filter( 'wp_mail_content_type', array(&$this, 'get_content_type') ); + } + + /** + * New order + **/ + function new_order( $order_id ) { + global $order, $email_heading; + + $email_heading = __('New Customer Order', 'woothemes'); + + $subject = sprintf(__('[%s] New Customer Order (# %s)', 'woothemes'), get_bloginfo('name'), $order_id); + + // Buffer + ob_start(); + + // Get mail template + woocommerce_get_template('emails/new_order.php', false); + + // Get contents + $message = ob_get_clean(); + + // Send the mail + $this->send( get_option('woocommerce_new_order_email_recipient'), $subject, $message ); + } + + /** + * Processing Order + **/ + function customer_processing_order( $order_id ) { + global $order, $email_heading; + + $order = &new woocommerce_order( $order_id ); + + $email_heading = __('Order Received', 'woothemes'); + + $subject = sprintf(__('[%s] Order Received', 'woothemes'), get_bloginfo('name')); + + // Buffer + ob_start(); + + // Get mail template + woocommerce_get_template('emails/customer_processing_order.php', false); + + // Get contents + $message = ob_get_clean(); + + // Send the mail + $this->send( $order->billing_email, $subject, $message ); + } + + /** + * Completed Order + **/ + function customer_completed_order( $order_id ) { + global $order, $email_heading; + + $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 + $this->send( $order->billing_email, $subject, $message ); + } + + /** + * Pay for order + **/ + function pay_for_order_customer_notification( $pay_for_order ) { + global $order, $email_heading; + + $order = $pay_for_order; + + $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 + $this->send( $order->billing_email, $subject, $message ); + } + + /** + * Customer notes + **/ + function customer_note( $args ) { + global $order, $email_heading, $customer_note; + + $defaults = array( + 'order_id' => '', + 'customer_note' => '' + ); + + $args = wp_parse_args( $args, $defaults ); + + extract( $args, EXTR_SKIP ); + + if (!$order_id || !$customer_note) return; + + $order = &new woocommerce_order( $order_id ); + + $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 + $this->send( $order->billing_email, $subject, $message ); + } + + /** + * Low stock notification email + **/ + function low_stock( $product ) { + $_product = &new woocommerce_product($product); + + $subject = '[' . get_bloginfo('name') . '] ' . __('Product low in stock', 'woothemes'); + + $message = $this->wrap_message( + __('Product low in stock', 'woothemes'), + '#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is low in stock.', 'woothemes') + ); + + // Send the mail + $this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message ); + } + + /** + * No stock notification email + **/ + function no_stock( $product ) { + $_product = &new woocommerce_product($product); + + $subject = '[' . get_bloginfo('name') . '] ' . __('Product out of stock', 'woothemes'); + + $message = $this->wrap_message( + __('Product out of stock', 'woothemes'), + '#' . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('is out of stock.', 'woothemes') + ); + + // Send the mail + $this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message ); + } + + + /** + * Backorder notification email + **/ + function backorder( $args ) { + + $defaults = array( + 'product' => '', + 'quantity' => '' + ); + + $args = wp_parse_args( $args, $defaults ); + + extract( $args, EXTR_SKIP ); + + if (!$product || !$quantity) return; + + $_product = &new woocommerce_product($product); + + $subject = '[' . get_bloginfo('name') . '] ' . __('Product Backorder', 'woothemes'); + + $message = $this->wrap_message( + __('Product Backorder', 'woothemes'), + $quantity . __(' units of #', 'woothemes') . $_product->id .' '. $_product->get_title() . ' ('. $_product->sku.') ' . __('have been backordered.', 'woothemes') + ); + + // Send the mail + $this->send( get_option('woocommerce_stock_email_recipient'), $subject, $message ); + } + + /** + * Add order meta to email templates + **/ + function order_meta( $order, $sent_to_admin ) { + + $meta = array(); + $show_fields = apply_filters('woocommerce_email_order_meta_keys', array('coupons'), $sent_to_admin); + + if ($order->customer_note) : + $meta[__('Note:', 'woothemes')] = wptexturize($order->customer_note); + endif; + + if ($show_fields) foreach ($show_fields as $field) : + + $value = get_post_meta( $order->id, $field, true ); + if ($value) $meta[ucwords(esc_attr($field))] = wptexturize($value); + + endforeach; + + if (sizeof($meta)>0) : + echo '

'.__('Order information', 'woothemes').'

'; + foreach ($meta as $key=>$value) : + echo '

'.$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 '

WooCommerce sit amet

'; - - echo wpautop('Ut ut est qui euismod parum. Dolor veniam tation nihil assum mazim. Possim fiant habent decima et claritatem. Erat me usus gothica laoreet consequat. Clari facer litterarum aliquam insitam dolor. - -Gothica minim lectores demonstraverunt ut soluta. Sequitur quam exerci veniam aliquip litterarum. Lius videntur nisl facilisis claritatem nunc. Praesent in iusto me tincidunt iusto. Dolore lectores sed putamus exerci est. '); - - do_action('woocommerce_email_footer'); - - exit; - - endif; -} - -/** - * Add order meta to email templates - **/ -add_action('woocommerce_email_after_order_table', 'woocommerce_email_order_meta', 10, 2); - -function woocommerce_email_order_meta( $order, $sent_to_admin ) { - - $meta = array(); - $show_fields = apply_filters('woocommerce_email_order_meta_keys', array('coupons'), $sent_to_admin); - - if ($order->customer_note) : - $meta[__('Note:', 'woothemes')] = wptexturize($order->customer_note); - endif; - - if ($show_fields) foreach ($show_fields as $field) : - - $value = get_post_meta( $order->id, $field, true ); - if ($value) $meta[ucwords(esc_attr($field))] = wptexturize($value); - - endforeach; - - if (sizeof($meta)>0) : - echo '

'.__('Order information', 'woothemes').'

'; - foreach ($meta as $key=>$value) : - 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 *