PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.5.21
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.5.21
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.21, at app/Hooks/Handlers/GlobalNotificationHandler.php

128 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
56 $status = $booking->status;
57
58 $maps = [
59 'scheduled' => 'after_booking_scheduled',
60 'cancelled' => 'booking_schedule_cancelled',
61 'completed' => 'booking_schedule_completed'
62 ];
63
64 if (!isset($maps[$status])) {
65 return false;
66 }
67
68 $currentHook = $maps[$status];
69
70 return $this->globalNotify($booking, $calendarSlot, $currentHook);
71 }
72
73
74 private function globalNotify($booking, $calendarEvent, $targetHook = null)
75 {
76 $this->globalNotificationService = new GlobalNotificationService();
77
78 // Let's find the feeds that are available for this form
79 $feedKeys = apply_filters('fluent_booking/global_notification_active_types', [], $calendarEvent->id);
80
81 if (!$feedKeys) {
82 return false;
83 }
84
85 $feedMetaKeys = array_keys($feedKeys);
86 $feeds = $this->globalNotificationService->getNotificationFeeds($calendarEvent->id, $feedMetaKeys);
87
88 if (!$feeds) {
89 return false;
90 }
91
92 // Now we have to filter the feeds which are enabled
93 $enabledFeeds = $this->globalNotificationService->getEnabledFeeds($feeds, $booking);
94
95 if (!$enabledFeeds) {
96 return false;
97 }
98
99 foreach ($enabledFeeds as $feed) {
100
101 $enabledTriggers = Arr::get($feed, 'settings.event_trigger', []);
102
103 if (!$enabledTriggers && !in_array($targetHook, $enabledTriggers)) {
104 continue;
105 }
106
107 // We will decide if this feed will run on async or sync
108 $integrationKey = Arr::get($feedKeys, $feed['key']);
109
110 if (apply_filters('fluent_booking/notifying_async_' . $integrationKey, false, $calendarEvent->id)) {
111 as_enqueue_async_action('fluent_booking/handle_global_notification_background', [
112 $booking->id,
113 $feed['id']
114 ]);
115 } else {
116 // It's sync
117 $processedValues = $feed['settings'];
118 $processedValues = EditorShortCodeParser::parse($processedValues, $booking);
119
120 $feed['processedValues'] = $processedValues;
121 do_action('fluent_booking/integration_notify_' . $feed['key'], $feed, $booking, $calendarEvent);
122 }
123 }
124
125 return true;
126 }
127 }
128