PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.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 1.7.2 All 33 releases
fluent-booking / app / Services / EmailNotificationService.php

EmailNotificationService.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.0, at app/Services/EmailNotificationService.php

389 lines 15.5 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\Services;
4
5 use FluentBooking\App\App;
6 use FluentBooking\App\Models\Booking;
7 use FluentBooking\App\Services\Libs\Emogrifier\Emogrifier;
8 use FluentBooking\Framework\Support\Arr;
9
10 class EmailNotificationService
11 {
12 /**
13 * @param \FluentBooking\App\Models\Booking $booking
14 * @param $email
15 * @param $emailTo
16 * @param $actionType
17 * @return bool|mixed
18 */
19 public static function emailOnBooked(Booking $booking, $email, $emailTo, $actionType = 'scheduled')
20 {
21 $emailSubject = EditorShortCodeParser::parse($email['subject'], $booking);
22 $emailBody = EditorShortCodeParser::parse($email['body'], $booking);
23
24 $globalSettings = Helper::getGlobalSettings();
25 $useHostName = Arr::get($globalSettings, 'emailing.use_host_name', 'yes');
26 $useHostEmailOnReply = Arr::get($globalSettings, 'emailing.use_host_email_on_reply', 'yes');
27 $attachIcsFile = Arr::get($globalSettings, 'emailing.attach_ics_on_confirmation', 'no');
28 $settingsFromName = Arr::get($globalSettings, 'emailing.from_name', '');
29 $settingsFromEmail = Arr::get($globalSettings, 'emailing.from_email', '');
30 $settingsReplyToName = Arr::get($globalSettings, 'emailing.reply_to_name', '');
31 $settingsReplyToEmail = Arr::get($globalSettings, 'emailing.reply_to_email', '');
32
33 $calendarEvent = $booking->calendar_event;
34
35 $attachments = [];
36 if ($attachIcsFile == 'yes' && $actionType == 'scheduled') {
37 $attachments = self::prepareAttachments($booking);
38 }
39
40 $guestAddress = self::getGuestAddress($booking);
41
42 $authors = $booking->getHostsDetails(false);
43
44 if ($emailTo == 'guest') {
45 $authors = [$authors[0]];
46 }
47
48 foreach ($authors as $author)
49 {
50 $hostName = '';
51 $hostAddress = $author['email'];
52 if ($author['name']) {
53 $hostName = $author['name'];
54 $hostAddress = sprintf('%1s <%2s>', $author['name'], $author['email']);
55 }
56
57 // Assign-To & Reply-To
58 if ('host' == $emailTo) {
59 $to = $hostAddress;
60 $replyTo = sprintf('%1s <%2s>', $settingsReplyToName, $settingsReplyToEmail ?: $settingsFromEmail);
61 $from = sprintf('%1s <%2s>', $settingsFromName, $settingsFromEmail);
62 } else {
63 $to = $guestAddress;
64 $replyName = $useHostName == 'no' ? $settingsReplyToName : $hostName;
65 $fromName = $useHostName == 'no' ? $settingsFromName : $hostName;
66
67 $replayToEmail = $useHostEmailOnReply == 'no' ? $settingsReplyToEmail : $author['email'];
68 $replyFromEmail = $useHostEmailOnReply == 'no' ? $settingsFromEmail : $replayToEmail;
69
70 $replyTo = sprintf('%1s <%2s>', $replyName, $replayToEmail);
71 $from = sprintf('%1s <%2s>', $fromName, $replyFromEmail);
72 }
73
74 $headers = self::prepareEmailHeaders($replyTo, $from, $email);
75
76 $body = (string)App::make('view')->make('emails.template', [
77 'email_body' => $emailBody,
78 'email_footer' => self::getGlobalEmailFooter(),
79 ]);
80
81 $emogrifier = new Emogrifier($body);
82 $emogrifier->disableInvisibleNodeRemoval();
83 $body = (string)$emogrifier->emogrify();
84
85 Mailer::send($to, $emailSubject, $body, $headers, $attachments);
86 }
87
88 if ($attachments) {
89 wp_delete_file($attachments[0]);
90 }
91 }
92
93 /**
94 * @param \FluentBooking\App\Models\Booking $booking
95 * @param $email
96 * @param $time
97 * @param $emailTo
98 * @return bool|mixed
99 */
100 public static function reminderEmail(Booking $booking, $email, $emailTo)
101 {
102 $globalSettings = Helper::getGlobalSettings();
103 $useHostName = Arr::get($globalSettings, 'emailing.use_host_name', 'yes');
104 $useHostEmailOnReply = Arr::get($globalSettings, 'emailing.use_host_email_on_reply', 'yes');
105 $settingsFromName = Arr::get($globalSettings, 'emailing.from_name', '');
106 $settingsFromEmail = Arr::get($globalSettings, 'emailing.from_email', '');
107 $settingsReplyToName = Arr::get($globalSettings, 'emailing.reply_to_name', '');
108 $settingsReplyToEmail = Arr::get($globalSettings, 'emailing.reply_to_email', '');
109
110 $calendarEvent = $booking->calendar_event;
111
112 $guestAddress = self::getGuestAddress($booking);
113
114 $authors = $booking->getHostsDetails(false);
115
116 if ($emailTo == 'guest') {
117 $authors = [$authors[0]];
118 }
119
120 foreach ($authors as $author) {
121 $hostAddress = $author['email'];
122 $hostName = '';
123 if ($author['name']) {
124 $hostName = $author['name'];
125 $hostAddress = sprintf('%1s <%2s>', $author['name'], $author['email']);
126 }
127
128 $from = '';
129 if ('host' == $emailTo) {
130 $to = $hostAddress;
131 $replyTo = sprintf('%1s <%2s>', $settingsReplyToName, $settingsReplyToEmail ?: $settingsFromEmail);
132 $from = sprintf('%1s <%2s>', $settingsFromName, $settingsFromEmail);
133 } else {
134 $to = $guestAddress;
135 $replyName = $useHostName == 'no' ? $settingsReplyToName : $hostName;
136 $formName = $useHostName == 'no' ? $settingsFromName : $hostName;
137
138 $replayToEmail = $useHostEmailOnReply == 'no' ? $settingsReplyToEmail : $author['email'];
139 $replyFromEmail = $useHostEmailOnReply == 'no' ? $settingsFromEmail : $replayToEmail;
140
141 $replyTo = sprintf('%1s <%2s>', $replyName, $replayToEmail);
142 $from = sprintf('%1s <%2s>', $formName, $replyFromEmail);
143 }
144
145 $headers = self::prepareEmailHeaders($replyTo, $from, $email);
146
147 $subject = EditorShortCodeParser::parse($email['subject'], $booking);
148 $html = EditorShortCodeParser::parse($email['body'], $booking);
149
150 $body = (string)App::make('view')->make('emails.template', [
151 'email_body' => $html,
152 'email_footer' => self::getGlobalEmailFooter()
153 ]);
154
155 $emogrifier = new Emogrifier($body);
156 $emogrifier->disableInvisibleNodeRemoval();
157 $body = (string)$emogrifier->emogrify();
158
159 Mailer::send($to, $subject, $body, $headers);
160
161 do_action('fluent_booking/log_booking_note', [
162 'title' => __('Reminder Email Sent', 'fluent-booking'),
163 'type' => 'activity',
164 /* translators: Email address where the reminder email was sent */
165 'description' => sprintf(__('Reminder email sent to %s.', 'fluent-booking'), $emailTo),
166 'booking_id' => $booking->id
167 ]);
168 }
169 }
170
171 /**
172 * @param \FluentBooking\App\Models\Booking $booking
173 * @param $email
174 * @param $emailTo
175 * @param $actionType
176 * @return bool|mixed
177 */
178 public static function bookingCancelOrRejectEmail(Booking $booking, $email, $emailTo, $actionType = 'cancel')
179 {
180 $globalSettings = Helper::getGlobalSettings();
181 $useHostName = Arr::get($globalSettings, 'emailing.use_host_name', 'yes');
182 $useHostEmailOnReply = Arr::get($globalSettings, 'emailing.use_host_email_on_reply', 'yes');
183 $settingsFromName = Arr::get($globalSettings, 'emailing.from_name', '');
184 $settingsFromEmail = Arr::get($globalSettings, 'emailing.from_email', '');
185 $settingsReplyToName = Arr::get($globalSettings, 'emailing.reply_to_name', '');
186 $settingsReplyToEmail = Arr::get($globalSettings, 'emailing.reply_to_email', '');
187
188 $calendarEvent = $booking->calendar_event;
189
190 $guestAddress = self::getGuestAddress($booking);
191
192 $authors = $booking->getHostsDetails(false);
193
194 if ($emailTo == 'guest') {
195 $authors = [$authors[0]];
196 }
197
198 foreach ($authors as $author) {
199 $hostAddress = $author['email'];
200 $hostName = '';
201 if ($author['name']) {
202 $hostName = $author['name'];
203 $hostAddress = sprintf('%1s <%2s>', $author['name'], $author['email']);
204 }
205
206 if ('host' == $emailTo) {
207 $to = $hostAddress;
208 $replyTo = sprintf('%1s <%2s>', $settingsReplyToName, $settingsReplyToEmail ?: $settingsFromEmail);
209 $from = sprintf('%1s <%2s>', $settingsFromName, $settingsFromEmail);
210 } else {
211 $to = $guestAddress;
212 $replyName = $useHostName == 'no' ? $settingsReplyToName : $hostName;
213 $formName = $useHostName == 'no' ? $settingsFromName : $hostName;
214
215 $replayToEmail = $useHostEmailOnReply == 'no' ? $settingsReplyToEmail : $author['email'];
216 $replyFromEmail = $useHostEmailOnReply == 'no' ? $settingsFromEmail : $replayToEmail;
217
218 $replyTo = sprintf('%1s <%2s>', $replyName, $replayToEmail);
219 $from = sprintf('%1s <%2s>', $formName, $replyFromEmail);
220 }
221
222 $headers = self::prepareEmailHeaders($replyTo, $from, $email);
223
224 $subject = EditorShortCodeParser::parse($email['subject'], $booking);
225 $html = EditorShortCodeParser::parse($email['body'], $booking);
226
227 $body = (string)App::make('view')->make('emails.template', [
228 'email_body' => $html,
229 'email_footer' => self::getGlobalEmailFooter()
230 ]);
231
232 $emogrifier = new Emogrifier($body);
233 $emogrifier->disableInvisibleNodeRemoval();
234 $body = (string)$emogrifier->emogrify();
235
236 Mailer::send($to, $subject, $body, $headers);
237
238 $actionType = $actionType == 'reject' ? __('Rejection', 'fluent-booking') : __('Cancellation', 'fluent-booking');
239
240 do_action('fluent_booking/log_booking_note', [
241 'title' => $actionType . __(' booking email sent to ', 'fluent-booking') . $emailTo,
242 'type' => 'activity',
243 'description' => $actionType . __(' email sent to ', 'fluent-booking') . $emailTo,
244 'booking_id' => $booking->id
245 ]);
246 }
247 }
248
249 /**
250 * @param \FluentBooking\App\Models\Booking $booking
251 * @param $email
252 * @param $emailTo
253 * @return bool|mixed
254 */
255 public static function bookingRescheduledEmail(Booking $booking, $email, $emailTo)
256 {
257 $globalSettings = Helper::getGlobalSettings();
258 $useHostName = Arr::get($globalSettings, 'emailing.use_host_name', 'yes');
259 $useHostEmailOnReply = Arr::get($globalSettings, 'emailing.use_host_email_on_reply', 'yes');
260 $attachIcsFile = Arr::get($globalSettings, 'emailing.attach_ics_on_confirmation', 'no');
261 $settingsFromName = Arr::get($globalSettings, 'emailing.from_name', '');
262 $settingsFromEmail = Arr::get($globalSettings, 'emailing.from_email', '');
263 $settingsReplyToName = Arr::get($globalSettings, 'emailing.reply_to_name', '');
264 $settingsReplyToEmail = Arr::get($globalSettings, 'emailing.reply_to_email', '');
265
266 $calendarEvent = $booking->calendar_event;
267
268 $attachments = [];
269 if ($attachIcsFile == 'yes') {
270 $attachments = self::prepareAttachments($booking);
271 }
272
273 $guestAddress = self::getGuestAddress($booking);
274
275 $authors = $booking->getHostsDetails(false);
276
277 if ($emailTo == 'guest') {
278 $authors = [$authors[0]];
279 }
280
281 foreach ($authors as $author) {
282 $hostAddress = $author['email'];
283 $hostName = '';
284 if ($author['name']) {
285 $hostName = $author['name'];
286 $hostAddress = sprintf('%1s <%2s>', $author['name'], $author['email']);
287 }
288
289 if ('host' == $emailTo) {
290 $to = $hostAddress;
291 $replyTo = sprintf('%1s <%2s>', $settingsReplyToName, $settingsReplyToEmail ?: $settingsFromEmail);
292 $from = sprintf('%1s <%2s>', $settingsFromName, $settingsFromEmail);
293 } else {
294 $to = $guestAddress;
295 $replyName = $useHostName == 'no' ? $settingsReplyToName : $hostName;
296 $formName = $useHostName == 'no' ? $settingsFromName : $hostName;
297
298 $replayToEmail = $useHostEmailOnReply == 'no' ? $settingsReplyToEmail : $author['email'];
299 $replyFromEmail = $useHostEmailOnReply == 'no' ? $settingsFromEmail : $replayToEmail;
300
301 $replyTo = sprintf('%1s <%2s>', $replyName, $replayToEmail);
302 $from = sprintf('%1s <%2s>', $formName, $replyFromEmail);
303 }
304
305 $headers = self::prepareEmailHeaders($replyTo, $from, $email);
306
307 $subject = EditorShortCodeParser::parse($email['subject'], $booking);
308 $html = EditorShortCodeParser::parse($email['body'], $booking);
309
310 $body = (string)App::make('view')->make('emails.template', [
311 'email_body' => $html,
312 'email_footer' => self::getGlobalEmailFooter()
313 ]);
314
315 $emogrifier = new Emogrifier($body);
316 $emogrifier->disableInvisibleNodeRemoval();
317 $body = (string)$emogrifier->emogrify();
318
319 Mailer::send($to, $subject, $body, $headers, $attachments);
320
321 do_action('fluent_booking/log_booking_note', [
322 'title' => __('Rescheduled booking email sent to', 'fluent-booking') . ' ' . $emailTo,
323 'type' => 'activity',
324 /* translators: Email address where the rescheduling email was sent */
325 'description' => sprintf(__('Rescheduling email sent to %s', 'fluent-booking'), $emailTo),
326 'booking_id' => $booking->id
327 ]);
328 }
329
330 if ($attachments) {
331 wp_delete_file($attachments[0]);
332 }
333 }
334
335 private static function getGuestAddress($booking)
336 {
337 $guestAddress = $booking->email;
338 if ($booking->first_name && $booking->last_name) {
339 $guestAddress = sprintf('%1s %2s <%3s>', $booking->first_name, $booking->last_name, $booking->email);
340 } else if ($booking->first_name) {
341 $guestAddress = sprintf('%1s <%2s>', $booking->first_name, $booking->email);
342 }
343
344 if ($additionalGuests = $booking->getAdditionalGuests()) {
345 $guestAddress .= ', ' . implode(', ', $additionalGuests);
346 }
347 return $guestAddress;
348 }
349
350 private static function prepareEmailHeaders($replyTo, $from, $email)
351 {
352 $headers = [
353 'Reply-To: ' . $replyTo
354 ];
355
356 if ($from) {
357 $headers[] = 'From: ' . $from;
358 }
359
360 if (isset($email['recipients'])) {
361 $headers[] = 'bcc: ' . implode(', ', $email['recipients']);
362 }
363
364 return $headers;
365 }
366
367 private static function prepareAttachments($booking)
368 {
369 $icsContent = BookingService::generateBookingICS($booking);
370
371 $filePath = wp_tempnam(null, 'event') . '.ics';
372
373 if (WP_Filesystem()) {
374 global $wp_filesystem;
375 $wp_filesystem->put_contents($filePath, $icsContent);
376 return [$filePath];
377 }
378 return [];
379 }
380
381 public static function getGlobalEmailFooter()
382 {
383 $globalSettings = Helper::getGlobalSettings();
384
385 return Arr::get($globalSettings, 'emailing.email_footer', '');
386 }
387
388 }
389