| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Hooks; |
| 6 |
|
| 7 |
use Yatra\Repositories\SavedTripRepository; |
| 8 |
|
| 9 |
/** |
| 10 |
* Saved Trip (Wishlist) Hooks |
| 11 |
* |
| 12 |
* Keeps the wishlist meta in sync with the trip lifecycle: when a trip is |
| 13 |
* deleted, its ID is purged from every user's saved-trips meta so orphan |
| 14 |
* IDs don't accumulate forever. |
| 15 |
*/ |
| 16 |
class SavedTripHooks |
| 17 |
{ |
| 18 |
public static function init(): void |
| 19 |
{ |
| 20 |
add_action('yatra_trip_deleted', [self::class, 'onTripDeleted'], 10, 1); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Garbage-collect a deleted trip's ID from every user's wishlist. |
| 25 |
*/ |
| 26 |
public static function onTripDeleted($tripId): void |
| 27 |
{ |
| 28 |
$tripId = (int) $tripId; |
| 29 |
if ($tripId <= 0) { |
| 30 |
return; |
| 31 |
} |
| 32 |
try { |
| 33 |
(new SavedTripRepository())->removeTripFromAllUsers($tripId); |
| 34 |
} catch (\Throwable $e) { |
| 35 |
// Don't let cleanup failure break the trip-delete flow. |
| 36 |
} |
| 37 |
} |
| 38 |
} |
| 39 |
|