| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Services; |
| 4 |
|
| 5 |
/** |
| 6 |
* Suppresses booking notifications for the current request, and nothing else: |
| 7 |
* calendar sync, CRM triggers and webhooks still run, and reminders still go |
| 8 |
* out later. For backfills, migrations and agent writes that shouldn't email |
| 9 |
* the attendee. |
| 10 |
* |
| 11 |
* $booking = NotificationGate::silently(function () use ($data) { |
| 12 |
* return BookingService::createBooking($data); |
| 13 |
* }); |
| 14 |
* |
| 15 |
* Add-ons that send their own notifications (SMS, push) should check |
| 16 |
* isSuppressed() or hook `fluent_booking/suppress_notifications`. |
| 17 |
* |
| 18 |
* @since 2.2.6 |
| 19 |
*/ |
| 20 |
class NotificationGate |
| 21 |
{ |
| 22 |
/** |
| 23 |
* @var bool |
| 24 |
*/ |
| 25 |
private static $suppressed = false; |
| 26 |
|
| 27 |
/** |
| 28 |
* Run $callback with notifications off, restoring the previous state even |
| 29 |
* if it throws. |
| 30 |
* |
| 31 |
* @param callable $callback |
| 32 |
* |
| 33 |
* @return mixed Whatever $callback returns. |
| 34 |
*/ |
| 35 |
public static function silently(callable $callback) |
| 36 |
{ |
| 37 |
$previous = self::$suppressed; |
| 38 |
self::$suppressed = true; |
| 39 |
|
| 40 |
try { |
| 41 |
return $callback(); |
| 42 |
} finally { |
| 43 |
self::$suppressed = $previous; |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* @param string $context A hint about which notification is being gated, |
| 49 |
* e.g. 'booking_scheduled' or 'booking_cancelled'. |
| 50 |
* @param mixed $booking The booking in play, when there is one. |
| 51 |
* |
| 52 |
* @return bool |
| 53 |
*/ |
| 54 |
public static function isSuppressed($context = '', $booking = null) |
| 55 |
{ |
| 56 |
/** |
| 57 |
* Whether booking notifications should be skipped for this call. |
| 58 |
* |
| 59 |
* @since 2.2.6 |
| 60 |
* |
| 61 |
* @param bool $suppressed Current state of the request-scoped gate. |
| 62 |
* @param string $context Which notification is being gated. |
| 63 |
* @param mixed $booking The booking in play, or null. |
| 64 |
*/ |
| 65 |
return (bool) apply_filters('fluent_booking/suppress_notifications', self::$suppressed, $context, $booking); |
| 66 |
} |
| 67 |
} |
| 68 |
|