PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
2.4.0 2.3.0 2.2.5 2.2.0 2.1.2 2.1.1 trunk 1.10.0 1.10.01 1.10.02 1.5.0 1.5.01 1.5.02 1.5.1 1.5.10 1.5.20 1.5.21 1.5.22 1.5.23 1.5.24 1.5.25 1.6.0 1.7.0 1.7.1 1.7.2 All 33 releases
fluent-booking / app / Hooks / Scheduler / FiveMinuteScheduler.php

FiveMinuteScheduler.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.21, at app/Hooks/Scheduler/FiveMinuteScheduler.php

120 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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