PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / trunk
Subscriptions for WooCommerce with Stripe Recurring Payments vtrunk
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
subscription / includes / Illuminate / Email.php

Email.php in Subscriptions for WooCommerce with Stripe Recurring Payments trunk, at includes/Illuminate/Email.php

132 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace SpringDevs\Subscription\Illuminate;
4
5 use SpringDevs\Subscription\Illuminate\Emails\CancellationAdmin;
6 use SpringDevs\Subscription\Illuminate\Emails\SavedAdmin;
7 use SpringDevs\Subscription\Illuminate\Emails\StatusChangedAdmin;
8 use SpringDevs\Subscription\Illuminate\Emails\SubscriptionCancelled;
9 use SpringDevs\Subscription\Illuminate\Emails\SubscriptionStockPaused;
10 use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired;
11
12 /**
13 * Class Email
14 *
15 * @package SpringDevs\Subscription\Illuminate
16 */
17 class Email {
18
19 /**
20 * The constructor method.
21 */
22 public function __construct() {
23 add_action( 'woocommerce_email_after_order_table', array( $this, 'add_subscription_table' ) );
24 add_filter( 'woocommerce_email_classes', array( $this, 'register_emails' ) );
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
30 add_action( 'subscrpt_status_changed_admin_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
31 add_action( 'subscrpt_subscription_expired_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
32 }
33
34 /**
35 * Schedule the "subscription expired" email to fire after a short delay.
36 *
37 * @param int $subscription_id Subscription id.
38 * @return void
39 */
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
73 WC()->mailer();
74 do_action( 'subscrpt_subscription_expired_email_notification', $subscription_id );
75 }
76
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 /**
94 * Register emails.
95 *
96 * @param array $emails Email classes.
97 *
98 * @return array
99 */
100 public function register_emails( array $emails ): array {
101 $emails['subscrpt_status_changed_admin_email'] = new StatusChangedAdmin();
102 $emails['subscrpt_subscription_expired_email'] = new SubscriptionExpired();
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
114 return $emails;
115 }
116
117 /**
118 * Add subscription sections inside order mail.
119 *
120 * @param \WC_Order $order Order Object.
121 *
122 * @return void
123 */
124 public function add_subscription_table( \WC_Order $order ) {
125 $histories = Helper::get_subscriptions_from_order( $order->get_id() );
126
127 if ( count( $histories ) > 0 ) {
128 include 'views/subscription-table.php';
129 }
130 }
131 }
132