| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\App\Hooks\Handlers\CleanupHandlers; |
| 4 |
|
| 5 |
use FluentBooking\App\Models\Booking; |
| 6 |
use FluentBooking\App\Models\Meta; |
| 7 |
|
| 8 |
class CalenderEventCleaner |
| 9 |
{ |
| 10 |
public function register() |
| 11 |
{ |
| 12 |
add_action('fluent_booking/before_delete_calendar_event', [$this, 'handleBeforeDelete'], 10, 2); |
| 13 |
} |
| 14 |
|
| 15 |
public function handleBeforeDelete($calendarEvent, $calendar) |
| 16 |
{ |
| 17 |
if (empty($calendarEvent)) { |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
Meta::query() |
| 22 |
->where('object_type', 'calendar_event') |
| 23 |
->where('object_id', $calendarEvent->id)->delete(); |
| 24 |
|
| 25 |
$bookings = Booking::query() |
| 26 |
->where('event_id', $calendarEvent->id) |
| 27 |
->get(); |
| 28 |
|
| 29 |
foreach ($bookings as $booking){ |
| 30 |
$bookingId = $booking->id; |
| 31 |
do_action('fluent_booking/before_delete_booking', $booking); |
| 32 |
$booking->delete(); |
| 33 |
do_action('fluent_booking/after_delete_booking', $bookingId); |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
} |
| 38 |
} |