PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.2.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.2.0
2.4.0 2.3.0 2.2.0 2.1.0 2.0.0 1.11.2 1.11.1 1.11.0 1.10.9 1.10.8 1.10.7 1.10.6 1.10.5 1.10.4 1.10.3 1.10.2 1.10.1 1.10.0 1.9.6 1.9.5 trunk 1.3.0 1.3.1 1.3.2 1.4.0 All 65 releases
← All changes | includes/Illuminate/Email.php +65 -3 1.9.5 → 2.2.0 View file →
@@ -1,8 +1,10 @@
1 1 <?php
2 2
3 3 namespace SpringDevs\Subscription\Illuminate;
4 4
5 +use SpringDevs\Subscription\Illuminate\Emails\CancellationAdmin;
6 +use SpringDevs\Subscription\Illuminate\Emails\SavedAdmin;
5 7 use SpringDevs\Subscription\Illuminate\Emails\StatusChangedAdmin;
6 8 use SpringDevs\Subscription\Illuminate\Emails\SubscriptionCancelled;
7 9 use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired;
8 10
@@ -18,26 +20,77 @@
18 20 */
19 21 public function __construct() {
20 22 add_action( 'woocommerce_email_after_order_table', array( $this, 'add_subscription_table' ) );
21 23 add_filter( 'woocommerce_email_classes', array( $this, 'register_emails' ) );
22 - add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) );
23 24
25 + add_action( 'subscrpt_subscription_expired', array( $this, 'schedule_expired_email' ) );
26 + add_action( 'subscrpt_send_delayed_expired_email', array( $this, 'send_expired_email' ), 10, 1 );
27 + add_action( 'subscrpt_subscription_activated', array( $this, 'cancel_pending_expired_email' ) );
28 +
24 29 add_action( 'subscrpt_status_changed_admin_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
25 30 add_action( 'subscrpt_subscription_expired_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
26 31 }
27 32
28 33 /**
29 - * Sent mail after subscription expired!
34 + * Schedule the "subscription expired" email to fire after a short delay.
30 35 *
31 36 * @param int $subscription_id Subscription id.
32 37 * @return void
33 38 */
34 - public function after_subscription_expired( int $subscription_id ) {
39 + public function schedule_expired_email( int $subscription_id ) {
40 + $delay_seconds = (int) apply_filters( 'subscrpt_expired_email_delay', HOUR_IN_SECONDS );
41 +
42 + // Zero delay: send immediately.
43 + if ( $delay_seconds <= 0 ) {
44 + $this->send_expired_email( $subscription_id );
45 + return;
46 + }
47 +
48 + $hook = 'subscrpt_send_delayed_expired_email';
49 + $args = array( $subscription_id );
50 + $scheduled_time = time() + $delay_seconds;
51 +
52 + // Existing queue check.
53 + $existing = wp_next_scheduled( $hook, $args );
54 + if ( $existing ) {
55 + wp_unschedule_event( $existing, $hook, $args );
56 + }
57 +
58 + wp_schedule_single_event( $scheduled_time, $hook, $args );
59 + }
60 +
61 + /**
62 + * Fire the delayed "subscription expired" email.
63 + *
64 + * @param int $subscription_id Subscription id.
65 + * @return void
66 + */
67 + public function send_expired_email( int $subscription_id ) {
68 + if ( 'expired' !== get_post_status( $subscription_id ) ) {
69 + return;
70 + }
71 +
35 72 WC()->mailer();
36 73 do_action( 'subscrpt_subscription_expired_email_notification', $subscription_id );
37 74 }
38 75
39 76 /**
77 + * Cancel any pending delayed "subscription expired" email.
78 + *
79 + * @param int $subscription_id Subscription id.
80 + * @return void
81 + */
82 + public function cancel_pending_expired_email( int $subscription_id ) {
83 + $hook = 'subscrpt_send_delayed_expired_email';
84 + $args = array( $subscription_id );
85 +
86 + if ( function_exists( 'as_unschedule_action' ) ) {
87 + as_unschedule_action( $hook, $args );
88 + }
89 + wp_clear_scheduled_hook( $hook, $args );
90 + }
91 +
92 + /**
40 93 * Register emails.
41 94 *
42 95 * @param array $emails Email classes.
43 96 *
@@ -46,8 +99,17 @@
46 99 public function register_emails( array $emails ): array {
47 100 $emails['subscrpt_status_changed_admin_email'] = new StatusChangedAdmin();
48 101 $emails['subscrpt_subscription_expired_email'] = new SubscriptionExpired();
49 102 $emails['subscrpt_subscription_cancelled_email'] = new SubscriptionCancelled();
103 +
104 + // The admin cancellation notices are free's reduced stand-ins. Pro ships
105 + // richer equivalents on the same events, so registering both would list
106 + // two of each in WooCommerce and mail the store owner twice.
107 + if ( ! subscrpt_pro_activated() ) {
108 + $emails['subscrpt_cancellation_admin'] = new CancellationAdmin();
109 + $emails['subscrpt_cancellation_saved_admin'] = new SavedAdmin();
110 + }
111 +
50 112 return $emails;
51 113 }
52 114
53 115 /**