| @@ -1,10 +1,13 @@ | ||
| 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; |
| 9 | +use SpringDevs\Subscription\Illuminate\Emails\SubscriptionStockPaused; | |
| 7 | 10 | use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired; |
| 8 | 11 | |
| 9 | 12 | /** |
| 10 | 13 | * Class Email |
| @@ -18,26 +21,77 @@ | ||
| 18 | 21 | */ |
| 19 | 22 | public function __construct() { |
| 20 | 23 | add_action( 'woocommerce_email_after_order_table', array( $this, 'add_subscription_table' ) ); |
| 21 | 24 | add_filter( 'woocommerce_email_classes', array( $this, 'register_emails' ) ); |
| 22 | - add_action( 'subscrpt_subscription_expired', array( $this, 'after_subscription_expired' ) ); | |
| 23 | 25 | |
| 26 | + add_action( 'subscrpt_subscription_expired', array( $this, 'schedule_expired_email' ) ); | |
| 27 | + add_action( 'subscrpt_send_delayed_expired_email', array( $this, 'send_expired_email' ), 10, 1 ); | |
| 28 | + add_action( 'subscrpt_subscription_activated', array( $this, 'cancel_pending_expired_email' ) ); | |
| 29 | + | |
| 24 | 30 | add_action( 'subscrpt_status_changed_admin_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 ); |
| 25 | 31 | add_action( 'subscrpt_subscription_expired_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 ); |
| 26 | 32 | } |
| 27 | 33 | |
| 28 | 34 | /** |
| 29 | - * Sent mail after subscription expired! | |
| 35 | + * Schedule the "subscription expired" email to fire after a short delay. | |
| 30 | 36 | * |
| 31 | 37 | * @param int $subscription_id Subscription id. |
| 32 | 38 | * @return void |
| 33 | 39 | */ |
| 34 | - public function after_subscription_expired( int $subscription_id ) { | |
| 40 | + public function schedule_expired_email( int $subscription_id ) { | |
| 41 | + $delay_seconds = (int) apply_filters( 'subscrpt_expired_email_delay', HOUR_IN_SECONDS ); | |
| 42 | + | |
| 43 | + // Zero delay: send immediately. | |
| 44 | + if ( $delay_seconds <= 0 ) { | |
| 45 | + $this->send_expired_email( $subscription_id ); | |
| 46 | + return; | |
| 47 | + } | |
| 48 | + | |
| 49 | + $hook = 'subscrpt_send_delayed_expired_email'; | |
| 50 | + $args = array( $subscription_id ); | |
| 51 | + $scheduled_time = time() + $delay_seconds; | |
| 52 | + | |
| 53 | + // Existing queue check. | |
| 54 | + $existing = wp_next_scheduled( $hook, $args ); | |
| 55 | + if ( $existing ) { | |
| 56 | + wp_unschedule_event( $existing, $hook, $args ); | |
| 57 | + } | |
| 58 | + | |
| 59 | + wp_schedule_single_event( $scheduled_time, $hook, $args ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Fire the delayed "subscription expired" email. | |
| 64 | + * | |
| 65 | + * @param int $subscription_id Subscription id. | |
| 66 | + * @return void | |
| 67 | + */ | |
| 68 | + public function send_expired_email( int $subscription_id ) { | |
| 69 | + if ( 'expired' !== get_post_status( $subscription_id ) ) { | |
| 70 | + return; | |
| 71 | + } | |
| 72 | + | |
| 35 | 73 | WC()->mailer(); |
| 36 | 74 | do_action( 'subscrpt_subscription_expired_email_notification', $subscription_id ); |
| 37 | 75 | } |
| 38 | 76 | |
| 39 | 77 | /** |
| 78 | + * Cancel any pending delayed "subscription expired" email. | |
| 79 | + * | |
| 80 | + * @param int $subscription_id Subscription id. | |
| 81 | + * @return void | |
| 82 | + */ | |
| 83 | + public function cancel_pending_expired_email( int $subscription_id ) { | |
| 84 | + $hook = 'subscrpt_send_delayed_expired_email'; | |
| 85 | + $args = array( $subscription_id ); | |
| 86 | + | |
| 87 | + if ( function_exists( 'as_unschedule_action' ) ) { | |
| 88 | + as_unschedule_action( $hook, $args ); | |
| 89 | + } | |
| 90 | + wp_clear_scheduled_hook( $hook, $args ); | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 40 | 94 | * Register emails. |
| 41 | 95 | * |
| 42 | 96 | * @param array $emails Email classes. |
| 43 | 97 | * |
| @@ -46,8 +100,18 @@ | ||
| 46 | 100 | public function register_emails( array $emails ): array { |
| 47 | 101 | $emails['subscrpt_status_changed_admin_email'] = new StatusChangedAdmin(); |
| 48 | 102 | $emails['subscrpt_subscription_expired_email'] = new SubscriptionExpired(); |
| 49 | 103 | $emails['subscrpt_subscription_cancelled_email'] = new SubscriptionCancelled(); |
| 104 | + $emails['subscrpt_subscription_stock_paused_email'] = new SubscriptionStockPaused(); | |
| 105 | + | |
| 106 | + // The admin cancellation notices are free's reduced stand-ins. Pro ships | |
| 107 | + // richer equivalents on the same events, so registering both would list | |
| 108 | + // two of each in WooCommerce and mail the store owner twice. | |
| 109 | + if ( ! subscrpt_pro_activated() ) { | |
| 110 | + $emails['subscrpt_cancellation_admin'] = new CancellationAdmin(); | |
| 111 | + $emails['subscrpt_cancellation_saved_admin'] = new SavedAdmin(); | |
| 112 | + } | |
| 113 | + | |
| 50 | 114 | return $emails; |
| 51 | 115 | } |
| 52 | 116 | |
| 53 | 117 | /** |