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

286 lines 10.9 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, 2);
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, 2);
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, $slot, $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, $bookingEvent, $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, $bookingEvent, $reminderTimes, 'host');
74 }
75 }
76
77 public function pushBookingPendingToQueue($booking, $bookingEvent)
78 {
79 if (!$bookingEvent->isConfirmationEnabled()) {
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, $slotId)
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 } elseif ('host' == $emailTo && Arr::isTrue($notifications, 'reminder_to_host.enabled')) {
158 $email = Arr::get($notifications, 'reminder_to_host.email', []);
159 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
160 if ($additionalRecipients) {
161 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
162 }
163 EmailNotificationService::reminderEmail($booking, $email, $emailTo);
164 }
165 }
166
167 public function bookingRequestEmails($bookingId, $calendarEventId)
168 {
169 $booking = Booking::with(['calendar', 'calendar_event'])->find($bookingId);
170
171 if (!$booking || !$booking->calendar_event) {
172 return '';
173 }
174
175 $notifications = $booking->calendar_event->getNotifications();
176
177 if (Arr::isTrue($notifications, 'booking_request_attendee.enabled')) {
178 $email = Arr::get($notifications, 'booking_request_attendee.email', []);
179 EmailNotificationService::emailOnBooked($booking, $email, 'guest', 'request');
180 }
181
182 if (Arr::isTrue($notifications, 'booking_request_host.enabled')) {
183 $email = Arr::get($notifications, 'booking_request_host.email', []);
184 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
185 if ($additionalRecipients) {
186 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
187 }
188 EmailNotificationService::emailOnBooked($booking, $email, 'host', 'request');
189 }
190
191 return true;
192 }
193
194 public function getAdditionalRecipients($additionalRecipients)
195 {
196 if ($additionalRecipients) {
197 $recipients = explode(',', $additionalRecipients);
198 $recipients = array_map('trim', $recipients);
199 return array_unique($recipients);
200 }
201 return [];
202 }
203
204 public function emailOnBookingCancelled(Booking $booking, $calendarEvent)
205 {
206 if (!$calendarEvent) {
207 return;
208 }
209
210 $notifications = $calendarEvent->getNotifications();
211 if (!$notifications) {
212 return;
213 }
214
215 $cancelledBy = $booking->getMeta('cancelled_by_type', 'host');
216
217 if ($cancelledBy == 'host') {
218 if (Arr::isTrue($notifications, 'cancelled_by_host.enabled')) {
219 // This from the host
220 $email = Arr::get($notifications, 'cancelled_by_host.email', []);
221 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest');
222 }
223 return;
224 }
225
226 if (Arr::isTrue($notifications, 'cancelled_by_attendee.enabled')) {
227 $email = Arr::get($notifications, 'cancelled_by_attendee.email', []);
228 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
229 if ($additionalRecipients) {
230 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
231 }
232 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'host');
233 }
234 }
235
236 public function emailOnBookingRescheduled(Booking $booking)
237 {
238 $calendarEvent = $booking->calendar_event;
239 if (!$calendarEvent) {
240 return;
241 }
242
243 $notifications = $calendarEvent->getNotifications();
244 if (!$notifications) {
245 return;
246 }
247
248 $rescheduledBy = $booking->getMeta('rescheduled_by_type', 'host');
249
250 if ($rescheduledBy == 'host') {
251 if (Arr::isTrue($notifications, 'rescheduled_by_host.enabled')) {
252 // This from the host
253 $email = Arr::get($notifications, 'rescheduled_by_host.email', []);
254 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'guest');
255 }
256 return;
257 }
258
259 if (Arr::isTrue($notifications, 'rescheduled_by_attendee.enabled')) {
260 $email = Arr::get($notifications, 'rescheduled_by_attendee.email', []);
261 $additionalRecipients = Arr::get($email, 'additional_recipients', false);
262 if ($additionalRecipients) {
263 $email['recipients'] = $this->getAdditionalRecipients($additionalRecipients);
264 }
265 EmailNotificationService::bookingRescheduledEmail($booking, $email, 'host');
266 }
267 }
268
269 public function emailOnBookingRejected(Booking $booking, $calendarEvent)
270 {
271 if (!$calendarEvent) {
272 return;
273 }
274
275 $notifications = $calendarEvent->getNotifications();
276 if (!$notifications) {
277 return;
278 }
279
280 if (Arr::isTrue($notifications, 'declined_by_host.enabled')) {
281 $email = Arr::get($notifications, 'declined_by_host.email', []);
282 EmailNotificationService::bookingCancelOrRejectEmail($booking, $email, 'guest', 'reject');
283 }
284 }
285 }
286