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

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