| 1 |
<?php |
| 2 |
|
| 3 |
namespace SpringDevs\Subscription\Illuminate; |
| 4 |
|
| 5 |
/** |
| 6 |
* Class Cron |
| 7 |
* |
| 8 |
* @package SpringDevs\Subscription\Illuminate |
| 9 |
*/ |
| 10 |
class Cron { |
| 11 |
|
| 12 |
/** |
| 13 |
* Initialize the class. |
| 14 |
*/ |
| 15 |
public function __construct() { |
| 16 |
add_action( 'subscrpt_daily_cron', array( $this, 'daily_cron_task' ) ); |
| 17 |
add_action( 'subscrpt_renew_reminder_cron', array( $this, 'send_renew_reminder_mail' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Send renew reminder mail. |
| 22 |
* |
| 23 |
* @return void |
| 24 |
*/ |
| 25 |
public function send_renew_reminder_mail() { |
| 26 |
$time = strtotime( '7 days' ); |
| 27 |
|
| 28 |
$args = array( |
| 29 |
'post_type' => 'subscrpt_order', |
| 30 |
'post_status' => array( 'active', 'pe_cancelled' ), |
| 31 |
'fields' => 'ids', |
| 32 |
'meta_query' => array( |
| 33 |
'relation' => 'AND', |
| 34 |
array( |
| 35 |
'key' => '_subscrpt_next_date', |
| 36 |
'value' => $time, |
| 37 |
'compare' => '<=', |
| 38 |
), |
| 39 |
array( |
| 40 |
'key' => '_subscrpt_trial', |
| 41 |
'compare' => 'NOT EXISTS', |
| 42 |
), |
| 43 |
array( |
| 44 |
'key' => '_subscrpt_reminder_mail_sent', |
| 45 |
'compare' => 'NOT EXISTS', |
| 46 |
), |
| 47 |
), |
| 48 |
); |
| 49 |
|
| 50 |
$subscriptions = get_posts( $args ); |
| 51 |
|
| 52 |
if ( 0 === count($subscriptions) ) { |
| 53 |
return; |
| 54 |
} |
| 55 |
|
| 56 |
WC()->mailer(); |
| 57 |
foreach ( $subscriptions as $subscription_id ) { |
| 58 |
do_action( 'subscrpt_renew_reminder_email_notification', $subscription_id ); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Run daily cron task to check if subscription expired. |
| 64 |
*/ |
| 65 |
public function daily_cron_task() { |
| 66 |
$args = array( |
| 67 |
'post_type' => 'subscrpt_order', |
| 68 |
'post_status' => array( 'active', 'pe_cancelled' ), |
| 69 |
'fields' => 'ids', |
| 70 |
'meta_query' => array( |
| 71 |
'relation' => 'OR', |
| 72 |
array( |
| 73 |
'key' => '_subscrpt_next_date', |
| 74 |
'value' => time(), |
| 75 |
'compare' => '<=', |
| 76 |
), |
| 77 |
array( |
| 78 |
'relation' => 'AND', |
| 79 |
array( |
| 80 |
'key' => '_subscrpt_trial', |
| 81 |
'value' => null, |
| 82 |
'compare' => '!=', |
| 83 |
), |
| 84 |
array( |
| 85 |
'key' => '_subscrpt_start_date', |
| 86 |
'value' => time(), |
| 87 |
'compare' => '<=', |
| 88 |
), |
| 89 |
), |
| 90 |
), |
| 91 |
); |
| 92 |
|
| 93 |
$expired_subscriptions = get_posts( $args ); |
| 94 |
|
| 95 |
if ( $expired_subscriptions && count( $expired_subscriptions ) > 0 ) { |
| 96 |
foreach ( $expired_subscriptions as $subscription ) { |
| 97 |
if ( 'pe_cancelled' === get_post_status( $subscription ) ) { |
| 98 |
Action::status( 'cancelled', $subscription ); |
| 99 |
} else { |
| 100 |
Action::status( 'expired', $subscription ); |
| 101 |
} |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
} |
| 106 |
|