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

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

317 lines 12.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\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, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
75 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
76 $booking->id,
77 $bookingEvent->id
78 ], 'fluent-booking');
79 }
80
81 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
82 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
83 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
84 }
85
86 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
87 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
88 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
89 }
90 }
91
92 public function pushBookingPendingToQueue($booking, $bookingEvent)
93 {
94 if (!$bookingEvent->isConfirmationEnabled() || $bookingEvent->isMultiGuestEvent()) {
95 return;
96 }
97
98 if ($booking->payment_method && $booking->payment_status != 'paid') {
99 return;
100 }
101
102 $notifications = $bookingEvent->getNotifications();
103
104 if (Arr::isTrue($notifications, 'booking_request_host.enabled') || (Arr::isTrue($notifications, 'booking_request_attendee.enabled'))) {
105 as_enqueue_async_action('fluent_booking/after_booking_pending_async', [
106 $booking->id,
107 $bookingEvent->id
108 ], 'fluent-booking');
109 }
110 }
111
112 public function emailToUpdatedEmail($booking, $calendarEvent)
113 {
114 $notifications = $calendarEvent->getNotifications();
115
116 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled') || (Arr::isTrue($notifications, 'booking_conf_host.enabled'))) {
117 as_enqueue_async_action('fluent_booking/after_booking_scheduled_async', [
118 $booking->id,
119 $calendarEvent->id
120 ], 'fluent-booking');
121 }
122 }
123
124 public function bookingScheduledEmails($bookingId)
125 {
126 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
127
128 if (!$booking || !$booking->calendar_event) {
129 return '';
130 }
131
132 $notifications = $booking->calendar_event->getNotifications();
133
134 if (Arr::isTrue($notifications, 'booking_conf_attendee.enabled')) {
135 $email = Arr::get($notifications, 'booking_conf_attendee.email', []);
136 EmailNotificationService::emailOnBooked($booking, $email, 'guest');
137 }
138
139 if (Arr::isTrue($notifications, 'booking_conf_host.enabled')) {
140 $email = Arr::get($notifications, 'booking_conf_host.email', []);
141 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
142 if ($additionalRecipients) {
143 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
144 }
145 EmailNotificationService::emailOnBooked($booking, $email, 'host');
146 }
147
148 return true;
149 }
150
151 /**
152 * @param $booking Booking
153 * @return void
154 */
155 public function bookingReminderEmails($bookingId, $emailTo)
156 {
157 $booking = Booking::with(['calendar_event', 'calendar'])->find($bookingId);
158
159 if (!$booking || $booking->status != 'scheduled') {
160 return false;
161 }
162
163 $notifications = $booking->calendar_event->getNotifications();
164
165 if (!$notifications) {
166 return;
167 }
168
169 if ('guest' == $emailTo && Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
170 $email = Arr::get($notifications, 'reminder_to_attendee.email', []);
171 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
172 }
173
174 if ('host' == $emailTo && Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
175 $email = Arr::get($notifications, 'reminder_to_host.email', []);
176 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
177 if ($additionalRecipients) {
178 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
179 }
180 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
181 }
182 }
183
184 public function bookingRequestEmails($bookingId, $calendarEventId)
185 {
186 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
187
188 if (!$booking || !$booking->calendar_event) {
189 return '';
190 }
191
192 $notifications = $booking->calendar_event->getNotifications();
193
194 if (Arr::isTrue($notifications, 'booking_request_attendee.enabled')) {
195 $email = Arr::get($notifications, 'booking_request_attendee.email', []);
196 EmailNotificationService::emailOnBooked($booking, $email, 'guest', 'request');
197 }
198
199 if (Arr::isTrue($notifications, 'booking_request_host.enabled')) {
200 $email = Arr::get($notifications, 'booking_request_host.email', []);
201 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
202 if ($additionalRecipients) {
203 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
204 }
205 EmailNotificationService::emailOnBooked($booking, $email, 'host', 'request');
206 }
207
208 return true;
209 }
210
211 public function getAdditionalRecipients($additionalRecipients)
212 {
213 if (!$additionalRecipients) {
214 return [];
215 }
216
217 $recipients = explode(',', $additionalRecipients);
218 $recipients = array_map('trim', $recipients);
219 return array_unique($recipients);
220 }
221
222 public function emailOnBookingCancelled(Booking $booking, $calendarEvent)
223 {
224 if (!$calendarEvent) {
225 return;
226 }
227
228 $notifications = $calendarEvent->getNotifications();
229 if (!$notifications) {
230 return;
231 }
232
233 $cancelledBy = $booking->getMeta('cancelled_by_type', 'host');
234
235 if ($cancelledBy == 'host') {
236 if (Arr::isTrue($notifications, 'cancelled_by_host.enabled')) {
237 // This from the host
238 $email = Arr::get($notifications, 'cancelled_by_host.email', []);
239 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest');
240 }
241 return;
242 }
243
244 if (Arr::isTrue($notifications, 'cancelled_by_attendee.enabled')) {
245 $email = Arr::get($notifications, 'cancelled_by_attendee.email', []);
246 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
247 if ($additionalRecipients) {
248 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
249 }
250 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'host');
251 }
252 }
253
254 public function emailOnBookingRescheduled(Booking $booking, $oldBooking, $calendarEvent)
255 {
256 if (!$calendarEvent) {
257 return;
258 }
259
260 $notifications = $calendarEvent->getNotifications();
261 if (!$notifications) {
262 return;
263 }
264
265 // Remove all reminders
266 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'host'], 'fluent-booking');
267 \as_unschedule_all_actions('fluent_booking/booking_schedule_reminder', [$oldBooking->id, 'guest'], 'fluent-booking');
268
269 if (Arr::isTrue($notifications, 'reminder_to_attendee.enabled')) {
270 $reminderTimes = Arr::get($notifications, 'reminder_to_attendee.email.times', []);
271 $this->pushRemindersToQueue($booking, $reminderTimes, 'guest');
272 }
273
274 if (Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
275 $reminderTimes = Arr::get($notifications, 'reminder_to_host.email.times', []);
276 $this->pushRemindersToQueue($booking, $reminderTimes, 'host');
277 }
278
279 $rescheduledBy = $booking->getMeta('rescheduled_by_type', 'host');
280
281 if ($rescheduledBy == 'host') {
282 if (Arr::isTrue($notifications, 'rescheduled_by_host.enabled')) {
283 // This from the host
284 $email = Arr::get($notifications, 'rescheduled_by_host.email', []);
285 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'guest');
286 }
287 return;
288 }
289
290 if (Arr::isTrue($notifications, 'rescheduled_by_attendee.enabled')) {
291 $email = Arr::get($notifications, 'rescheduled_by_attendee.email', []);
292 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
293 if ($additionalRecipients) {
294 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
295 }
296 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'host');
297 }
298 }
299
300 public function emailOnBookingRejected(Booking $booking, $calendarEvent)
301 {
302 if (!$calendarEvent) {
303 return;
304 }
305
306 $notifications = $calendarEvent->getNotifications();
307 if (!$notifications) {
308 return;
309 }
310
311 if (Arr::isTrue($notifications, 'declined_by_host.enabled')) {
312 $email = Arr::get($notifications, 'declined_by_host.email', []);
313 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest', 'reject');
314 }
315 }
316 }
317