PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 2.2.5
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v2.2.5
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 2.2.5, at app/Hooks/Handlers/NotificationHandler.php

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