| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Illuminate; |
| 4 |
|
| 5 |
use SpringDevs\Subscription\Illuminate\Emails\StatusChangedAdmin; |
| 6 |
use SpringDevs\Subscription\Illuminate\Emails\SubscriptionCancelled; |
| 7 |
use SpringDevs\Subscription\Illuminate\Emails\SubscriptionExpired; |
| 8 |
|
| 9 |
/** |
| 10 |
* Class Email |
| 11 |
* |
| 12 |
* @package SpringDevs\Subscription\Illuminate |
| 13 |
*/ |
| 14 |
class Email { |
| 15 |
|
| 16 |
/** |
| 17 |
* The constructor method. |
| 18 |
*/ |
| 19 |
public function __construct() { |
| 20 |
add_action( 'woocommerce_email_after_order_table', array( $this, 'add_subscription_table' ) ); |
| 21 |
add_filter( 'woocommerce_email_classes', array( $this, 'register_emails' ) ); |
| 22 |
|
| 23 |
add_action( 'subscrpt_subscription_expired', array( $this, 'schedule_expired_email' ) ); |
| 24 |
add_action( 'subscrpt_send_delayed_expired_email', array( $this, 'send_expired_email' ), 10, 1 ); |
| 25 |
add_action( 'subscrpt_subscription_activated', array( $this, 'cancel_pending_expired_email' ) ); |
| 26 |
|
| 27 |
add_action( 'subscrpt_status_changed_admin_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 ); |
| 28 |
add_action( 'subscrpt_subscription_expired_email', array( 'WC_Emails', 'send_transactional_email' ), 10, 3 ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Schedule the "subscription expired" email to fire after a short delay. |
| 33 |
* |
| 34 |
* @param int $subscription_id Subscription id. |
| 35 |
* @return void |
| 36 |
*/ |
| 37 |
public function schedule_expired_email( int $subscription_id ) { |
| 38 |
$delay_seconds = (int) apply_filters( 'subscrpt_expired_email_delay', HOUR_IN_SECONDS ); |
| 39 |
|
| 40 |
// Zero delay: send immediately. |
| 41 |
if ( $delay_seconds <= 0 ) { |
| 42 |
$this->send_expired_email( $subscription_id ); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
$hook = 'subscrpt_send_delayed_expired_email'; |
| 47 |
$args = array( $subscription_id ); |
| 48 |
$scheduled_time = time() + $delay_seconds; |
| 49 |
|
| 50 |
// Existing queue check. |
| 51 |
$existing = wp_next_scheduled( $hook, $args ); |
| 52 |
if ( $existing ) { |
| 53 |
wp_unschedule_event( $existing, $hook, $args ); |
| 54 |
} |
| 55 |
|
| 56 |
wp_schedule_single_event( $scheduled_time, $hook, $args ); |
| 57 |
} |
| 58 |
|
| 59 |
/** |
| 60 |
* Fire the delayed "subscription expired" email. |
| 61 |
* |
| 62 |
* @param int $subscription_id Subscription id. |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function send_expired_email( int $subscription_id ) { |
| 66 |
if ( 'expired' !== get_post_status( $subscription_id ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
WC()->mailer(); |
| 71 |
do_action( 'subscrpt_subscription_expired_email_notification', $subscription_id ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Cancel any pending delayed "subscription expired" email. |
| 76 |
* |
| 77 |
* @param int $subscription_id Subscription id. |
| 78 |
* @return void |
| 79 |
*/ |
| 80 |
public function cancel_pending_expired_email( int $subscription_id ) { |
| 81 |
$hook = 'subscrpt_send_delayed_expired_email'; |
| 82 |
$args = array( $subscription_id ); |
| 83 |
|
| 84 |
if ( function_exists( 'as_unschedule_action' ) ) { |
| 85 |
as_unschedule_action( $hook, $args ); |
| 86 |
} |
| 87 |
wp_clear_scheduled_hook( $hook, $args ); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Register emails. |
| 92 |
* |
| 93 |
* @param array $emails Email classes. |
| 94 |
* |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public function register_emails( array $emails ): array { |
| 98 |
$emails['subscrpt_status_changed_admin_email'] = new StatusChangedAdmin(); |
| 99 |
$emails['subscrpt_subscription_expired_email'] = new SubscriptionExpired(); |
| 100 |
$emails['subscrpt_subscription_cancelled_email'] = new SubscriptionCancelled(); |
| 101 |
return $emails; |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* Add subscription sections inside order mail. |
| 106 |
* |
| 107 |
* @param \WC_Order $order Order Object. |
| 108 |
* |
| 109 |
* @return void |
| 110 |
*/ |
| 111 |
public function add_subscription_table( \WC_Order $order ) { |
| 112 |
$histories = Helper::get_subscriptions_from_order( $order->get_id() ); |
| 113 |
|
| 114 |
if ( count( $histories ) > 0 ) { |
| 115 |
include 'views/subscription-table.php'; |
| 116 |
} |
| 117 |
} |
| 118 |
} |
| 119 |
|