PluginProbe
Subscriptions for WooCommerce with Stripe Recurring Payments / 2.3.0
Subscriptions for WooCommerce with Stripe Recurring Payments v2.3.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 +101 -69 1.3.1 → 2.3.0 View file →
@@ -1,8 +1,14 @@
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;
7 +use SpringDevs\Subscription\Illuminate\Emails\StatusChangedAdmin;
8 +use SpringDevs\Subscription\Illuminate\Emails\SubscriptionCancelled;
9 +use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired;
10 +
5 11 /**
6 12 * Class Email
7 13 *
8 14 * @package SpringDevs\Subscription\Illuminate
@@ -9,15 +15,105 @@
9 15 */
10 16 class Email {
11 17
12 18 /**
13 - * The contsructor method.
19 + * The constructor method.
14 20 */
15 21 public function __construct() {
16 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 );
17 31 }
18 32
19 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 + /**
20 116 * Add subscription sections inside order mail.
21 117 *
22 118 * @param \WC_Order $order Order Object.
23 119 *
@@ -22,76 +118,12 @@
22 118 * @param \WC_Order $order Order Object.
23 119 *
24 120 * @return void
25 121 */
26 - public function add_subscription_table( $order ) {
122 + public function add_subscription_table( \WC_Order $order ) {
27 123 $histories = Helper::get_subscriptions_from_order( $order->get_id() );
28 124
29 - if ( count( $histories ) > 0 ) :
30 - ?>
31 - <div style="margin-bottom: 40px;">
32 - <table class="td" cellspacing="0" cellpadding="6" style="width: 100%; font-family: 'Helvetica Neue', Helvetica, Roboto, Arial, sans-serif;" border="1">
33 - <tbody>
34 - <tr>
35 - <h2><?php esc_html_e( 'Related Subscriptions', 'sdevs_subscrpt' ); ?></h2>
36 - <?php
37 - if ( ! $order->has_status( 'completed' ) ) :
38 - ?>
39 - <p><small>Your subscription will be activated when order status is completed.</small></p>
40 - <?php endif; ?>
41 - </tr>
42 - <?php
43 - foreach ( $histories as $history ) :
44 - $item = $order->get_item( $history->order_item_id );
45 - $item_meta = wc_get_order_item_meta( $history->order_item_id, '_subscrpt_meta', true );
46 - $subscription_id = $history->subscription_id;
47 - $subscription_status_object = get_post_status_object( get_post_status( $subscription_id ) );
48 - $cost = get_post_meta( $subscription_id, '_subscrpt_price', true );
49 - $has_trial = isset( $item_meta['trial'] ) && strlen( $item_meta['trial'] ) > 2;
50 - $start_date = get_post_meta( $subscription_id, '_subscrpt_start_date', true );
51 - $next_date = get_post_meta( $subscription_id, '_subscrpt_next_date', true );
52 - ?>
53 - <tr>
54 - <th class="td" scope="row" colspan="3" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: center;"><?php echo get_the_title( $subscription_id ); ?></th>
55 - </tr>
56 - <tr>
57 - <th class="td" scope="row" colspan="3" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><a href="<?php echo get_permalink( $item->get_product_id() ); ?>"><?php echo $item->get_name(); ?></a>
58 - <strong class="product-quantity">×&nbsp;<?php echo esc_html( $item->get_quantity() ); ?></strong>
59 - </th>
60 - </tr>
61 - <tr>
62 - <th class="td" scope="row" colspan="2" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php esc_html_e( 'Status:', 'sdevs_subscrpt' ); ?> </th>
63 - <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php echo esc_html( $subscription_status_object->label ); ?></td>
64 - </tr>
65 - <tr>
66 - <th class="td" scope="row" colspan="2" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;">
67 - <?php esc_html_e( 'Recurring amount:', 'sdevs_subscrpt' ); ?> </th>
68 - <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php echo wp_kses_post( Helper::format_price_with_order_item( $cost, $item->get_id() ) ); ?></td>
69 - </tr>
70 - <?php if ( ! $has_trial ) { ?>
71 - <tr>
72 - <th class="td" scope="row" colspan="2" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php esc_html_e( 'Next billing on', 'sdevs_subscrpt' ); ?>: </th>
73 - <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php echo ! empty( $next_date ) ? esc_html( gmdate( 'F d, Y', $next_date ) ) : '-'; ?></td>
74 - </tr>
75 - <?php } else { ?>
76 - <tr>
77 - <th class="td" scope="row" colspan="2" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php esc_html_e( 'Trial', 'sdevs_subscrpt' ); ?>: </th>
78 - <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php echo esc_html( $item_meta['trial'] ); ?></td>
79 - </tr>
80 - <tr>
81 - <th class="td" scope="row" colspan="2" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php esc_html_e( 'First billing on', 'sdevs_subscrpt' ); ?>: </th>
82 - <td class="td" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left;"><?php echo ! empty( $start_date ) ? esc_html( gmdate( 'F d, Y', $start_date ) ) : '-'; ?></td>
83 - </tr>
84 - <?php } ?>
85 - <tr>
86 - <th class="td" scope="row" colspan="3" style="color: #636363; border: 1px solid #e5e5e5; vertical-align: middle; padding: 12px; text-align: left; padding-bottom: 30px;"></th>
87 - </tr>
88 - <?php
89 - endforeach;
90 - ?>
91 - </tbody>
92 - </table>
93 - </div>
94 - <?php
95 - endif;
125 + if ( count( $histories ) > 0 ) {
126 + include 'views/subscription-table.php';
127 + }
96 128 }
97 129 }