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

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