From adc679633bdb627bbebab5c0f537e5dad856ca4e Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 2 Jun 2017 18:25:05 +0100 Subject: [PATCH 1/4] Handle default subjects and headings at time of sending, not in constructor --- .../emails/class-wc-email-cancelled-order.php | 28 +++- ...lass-wc-email-customer-completed-order.php | 60 ++------- .../class-wc-email-customer-invoice.php | 108 +++++++++------ .../class-wc-email-customer-new-account.php | 26 ++-- .../emails/class-wc-email-customer-note.php | 28 ++-- .../class-wc-email-customer-on-hold-order.php | 26 ++-- ...ass-wc-email-customer-processing-order.php | 26 ++-- ...class-wc-email-customer-refunded-order.php | 125 ++++++++++-------- ...class-wc-email-customer-reset-password.php | 27 ++-- .../emails/class-wc-email-failed-order.php | 28 +++- includes/emails/class-wc-email-new-order.php | 28 +++- includes/emails/class-wc-email.php | 62 ++++++--- 12 files changed, 349 insertions(+), 223 deletions(-) diff --git a/includes/emails/class-wc-email-cancelled-order.php b/includes/emails/class-wc-email-cancelled-order.php index d796fd14d76..d804f776b0c 100644 --- a/includes/emails/class-wc-email-cancelled-order.php +++ b/includes/emails/class-wc-email-cancelled-order.php @@ -26,8 +26,6 @@ class WC_Email_Cancelled_Order extends WC_Email { $this->id = 'cancelled_order'; $this->title = __( 'Cancelled order', 'woocommerce' ); $this->description = __( 'Cancelled order emails are sent to chosen recipient(s) when orders have been marked cancelled (if they were previously processing or on-hold).', 'woocommerce' ); - $this->heading = __( 'Cancelled order', 'woocommerce' ); - $this->subject = __( '[{site_title}] Cancelled order ({order_number})', 'woocommerce' ); $this->template_html = 'emails/admin-cancelled-order.php'; $this->template_plain = 'emails/plain/admin-cancelled-order.php'; @@ -42,6 +40,26 @@ class WC_Email_Cancelled_Order extends WC_Email { $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); } + /** + * Get email subject. + * + * @since 3.1.0 + * @return string + */ + public function get_default_subject() { + return __( '[{site_title}] Cancelled order ({order_number})', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Cancelled order', 'woocommerce' ); + } + /** * Trigger the sending of this email. * @@ -65,7 +83,9 @@ class WC_Email_Cancelled_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** @@ -123,7 +143,7 @@ class WC_Email_Cancelled_Order extends WC_Email { 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -132,7 +152,7 @@ class WC_Email_Cancelled_Order extends WC_Email { 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->heading . '' ), + 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, diff --git a/includes/emails/class-wc-email-customer-completed-order.php b/includes/emails/class-wc-email-customer-completed-order.php index 55262c886cd..b41975f7238 100644 --- a/includes/emails/class-wc-email-customer-completed-order.php +++ b/includes/emails/class-wc-email-customer-completed-order.php @@ -33,29 +33,13 @@ class WC_Email_Customer_Completed_Order extends WC_Email { $this->template_html = 'emails/customer-completed-order.php'; $this->template_plain = 'emails/plain/customer-completed-order.php'; - $this->set_email_strings(); - // Triggers for this email add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ), 10, 2 ); - // Other settings - $this->heading_downloadable = $this->get_option( 'heading_downloadable', __( 'Your order is complete - download your files', 'woocommerce' ) ); - $this->subject_downloadable = $this->get_option( 'subject_downloadable', __( 'Your {site_title} order from {order_date} is complete - download your files', 'woocommerce' ) ); - // Call parent constuctor parent::__construct(); } - /** - * Set email strings. - */ - public function set_email_strings() { - $this->setup_locale(); - $this->heading = __( 'Your order is complete', 'woocommerce' ); - $this->subject = __( 'Your {site_title} order from {order_date} is complete', 'woocommerce' ); - $this->restore_locale(); - } - /** * Trigger the sending of this email. * @@ -82,35 +66,29 @@ class WC_Email_Customer_Completed_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** * Get email subject. * - * @access public + * @since 3.1.0 * @return string */ - public function get_subject() { - if ( ! empty( $this->object ) && $this->object->has_downloadable_item() ) { - return apply_filters( 'woocommerce_email_subject_customer_completed_order', $this->format_string( $this->subject_downloadable ), $this->object ); - } else { - return apply_filters( 'woocommerce_email_subject_customer_completed_order', $this->format_string( $this->subject ), $this->object ); - } + public function get_default_subject() { + return __( 'Your {site_title} order from {order_date} is complete', 'woocommerce' ); } /** * Get email heading. * - * @access public + * @since 3.1.0 * @return string */ - public function get_heading() { - if ( ! empty( $this->object ) && $this->object->has_downloadable_item() ) { - return apply_filters( 'woocommerce_email_heading_customer_completed_order', $this->format_string( $this->heading_downloadable ), $this->object ); - } else { - return apply_filters( 'woocommerce_email_heading_customer_completed_order', $this->format_string( $this->heading ), $this->object ); - } + public function get_default_heading() { + return __( 'Your order is complete', 'woocommerce' ); } /** @@ -159,7 +137,7 @@ class WC_Email_Customer_Completed_Order extends WC_Email { 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -168,25 +146,7 @@ class WC_Email_Customer_Completed_Order extends WC_Email { 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading . '' ), - 'placeholder' => '', - 'default' => '', - 'desc_tip' => true, - ), - 'subject_downloadable' => array( - 'title' => __( 'Subject (downloadable)', 'woocommerce' ), - 'type' => 'text', - /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject_downloadable . '' ), - 'placeholder' => '', - 'default' => '', - 'desc_tip' => true, - ), - 'heading_downloadable' => array( - 'title' => __( 'Email heading (downloadable)', 'woocommerce' ), - 'type' => 'text', - /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading_downloadable . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, diff --git a/includes/emails/class-wc-email-customer-invoice.php b/includes/emails/class-wc-email-customer-invoice.php index 38fab9aa32f..8a61f2810d2 100644 --- a/includes/emails/class-wc-email-customer-invoice.php +++ b/includes/emails/class-wc-email-customer-invoice.php @@ -46,26 +46,72 @@ class WC_Email_Customer_Invoice extends WC_Email { $this->template_html = 'emails/customer-invoice.php'; $this->template_plain = 'emails/plain/customer-invoice.php'; - $this->set_email_strings(); - // Call parent constructor parent::__construct(); $this->manual = true; - $this->heading_paid = $this->get_option( 'heading_paid', $this->heading_paid ); - $this->subject_paid = $this->get_option( 'subject_paid', $this->subject_paid ); } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->subject = __( 'Invoice for order {order_number} from {order_date}', 'woocommerce' ); - $this->heading = __( 'Invoice for order {order_number}', 'woocommerce' ); - $this->subject_paid = __( 'Your {site_title} order from {order_date}', 'woocommerce' ); - $this->heading_paid = __( 'Order {order_number} details', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject( $paid = false ) { + if ( $paid ) { + return __( 'Your {site_title} order from {order_date}', 'woocommerce' ); + } else { + return __( 'Invoice for order {order_number} from {order_date}', 'woocommerce' ); + } + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading( $paid = false ) { + if ( $paid ) { + return __( 'Order {order_number} details', 'woocommerce' ); + } else { + return __( 'Invoice for order {order_number}', 'woocommerce' ); + } + } + + /** + * Get email subject. + * + * @access public + * @return string + */ + public function get_subject() { + if ( $this->object->has_status( array( 'completed', 'processing' ) ) ) { + $subject = $this->get_option( 'subject_paid', $this->get_default_subject( true ) ); + $action = 'woocommerce_email_subject_customer_invoice_paid'; + } else { + $subject = $this->get_option( 'subject', $this->get_default_subject() ); + $action = 'woocommerce_email_subject_customer_invoice'; + } + return apply_filters( $action, $this->format_string( $subject ), $this->object ); + } + + /** + * Get email heading. + * + * @access public + * @return string + */ + public function get_heading() { + if ( $this->object->has_status( wc_get_is_paid_statuses() ) ) { + $heading = $this->get_option( 'heading_paid', $this->get_default_heading( true ) ); + $action = 'woocommerce_email_heading_customer_invoice_paid'; + } else { + $heading = $this->get_option( 'heading', $this->get_default_heading() ); + $action = 'woocommerce_email_heading_customer_invoice'; + } + return apply_filters( $action, $this->format_string( $heading ), $this->object ); } /** @@ -94,35 +140,9 @@ class WC_Email_Customer_Invoice extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); - } - - /** - * Get email subject. - * - * @access public - * @return string - */ - public function get_subject() { - if ( $this->object->has_status( wc_get_is_paid_statuses() ) ) { - return apply_filters( 'woocommerce_email_subject_customer_invoice_paid', $this->format_string( $this->subject_paid ), $this->object ); - } else { - return apply_filters( 'woocommerce_email_subject_customer_invoice', $this->format_string( $this->subject ), $this->object ); - } - } - - /** - * Get email heading. - * - * @access public - * @return string - */ - public function get_heading() { - if ( $this->object->has_status( array( 'completed', 'processing' ) ) ) { - return apply_filters( 'woocommerce_email_heading_customer_invoice_paid', $this->format_string( $this->heading_paid ), $this->object ); - } else { - return apply_filters( 'woocommerce_email_heading_customer_invoice', $this->format_string( $this->heading ), $this->object ); - } + $this->restore_locale(); } /** @@ -166,7 +186,7 @@ class WC_Email_Customer_Invoice extends WC_Email { 'title' => __( 'Email subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -175,7 +195,7 @@ class WC_Email_Customer_Invoice extends WC_Email { 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -184,7 +204,7 @@ class WC_Email_Customer_Invoice extends WC_Email { 'title' => __( 'Email subject (paid)', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject_paid . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject( true ) . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -193,7 +213,7 @@ class WC_Email_Customer_Invoice extends WC_Email { 'title' => __( 'Email heading (paid)', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading_paid . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading( true ) . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, diff --git a/includes/emails/class-wc-email-customer-new-account.php b/includes/emails/class-wc-email-customer-new-account.php index 4dcd5b96fa0..c9a0ddc25e6 100644 --- a/includes/emails/class-wc-email-customer-new-account.php +++ b/includes/emails/class-wc-email-customer-new-account.php @@ -61,20 +61,28 @@ class WC_Email_Customer_New_Account extends WC_Email { $this->template_html = 'emails/customer-new-account.php'; $this->template_plain = 'emails/plain/customer-new-account.php'; - $this->set_email_strings(); - // Call parent constructor parent::__construct(); } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->subject = __( 'Your account on {site_title}', 'woocommerce' ); - $this->heading = __( 'Welcome to {site_title}', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject() { + return __( 'Your account on {site_title}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Welcome to {site_title}', 'woocommerce' ); } /** @@ -100,7 +108,9 @@ class WC_Email_Customer_New_Account extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** diff --git a/includes/emails/class-wc-email-customer-note.php b/includes/emails/class-wc-email-customer-note.php index 97169291b11..2c9bc939eec 100644 --- a/includes/emails/class-wc-email-customer-note.php +++ b/includes/emails/class-wc-email-customer-note.php @@ -40,8 +40,6 @@ class WC_Email_Customer_Note extends WC_Email { $this->template_html = 'emails/customer-note.php'; $this->template_plain = 'emails/plain/customer-note.php'; - $this->set_email_strings(); - // Triggers add_action( 'woocommerce_new_customer_note_notification', array( $this, 'trigger' ) ); @@ -50,13 +48,23 @@ class WC_Email_Customer_Note extends WC_Email { } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->subject = __( 'Note added to your {site_title} order from {order_date}', 'woocommerce' ); - $this->heading = __( 'A note has been added to your order', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject() { + return __( 'Note added to your {site_title} order from {order_date}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'A note has been added to your order', 'woocommerce' ); } /** @@ -65,9 +73,7 @@ class WC_Email_Customer_Note extends WC_Email { * @param array $args */ public function trigger( $args ) { - if ( ! empty( $args ) ) { - $defaults = array( 'order_id' => '', 'customer_note' => '', @@ -95,7 +101,9 @@ class WC_Email_Customer_Note extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** diff --git a/includes/emails/class-wc-email-customer-on-hold-order.php b/includes/emails/class-wc-email-customer-on-hold-order.php index 4395ceb53a8..300e8516c00 100644 --- a/includes/emails/class-wc-email-customer-on-hold-order.php +++ b/includes/emails/class-wc-email-customer-on-hold-order.php @@ -31,8 +31,6 @@ class WC_Email_Customer_On_Hold_Order extends WC_Email { $this->template_html = 'emails/customer-on-hold-order.php'; $this->template_plain = 'emails/plain/customer-on-hold-order.php'; - $this->set_email_strings(); - // Triggers for this email add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 ); add_action( 'woocommerce_order_status_failed_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 ); @@ -42,13 +40,23 @@ class WC_Email_Customer_On_Hold_Order extends WC_Email { } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->heading = __( 'Thank you for your order', 'woocommerce' ); - $this->subject = __( 'Your {site_title} order receipt from {order_date}', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject() { + return __( 'Your {site_title} order receipt from {order_date}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Thank you for your order', 'woocommerce' ); } /** @@ -77,7 +85,9 @@ class WC_Email_Customer_On_Hold_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** diff --git a/includes/emails/class-wc-email-customer-processing-order.php b/includes/emails/class-wc-email-customer-processing-order.php index c912c67c9cc..85980d9f1f5 100644 --- a/includes/emails/class-wc-email-customer-processing-order.php +++ b/includes/emails/class-wc-email-customer-processing-order.php @@ -31,8 +31,6 @@ class WC_Email_Customer_Processing_Order extends WC_Email { $this->template_html = 'emails/customer-processing-order.php'; $this->template_plain = 'emails/plain/customer-processing-order.php'; - $this->set_email_strings(); - // Triggers for this email add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); add_action( 'woocommerce_order_status_on-hold_to_processing_notification', array( $this, 'trigger' ), 10, 2 ); @@ -43,13 +41,23 @@ class WC_Email_Customer_Processing_Order extends WC_Email { } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->heading = __( 'Thank you for your order', 'woocommerce' ); - $this->subject = __( 'Your {site_title} order receipt from {order_date}', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject() { + return __( 'Your {site_title} order receipt from {order_date}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Thank you for your order', 'woocommerce' ); } /** @@ -78,7 +86,9 @@ class WC_Email_Customer_Processing_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** diff --git a/includes/emails/class-wc-email-customer-refunded-order.php b/includes/emails/class-wc-email-customer-refunded-order.php index 4eb21c0f0f9..67a3e002799 100644 --- a/includes/emails/class-wc-email-customer-refunded-order.php +++ b/includes/emails/class-wc-email-customer-refunded-order.php @@ -38,8 +38,11 @@ class WC_Email_Customer_Refunded_Order extends WC_Email { */ public function __construct() { $this->customer_email = true; - - $this->set_email_strings(); + $this->id = 'customer_refunded_order'; + $this->title = __( 'Refunded order', 'woocommerce' ); + $this->description = __( 'Order refunded emails are sent to customers when their orders are refunded.', 'woocommerce' ); + $this->template_html = 'emails/customer-refunded-order.php'; + $this->template_plain = 'emails/plain/customer-refunded-order.php'; // Triggers for this email add_action( 'woocommerce_order_fully_refunded_notification', array( $this, 'trigger_full' ), 10, 2 ); @@ -50,37 +53,69 @@ class WC_Email_Customer_Refunded_Order extends WC_Email { } /** - * Set email strings. + * Get email subject. * - * @param bool $partial_refund + * @since 3.1.0 + * @return string */ - public function set_email_strings( $partial_refund = false ) { - $this->setup_locale(); - $this->subject_partial = $this->get_option( 'subject_partial', __( 'Your {site_title} order from {order_date} has been partially refunded', 'woocommerce' ) ); - $this->subject_full = $this->get_option( 'subject_full', __( 'Your {site_title} order from {order_date} has been refunded', 'woocommerce' ) ); - - $this->heading_full = $this->get_option( 'heading_full', __( 'Your order has been fully refunded', 'woocommerce' ) ); - $this->heading_partial = $this->get_option( 'heading_partial', __( 'Your order has been partially refunded', 'woocommerce' ) ); - $this->restore_locale(); - - $this->template_html = 'emails/customer-refunded-order.php'; - $this->template_plain = 'emails/plain/customer-refunded-order.php'; - - if ( $partial_refund ) { - $this->id = 'customer_partially_refunded_order'; - $this->title = __( 'Partially refunded order', 'woocommerce' ); - $this->description = __( 'Order partially refunded emails are sent to customers when their orders are partially refunded.', 'woocommerce' ); - $this->heading = $this->heading_partial; - $this->subject = $this->subject_partial; + public function get_default_subject( $partial = false ) { + if ( $partial ) { + return __( 'Your {site_title} order from {order_date} has been partially refunded', 'woocommerce' ); } else { - $this->id = 'customer_refunded_order'; - $this->title = __( 'Refunded order', 'woocommerce' ); - $this->description = __( 'Order refunded emails are sent to customers when their orders are marked refunded.', 'woocommerce' ); - $this->heading = $this->heading_full; - $this->subject = $this->subject_full; + return __( 'Your {site_title} order from {order_date} has been refunded', 'woocommerce' ); } } + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading( $partial = false ) { + if ( $partial ) { + return __( 'Order {order_number} details', 'woocommerce' ); + } else { + return __( 'Your order has been partially refunded', 'woocommerce' ); + } + } + + /** + * Get email subject. + * + * @access public + * @return string + */ + public function get_subject() { + if ( $this->partial_refund ) { + $subject = $this->get_option( 'subject_partial', $this->get_default_subject( true ) ); + } else { + $subject = $this->get_option( 'subject_full', $this->get_default_heading() ); + } + return apply_filters( 'woocommerce_email_subject_customer_refunded_order', $this->format_string( $subject ), $this->object ); + } + + /** + * Get email heading. + * + * @access public + * @return string + */ + public function get_heading() { + if ( $this->partial_refund ) { + $heading = $this->get_option( 'heading_partial', $this->get_default_heading( true ) ); + } else { + $heading = $this->get_option( 'heading_full', $this->get_default_heading() ); + } + return apply_filters( 'woocommerce_email_heading_customer_refunded_order', $this->format_string( $heading ), $this->object ); + } + + /** + * Set email strings. + * @deprecated 3.1.0 Unused. + */ + public function set_email_strings( $partial_refund = false ) {} + /** * Full refund notification. * @@ -110,7 +145,7 @@ class WC_Email_Customer_Refunded_Order extends WC_Email { */ public function trigger( $order_id, $partial_refund = false, $refund_id = null ) { $this->partial_refund = $partial_refund; - $this->set_email_strings( $partial_refund ); + $this->id = $this->partial_refund ? 'customer_partially_refunded_order' : 'customer_refunded_order'; if ( $order_id ) { $this->object = wc_get_order( $order_id ); @@ -133,27 +168,9 @@ class WC_Email_Customer_Refunded_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); - } - - /** - * Get email subject. - * - * @access public - * @return string - */ - public function get_subject() { - return apply_filters( 'woocommerce_email_subject_customer_refunded_order', $this->format_string( $this->subject ), $this->object ); - } - - /** - * Get email heading. - * - * @access public - * @return string - */ - public function get_heading() { - return apply_filters( 'woocommerce_email_heading_customer_refunded_order', $this->format_string( $this->heading ), $this->object ); + $this->restore_locale(); } /** @@ -206,36 +223,32 @@ class WC_Email_Customer_Refunded_Order extends WC_Email { 'title' => __( 'Full refund subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject_full . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', - 'default' => $this->subject_full, 'desc_tip' => true, ), 'subject_partial' => array( 'title' => __( 'Partial refund subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject_partial . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject( true ) . '' ), 'placeholder' => '', - 'default' => $this->subject_partial, 'desc_tip' => true, ), 'heading_full' => array( 'title' => __( 'Full refund email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading_full . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', - 'default' => $this->heading_full, 'desc_tip' => true, ), 'heading_partial' => array( 'title' => __( 'Partial refund email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading_partial . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading( true ) . '' ), 'placeholder' => '', - 'default' => $this->heading_partial, 'desc_tip' => true, ), 'email_type' => array( diff --git a/includes/emails/class-wc-email-customer-reset-password.php b/includes/emails/class-wc-email-customer-reset-password.php index 09d6a40975b..d7812f60b52 100644 --- a/includes/emails/class-wc-email-customer-reset-password.php +++ b/includes/emails/class-wc-email-customer-reset-password.php @@ -54,8 +54,6 @@ class WC_Email_Customer_Reset_Password extends WC_Email { $this->template_html = 'emails/customer-reset-password.php'; $this->template_plain = 'emails/plain/customer-reset-password.php'; - $this->set_email_strings(); - // Trigger add_action( 'woocommerce_reset_password_notification', array( $this, 'trigger' ), 10, 2 ); @@ -64,13 +62,23 @@ class WC_Email_Customer_Reset_Password extends WC_Email { } /** - * Set email strings. + * Get email subject. + * + * @since 3.1.0 + * @return string */ - public function set_email_strings() { - $this->setup_locale(); - $this->subject = __( 'Password reset for {site_title}', 'woocommerce' ); - $this->heading = __( 'Password reset instructions', 'woocommerce' ); - $this->restore_locale(); + public function get_default_subject() { + return __( 'Password reset for {site_title}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Password reset instructions', 'woocommerce' ); } /** @@ -93,8 +101,9 @@ class WC_Email_Customer_Reset_Password extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); - + $this->restore_locale(); } /** diff --git a/includes/emails/class-wc-email-failed-order.php b/includes/emails/class-wc-email-failed-order.php index b45f4480e41..4dcb6291aa9 100644 --- a/includes/emails/class-wc-email-failed-order.php +++ b/includes/emails/class-wc-email-failed-order.php @@ -26,8 +26,6 @@ class WC_Email_Failed_Order extends WC_Email { $this->id = 'failed_order'; $this->title = __( 'Failed order', 'woocommerce' ); $this->description = __( 'Failed order emails are sent to chosen recipient(s) when orders have been marked failed (if they were previously processing or on-hold).', 'woocommerce' ); - $this->heading = __( 'Failed order', 'woocommerce' ); - $this->subject = __( '[{site_title}] Failed order ({order_number})', 'woocommerce' ); $this->template_html = 'emails/admin-failed-order.php'; $this->template_plain = 'emails/plain/admin-failed-order.php'; @@ -42,6 +40,26 @@ class WC_Email_Failed_Order extends WC_Email { $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); } + /** + * Get email subject. + * + * @since 3.1.0 + * @return string + */ + public function get_default_subject() { + return __( '[{site_title}] Failed order ({order_number})', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'Failed order', 'woocommerce' ); + } + /** * Trigger the sending of this email. * @@ -65,7 +83,9 @@ class WC_Email_Failed_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** @@ -121,7 +141,7 @@ class WC_Email_Failed_Order extends WC_Email { 'subject' => array( 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', - 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -129,7 +149,7 @@ class WC_Email_Failed_Order extends WC_Email { 'heading' => array( 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', - 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->heading . '' ), + 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, diff --git a/includes/emails/class-wc-email-new-order.php b/includes/emails/class-wc-email-new-order.php index bf98bfa8f64..fe90d890601 100644 --- a/includes/emails/class-wc-email-new-order.php +++ b/includes/emails/class-wc-email-new-order.php @@ -26,8 +26,6 @@ class WC_Email_New_Order extends WC_Email { $this->id = 'new_order'; $this->title = __( 'New order', 'woocommerce' ); $this->description = __( 'New order emails are sent to chosen recipient(s) when a new order is received.', 'woocommerce' ); - $this->heading = __( 'New customer order', 'woocommerce' ); - $this->subject = __( '[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce' ); $this->template_html = 'emails/admin-new-order.php'; $this->template_plain = 'emails/plain/admin-new-order.php'; @@ -46,6 +44,26 @@ class WC_Email_New_Order extends WC_Email { $this->recipient = $this->get_option( 'recipient', get_option( 'admin_email' ) ); } + /** + * Get email subject. + * + * @since 3.1.0 + * @return string + */ + public function get_default_subject() { + return __( '[{site_title}] New customer order ({order_number}) - {order_date}', 'woocommerce' ); + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return __( 'New customer order', 'woocommerce' ); + } + /** * Trigger the sending of this email. * @@ -69,7 +87,9 @@ class WC_Email_New_Order extends WC_Email { return; } + $this->setup_locale(); $this->send( $this->get_recipient(), $this->get_subject(), $this->get_content(), $this->get_headers(), $this->get_attachments() ); + $this->restore_locale(); } /** @@ -126,7 +146,7 @@ class WC_Email_New_Order extends WC_Email { 'subject' => array( 'title' => __( 'Subject', 'woocommerce' ), 'type' => 'text', - 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'This controls the email subject line. Leave blank to use the default subject: %s.', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -134,7 +154,7 @@ class WC_Email_New_Order extends WC_Email { 'heading' => array( 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', - 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->heading . '' ), + 'description' => sprintf( __( 'This controls the main heading contained within the email notification. Leave blank to use the default heading: %s.', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, diff --git a/includes/emails/class-wc-email.php b/includes/emails/class-wc-email.php index 001d7151498..348efefb89a 100644 --- a/includes/emails/class-wc-email.php +++ b/includes/emails/class-wc-email.php @@ -45,6 +45,26 @@ class WC_Email extends WC_Settings_API { */ public $description; + /** + * Default heading. + * + * Supported for backwards compatibility but we recommend overloading the + * get_default_x methods instead so localication can be done when needed. + * + * @var string + */ + public $heading = ''; + + /** + * Default subject. + * + * Supported for backwards compatibility but we recommend overloading the + * get_default_x methods instead so localication can be done when needed. + * + * @var string + */ + public $subject = ''; + /** * Plain text template path. * @var string @@ -69,18 +89,6 @@ class WC_Email extends WC_Settings_API { */ public $recipient; - /** - * Heading for the email content. - * @var string - */ - public $heading; - - /** - * Subject for the email. - * @var string - */ - public $subject; - /** * Object this email is for, for example a customer, product, or email. * @var object|bool @@ -204,8 +212,6 @@ class WC_Email extends WC_Settings_API { } // Settings - $this->heading = $this->get_option( 'heading', $this->heading ); - $this->subject = $this->get_option( 'subject', $this->subject ); $this->email_type = $this->get_option( 'email_type' ); $this->enabled = $this->get_option( 'enabled' ); @@ -261,13 +267,33 @@ class WC_Email extends WC_Settings_API { } } + /** + * Get email subject. + * + * @since 3.1.0 + * @return string + */ + public function get_default_subject() { + return $this->subject; + } + + /** + * Get email heading. + * + * @since 3.1.0 + * @return string + */ + public function get_default_heading() { + return $this->heading; + } + /** * Get email subject. * * @return string */ public function get_subject() { - return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->subject ), $this->object ); + return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->get_option( 'subject', $this->get_default_subject() ), $this->object ); } /** @@ -276,7 +302,7 @@ class WC_Email extends WC_Settings_API { * @return string */ public function get_heading() { - return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->heading ), $this->object ); + return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->get_option( 'heading', $this->get_default_heading() ), $this->object ); } /** @@ -513,7 +539,7 @@ class WC_Email extends WC_Settings_API { 'title' => __( 'Email subject', 'woocommerce' ), 'type' => 'text', /* translators: %s: default subject */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->subject . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_subject() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, @@ -522,7 +548,7 @@ class WC_Email extends WC_Settings_API { 'title' => __( 'Email heading', 'woocommerce' ), 'type' => 'text', /* translators: %s: default heading */ - 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->heading . '' ), + 'description' => sprintf( __( 'Defaults to %s', 'woocommerce' ), '' . $this->get_default_heading() . '' ), 'placeholder' => '', 'default' => '', 'desc_tip' => true, From 260cfce497bb36153b8b67316df83b94669276f5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 2 Jun 2017 18:44:43 +0100 Subject: [PATCH 2/4] Format strings --- includes/emails/class-wc-email.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/includes/emails/class-wc-email.php b/includes/emails/class-wc-email.php index 348efefb89a..a76c90e2256 100644 --- a/includes/emails/class-wc-email.php +++ b/includes/emails/class-wc-email.php @@ -293,7 +293,7 @@ class WC_Email extends WC_Settings_API { * @return string */ public function get_subject() { - return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->get_option( 'subject', $this->get_default_subject() ), $this->object ); + return apply_filters( 'woocommerce_email_subject_' . $this->id, $this->format_string( $this->get_option( 'subject', $this->get_default_subject() ) ), $this->object ); } /** @@ -302,7 +302,7 @@ class WC_Email extends WC_Settings_API { * @return string */ public function get_heading() { - return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->get_option( 'heading', $this->get_default_heading() ), $this->object ); + return apply_filters( 'woocommerce_email_heading_' . $this->id, $this->format_string( $this->get_option( 'heading', $this->get_default_heading() ) ), $this->object ); } /** @@ -432,7 +432,6 @@ class WC_Email extends WC_Settings_API { * @return string */ public function get_content() { - $this->setup_locale(); $this->sending = true; if ( 'plain' === $this->get_email_type() ) { @@ -440,7 +439,6 @@ class WC_Email extends WC_Settings_API { } else { $email_content = $this->get_content_html(); } - $this->restore_locale(); return wordwrap( $email_content, 70 ); } From 67a0637417f327a940c6c081b80a95d3e92b345e Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 5 Jun 2017 14:18:39 +0100 Subject: [PATCH 3/4] Advanced locale switching --- .../class-wc-admin-permalink-settings.php | 9 +---- .../class-wc-meta-box-order-actions.php | 8 +--- includes/emails/class-wc-email.php | 8 ++-- includes/wc-core-functions.php | 40 +++++++++++++++++-- woocommerce.php | 1 + 5 files changed, 45 insertions(+), 21 deletions(-) diff --git a/includes/admin/class-wc-admin-permalink-settings.php b/includes/admin/class-wc-admin-permalink-settings.php index ad50c0ebd43..3614426cf00 100644 --- a/includes/admin/class-wc-admin-permalink-settings.php +++ b/includes/admin/class-wc-admin-permalink-settings.php @@ -172,9 +172,7 @@ class WC_Admin_Permalink_Settings { // We need to save the options ourselves; settings api does not trigger save for the permalinks page. if ( isset( $_POST['permalink_structure'] ) ) { - if ( function_exists( 'switch_to_locale' ) ) { - switch_to_locale( get_locale() ); - } + wc_switch_to_site_locale(); $permalinks = (array) get_option( 'woocommerce_permalinks', array() ); $permalinks['category_base'] = wc_sanitize_permalink( trim( $_POST['woocommerce_product_category_slug'] ) ); @@ -210,10 +208,7 @@ class WC_Admin_Permalink_Settings { } update_option( 'woocommerce_permalinks', $permalinks ); - - if ( function_exists( 'restore_current_locale' ) ) { - restore_current_locale(); - } + wc_restore_locale(); } } } diff --git a/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php b/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php index b962f7c73c3..94adb943dae 100644 --- a/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php +++ b/includes/admin/meta-boxes/class-wc-meta-box-order-actions.php @@ -108,9 +108,7 @@ class WC_Meta_Box_Order_Actions { if ( strstr( $action, 'send_email_' ) ) { // Switch back to the site locale. - if ( function_exists( 'switch_to_locale' ) ) { - switch_to_locale( get_locale() ); - } + wc_switch_to_site_locale(); do_action( 'woocommerce_before_resend_order_emails', $order ); @@ -136,9 +134,7 @@ class WC_Meta_Box_Order_Actions { do_action( 'woocommerce_after_resend_order_email', $order, $email_to_send ); // Restore user locale. - if ( function_exists( 'restore_current_locale' ) ) { - restore_current_locale(); - } + wc_restore_locale(); // Change the post saved message. add_filter( 'redirect_post_location', array( __CLASS__, 'set_email_sent_message' ) ); diff --git a/includes/emails/class-wc-email.php b/includes/emails/class-wc-email.php index a76c90e2256..b510d026e93 100644 --- a/includes/emails/class-wc-email.php +++ b/includes/emails/class-wc-email.php @@ -253,8 +253,8 @@ class WC_Email extends WC_Settings_API { * Set the locale to the store locale for customer emails to make sure emails are in the store language. */ public function setup_locale() { - if ( function_exists( 'switch_to_locale' ) && $this->is_customer_email() ) { - switch_to_locale( get_locale() ); + if ( $this->is_customer_email() ) { + wc_switch_to_site_locale(); } } @@ -262,8 +262,8 @@ class WC_Email extends WC_Settings_API { * Restore the locale to the default locale. Use after finished with setup_locale. */ public function restore_locale() { - if ( function_exists( 'restore_previous_locale' ) && $this->is_customer_email() ) { - restore_previous_locale(); + if ( $this->is_customer_email() ) { + wc_restore_locale(); } } diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 79c47c090cb..3e68a500f72 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -1606,8 +1606,8 @@ function wc_list_pluck( $list, $callback_or_field, $index_key = null ) { * @return array */ function wc_get_permalink_structure() { - if ( function_exists( 'switch_to_locale' ) && did_action( 'admin_init' ) ) { - switch_to_locale( get_locale() ); + if ( did_action( 'admin_init' ) ) { + wc_switch_to_site_locale(); } $permalinks = wp_parse_args( (array) get_option( 'woocommerce_permalinks', array() ), array( @@ -1624,8 +1624,40 @@ function wc_get_permalink_structure() { $permalinks['tag_rewrite_slug'] = untrailingslashit( empty( $permalinks['tag_base'] ) ? _x( 'product-tag', 'slug', 'woocommerce' ) : $permalinks['tag_base'] ); $permalinks['attribute_rewrite_slug'] = untrailingslashit( empty( $permalinks['attribute_base'] ) ? '' : $permalinks['attribute_base'] ); - if ( function_exists( 'restore_current_locale' ) && did_action( 'admin_init' ) ) { - restore_current_locale(); + if ( did_action( 'admin_init' ) ) { + wc_restore_locale(); } return $permalinks; } + +/** + * Switch WooCommerce to site language. + * + * @since 3.1.0 + */ +function wc_switch_to_site_locale() { + if ( function_exists( 'switch_to_locale' ) ) { + switch_to_locale( $switch_to_locale ); + + // Filter on plugin_locale so load_plugin_textdomain loads the correct locale. + add_filter( 'plugin_locale', 'get_locale' ); + + // Init WC locale. + WC()->load_plugin_textdomain(); + } +} + +/** + * Switch WooCommerce language to original. + * + * @since 3.1.0 + */ +function wc_restore_locale() { + restore_previous_locale(); + + // Remove filter. + remove_filter( 'plugin_locale', 'get_locale' ); + + // Init WC locale. + WC()->load_plugin_textdomain(); +} diff --git a/woocommerce.php b/woocommerce.php index dfaef0eee23..339bbecc68f 100644 --- a/woocommerce.php +++ b/woocommerce.php @@ -461,6 +461,7 @@ final class WooCommerce { $locale = is_admin() && function_exists( 'get_user_locale' ) ? get_user_locale() : get_locale(); $locale = apply_filters( 'plugin_locale', $locale, 'woocommerce' ); + unload_textdomain( 'woocommerce' ); load_textdomain( 'woocommerce', WP_LANG_DIR . '/woocommerce/woocommerce-' . $locale . '.mo' ); load_plugin_textdomain( 'woocommerce', false, plugin_basename( dirname( __FILE__ ) ) . '/i18n/languages' ); } From 09d43670fc1e9de6693bccafaf804550c0de36a5 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 6 Jun 2017 12:54:49 +0100 Subject: [PATCH 4/4] function exists --- includes/wc-core-functions.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/includes/wc-core-functions.php b/includes/wc-core-functions.php index 3e68a500f72..9060aed8570 100644 --- a/includes/wc-core-functions.php +++ b/includes/wc-core-functions.php @@ -1653,11 +1653,13 @@ function wc_switch_to_site_locale() { * @since 3.1.0 */ function wc_restore_locale() { - restore_previous_locale(); + if ( function_exists( 'restore_previous_locale' ) ) { + restore_previous_locale(); - // Remove filter. - remove_filter( 'plugin_locale', 'get_locale' ); + // Remove filter. + remove_filter( 'plugin_locale', 'get_locale' ); - // Init WC locale. - WC()->load_plugin_textdomain(); + // Init WC locale. + WC()->load_plugin_textdomain(); + } }