PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.5.0
2.5.0 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 All 34 releases
fluent-booking / app / Hooks / Scheduler / FiveMinuteScheduler.php

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

190 lines 6.5 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::with('calendar_event')
32 ->where('status', 'scheduled')
33 ->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
34 ->limit(500)
35 ->get();
36
37 if ($bookings->isEmpty()) {
38 return true;
39 }
40
41 foreach ($bookings as $booking) {
42 // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run.
43 if (!Booking::where('id', $booking->id)->where('status', 'scheduled')->update(['status' => 'completed'])) {
44 continue;
45 }
46
47 $booking->status = 'completed';
48 do_action('fluent_booking/booking_schedule_completed', $booking, $booking->calendar_event);
49 }
50
51 return true;
52 }
53
54 private function maybeAutoCancelPastBookings()
55 {
56 $autoCompleteTimeOut = (int)Helper::getGlobalAdminSetting('auto_complete_timing', 60) * 60; // 10 minutes
57
58 $bookings = Booking::with('calendar_event')
59 ->where('status', 'pending')
60 ->where('end_time', '<', gmdate('Y-m-d H:i:s', time() - $autoCompleteTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
61 ->limit(500)
62 ->get();
63
64 if ($bookings->isEmpty()) {
65 return true;
66 }
67
68 foreach ($bookings as $booking) {
69 // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run.
70 $flagged = Booking::where('id', $booking->id)->where('status', 'pending')->update([
71 'status' => 'cancelled',
72 'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
73 ]);
74
75 if (!$flagged) {
76 continue;
77 }
78
79 $booking->status = 'cancelled';
80 do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event);
81 }
82
83 return true;
84 }
85
86 private function maybeAutoCancelBooking()
87 {
88 $autoCancelTimeOut = (int)Helper::getGlobalAdminSetting('auto_cancel_timing', 10) * 60; // 10 minutes
89
90 $bookings = Booking::query()
91 ->with('calendar_event')
92 ->where('created_at', '<=', gmdate('Y-m-d H:i:s', time() - $autoCancelTimeOut)) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
93 ->where('status', 'pending')
94 ->where('payment_status', 'pending')
95 ->where('payment_method', '!=', 'offline')
96 ->limit(500)
97 ->get();
98
99 if ($bookings->isEmpty()) {
100 return true;
101 }
102
103 foreach ($bookings as $booking) {
104 // Flag each row just before its hook, so a run that dies mid-loop leaves the rest for the next run.
105 $flagged = Booking::where('id', $booking->id)->where('status', 'pending')->update([
106 'status' => 'cancelled',
107 'updated_at' => gmdate('Y-m-d H:i:s'), // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
108 ]);
109
110 if (!$flagged) {
111 continue;
112 }
113
114 $booking->status = 'cancelled';
115 do_action('fluent_booking/booking_schedule_auto_cancelled', $booking, $booking->calendar_event);
116 }
117
118 return true;
119 }
120
121 private function maybeAutoExpireCalendars()
122 {
123 $calendarIds = Calendar::query()
124 ->where('type', 'event')
125 ->where('status', 'active')
126 ->whereDoesntHave('events', function ($query) {
127 $query->whereIn('status', ['active', 'draft']);
128 })
129 ->limit(500)
130 ->pluck('id');
131
132 if ($calendarIds->isEmpty()) {
133 return true;
134 }
135
136 Calendar::whereIn('id', $calendarIds)
137 ->where('type', 'event')
138 ->where('status', 'active')
139 ->whereDoesntHave('events', function ($query) {
140 $query->whereIn('status', ['active', 'draft']);
141 })
142 ->update(['status' => 'expired']);
143
144 return true;
145 }
146
147 private function maybeAutoExpireCalendarEvents()
148 {
149 Meta::query()
150 ->where('object_type', 'calendar_event')
151 ->where('key', 'expire_time')
152 ->where('value', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
153 ->whereHas('calendar_event', function ($query) {
154 $query->where('status', 'active');
155 })
156 ->with(['calendar_event' => function ($query) {
157 $query->where('status', 'active');
158 }])
159 ->limit(500)
160 ->get()
161 ->each(function ($meta) {
162 $meta->calendar_event->update(['status' => 'expired']);
163 });
164
165 return true;
166 }
167
168 private function maybeAutoDeleteReservations()
169 {
170 $reservationIds = Booking::query()
171 ->whereIn('event_type', ['single_event', 'group_event'])
172 ->where('status', 'reserved')
173 ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
174 ->limit(500)
175 ->pluck('id');
176
177 if ($reservationIds->isEmpty()) {
178 return true;
179 }
180
181 Booking::whereIn('id', $reservationIds)
182 ->whereIn('event_type', ['single_event', 'group_event'])
183 ->where('status', 'reserved')
184 ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
185 ->delete();
186
187 return true;
188 }
189 }
190