Add placeholders array used in descriptions and for find and replace

Fixes #16156
This commit is contained in:
Mike Jolley 2017-07-19 14:25:00 +01:00
parent 263a9123a7
commit 28cb0909c8
11 changed files with 135 additions and 122 deletions

View File

@ -23,11 +23,16 @@ class WC_Email_Cancelled_Order extends WC_Email {
* Constructor.
*/
public function __construct() {
$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->template_html = 'emails/admin-cancelled-order.php';
$this->template_plain = 'emails/plain/admin-cancelled-order.php';
$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->template_html = 'emails/admin-cancelled-order.php';
$this->template_plain = 'emails/plain/admin-cancelled-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_processing_to_cancelled_notification', array( $this, 'trigger' ), 10, 2 );
@ -72,11 +77,9 @@ class WC_Email_Cancelled_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -23,15 +23,17 @@ class WC_Email_Customer_Completed_Order extends WC_Email {
* Constructor.
*/
public function __construct() {
$this->id = 'customer_completed_order';
$this->customer_email = true;
$this->title = __( 'Completed order', 'woocommerce' );
$this->description = __( 'Order complete emails are sent to customers when their orders are marked completed and usually indicate that their orders have been shipped.', 'woocommerce' );
$this->template_html = 'emails/customer-completed-order.php';
$this->template_plain = 'emails/plain/customer-completed-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_completed_notification', array( $this, 'trigger' ), 10, 2 );
@ -52,14 +54,10 @@ class WC_Email_Customer_Completed_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -37,14 +37,17 @@ class WC_Email_Customer_Invoice extends WC_Email {
* Constructor.
*/
public function __construct() {
$this->id = 'customer_invoice';
$this->customer_email = true;
$this->title = __( 'Customer invoice', 'woocommerce' );
$this->description = __( 'Customer invoice emails can be sent to customers containing their order information and payment links.', 'woocommerce' );
$this->template_html = 'emails/customer-invoice.php';
$this->template_plain = 'emails/plain/customer-invoice.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Call parent constructor
parent::__construct();
@ -126,14 +129,10 @@ class WC_Email_Customer_Invoice extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->get_recipient() ) {

View File

@ -51,13 +51,10 @@ class WC_Email_Customer_New_Account extends WC_Email {
* Constructor.
*/
public function __construct() {
$this->id = 'customer_new_account';
$this->customer_email = true;
$this->title = __( 'New account', 'woocommerce' );
$this->description = __( 'Customer "new account" emails are sent to the customer when a customer signs up via checkout or account pages.', 'woocommerce' );
$this->template_html = 'emails/customer-new-account.php';
$this->template_plain = 'emails/plain/customer-new-account.php';

View File

@ -30,15 +30,17 @@ class WC_Email_Customer_Note extends WC_Email {
* Constructor.
*/
public function __construct() {
$this->id = 'customer_note';
$this->customer_email = true;
$this->title = __( 'Customer note', 'woocommerce' );
$this->description = __( 'Customer note emails are sent when you add a note to an order.', 'woocommerce' );
$this->template_html = 'emails/customer-note.php';
$this->template_plain = 'emails/plain/customer-note.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers
add_action( 'woocommerce_new_customer_note_notification', array( $this, 'trigger' ) );
@ -84,14 +86,10 @@ class WC_Email_Customer_Note extends WC_Email {
extract( $args );
if ( $order_id && ( $this->object = wc_get_order( $order_id ) ) ) {
$this->recipient = $this->object->get_billing_email();
$this->customer_note = $customer_note;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->recipient = $this->object->get_billing_email();
$this->customer_note = $customer_note;
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
} else {
return;
}

View File

@ -23,13 +23,17 @@ class WC_Email_Customer_On_Hold_Order extends WC_Email {
* Constructor.
*/
public function __construct() {
$this->id = 'customer_on_hold_order';
$this->customer_email = true;
$this->title = __( 'Order on-hold', 'woocommerce' );
$this->description = __( 'This is an order notification sent to customers containing order details after an order is placed on-hold.', 'woocommerce' );
$this->template_html = 'emails/customer-on-hold-order.php';
$this->template_plain = 'emails/plain/customer-on-hold-order.php';
$this->id = 'customer_on_hold_order';
$this->customer_email = true;
$this->title = __( 'Order on-hold', 'woocommerce' );
$this->description = __( 'This is an order notification sent to customers containing order details after an order is placed on-hold.', 'woocommerce' );
$this->template_html = 'emails/customer-on-hold-order.php';
$this->template_plain = 'emails/plain/customer-on-hold-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_pending_to_on-hold_notification', array( $this, 'trigger' ), 10, 2 );
@ -71,14 +75,10 @@ class WC_Email_Customer_On_Hold_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -30,6 +30,11 @@ class WC_Email_Customer_Processing_Order extends WC_Email {
$this->description = __( 'This is an order notification sent to customers containing order details after payment.', 'woocommerce' );
$this->template_html = 'emails/customer-processing-order.php';
$this->template_plain = 'emails/plain/customer-processing-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_failed_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
@ -72,14 +77,10 @@ class WC_Email_Customer_Processing_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -43,6 +43,11 @@ class WC_Email_Customer_Refunded_Order extends WC_Email {
$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';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_fully_refunded_notification', array( $this, 'trigger_full' ), 10, 2 );
@ -148,14 +153,10 @@ class WC_Email_Customer_Refunded_Order extends WC_Email {
$this->id = $this->partial_refund ? 'customer_partially_refunded_order' : 'customer_refunded_order';
if ( $order_id ) {
$this->object = wc_get_order( $order_id );
$this->recipient = $this->object->get_billing_email();
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = wc_get_order( $order_id );
$this->recipient = $this->object->get_billing_email();
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! empty( $refund_id ) ) {

View File

@ -23,11 +23,16 @@ class WC_Email_Failed_Order extends WC_Email {
* Constructor.
*/
public function __construct() {
$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->template_html = 'emails/admin-failed-order.php';
$this->template_plain = 'emails/plain/admin-failed-order.php';
$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->template_html = 'emails/admin-failed-order.php';
$this->template_plain = 'emails/plain/admin-failed-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_pending_to_failed_notification', array( $this, 'trigger' ), 10, 2 );
@ -72,11 +77,9 @@ class WC_Email_Failed_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -23,11 +23,16 @@ class WC_Email_New_Order extends WC_Email {
* Constructor.
*/
public function __construct() {
$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->template_html = 'emails/admin-new-order.php';
$this->template_plain = 'emails/plain/admin-new-order.php';
$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->template_html = 'emails/admin-new-order.php';
$this->template_plain = 'emails/plain/admin-new-order.php';
$this->placeholders = array(
'{site_title}' => $this->get_blogname(),
'{order_date}' => '',
'{order_number}' => '',
);
// Triggers for this email
add_action( 'woocommerce_order_status_pending_to_processing_notification', array( $this, 'trigger' ), 10, 2 );
@ -76,11 +81,9 @@ class WC_Email_New_Order extends WC_Email {
}
if ( is_a( $order, 'WC_Order' ) ) {
$this->object = $order;
$this->find['order-date'] = '{order_date}';
$this->find['order-number'] = '{order_number}';
$this->replace['order-date'] = wc_format_datetime( $this->object->get_date_created() );
$this->replace['order-number'] = $this->object->get_order_number();
$this->object = $order;
$this->placeholders['{order_date}'] = wc_format_datetime( $this->object->get_date_created() );
$this->placeholders['{order_number}'] = $this->object->get_order_number();
}
if ( ! $this->is_enabled() || ! $this->get_recipient() ) {

View File

@ -95,18 +95,6 @@ class WC_Email extends WC_Settings_API {
*/
public $object;
/**
* Strings to find in subjects/headings.
* @var array
*/
public $find = array();
/**
* Strings to replace in subjects/headings.
* @var array
*/
public $replace = array();
/**
* Mime boundary (for multipart emails).
* @var string
@ -195,34 +183,54 @@ class WC_Email extends WC_Settings_API {
' ', // Runs of spaces, post-handling
);
/**
* Strings to find/replace in subjects/headings.
*
* @var array
*/
protected $placeholders = array();
/**
* Strings to find in subjects/headings.
*
* @deprecated 3.2.0 in favour of placeholders
* @var array
*/
public $find = array();
/**
* Strings to replace in subjects/headings.
*
* @deprecated 3.2.0 in favour of placeholders
* @var array
*/
public $replace = array();
/**
* Constructor.
*/
public function __construct() {
// Find/replace
if ( empty( $this->placeholders ) ) {
$this->placeholders = array(
'{site_title}' => $this->get_blogname()
);
}
// Init settings
$this->init_form_fields();
$this->init_settings();
// Save settings hook
add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
// Default template base if not declared in child constructor
if ( is_null( $this->template_base ) ) {
$this->template_base = WC()->plugin_path() . '/templates/';
}
// Settings
$this->email_type = $this->get_option( 'email_type' );
$this->enabled = $this->get_option( 'enabled' );
// Find/replace
$this->find['blogname'] = '{blogname}';
$this->find['site-title'] = '{site_title}';
$this->replace['blogname'] = $this->get_blogname();
$this->replace['site-title'] = $this->get_blogname();
// For multipart messages
add_action( 'phpmailer_init', array( $this, 'handle_multipart' ) );
add_action( 'woocommerce_update_options_email_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
@ -246,7 +254,9 @@ class WC_Email extends WC_Settings_API {
* @return string
*/
public function format_string( $string ) {
return str_replace( apply_filters( 'woocommerce_email_format_string_find', $this->find, $this ), apply_filters( 'woocommerce_email_format_string_replace', $this->replace, $this ), $string );
// handle legacy find and replace.
$string = str_replace( $this->find, $this->replace, $string );
return str_replace( apply_filters( 'woocommerce_email_format_string_find', array_keys( $this->placeholders ), $this ), apply_filters( 'woocommerce_email_format_string_replace', array_values( $this->placeholders ), $this ), $string );
}
/**
@ -538,7 +548,7 @@ class WC_Email extends WC_Settings_API {
'type' => 'text',
'desc_tip' => true,
/* translators: %s: list of placeholders */
'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}</code>' ),
'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ),
'placeholder' => $this->get_default_subject(),
'default' => '',
),
@ -547,7 +557,7 @@ class WC_Email extends WC_Settings_API {
'type' => 'text',
'desc_tip' => true,
/* translators: %s: list of placeholders */
'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>{site_title}</code>' ),
'description' => sprintf( __( 'Available placeholders: %s', 'woocommerce' ), '<code>' . implode( '</code>, <code>', array_keys( $this->placeholders ) ) . '</code>' ),
'placeholder' => $this->get_default_heading(),
'default' => '',
),