PluginProbe
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution / 1.10.0
Fluent Booking – The Ultimate Appointments Scheduling, Events Booking, Events Calendar Solution v1.10.0
2.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 All 34 releases
fluent-booking / app / Hooks / Handlers / GlobalNotificationHandler.php

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

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