| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Scheduler; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Models\Calendar; |
| 7 |
use FluentBooking\App\Models\Meta; |
| 8 |
use FluentBooking\App\Services\Helper; |
| 9 |
|
| 10 |
class FiveMinuteScheduler |
| 11 |
{ |
| 12 |
public function register() |
| 13 |
{ |
| 14 |
add_action('fluent_booking_five_minutes_tasks', [$this, 'handle']); |
| 15 |
} |
| 16 |
|
| 17 |
public function handle() |
| 18 |
{ |
| 19 |
$this->maybeAutoCancelBooking(); |
| 20 |
$this->maybeAutoCompleteBookings(); |
| 21 |
$this->maybeAutoCancelPastBookings(); |
| 22 |
$this->maybeAutoDeleteReservations(); |
| 23 |
$this->maybeAutoExpireCalendarEvents(); |
| 24 |
$this->maybeAutoExpireCalendars(); |
| 25 |
} |
| 26 |
|
| 27 |
private function maybeAutoCompleteBookings() |
| 28 |
{ |
| 29 |
$autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 30 |
|
| 31 |
$bookings = Booking::where('status', 'scheduled') |
| 32 |
->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 33 |
->limit(500) |
| 34 |
->get(); |
| 35 |
|
| 36 |
foreach ($bookings as $booking) { |
| 37 |
$booking->status = 'completed'; |
| 38 |
$booking->save(); |
| 39 |
do_action('fluent_booking/booking_schedule_completed', $booking, $booking->calendar_event); |
| 40 |
} |
| 41 |
|
| 42 |
return true; |
| 43 |
} |
| 44 |
|
| 45 |
private function maybeAutoCancelPastBookings() |
| 46 |
{ |
| 47 |
$autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes |
| 48 |
|
| 49 |
Booking::query() |
| 50 |
->where('status', 'pending') |
| 51 |
->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 52 |
->update([ |
| 53 |
'status' => 'cancelled', |
| 54 |
'updated_at' => gmdate('Y-m-d H:i:s') // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 55 |
]); |
| 56 |
|
| 57 |
return true; |
| 58 |
} |
| 59 |
|
| 60 |
private function maybeAutoCancelBooking() |
| 61 |
{ |
| 62 |
$autoCancelTimeOut = (int)Helper::getGlobalAdminSetting('auto_cancel_timing', 10) * 60; // 10 minutes |
| 63 |
|
| 64 |
Booking::query() |
| 65 |
->where('created_at', '<=', gmdate('Y-m-d H:i:s', time() - $autoCancelTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 66 |
->where('status', 'pending') |
| 67 |
->where('payment_status', 'pending') |
| 68 |
->update([ |
| 69 |
'status' => 'cancelled', |
| 70 |
'updated_at' => gmdate('Y-m-d H:i:s') // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 71 |
]); |
| 72 |
|
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
private function maybeAutoExpireCalendars() |
| 77 |
{ |
| 78 |
Calendar::query() |
| 79 |
->where('type', 'event') |
| 80 |
->where('status', 'active') |
| 81 |
->whereDoesntHave('events', function ($query) { |
| 82 |
$query->whereIn('status', ['active', 'draft']); |
| 83 |
}) |
| 84 |
->update(['status' => 'expired']); |
| 85 |
|
| 86 |
return true; |
| 87 |
} |
| 88 |
|
| 89 |
private function maybeAutoExpireCalendarEvents() |
| 90 |
{ |
| 91 |
Meta::query() |
| 92 |
->where('object_type', 'calendar_event') |
| 93 |
->where('key', 'expire_time') |
| 94 |
->where('value', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 95 |
->whereHas('calendar_event', function ($query) { |
| 96 |
$query->where('status', 'active'); |
| 97 |
}) |
| 98 |
->with(['calendar_event' => function ($query) { |
| 99 |
$query->where('status', 'active'); |
| 100 |
}]) |
| 101 |
->get() |
| 102 |
->each(function ($meta) { |
| 103 |
$meta->calendar_event->update(['status' => 'expired']); |
| 104 |
}); |
| 105 |
|
| 106 |
return true; |
| 107 |
} |
| 108 |
|
| 109 |
private function maybeAutoDeleteReservations() |
| 110 |
{ |
| 111 |
Booking::query() |
| 112 |
->whereIn('event_type', ['single_event', 'group_event']) |
| 113 |
->where('status', 'reserved') |
| 114 |
->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date |
| 115 |
->delete(); |
| 116 |
|
| 117 |
return true; |
| 118 |
} |
| 119 |
} |
| 120 |
|