| @@ -1,8 +1,15 @@ | ||
| 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\SubscriptionStockPaused; | |
| 10 | +use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired; | |
| 11 | + | |
| 5 | 12 | /** |
| 6 | 13 | * Class Email |
| 7 | 14 | * |
| 8 | 15 | * @package SpringDevs\Subscription\Illuminate |
| @@ -9,15 +16,106 @@ | ||
| 9 | 16 | */ |
| 10 | 17 | class Email { |
| 11 | 18 | |
| 12 | 19 | /** |
| 13 | - * The contsructor method. | |
| 20 | + * The constructor method. | |
| 14 | 21 | */ |
| 15 | 22 | public function __construct() { |
| 16 | 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 ); | |
| 17 | 32 | } |
| 18 | 33 | |
| 19 | 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 | + /** | |
| 20 | 118 | * Add subscription sections inside order mail. |
| 21 | 119 | * |
| 22 | 120 | * @param \WC_Order $order Order Object. |
| 23 | 121 | * |
| @@ -22,76 +120,12 @@ | ||
| 22 | 120 | * @param \WC_Order $order Order Object. |
| 23 | 121 | * |
| 24 | 122 | * @return void |
| 25 | 123 | */ |
| 26 | - public function add_subscription_table( $order ) { | |
| 124 | + public function add_subscription_table( \WC_Order $order ) { | |
| 27 | 125 | $histories = Helper::get_subscriptions_from_order( $order->get_id() ); |
| 28 | 126 | |
| 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">× <?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; | |
| 127 | + if ( count( $histories ) > 0 ) { | |
| 128 | + include 'views/subscription-table.php'; | |
| 129 | + } | |
| 96 | 130 | } |
| 97 | 131 | } |