Skip to content
This repository was archived by the owner on May 21, 2025. It is now read-only.

Commit 707a4d2

Browse files
committed
Made the setting work.
1 parent ade0f50 commit 707a4d2

File tree

2 files changed

+43
-6
lines changed

2 files changed

+43
-6
lines changed

includes/class-wc-subscriptions-email-notifications.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
* @category Class
1111
*/
1212
class WC_Subscriptions_Email_Notifications {
13+
14+
public static string $offset_setting_string = '_customer_notifications_offset';
15+
1316
public static function init() {
1417

1518
add_action( 'woocommerce_email_classes', __CLASS__ . '::add_emails', 10, 1 );
@@ -205,13 +208,13 @@ public static function add_settings( $settings ) {
205208
'type' => 'title',
206209
'id' => WC_Subscriptions_Admin::$option_prefix . '_customer_notifications',
207210
/* translators: Link to WC Settings > Email. */
208-
'desc' => sprintf( __( 'To enable and disable notification, visit the <a href="%s">Email settings</a>.', 'woocommerce-subscriptions' ), admin_url( 'admin.php?page=wc-settings&tab=email' ) ),
211+
'desc' => sprintf( __( 'To enable and disable individual notifications, visit the <a href="%s">Email settings</a>.', 'woocommerce-subscriptions' ), admin_url( 'admin.php?page=wc-settings&tab=email' ) ),
209212
),
210213
array(
211-
'name' => __( 'Hour Offset', 'woocommerce-subscriptions' ),
212-
'desc' => __( 'How many hours in before the event should the notification be sent.', 'woocommerce-subscriptions' ),
214+
'name' => __( 'Time Offset', 'woocommerce-subscriptions' ),
215+
'desc' => __( 'How long before the event should the notification be sent.', 'woocommerce-subscriptions' ),
213216
'tip' => '',
214-
'id' => WC_Subscriptions_Admin::$option_prefix . '_customer_notifications_offset',
217+
'id' => WC_Subscriptions_Admin::$option_prefix . self::$offset_setting_string,
215218
'desc_tip' => true,
216219
'type' => 'relative_date_selector',
217220
'placeholder' => __( 'N/A', 'woocommerce-subscriptions' ),

includes/class-wcs-action-scheduler-customer-notifications.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,42 @@ public function set_hours_offset( $hours_offset ) {
4545
public function __construct() {
4646
parent::__construct();
4747

48-
//TODO: make number of hours configurable.
49-
$this->hours_offset = 3 * DAY_IN_SECONDS / HOUR_IN_SECONDS; // aka 3 days in hours.
48+
$setting_option = get_option(
49+
WC_Subscriptions_Admin::$option_prefix . WC_Subscriptions_Email_Notifications::$offset_setting_string,
50+
array(
51+
'number' => 3,
52+
'unit' => 'days',
53+
)
54+
);
55+
$this->hours_offset = self::convert_offset_to_hours( $setting_option );
56+
}
57+
58+
/**
59+
* Calculate time offset in hours from the settings array.
60+
*
61+
* @param array $offset Format: array( 'number' => 3, 'unit' => 'days' )
62+
*
63+
* @return float|int
64+
*/
65+
protected static function convert_offset_to_hours( $offset ) {
66+
$default_offset = 3 * DAY_IN_SECONDS / HOUR_IN_SECONDS;
67+
68+
if ( ! isset( $offset['unit'] ) || ! isset( $offset['number'] ) ) {
69+
return $default_offset;
70+
}
71+
72+
switch ( $offset['unit'] ) {
73+
case 'days':
74+
return ( $offset['number'] * DAY_IN_SECONDS / HOUR_IN_SECONDS );
75+
case 'weeks':
76+
return ( $offset['number'] * WEEK_IN_SECONDS / HOUR_IN_SECONDS );
77+
case 'months':
78+
return ( $offset['number'] * MONTH_IN_SECONDS / HOUR_IN_SECONDS );
79+
case 'years':
80+
return ( $offset['number'] * YEAR_IN_SECONDS / HOUR_IN_SECONDS );
81+
default:
82+
return $default_offset;
83+
}
5084
}
5185

5286
public function set_date_types_to_schedule() {

0 commit comments

Comments
 (0)