PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.0.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.0.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 1.4.1 1.4.2 1.5.0 1.5.1 All 61 releases
subscription / includes / Illuminate / Email.php

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

130 lines 3.9 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\SubscriptionExpired;
10
11 /**
12 * Class Email
13 *
14 * @package SpringDevs\Subscription\Illuminate
15 */
16 class Email {
17
18 /**
19 * The constructor method.
20 */
21 public function __construct() {
22 add_action( 'woocommerce_email_after_order_table', array( $this, 'add_subscription_table' ) );
23 add_filter( 'woocommerce_email_classes', array( $this, 'register_emails' ) );
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
29 add_action( 'subscrpt_status_changed_admin_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
30 add_action( 'subscrpt_subscription_expired_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 );
31 }
32
33 /**
34 * Schedule the "subscription expired" email to fire after a short delay.
35 *
36 * @param int $subscription_id Subscription id.
37 * @return void
38 */
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
72 WC()->mailer();
73 do_action( 'subscrpt_subscription_expired_email_notification', $subscription_id );
74 }
75
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 /**
93 * Register emails.
94 *
95 * @param array $emails Email classes.
96 *
97 * @return array
98 */
99 public function register_emails( array $emails ): array {
100 $emails['subscrpt_status_changed_admin_email'] = new StatusChangedAdmin();
101 $emails['subscrpt_subscription_expired_email'] = new SubscriptionExpired();
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
112 return $emails;
113 }
114
115 /**
116 * Add subscription sections inside order mail.
117 *
118 * @param \WC_Order $order Order Object.
119 *
120 * @return void
121 */
122 public function add_subscription_table( \WC_Order $order ) {
123 $histories = Helper::get_subscriptions_from_order( $order->get_id() );
124
125 if ( count( $histories ) > 0 ) {
126 include 'views/subscription-table.php';
127 }
128 }
129 }
130