PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.6.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.6.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 / EditorShortCodeParser.php

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

483 lines 15.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\Services;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Models\Calendar;
7 use FluentBooking\App\Models\CalendarSlot;
8 use FluentBooking\Framework\Support\Arr;
9
10 class EditorShortCodeParser
11 {
12 protected static $requireHtml = true;
13
14 protected static $store = [
15 'booking' => null,
16 'calendar_booking' => null,
17 'calendar' => null,
18 'host' => null,
19 'user' => null,
20 'custom_booking_data' => null,
21 'payment_order' => null
22 ];
23
24 public static function parse($parsable, $booking, $requireHtml = true)
25 {
26 try {
27 static::$requireHtml = $requireHtml;
28 static::setData($booking);
29 return static::parseShortCodes($parsable);
30 } catch (\Exception $e) {
31 if (defined('WP_DEBUG') && WP_DEBUG) {
32 error_log($e->getTraceAsString());
33 }
34 return '';
35 }
36 }
37
38 protected static function setData($booking)
39 {
40 static::$store['booking'] = $booking;
41 static::$store['booking_event'] = $booking->calendar_event;
42 static::$store['calendar'] = $booking->calendar;
43 static::$store['host'] = $booking->getHostDetails(false);
44 static::$store['team_members'] = $booking->getHostsDetails(false, $booking->host_user_id);
45 static::$store['custom_booking_data'] = null;
46 static::$store['payment_order'] = null;
47 static::$store['meeting_bookmarks'] = null;
48 }
49
50 protected static function getBookingData($key)
51 {
52 $bookingEvent = static::$store['booking_event'];
53 $calendar = static::$store['calendar'];
54 $booking = static::$store['booking'];
55
56 if (!$bookingEvent || !$calendar || !$booking) {
57 return '';
58 }
59
60 if ($key == 'event_name') {
61 return $bookingEvent->title;
62 }
63
64 if ($key == 'description') {
65 return $bookingEvent->description;
66 }
67
68 if ($key == 'booking_title') {
69 return $booking->getBookingTitle();
70 }
71
72 if ($key == 'additional_guests') {
73 return $booking->getAdditionalGuests(true);
74 }
75
76 if ($key == 'full_start_end_guest_timezone') {
77 return $booking->getShortBookingDateTime($booking->person_time_zone) . ' (' . $booking->person_time_zone . ')';
78 }
79
80 if ($key == 'full_start_end_host_timezone') {
81 return $booking->getShortBookingDateTime($booking->getHostTimezone()) . ' (' . $booking->getHostTimezone() . ')';
82 }
83
84 if ($key == 'full_start_and_end_guest_timezone') {
85 return $booking->getFullBookingDateTimeText($booking->person_time_zone) . ' (' . $booking->person_time_zone . ')';
86 }
87
88 if ($key == 'full_start_and_end_host_timezone') {
89 return $booking->getFullBookingDateTimeText($booking->getHostTimezone()) . ' (' . $booking->getHostTimezone() . ')';
90 }
91
92 if ($key == 'start_date_time') {
93 return $booking->start_time;
94 }
95
96 if (str_starts_with($key, 'start_date_time_for_attendee')) {
97 $format = preg_match('/format\.([a-zA-Z\-]+)/', $key, $matches) ? $matches[1] : 'Y-m-d H:i:s';
98 return DateTimeHelper::convertFromUtc($booking->start_time, $booking->person_time_zone, $format);
99 }
100
101 if (str_starts_with($key, 'start_date_time_for_host')) {
102 $format = preg_match('/format\.([a-zA-Z\-]+)/', $key, $matches) ? $matches[1] : 'Y-m-d H:i:s';
103 return DateTimeHelper::convertFromUtc($booking->start_time, $booking->getHostTimezone(), $format);
104 }
105
106 if ($key == 'cancel_reason') {
107 return $booking->getCancelReason(false, true);
108 }
109
110 if ($key == 'reject_reason') {
111 return $booking->getRejectReason(false, true);
112 }
113
114 if ($key == 'reschedule_reason') {
115 return $booking->getRescheduleReason();
116 }
117
118 if ($key == 'previous_meeting_time') {
119 return $booking->getPreviousMeetingTime($booking->getHostTimezone()) . ' (' . $booking->getHostTimezone() . ')';
120 }
121
122 if ($key == 'start_time_human_format') {
123 if (time() > strtotime($booking->start_time)) {
124 $suffix = __(' ago', 'fluent-booking');
125 } else {
126 $suffix = __(' from now', 'fluent-booking');
127 }
128
129 return human_time_diff(time(), strtotime($booking->start_time)) . ' ' . $suffix;
130 }
131
132 if ($key == 'cancelation_url') {
133 return $booking->getCancelUrl();
134 }
135
136 if ($key == 'reschedule_url') {
137 return $booking->getRescheduleUrl();
138 }
139
140 if ($key == 'admin_booking_url') {
141 return Helper::getAdminBookingUrl($booking->id) . '&period=upcoming';
142 }
143
144 if ($key == 'booking_confirm_url') {
145 return Helper::getAdminBookingUrl($booking->id) . '&period=pending&confirm_booking=true';
146 }
147
148 if ($key == 'booking_reject_url') {
149 return Helper::getAdminBookingUrl($booking->id) . '&period=pending&reject_booking=true';
150 }
151
152 if ($key == 'location_details_html') {
153 return $booking->getLocationDetailsHtml();
154 }
155
156 if ($key == 'location_details_text') {
157 return $booking->getLocationAsText();
158 }
159
160 if ($key == 'booking_hash') {
161 return $booking->hash;
162 }
163
164 $fillables = (new Booking())->getFillable();
165 $fillables[] = 'id';
166 $fillables[] = 'created_at';
167 $fillables[] = 'updated_at';
168
169 if (in_array($key, $fillables)) {
170 return $booking->{$key};
171 }
172
173 return '';
174 }
175
176 protected static function getBookingCustomData($key)
177 {
178 $booking = static::$store['booking'];
179 if (!$booking) {
180 return '';
181 }
182
183 if (self::$store['custom_booking_data'] === null) {
184 self::$store['custom_booking_data'] = $booking->getMeta('custom_fields_data', []);
185 }
186
187 if (self::$store['custom_booking_data']) {
188 if (preg_match('/format\.([a-zA-Z\-]+)/', $key, $matches)) {
189 $value = Arr::get(self::$store['custom_booking_data'], preg_split('/\.format\./', $key)[0]);
190 return $value ? gmdate($matches[1], strtotime($value)) : ''; // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
191 }
192
193 $customField = BookingFieldService::getBookingFieldByName($booking->calendar_event, $key);
194
195 if (Arr::get($customField, 'type') == 'file') {
196 return self::getUploadedFileUrl(Arr::get(self::$store['custom_booking_data'], $key));
197 }
198
199 if (Arr::get($customField, 'type') == 'hidden') {
200 return self::parseShortCodes(Arr::get(self::$store['custom_booking_data'], $key));
201 }
202
203 return Arr::get(self::$store['custom_booking_data'], $key);
204 }
205
206 return '';
207 }
208
209 protected static function getHostData($key)
210 {
211 $host = static::$store['host'];
212
213 if (is_null($host)) {
214 return '';
215 }
216
217 if ($key == 'timezone') {
218 $booking = static::$store['booking'];
219 return $booking->getHostTimezone();
220 }
221
222 return Arr::get($host, $key, '');
223 }
224
225 protected static function getTeamMembersData($key)
226 {
227 $teamMembers = static::$store['team_members'];
228
229 if (empty($teamMembers)) {
230 return '';
231 }
232
233 list($key, $value) = explode('.', $key);
234
235 return Arr::get($teamMembers, $key - 1 . '.' . $value, '');
236 }
237
238 protected static function getGuestData($key)
239 {
240 $guest = static::$store['booking'];
241 if (is_null($guest)) {
242 return '';
243 }
244
245 if ('full_name' == $key) {
246 return $guest['first_name'] . ' ' . $guest['last_name'];
247 }
248 if ('timezone' == $key) {
249 $booking = static::$store['booking'];
250 return $booking->person_time_zone;
251 }
252 if ('note' == $key) {
253 return $guest->getMessage();
254 }
255
256 if ($key == 'form_data_html') {
257 return __('will be available soon', 'fluent-booking');
258 }
259
260 return Arr::get($guest, $key, '');
261 }
262
263 protected static function getBookingEventData($key)
264 {
265 $bookingEvent = static::$store['booking_event'];
266
267 if (is_null($bookingEvent)) {
268 return '';
269 }
270
271 $fillables = (new CalendarSlot())->getFillable();
272
273 if (in_array($key, $fillables) || isset($bookingEvent->{$key})) {
274 return $bookingEvent->{$key};
275 }
276
277 return '';
278 }
279
280 protected static function getCalendarData($key)
281 {
282 $calendar = static::$store['calendar'];
283
284 if (is_null($calendar)) {
285 return '';
286 }
287
288 $fillables = (new Calendar())->getFillable();
289
290 if (in_array($key, $fillables) || isset($calendar->{$key})) {
291 return $calendar->{$key};
292 }
293
294 return '';
295 }
296
297 protected static function getPaymentData($key)
298 {
299 $booking = static::$store['booking'];
300
301 if (is_null($booking)) {
302 return '';
303 }
304
305 if (!$booking->payment_status) {
306 return '';
307 }
308
309 if (is_null(static::$store['payment_order'])) {
310 static::$store['payment_order'] = $booking->payment_order;
311 }
312
313 if (!static::$store['payment_order']) {
314 return '';
315 }
316
317 $order = static::$store['payment_order'];
318
319 if ($key == 'payment_total') {
320 $isZeroDecimal = CurrenciesHelper::isZeroDecimal($order->currency);
321 if ($isZeroDecimal) {
322 return $order->total_amount;
323 } else {
324 return $order->total_amount / 100;
325 }
326 }
327
328 if ($key == 'receipt_html') {
329 return apply_filters('fluent_booking/payment_receipt_html', '', $booking->hash);
330 }
331
332 if ($key == 'payment_status') {
333 return $order->status;
334 }
335
336 if ($key == 'payment_currency') {
337 return $order->currency;
338 }
339
340 if ($key == 'payment_date') {
341 return $order->created_at;
342 }
343
344 $fillables = (new \FluentBookingPro\App\Models\Order())->getFillable();
345
346 if (in_array($key, $fillables)) {
347 return $order->{$key};
348 }
349
350 return '';
351 }
352
353 protected static function getUserData($key)
354 {
355 if (is_null(static::$store['user'])) {
356 static::$store['user'] = wp_get_current_user();
357 }
358 return static::$store['user']->{$key};
359 }
360
361 protected static function getWPData($key)
362 {
363 if ('site_url' == $key) {
364 return site_url();
365 }
366 if ('admin_email' == $key) {
367 return get_option('admin_email');
368 }
369 if ('site_title' == $key) {
370 return get_option('blogname');
371 }
372 return $key;
373 }
374
375 protected static function getOtherData($key)
376 {
377 self::$store['meeting_bookmarks'] ??= static::$store['booking']->getMeetingBookmarks();
378
379 if (0 === strpos($key, 'date.')) {
380 $format = str_replace('date.', '', $key);
381 return gmdate($format, strtotime(current_time('mysql'))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
382 } elseif ('add_booking_to_calendar' === $key) {
383 return static::parseShortCodes(Helper::getAddToCalendarHtml());
384 } elseif ('add_to_g_calendar_url' === $key) {
385 return Arr::get(self::$store['meeting_bookmarks'], 'google.url');
386 } elseif ('add_to_ol_calendar_url' === $key) {
387 return Arr::get(self::$store['meeting_bookmarks'], 'outlook.url');
388 } elseif ('add_to_ms_calendar_url' === $key) {
389 return Arr::get(self::$store['meeting_bookmarks'], 'msoffice.url');
390 } elseif ('add_to_ics_calendar_url' === $key) {
391 return Arr::get(self::$store['meeting_bookmarks'], 'other.url');
392 }
393
394 return $key;
395 }
396
397 protected static function getUploadedFileUrl($fieldValue)
398 {
399 if (empty($fieldValue)) {
400 return '';
401 }
402
403 $files = array_map(function($file) {
404 return '<a href="' . esc_url($file) . '" style="color:#222;font-size:15px;" target="_blank" download="' . esc_attr(basename($file)) . '">' . esc_html(basename($file)) . '</a>';
405 }, $fieldValue);
406
407 return '<ul><li>' . implode('</li><li>', $files) . '</li></ul>';
408 }
409
410 protected static function parseShortCodes($parsable)
411 {
412 if (is_array($parsable)) {
413 return static::parseFromArray($parsable);
414 }
415
416 return static::parseFromString($parsable);
417 }
418
419 protected static function parseFromArray($parsable)
420 {
421 foreach ($parsable as $key => $value) {
422 if (is_array($value)) {
423 $parsable[$key] = static::parseFromArray($value);
424 } else {
425 $parsable[$key] = static::parseFromString($value);
426 }
427 }
428
429 return $parsable;
430 }
431
432 protected static function parseFromString($parsable)
433 {
434 if (!$parsable) {
435 return '';
436 }
437
438 return preg_replace_callback('/({{|##)+(.*?)(}}|##)/', function ($matches) {
439 $value = '';
440
441 if (empty($matches[2])) {
442 return '';
443 }
444
445 $match = $matches[2];
446
447 if (false !== strpos($match, 'guest.')) {
448 $guestProperty = substr($match, strlen('guest.'));
449 $value = static::getGuestData($guestProperty);
450 } elseif (false !== strpos($match, 'booking.custom.')) {
451 $customBookingProp = substr($match, strlen('booking.custom.'));
452 $value = static::getBookingCustomData($customBookingProp);
453 } elseif (false !== strpos($match, 'booking.')) {
454 $bookingProperty = substr($match, strlen('booking.'));
455 $value = static::getBookingData($bookingProperty);
456 } elseif (false !== strpos($match, 'host.')) {
457 $hostProperty = substr($match, strlen('host.'));
458 $value = static::getHostData($hostProperty);
459 } elseif (false !== strpos($match, 'team_member.')) {
460 $teamMemberProperty = substr($match, strlen('team_member.'));
461 $value = static::getTeamMembersData($teamMemberProperty);
462 } elseif (false !== strpos($match, 'event.')) {
463 $eventProperty = substr($match, strlen('event.'));
464 $value = static::getBookingEventData($eventProperty);
465 } elseif (false !== strpos($match, 'calendar.')) {
466 $calendarProperty = substr($match, strlen('calendar.'));
467 $value = static::getCalendarData($calendarProperty);
468 } elseif (false !== strpos($match, 'payment.')) {
469 $paymentProperty = substr($match, strlen('payment.'));
470 $value = static::getPaymentData($paymentProperty);
471 } else {
472 $value = static::getOtherData($match);
473 }
474
475 if (static::$requireHtml && is_array($value)) {
476 $value = Helper::fcalImplodeRecursive(', ', $value);
477 }
478
479 return $value;
480 }, $parsable);
481 }
482 }
483