Admin
1 hour ago
AdminBarDashboardAccess
3 years ago
Classes
1 hour ago
ContentProtection
2 months ago
Functions
4 months ago
Integrations
1 year ago
Membership
1 hour ago
NavigationMenuLinks
2 years ago
RegisterActivation
9 months ago
ShortcodeParser
1 month ago
Themes
2 months ago
Widgets
11 months ago
lib
3 years ago
templates
1 hour ago
Base.php
2 months ago
Cron.php
2 years ago
DBTables.php
3 years ago
DBUpdates.php
2 months ago
LoginRedirect.php
9 months ago
RegisterScripts.php
4 months ago
eu-vat-rates.json
10 months ago
Cron.php
58 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core; |
| 4 | |
| 5 | use ProfilePress\Core\Membership\Models\Subscription\SubscriptionStatus; |
| 6 | use ProfilePress\Core\Membership\Repositories\SubscriptionRepository; |
| 7 | use ProfilePressVendor\Carbon\CarbonImmutable; |
| 8 | |
| 9 | class Cron |
| 10 | { |
| 11 | public function __construct() |
| 12 | { |
| 13 | add_action('init', [$this, 'create_recurring_schedule']); |
| 14 | |
| 15 | add_action('ppress_daily_recurring_job', [$this, 'check_for_expired_subscriptions']); |
| 16 | } |
| 17 | |
| 18 | public function create_recurring_schedule() |
| 19 | { |
| 20 | if ( ! wp_next_scheduled('ppress_daily_recurring_job')) { |
| 21 | wp_schedule_event(time(), 'daily', 'ppress_daily_recurring_job'); |
| 22 | } |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Check for expired subscriptions once per day and mark them as expired |
| 27 | */ |
| 28 | public function check_for_expired_subscriptions() |
| 29 | { |
| 30 | $subs = SubscriptionRepository::init()->retrieveBy([ |
| 31 | 'status' => [SubscriptionStatus::ACTIVE, SubscriptionStatus::TRIALLING, SubscriptionStatus::CANCELLED], |
| 32 | 'expiration_date' => CarbonImmutable::now('UTC')->endOfDay()->toDateTimeString(), |
| 33 | 'date_compare' => '<=', |
| 34 | 'number' => 100 |
| 35 | ]); |
| 36 | |
| 37 | if ( ! empty($subs)) { |
| 38 | |
| 39 | foreach ($subs as $sub) { |
| 40 | $sub->expire(true, true); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * @return self |
| 47 | */ |
| 48 | public static function get_instance() |
| 49 | { |
| 50 | static $instance = null; |
| 51 | |
| 52 | if (is_null($instance)) { |
| 53 | $instance = new self(); |
| 54 | } |
| 55 | |
| 56 | return $instance; |
| 57 | } |
| 58 | } |