PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / trunk
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution vtrunk
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 / Handlers / NotificationHandler.php

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

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