| 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_hourly_cron', [ $this, 'hourly_cron_task' ] ); |
| 17 |
|
| 18 |
// ? Dev Note: This is a backward compatibility measure. Remove following action and maybe_reschedule_cron() method after 1 Jan, 2027. |
| 19 |
// Safety net: if the old WP-Cron event fires before migration clears it, still process subscriptions. |
| 20 |
add_action( 'subscrpt_daily_cron', [ $this, 'hourly_cron_task' ] ); |
| 21 |
$this->maybe_reschedule_cron(); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Migrate to subscrpt_hourly_cron and ensure correct interval. |
| 26 |
* |
| 27 |
* Skips rescheduling when running inside wp-cron.php (DOING_CRON) to avoid |
| 28 |
* a race condition where WP temporarily removes an event from the queue before |
| 29 |
* firing it — without the guard, we would reschedule at midnight and corrupt |
| 30 |
* WP-Cron's own next-run timestamp. |
| 31 |
* |
| 32 |
* @return void |
| 33 |
*/ |
| 34 |
private function maybe_reschedule_cron() { |
| 35 |
// Remove legacy event name — no-op once migration is complete. |
| 36 |
wp_clear_scheduled_hook( 'subscrpt_daily_cron' ); |
| 37 |
|
| 38 |
// Do not interfere with WP-Cron's own event lifecycle when running via wp-cron.php. |
| 39 |
if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$event = wp_get_scheduled_event( 'subscrpt_hourly_cron' ); |
| 44 |
if ( $event && 'hourly' === $event->schedule ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
wp_clear_scheduled_hook( 'subscrpt_hourly_cron' ); |
| 49 |
wp_schedule_event( strtotime( 'tomorrow midnight' ), 'hourly', 'subscrpt_hourly_cron' ); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Run hourly cron task to check if subscriptions have expired. |
| 54 |
*/ |
| 55 |
public function hourly_cron_task() { |
| 56 |
do_action( 'before_subscription_update_cron' ); |
| 57 |
|
| 58 |
$this->update_subscription_statusses(); |
| 59 |
|
| 60 |
do_action( 'after_subscription_update_cron' ); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Expire active subscriptions whose term has ended. |
| 65 |
* |
| 66 |
* Pending cancellations (`pe_cancelled`) are handled separately by |
| 67 |
* {@see Cancellation}, not here. |
| 68 |
*/ |
| 69 |
public function update_subscription_statusses() { |
| 70 |
$args = [ |
| 71 |
'post_type' => 'subscrpt_order', |
| 72 |
'post_status' => [ 'active' ], |
| 73 |
'fields' => 'ids', |
| 74 |
'meta_query' => [ |
| 75 |
'relation' => 'OR', |
| 76 |
[ |
| 77 |
'key' => '_subscrpt_next_date', |
| 78 |
'value' => time(), |
| 79 |
'compare' => '<=', |
| 80 |
], |
| 81 |
[ |
| 82 |
'relation' => 'AND', |
| 83 |
[ |
| 84 |
'key' => '_subscrpt_trial', |
| 85 |
'value' => null, |
| 86 |
'compare' => '!=', |
| 87 |
], |
| 88 |
[ |
| 89 |
'key' => '_subscrpt_start_date', |
| 90 |
'value' => time(), |
| 91 |
'compare' => '<=', |
| 92 |
], |
| 93 |
], |
| 94 |
], |
| 95 |
]; |
| 96 |
|
| 97 |
$expired_subscriptions = get_posts( $args ); |
| 98 |
|
| 99 |
if ( $expired_subscriptions && count( $expired_subscriptions ) > 0 ) { |
| 100 |
// Initialize WooCommerce mailer before processing |
| 101 |
if ( function_exists( 'WC' ) && WC()->mailer() ) { |
| 102 |
foreach ( $expired_subscriptions as $subscription ) { |
| 103 |
Action::status( 'expired', $subscription ); |
| 104 |
} |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
} |
| 109 |
|