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 / EditorShortCodeParser.php

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

458 lines 14.0 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 return Arr::get(self::$store['custom_booking_data'], $key);
192 }
193
194 return '';
195 }
196
197 protected static function getHostData($key)
198 {
199 $host = static::$store['host'];
200
201 if (is_null($host)) {
202 return '';
203 }
204
205 if ($key == 'timezone') {
206 $booking = static::$store['booking'];
207 return $booking->getHostTimezone();
208 }
209
210 return Arr::get($host, $key, '');
211 }
212
213 protected static function getTeamMembersData($key)
214 {
215 $teamMembers = static::$store['team_members'];
216
217 if (empty($teamMembers)) {
218 return '';
219 }
220
221 list($key, $value) = explode('.', $key);
222
223 return Arr::get($teamMembers, $key - 1 . '.' . $value, '');
224 }
225
226 protected static function getGuestData($key)
227 {
228 $guest = static::$store['booking'];
229 if (is_null($guest)) {
230 return '';
231 }
232
233 if ('full_name' == $key) {
234 return $guest['first_name'] . ' ' . $guest['last_name'];
235 }
236 if ('timezone' == $key) {
237 $booking = static::$store['booking'];
238 return $booking->person_time_zone;
239 }
240 if ('note' == $key) {
241 return $guest->getMessage();
242 }
243
244 if ($key == 'form_data_html') {
245 return __('will be available soon', 'fluent-booking');
246 }
247
248 return Arr::get($guest, $key, '');
249 }
250
251 protected static function getBookingEventData($key)
252 {
253 $bookingEvent = static::$store['booking_event'];
254
255 if (is_null($bookingEvent)) {
256 return '';
257 }
258
259 $fillables = (new CalendarSlot())->getFillable();
260
261 if (in_array($key, $fillables)) {
262 return $bookingEvent->{$key};
263 }
264
265 return '';
266 }
267
268 protected static function getCalendarData($key)
269 {
270 $calendar = static::$store['calendar'];
271
272 if (is_null($calendar)) {
273 return '';
274 }
275
276 $fillables = (new Calendar())->getFillable();
277
278 if (in_array($key, $fillables)) {
279 return $calendar->{$key};
280 }
281
282 return '';
283 }
284
285 protected static function getPaymentData($key)
286 {
287 $booking = static::$store['booking'];
288
289 if (is_null($booking)) {
290 return '';
291 }
292
293 if (!$booking->payment_status) {
294 return '';
295 }
296
297 if (is_null(static::$store['payment_order'])) {
298 static::$store['payment_order'] = $booking->payment_order;
299 }
300
301 if (!static::$store['payment_order']) {
302 return '';
303 }
304
305 $order = static::$store['payment_order'];
306
307 if ($key == 'payment_total') {
308 $isZeroDecimal = CurrenciesHelper::isZeroDecimal($order->currency);
309 if ($isZeroDecimal) {
310 return $order->total_amount;
311 } else {
312 return $order->total_amount / 100;
313 }
314 }
315
316 if ($key == 'receipt_html') {
317 return apply_filters('fluent_booking/payment_receipt_html', '', $booking->hash);
318 }
319
320 if ($key == 'payment_status') {
321 return $order->status;
322 }
323
324 if ($key == 'payment_currency') {
325 return $order->currency;
326 }
327
328 if ($key == 'payment_date') {
329 return $order->created_at;
330 }
331
332 $fillables = (new \FluentBookingPro\App\Models\Order())->getFillable();
333
334 if (in_array($key, $fillables)) {
335 return $order->{$key};
336 }
337
338 return '';
339 }
340
341 protected static function getUserData($key)
342 {
343 if (is_null(static::$store['user'])) {
344 static::$store['user'] = wp_get_current_user();
345 }
346 return static::$store['user']->{$key};
347 }
348
349 protected static function getWPData($key)
350 {
351 if ('site_url' == $key) {
352 return site_url();
353 }
354 if ('admin_email' == $key) {
355 return get_option('admin_email');
356 }
357 if ('site_title' == $key) {
358 return get_option('blogname');
359 }
360 return $key;
361 }
362
363 protected static function getOtherData($key)
364 {
365 self::$store['meeting_bookmarks'] ??= static::$store['booking']->getMeetingBookmarks();
366
367 if (0 === strpos($key, 'date.')) {
368 $format = str_replace('date.', '', $key);
369 return gmdate($format, strtotime(current_time('mysql'))); // phpcs:ignore WordPress.DateTime.RestrictedFunctions.date_date
370 } elseif ('add_booking_to_calendar' === $key) {
371 return static::parseShortCodes(Helper::getAddToCalendarHtml());
372 } elseif ('add_to_g_calendar_url' === $key) {
373 return Arr::get(self::$store['meeting_bookmarks'], 'google.url');
374 } elseif ('add_to_ol_calendar_url' === $key) {
375 return Arr::get(self::$store['meeting_bookmarks'], 'outlook.url');
376 } elseif ('add_to_ms_calendar_url' === $key) {
377 return Arr::get(self::$store['meeting_bookmarks'], 'msoffice.url');
378 } elseif ('add_to_ics_calendar_url' === $key) {
379 return Arr::get(self::$store['meeting_bookmarks'], 'other.url');
380 }
381
382 return $key;
383 }
384
385 protected static function parseShortCodes($parsable)
386 {
387 if (is_array($parsable)) {
388 return static::parseFromArray($parsable);
389 }
390
391 return static::parseFromString($parsable);
392 }
393
394 protected static function parseFromArray($parsable)
395 {
396 foreach ($parsable as $key => $value) {
397 if (is_array($value)) {
398 $parsable[$key] = static::parseFromArray($value);
399 } else {
400 $parsable[$key] = static::parseFromString($value);
401 }
402 }
403
404 return $parsable;
405 }
406
407 protected static function parseFromString($parsable)
408 {
409 if (!$parsable) {
410 return '';
411 }
412
413 return preg_replace_callback('/({{|##)+(.*?)(}}|##)/', function ($matches) {
414 $value = '';
415
416 if (empty($matches[2])) {
417 return '';
418 }
419
420 $match = $matches[2];
421
422 if (false !== strpos($match, 'guest.')) {
423 $guestProperty = substr($match, strlen('guest.'));
424 $value = static::getGuestData($guestProperty);
425 } elseif (false !== strpos($match, 'booking.custom.')) {
426 $customBookingProp = substr($match, strlen('booking.custom.'));
427 $value = static::getBookingCustomData($customBookingProp);
428 } elseif (false !== strpos($match, 'booking.')) {
429 $bookingProperty = substr($match, strlen('booking.'));
430 $value = static::getBookingData($bookingProperty);
431 } elseif (false !== strpos($match, 'host.')) {
432 $hostProperty = substr($match, strlen('host.'));
433 $value = static::getHostData($hostProperty);
434 } elseif (false !== strpos($match, 'team_member.')) {
435 $teamMemberProperty = substr($match, strlen('team_member.'));
436 $value = static::getTeamMembersData($teamMemberProperty);
437 } elseif (false !== strpos($match, 'event.')) {
438 $eventProperty = substr($match, strlen('event.'));
439 $value = static::getBookingEventData($eventProperty);
440 } elseif (false !== strpos($match, 'calendar.')) {
441 $calendarProperty = substr($match, strlen('calendar.'));
442 $value = static::getCalendarData($calendarProperty);
443 } elseif (false !== strpos($match, 'payment.')) {
444 $paymentProperty = substr($match, strlen('payment.'));
445 $value = static::getPaymentData($paymentProperty);
446 } else {
447 $value = static::getOtherData($match);
448 }
449
450 if (static::$requireHtml && is_array($value)) {
451 $value = Helper::fcalImplodeRecursive(', ', $value);
452 }
453
454 return $value;
455 }, $parsable);
456 }
457 }
458