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