| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Yatra\Controllers; |
| 6 |
|
| 7 |
use WP_REST_Request; |
| 8 |
use WP_REST_Response; |
| 9 |
use Yatra\Services\SavedTripService; |
| 10 |
use Yatra\Services\SettingsService; |
| 11 |
|
| 12 |
/** |
| 13 |
* Saved Trip REST API Controller |
| 14 |
* |
| 15 |
* Handles HTTP requests for saved trips/wishlist functionality. |
| 16 |
* |
| 17 |
* @package Yatra\Controllers |
| 18 |
*/ |
| 19 |
class SavedTripController extends BaseController |
| 20 |
{ |
| 21 |
private SavedTripService $savedTripService; |
| 22 |
|
| 23 |
public function __construct() |
| 24 |
{ |
| 25 |
$this->savedTripService = new SavedTripService(); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Register routes |
| 30 |
*/ |
| 31 |
public function register_routes(): void |
| 32 |
{ |
| 33 |
if (!SettingsService::wishlistEnabled()) { |
| 34 |
return; |
| 35 |
} |
| 36 |
|
| 37 |
$namespace = 'yatra/v1'; |
| 38 |
$base = 'saved-trips'; |
| 39 |
|
| 40 |
// Check if trip is saved |
| 41 |
register_rest_route($namespace, '/' . $base . '/check/(?P<trip_id>\d+)', [ |
| 42 |
[ |
| 43 |
'methods' => \WP_REST_Server::READABLE, |
| 44 |
'callback' => [$this, 'checkSaved'], |
| 45 |
'permission_callback' => [$this, 'checkUserPermission'], |
| 46 |
], |
| 47 |
]); |
| 48 |
|
| 49 |
// Save trip |
| 50 |
register_rest_route($namespace, '/' . $base, [ |
| 51 |
[ |
| 52 |
'methods' => \WP_REST_Server::CREATABLE, |
| 53 |
'callback' => [$this, 'saveTrip'], |
| 54 |
'permission_callback' => [$this, 'checkUserPermission'], |
| 55 |
], |
| 56 |
]); |
| 57 |
|
| 58 |
// Remove saved trip |
| 59 |
register_rest_route($namespace, '/' . $base . '/(?P<trip_id>\d+)', [ |
| 60 |
[ |
| 61 |
'methods' => \WP_REST_Server::DELETABLE, |
| 62 |
'callback' => [$this, 'removeTrip'], |
| 63 |
'permission_callback' => [$this, 'checkUserPermission'], |
| 64 |
], |
| 65 |
]); |
| 66 |
|
| 67 |
// Get user's saved trips |
| 68 |
register_rest_route($namespace, '/' . $base, [ |
| 69 |
[ |
| 70 |
'methods' => \WP_REST_Server::READABLE, |
| 71 |
'callback' => [$this, 'getSavedTrips'], |
| 72 |
'permission_callback' => [$this, 'checkUserPermission'], |
| 73 |
], |
| 74 |
]); |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check user permission (must be logged in) |
| 79 |
*/ |
| 80 |
public function checkUserPermission(): bool |
| 81 |
{ |
| 82 |
return SettingsService::wishlistEnabled() && is_user_logged_in(); |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Resolve trip ID from route params, merged params, and raw JSON body (some stacks omit JSON on get_param). |
| 87 |
*/ |
| 88 |
private function parseTripIdFromRequest(WP_REST_Request $request): int |
| 89 |
{ |
| 90 |
foreach (['trip_id', 'tripId', 'id'] as $key) { |
| 91 |
$v = $request->get_param($key); |
| 92 |
if ($v !== null && $v !== '' && absint($v) > 0) { |
| 93 |
return absint($v); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$bodyParams = $request->get_body_params(); |
| 98 |
if (is_array($bodyParams)) { |
| 99 |
foreach (['trip_id', 'tripId', 'id'] as $key) { |
| 100 |
if (isset($bodyParams[$key]) && $bodyParams[$key] !== '' && absint($bodyParams[$key]) > 0) { |
| 101 |
return absint($bodyParams[$key]); |
| 102 |
} |
| 103 |
} |
| 104 |
} |
| 105 |
|
| 106 |
if (!empty($_POST['trip_id']) && absint($_POST['trip_id']) > 0) { |
| 107 |
return absint($_POST['trip_id']); |
| 108 |
} |
| 109 |
|
| 110 |
$data = $request->get_json_params(); |
| 111 |
if (is_array($data)) { |
| 112 |
foreach (['trip_id', 'tripId', 'id'] as $key) { |
| 113 |
if (isset($data[$key]) && $data[$key] !== '' && absint($data[$key]) > 0) { |
| 114 |
return absint($data[$key]); |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
$raw = $request->get_body(); |
| 120 |
if (is_string($raw) && $raw !== '') { |
| 121 |
$decoded = json_decode($raw, true); |
| 122 |
if (is_array($decoded)) { |
| 123 |
foreach (['trip_id', 'tripId', 'id'] as $key) { |
| 124 |
if (isset($decoded[$key]) && $decoded[$key] !== '' && absint($decoded[$key]) > 0) { |
| 125 |
return absint($decoded[$key]); |
| 126 |
} |
| 127 |
} |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
/** |
| 135 |
* GET /saved-trips/check/{trip_id} - Check if trip is saved |
| 136 |
*/ |
| 137 |
public function checkSaved(WP_REST_Request $request): WP_REST_Response |
| 138 |
{ |
| 139 |
$userId = get_current_user_id(); |
| 140 |
$tripId = (int) $request->get_param('trip_id'); |
| 141 |
|
| 142 |
$isSaved = $this->savedTripService->isSaved($userId, $tripId); |
| 143 |
|
| 144 |
return new WP_REST_Response([ |
| 145 |
'success' => true, |
| 146 |
'data' => [ |
| 147 |
'is_saved' => $isSaved, |
| 148 |
], |
| 149 |
]); |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* POST /saved-trips - Save trip |
| 154 |
*/ |
| 155 |
public function saveTrip(WP_REST_Request $request): WP_REST_Response |
| 156 |
{ |
| 157 |
$userId = get_current_user_id(); |
| 158 |
|
| 159 |
$tripId = $this->parseTripIdFromRequest($request); |
| 160 |
|
| 161 |
if ($tripId <= 0) { |
| 162 |
return new WP_REST_Response([ |
| 163 |
'success' => false, |
| 164 |
'message' => __('Trip ID is required.', 'yatra'), |
| 165 |
], 400); |
| 166 |
} |
| 167 |
|
| 168 |
// Verify trip ID is valid (must be > 0) |
| 169 |
// Note: User ID and Trip ID can be the same number (e.g., both 1), so we don't block that |
| 170 |
// Instead, we verify the trip exists in the database |
| 171 |
|
| 172 |
$result = $this->savedTripService->saveTrip($userId, $tripId); |
| 173 |
|
| 174 |
if (!$result['success']) { |
| 175 |
return new WP_REST_Response($result, 400); |
| 176 |
} |
| 177 |
|
| 178 |
return new WP_REST_Response($result); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* DELETE /saved-trips/{trip_id} - Remove saved trip |
| 183 |
*/ |
| 184 |
public function removeTrip(WP_REST_Request $request): WP_REST_Response |
| 185 |
{ |
| 186 |
$userId = get_current_user_id(); |
| 187 |
$tripId = absint($request->get_param('trip_id')); |
| 188 |
if ($tripId <= 0) { |
| 189 |
$tripId = $this->parseTripIdFromRequest($request); |
| 190 |
} |
| 191 |
|
| 192 |
if ($tripId <= 0) { |
| 193 |
return new WP_REST_Response([ |
| 194 |
'success' => false, |
| 195 |
'message' => __('Trip ID is required.', 'yatra'), |
| 196 |
], 400); |
| 197 |
} |
| 198 |
|
| 199 |
$result = $this->savedTripService->removeTrip($userId, $tripId); |
| 200 |
|
| 201 |
if (!$result['success']) { |
| 202 |
return new WP_REST_Response($result, 400); |
| 203 |
} |
| 204 |
|
| 205 |
return new WP_REST_Response($result); |
| 206 |
} |
| 207 |
|
| 208 |
/** |
| 209 |
* GET /saved-trips - Get user's saved trips |
| 210 |
*/ |
| 211 |
public function getSavedTrips(WP_REST_Request $request): WP_REST_Response |
| 212 |
{ |
| 213 |
$userId = get_current_user_id(); |
| 214 |
|
| 215 |
$trips = $this->savedTripService->getUserSavedTrips($userId); |
| 216 |
|
| 217 |
return new WP_REST_Response([ |
| 218 |
'success' => true, |
| 219 |
'data' => $trips, |
| 220 |
]); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|