PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.24
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.24
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 / Hooks / Handlers / GlobalNotificationHandler.php

GlobalNotificationHandler.php in Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution 1.5.24, at app/Hooks/Handlers/GlobalNotificationHandler.php

127 lines 4.1 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\Hooks\Handlers;
4
5 use FluentBooking\App\Models\Booking;
6 use FluentBooking\App\Models\Meta;
7 use FluentBooking\Framework\Support\Arr;
8 use FluentBooking\App\Services\EditorShortCodeParser;
9 use FluentBooking\App\Services\Integrations\GlobalNotificationService;
10
11 class GlobalNotificationHandler
12 {
13 /**
14 * @var GlobalNotificationService
15 */
16 private $globalNotificationService;
17
18 public function register()
19 {
20 add_action('fluent_booking/handle_global_notification_background', function ($bookingId, $feedId) {
21 $booking = Booking::with(['calendar_event'])->find($bookingId);
22 if (!$booking) {
23 return;
24 }
25
26 $item = Meta::where('id', $feedId)->where('object_id', $booking->event_id)->first();
27
28 if (!$item) {
29 return;
30 }
31
32 // Prepare the data
33 $feed = [
34 'id' => $item->id,
35 'key' => $item->key,
36 'settings' => $item->value,
37 ];
38
39 $processedValues = $feed['settings'];
40 $processedValues = EditorShortCodeParser::parse($processedValues, $booking);
41 $feed['processedValues'] = $processedValues;
42
43 do_action('fluent_booking/integration_notify_' . $feed['key'], $feed, $booking, $booking->calendar_event);
44 return true;
45 }, 10, 2);
46
47 add_action('fluent_booking/after_booking_scheduled', [$this, 'maybeHandleGlobalIntegration'], 10, 2);
48 add_action('fluent_booking/booking_schedule_cancelled', [$this, 'maybeHandleGlobalIntegration'], 10, 2);
49 add_action('fluent_booking/booking_schedule_completed', [$this, 'maybeHandleGlobalIntegration'], 10, 2);
50
51 }
52
53 public function maybeHandleGlobalIntegration($booking, $calendarSlot)
54 {
55 $status = $booking->status;
56
57 $maps = [
58 'scheduled' => 'after_booking_scheduled',
59 'cancelled' => 'booking_schedule_cancelled',
60 'completed' => 'booking_schedule_completed'
61 ];
62
63 if (!isset($maps[$status])) {
64 return false;
65 }
66
67 $currentHook = $maps[$status];
68
69 return $this->globalNotify($booking, $calendarSlot, $currentHook);
70 }
71
72
73 private function globalNotify($booking, $calendarEvent, $targetHook = null)
74 {
75 $this->globalNotificationService = new GlobalNotificationService();
76
77 // Let's find the feeds that are available for this form
78 $feedKeys = apply_filters('fluent_booking/global_notification_active_types', [], $calendarEvent->id);
79
80 if (!$feedKeys) {
81 return false;
82 }
83
84 $feedMetaKeys = array_keys($feedKeys);
85 $feeds = $this->globalNotificationService->getNotificationFeeds($calendarEvent->id, $feedMetaKeys);
86
87 if (!$feeds) {
88 return false;
89 }
90
91 // Now we have to filter the feeds which are enabled
92 $enabledFeeds = $this->globalNotificationService->getEnabledFeeds($feeds, $booking);
93
94 if (!$enabledFeeds) {
95 return false;
96 }
97
98 foreach ($enabledFeeds as $feed) {
99
100 $enabledTriggers = Arr::get($feed, 'settings.event_trigger', []);
101
102 if (!$enabledTriggers || !in_array($targetHook, $enabledTriggers)) {
103 continue;
104 }
105
106 // We will decide if this feed will run on async or sync
107 $integrationKey = Arr::get($feedKeys, $feed['key']);
108
109 if (apply_filters('fluent_booking/notifying_async_' . $integrationKey, false, $calendarEvent->id)) {
110 as_enqueue_async_action('fluent_booking/handle_global_notification_background', [
111 $booking->id,
112 $feed['id']
113 ]);
114 } else {
115 // It's sync
116 $processedValues = $feed['settings'];
117 $processedValues = EditorShortCodeParser::parse($processedValues, $booking);
118
119 $feed['processedValues'] = $processedValues;
120 do_action('fluent_booking/integration_notify_' . $feed['key'], $feed, $booking, $calendarEvent);
121 }
122 }
123
124 return true;
125 }
126 }
127