PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.10.02
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.10.02
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 1.10.02, at app/Hooks/Scheduler/FiveMinuteScheduler.php

125 lines 4.2 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 $bookings = 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 ->where('payment_method', '!=', 'offline')
69 ->get();
70
71 foreach ($bookings as $booking) {
72 $booking->status = 'cancelled';
73 $booking->updated_at = gmdate('Y-m-d H:i:s');
74 $booking->save();
75 do_action('fluent_booking/booking_schedule_auto_cancelled', $booking);
76 }
77
78 return true;
79 }
80
81 private function maybeAutoExpireCalendars()
82 {
83 Calendar::query()
84 ->where('type', 'event')
85 ->where('status', 'active')
86 ->whereDoesntHave('events', function ($query) {
87 $query->whereIn('status', ['active', 'draft']);
88 })
89 ->update(['status' => 'expired']);
90
91 return true;
92 }
93
94 private function maybeAutoExpireCalendarEvents()
95 {
96 Meta::query()
97 ->where('object_type', 'calendar_event')
98 ->where('key', 'expire_time')
99 ->where('value', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
100 ->whereHas('calendar_event', function ($query) {
101 $query->where('status', 'active');
102 })
103 ->with(['calendar_event' => function ($query) {
104 $query->where('status', 'active');
105 }])
106 ->get()
107 ->each(function ($meta) {
108 $meta->calendar_event->update(['status' => 'expired']);
109 });
110
111 return true;
112 }
113
114 private function maybeAutoDeleteReservations()
115 {
116 Booking::query()
117 ->whereIn('event_type', ['single_event', 'group_event'])
118 ->where('status', 'reserved')
119 ->where('start_time', '<=', gmdate('Y-m-d H:i:s', time())) // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
120 ->delete();
121
122 return true;
123 }
124 }
125