sender = new EmailSender(); } public function init(): void { add_action('DayTidy/A__DayTidy/Booking/Booking__add/added', [$this, 'created'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__confirm/after', [$this, 'confirmed'], 10, 3); add_action('DayTidy/A__DayTidy/Booking/Booking__decline/after', [$this, 'declined'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__cancel/after', [$this, 'cancelled'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__complete/after', [$this, 'completed'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__paid/after', [$this, 'paid'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__payment_failed/after', [$this, 'paymentFailed'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Refund__processed/after', [$this, 'refunded'], 10, 2); add_action('DayTidy/A__DayTidy/Booking/Booking__reschedule/after', [$this, 'rescheduled'], 10, 3); } /** * @param mixed[] $context */ public function created(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_CREATED, $booking_id, [], $context); } /** * @param mixed[] $context */ public function paid(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_PAID, $booking_id, [], $context); } /** * @param mixed[] $context */ public function refunded(int $booking_id, array $context): void { $this->dispatch(Registry::TRIGGER_BOOKING_REFUNDED, $booking_id, ['refund' => $context]); } /** * @param mixed[] $context * @param mixed[] $history */ public function rescheduled(int $booking_id, array $context, array $history = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_RESCHEDULED, $booking_id, ['reschedule' => $history], $context); } /** * @param mixed[] $context */ public function confirmed(int $booking_id, string $mode = '', array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_CONFIRMED, $booking_id, ['mode' => $mode], $context); } /** * @param mixed[] $context */ public function declined(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_DECLINED, $booking_id, [], $context); } /** * @param mixed[] $context */ public function cancelled(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_CANCELLED, $booking_id, [], $context); } /** * @param mixed[] $context */ public function completed(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_COMPLETED, $booking_id, [], $context); } /** * @param mixed[] $context */ public function paymentFailed(int $booking_id, array $context = []): void { $this->dispatch(Registry::TRIGGER_BOOKING_PAYMENT_FAILED, $booking_id, [], $context); } /** * @param mixed[] $event_args * @param mixed[] $context */ private function dispatch(string $trigger, int $booking_id, array $event_args = [], array $context = []): void { if (array_key_exists('send_notifications', $context) && !$context['send_notifications']) return; $booking = new Booking($booking_id); $notification_ids = $this->notificationIds($trigger); foreach ($notification_ids as $notification_id) { $notification = new Notification($notification_id); if (!$notification->enabled()) continue; if ($notification->type() !== Registry::TYPE_EMAIL) continue; foreach (Registry::contexts($trigger) as $context => $details) { $this->sender->send($notification, $booking, $context, [ 'event_args' => $event_args, ]); } } } /** * @return int[] */ private function notificationIds(string $trigger): array { if (!post_type_exists(NotificationPost::PT)) return []; $ids = get_posts([ 'post_type' => NotificationPost::PT, 'post_status' => 'publish', 'posts_per_page' => -1, 'fields' => 'ids', 'meta_query' => [ [ 'key' => 'daytidy_notification_trigger', 'value' => $trigger, ], [ 'key' => 'daytidy_notification_type', 'value' => Registry::TYPE_EMAIL, ], ], ]); $ids = array_map('absint', $ids); return (array) apply_filters( 'DayTidy/F__DayTidy/Notifications/Dispatcher__notification_ids/ids', $ids, $trigger ); } }