Skip to content

Commit 8f48f36

Browse files
committed
Refactor add_placeholders() into a switch statement and add retry placeholders
1 parent c271656 commit 8f48f36

File tree

1 file changed

+26
-10
lines changed

1 file changed

+26
-10
lines changed

includes/emails/class-wc-subscriptions-email-preview.php

+26-10
Original file line numberDiff line numberDiff line change
@@ -254,20 +254,36 @@ private function add_placeholders( $email ) {
254254
return;
255255
}
256256

257-
$placeholders = [
258-
'{time_until_renewal}' => human_time_diff( time(), time() + WEEK_IN_SECONDS ),
259-
'{customers_first_name}' => 'John',
260-
];
257+
$placeholders = [];
261258

262-
// Pull the real values from the email object (Order or Subscription) if available.
263-
if ( is_a( $email->object, 'WC_Subscription' ) ) {
264-
$placeholders['{time_until_renewal}'] = human_time_diff( time(), $email->object->get_time( 'next_payment' ) );
265-
}
259+
switch ( $this->email_type ) {
260+
case 'WCS_Email_Customer_Notification_Manual_Trial_Expiration':
261+
case 'WCS_Email_Customer_Notification_Auto_Trial_Expiration':
262+
case 'WCS_Email_Customer_Notification_Manual_Renewal':
263+
case 'WCS_Email_Customer_Notification_Auto_Renewal':
264+
case 'WCS_Email_Customer_Notification_Subscription_Expiration':
265+
if ( is_a( $email->object, 'WC_Subscription' ) ) {
266+
$time_until_renewal = $email->get_time_until_date( $email->object, 'next_payment' );
267+
$customer_first_name = $email->object->get_billing_first_name();
268+
} else {
269+
$time_until_renewal = human_time_diff( time(), time() + WEEK_IN_SECONDS );
270+
$customer_first_name = 'John';
271+
}
272+
273+
$placeholders['{time_until_renewal}'] = $time_until_renewal;
274+
$placeholders['{customers_first_name}'] = $customer_first_name;
275+
break;
276+
case 'WCS_Email_Customer_Payment_Retry':
277+
case 'WCS_Email_Payment_Retry':
278+
$retry_time = is_a( $email->retry, 'WCS_Retry' )
279+
? $email->retry->get_time()
280+
: time() + ( 12 * HOUR_IN_SECONDS );
266281

267-
if ( is_a( $email->object, 'WC_Abstract_Order' ) ) {
268-
$placeholders['{customers_first_name}'] = $email->object->get_billing_first_name();
282+
$placeholders['{retry_time}'] = wcs_get_human_time_diff( $retry_time );
283+
break;
269284
}
270285

286+
// Merge placeholders without overriding existing ones, and only adding those in the email.
271287
$email->placeholders = wp_parse_args(
272288
$placeholders,
273289
$email->placeholders

0 commit comments

Comments
 (0)