PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.7.1
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.7.1
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.7.1, at app/Services/EditorShortCodeParser.php

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