| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Services; |
| 6 |
|
| 7 |
use Yatra\Repositories\TripRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Trip Lifecycle Cron Service |
| 11 |
* Handles scheduled publish/unpublish and seasonal auto enable/disable. |
| 12 |
*/ |
| 13 |
class TripLifecycleCronService |
| 14 |
{ |
| 15 |
public const CRON_HOOK = 'yatra_daily_trip_lifecycle'; |
| 16 |
|
| 17 |
/** |
| 18 |
* Register WordPress cron hook (daily) |
| 19 |
*/ |
| 20 |
public static function registerCronHook(): void |
| 21 |
{ |
| 22 |
if (!wp_next_scheduled(self::CRON_HOOK)) { |
| 23 |
wp_schedule_event(time(), 'daily', self::CRON_HOOK); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Unregister cron hook |
| 29 |
*/ |
| 30 |
public static function unregisterCronHook(): void |
| 31 |
{ |
| 32 |
$timestamp = wp_next_scheduled(self::CRON_HOOK); |
| 33 |
if ($timestamp) { |
| 34 |
wp_unschedule_event($timestamp, self::CRON_HOOK); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Run lifecycle tasks: |
| 40 |
* - Scheduled publish/unpublish |
| 41 |
* - Seasonal auto enable/disable |
| 42 |
*/ |
| 43 |
public function runDaily(): void |
| 44 |
{ |
| 45 |
// Use WP local time |
| 46 |
$now = current_time('mysql'); |
| 47 |
$today = current_time('Y-m-d'); |
| 48 |
|
| 49 |
$repo = new TripRepository(); |
| 50 |
|
| 51 |
// Publish trips whose scheduled_publish_date has passed |
| 52 |
$repo->publishScheduledTrips($now); |
| 53 |
|
| 54 |
// Unpublish/archive trips whose scheduled_unpublish_date has passed |
| 55 |
$repo->archiveScheduledTrips($now); |
| 56 |
|
| 57 |
// Seasonal auto-enable: activate trips when enable date reached |
| 58 |
$repo->enableSeasonalTrips($today, $now); |
| 59 |
|
| 60 |
// Seasonal auto-disable: archive trips when disable date reached |
| 61 |
$repo->disableSeasonalTrips($today, $now); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
|