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 / Handlers / NotificationHandler.php

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

345 lines 13.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\Handlers;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Services\EmailNotificationService;
7 use FluentBooking\App\Services\NotificationGate;
8 use FluentBooking\Framework\Support\Arr;
9
10 class NotificationHandler
11 {
12 public function register()
13 {
14 add_action('fluent_booking/after_booking_scheduled', [$this, 'pushBookingScheduledToQueue'], 10, 2);
15 add_action('fluent_booking/after_booking_scheduled_async', [$this, 'bookingScheduledEmails'], 10, 1);
16 add_action('fluent_booking/after_booking_pending', [$this, 'pushBookingPendingToQueue'], 10, 2);
17 add_action('fluent_booking/after_booking_pending_async', [$this, 'bookingRequestEmails'], 10, 2);
18 add_action('fluent_booking/booking_schedule_reminder', [$this, 'bookingReminderEmails'], 10, 2);
19 add_action('fluent_booking/after_booking_rescheduled', [$this, 'emailOnBookingRescheduled'], 10, 3);
20 add_action('fluent_booking/booking_schedule_cancelled', [$this, 'emailOnBookingCancelled'], 10, 2);
21 add_action('fluent_booking/booking_schedule_rejected', [$this, 'emailOnBookingRejected'], 10, 2);
22 add_action('fluent_booking/after_patch_booking_email', [$this, 'emailToUpdatedEmail'], 10, 2);
23 }
24
25 private function getReminderTime($time)
26 {
27 $timestamp = $time['value'] * 60;
28
29 if ($time['unit'] == 'hours') {
30 $timestamp = $timestamp * 60;
31 } elseif ($time['unit'] == 'days') {
32 $timestamp = $timestamp * 60 * 24;
33 }
34
35 return $timestamp;
36 }
37
38 private function pushRemindersToQueue($booking, $reminderTimes, $emailTo)
39 {
40 // A booking can be scheduled again (e.g. no_show back to scheduled); replace, don't stack.
41 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$booking->id, $emailTo], 'fluent-booking');
42
43 $this->clearGroupRemindersForHost($booking, $emailTo);
44
45 foreach ($reminderTimes as $time) {
46 $reminderTimestamp = $this->getReminderTime($time);
47
48 $happeningTimestamp = strtotime($booking->start_time);
49 $startingTo = $happeningTimestamp - time();
50
51 $bufferTime = 2 * 60; // 2 Minute Buffer Time
52 if ($startingTo > ($reminderTimestamp + $bufferTime)) {
53 as_schedule_single_action(($happeningTimestamp - $reminderTimestamp), 'fluent_booking/booking_schedule_reminder', [
54 $booking->id,
55 $emailTo
56 ], 'fluent-booking');
57 }
58 }
59 }
60
61 private function clearGroupRemindersForHost($booking, $emailTo)
62 {
63 if (!$booking->isMultiGuestBooking() || $emailTo != 'host') {
64 return;
65 }
66
67 $otherBookingIds = Booking::where('group_id', $booking->group_id)->where('id', '!=', $booking->id)->pluck('id')->toArray();
68
69 foreach ($otherBookingIds as $otherBookingId) {
70 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$otherBookingId, 'host'], 'fluent-booking');
71 }
72 }
73
74 public function pushBookingScheduledToQueue($booking, $bookingEvent)
75 {
76 $notifications = $bookingEvent->getNotifications();
77
78 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
79 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
80 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
81 }
82
83 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
84 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
85 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
86 }
87
88 if (!is_null($booking->parent_id) && $booking->isRecurringBooking()) {
89 return;
90 }
91
92 if (NotificationGate::isSuppressed('booking_scheduled', $booking)) {
93 return;
94 }
95
96 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
97 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
98 $booking->id,
99 $bookingEvent->id
100 ], 'fluent-booking');
101 }
102 }
103
104 public function pushBookingPendingToQueue($booking, $bookingEvent)
105 {
106 if (NotificationGate::isSuppressed('booking_pending', $booking)) {
107 return;
108 }
109
110 if (!$bookingEvent->isConfirmationEnabled() || $bookingEvent->isMultiGuestEvent()) {
111 return;
112 }
113
114 if ($booking->payment_method && $booking->payment_status != 'paid') {
115 return;
116 }
117
118 if (!is_null($booking->parent_id) && $booking->isRecurringBooking()) {
119 return;
120 }
121
122 $notifications = $bookingEvent->getNotifications();
123
124 if (Arr::isTrue($notifications, 'booking_request_host.enabled') || (Arr::isTrue($notifications, 'booking_request_attendee.enabled'))) {
125 as_enqueue_async_action('fluent_booking/after_booking_pending_async', [
126 $booking->id,
127 $bookingEvent->id
128 ], 'fluent-booking');
129 }
130 }
131
132 public function emailToUpdatedEmail($booking, $calendarEvent)
133 {
134 if (NotificationGate::isSuppressed('booking_email_changed', $booking)) {
135 return;
136 }
137
138 $notifications = $calendarEvent->getNotifications();
139
140 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
141 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
142 $booking->id,
143 $calendarEvent->id
144 ], 'fluent-booking');
145 }
146 }
147
148 public function bookingScheduledEmails($bookingId)
149 {
150 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
151
152 if (!$booking || !$booking->calendar_event) {
153 return '';
154 }
155
156 $notifications = $booking->calendar_event->getNotifications();
157
158 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled')) {
159 $email = Arr::get($notifications, 'booking_conf_attendee.email', []);
160 EmailNotificationService::emailOnBooked($booking, $email, 'guest');
161 }
162
163 if (Arr::isTrue($notifications, 'booking_conf_host.enabled')) {
164 $email = Arr::get($notifications, 'booking_conf_host.email', []);
165 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
166 if ($additionalRecipients) {
167 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
168 }
169 EmailNotificationService::emailOnBooked($booking, $email, 'host');
170 }
171
172 return true;
173 }
174
175 /**
176 * @param $booking Booking
177 * @return void
178 */
179 public function bookingReminderEmails($bookingId, $emailTo)
180 {
181 $booking = Booking::with(['calendar_event', 'calendar'])->find($bookingId);
182
183 if (!$booking || $booking->status != 'scheduled') {
184 return false;
185 }
186
187 $notifications = $booking->calendar_event->getNotifications();
188
189 if (!$notifications) {
190 return;
191 }
192
193 if ('guest' == $emailTo && Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
194 $email = Arr::get($notifications, 'reminder_to_attendee.email', []);
195 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
196 }
197
198 if ('host' == $emailTo && Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
199 $email = Arr::get($notifications, 'reminder_to_host.email', []);
200 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
201 if ($additionalRecipients) {
202 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
203 }
204 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
205 }
206 }
207
208 public function bookingRequestEmails($bookingId, $calendarEventId)
209 {
210 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
211
212 if (!$booking || !$booking->calendar_event) {
213 return '';
214 }
215
216 $notifications = $booking->calendar_event->getNotifications();
217
218 if (Arr::isTrue($notifications, 'booking_request_attendee.enabled')) {
219 $email = Arr::get($notifications, 'booking_request_attendee.email', []);
220 EmailNotificationService::emailOnBooked($booking, $email, 'guest', 'request');
221 }
222
223 if (Arr::isTrue($notifications, 'booking_request_host.enabled')) {
224 $email = Arr::get($notifications, 'booking_request_host.email', []);
225 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
226 if ($additionalRecipients) {
227 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
228 }
229 EmailNotificationService::emailOnBooked($booking, $email, 'host', 'request');
230 }
231
232 return true;
233 }
234
235 public function getAdditionalRecipients($additionalRecipients)
236 {
237 if (!$additionalRecipients) {
238 return [];
239 }
240
241 $recipients = explode(',', $additionalRecipients);
242 $recipients = array_map('trim', $recipients);
243 return array_unique($recipients);
244 }
245
246 public function emailOnBookingCancelled(Booking $booking, $calendarEvent)
247 {
248 if (!$calendarEvent || NotificationGate::isSuppressed('booking_cancelled', $booking)) {
249 return;
250 }
251
252 $notifications = $calendarEvent->getNotifications();
253 if (!$notifications) {
254 return;
255 }
256
257 $cancelledBy = $booking->getMeta('cancelled_by_type', 'host');
258
259 if ($cancelledBy == 'host') {
260 if (Arr::isTrue($notifications, 'cancelled_by_host.enabled')) {
261 // This from the host
262 $email = Arr::get($notifications, 'cancelled_by_host.email', []);
263 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest');
264 }
265 return;
266 }
267
268 if (Arr::isTrue($notifications, 'cancelled_by_attendee.enabled')) {
269 $email = Arr::get($notifications, 'cancelled_by_attendee.email', []);
270 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
271 if ($additionalRecipients) {
272 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
273 }
274 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'host');
275 }
276 }
277
278 public function emailOnBookingRescheduled(Booking $booking, $oldBooking, $calendarEvent)
279 {
280 if (!$calendarEvent) {
281 return;
282 }
283
284 $notifications = $calendarEvent->getNotifications();
285 if (!$notifications) {
286 return;
287 }
288
289 // Remove all reminders
290 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'host'], 'fluent-booking');
291 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'guest'], 'fluent-booking');
292
293 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
294 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
295 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
296 }
297
298 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
299 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
300 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
301 }
302
303 if (NotificationGate::isSuppressed('booking_rescheduled', $booking)) {
304 return;
305 }
306
307 $rescheduledBy = $booking->getMeta('rescheduled_by_type', 'host');
308
309 if ($rescheduledBy == 'host') {
310 if (Arr::isTrue($notifications, 'rescheduled_by_host.enabled')) {
311 // This from the host
312 $email = Arr::get($notifications, 'rescheduled_by_host.email', []);
313 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'guest');
314 }
315 return;
316 }
317
318 if (Arr::isTrue($notifications, 'rescheduled_by_attendee.enabled')) {
319 $email = Arr::get($notifications, 'rescheduled_by_attendee.email', []);
320 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
321 if ($additionalRecipients) {
322 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
323 }
324 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'host');
325 }
326 }
327
328 public function emailOnBookingRejected(Booking $booking, $calendarEvent)
329 {
330 if (!$calendarEvent || NotificationGate::isSuppressed('booking_rejected', $booking)) {
331 return;
332 }
333
334 $notifications = $calendarEvent->getNotifications();
335 if (!$notifications) {
336 return;
337 }
338
339 if (Arr::isTrue($notifications, 'declined_by_host.enabled')) {
340 $email = Arr::get($notifications, 'declined_by_host.email', []);
341 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest', 'reject');
342 }
343 }
344 }
345