| 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 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Run daily cron task to check if subscription expired. |
| 21 |
*/ |
| 22 |
public function daily_cron_task() { |
| 23 |
$args = array( |
| 24 |
'post_type' => 'subscrpt_order', |
| 25 |
'post_status' => array( 'active', 'pe_cancelled' ), |
| 26 |
'fields' => 'ids', |
| 27 |
'author' => get_current_user_id(), |
| 28 |
); |
| 29 |
|
| 30 |
$active_subscriptions = get_posts( $args ); |
| 31 |
|
| 32 |
if ( $active_subscriptions && count( $active_subscriptions ) > 0 ) { |
| 33 |
foreach ( $active_subscriptions as $subscription ) { |
| 34 |
$start_date = get_post_meta( $subscription, '_subscrpt_start_date', true ); |
| 35 |
$next_date = get_post_meta( $subscription, '_subscrpt_next_date', true ); |
| 36 |
$trial = get_post_meta( $subscription, '_subscrpt_trial', true ); |
| 37 |
|
| 38 |
if ( time() >= $next_date || ( null !== $trial && time() >= $start_date ) ) { |
| 39 |
if ( 'pe_cancelled' === get_post_status( $subscription ) ) { |
| 40 |
Action::status( 'cancelled', $subscription ); |
| 41 |
} else { |
| 42 |
Action::status( 'expired', $subscription ); |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
} |
| 47 |
} |
| 48 |
} |
| 49 |
|