| 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 WP_Error; |
| 10 |
use Yatra\Services\ItemTypeService; |
| 11 |
use Yatra\Repositories\BaseRepository; |
| 12 |
|
| 13 |
/** |
| 14 |
* Item Type REST API Controller |
| 15 |
* |
| 16 |
* Endpoints: |
| 17 |
* - GET /item-types - List item types |
| 18 |
* - POST /item-types - Create item type |
| 19 |
* - GET /item-types/{id} - Get single item type |
| 20 |
* - PUT /item-types/{id} - Update item type |
| 21 |
* - DELETE /item-types/{id} - Delete item type |
| 22 |
*/ |
| 23 |
class ItemTypeController extends BaseController |
| 24 |
{ |
| 25 |
protected string $rest_base = 'item-types'; |
| 26 |
|
| 27 |
private ItemTypeService $service; |
| 28 |
|
| 29 |
public function __construct() |
| 30 |
{ |
| 31 |
$this->service = new ItemTypeService(); |
| 32 |
} |
| 33 |
|
| 34 |
public function register_routes(): void |
| 35 |
{ |
| 36 |
$this->registerCrudRoutes($this->getStatusArg()); |
| 37 |
|
| 38 |
// Status statistics for admin views (All / Published / Draft / Trash) |
| 39 |
register_rest_route($this->namespace, '/' . $this->rest_base . '/stats', [ |
| 40 |
[ |
| 41 |
'methods' => \WP_REST_Server::READABLE, |
| 42 |
'callback' => [$this, 'getStats'], |
| 43 |
'permission_callback' => [$this, 'check_permission'], |
| 44 |
], |
| 45 |
]); |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* GET /item-types/stats |
| 50 |
* Return stable status counts for admin list views. |
| 51 |
*/ |
| 52 |
public function getStats(WP_REST_Request $request) |
| 53 |
{ |
| 54 |
try { |
| 55 |
$stats = $this->service->getStatusCounts(); |
| 56 |
return $this->success_response($stats); |
| 57 |
} catch (\Exception $e) { |
| 58 |
return $this->error_response($e->getMessage(), 500); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
public function check_permission(?WP_REST_Request $request = null): bool |
| 63 |
{ |
| 64 |
if ($request === null) { |
| 65 |
return true; |
| 66 |
} |
| 67 |
|
| 68 |
if (!is_user_logged_in()) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
if (current_user_can('manage_options')) { |
| 73 |
return true; |
| 74 |
} |
| 75 |
|
| 76 |
switch ($request->get_method()) { |
| 77 |
case 'GET': |
| 78 |
return current_user_can('yatra_view_trips'); |
| 79 |
case 'POST': |
| 80 |
case 'PUT': |
| 81 |
case 'PATCH': |
| 82 |
case 'DELETE': |
| 83 |
return current_user_can('yatra_edit_trips'); |
| 84 |
default: |
| 85 |
// Unknown HTTP method — deny explicitly. The earlier |
| 86 |
// fallback to `manage_options` was a regression that |
| 87 |
// silently broadened admin-only access for any verb not |
| 88 |
// in the explicit switch. |
| 89 |
return false; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
public function get_items(WP_REST_Request $request) |
| 94 |
{ |
| 95 |
try { |
| 96 |
$params = $this->getPaginationParams($request); |
| 97 |
|
| 98 |
$args = [ |
| 99 |
'limit' => $params['per_page'], |
| 100 |
'offset' => ($params['page'] - 1) * $params['per_page'], |
| 101 |
'order_by' => $params['orderby'], |
| 102 |
'order' => $params['order'], |
| 103 |
]; |
| 104 |
|
| 105 |
if (!empty($params['search'])) { |
| 106 |
$args['search'] = $params['search']; |
| 107 |
} |
| 108 |
|
| 109 |
$status = $request->get_param('status'); |
| 110 |
if ($status && $status !== 'all') { |
| 111 |
$args['status'] = sanitize_text_field($status); |
| 112 |
} |
| 113 |
|
| 114 |
$items = $this->service->getAll($args); |
| 115 |
$total = $this->service->count($args); |
| 116 |
|
| 117 |
$repository = $this->service->getRepositoryInstance(); |
| 118 |
$prepared = array_map(fn($item) => $this->prepareItem($item, $repository), $items); |
| 119 |
|
| 120 |
return $this->paginated_response($prepared, $total, $params['page'], $params['per_page']); |
| 121 |
} catch (\Exception $e) { |
| 122 |
return $this->error_response($e->getMessage(), 500); |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
public function get_item(WP_REST_Request $request) |
| 127 |
{ |
| 128 |
try { |
| 129 |
$item = $this->service->getById($this->getId($request)); |
| 130 |
|
| 131 |
if (!$item) { |
| 132 |
return $this->not_found(__('Item type not found', 'yatra')); |
| 133 |
} |
| 134 |
|
| 135 |
$repository = $this->service->getRepositoryInstance(); |
| 136 |
return $this->success_response($this->prepareItem($item, $repository)); |
| 137 |
} catch (\Exception $e) { |
| 138 |
return $this->error_response($e->getMessage(), 500); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
public function create_item(WP_REST_Request $request) |
| 143 |
{ |
| 144 |
try { |
| 145 |
$id = $this->service->create($this->getBody($request)); |
| 146 |
|
| 147 |
// Get the created item type to return full data |
| 148 |
$repository = $this->service->getRepositoryInstance(); |
| 149 |
$itemType = $repository->find($id); |
| 150 |
|
| 151 |
return $this->success_response([ |
| 152 |
'id' => $id, |
| 153 |
'message' => __('Item type created successfully', 'yatra'), |
| 154 |
'data' => $itemType, |
| 155 |
], 201); |
| 156 |
} catch (\InvalidArgumentException $e) { |
| 157 |
return $this->validation_error($e->getMessage()); |
| 158 |
} catch (\Exception $e) { |
| 159 |
return $this->error_response($e->getMessage(), 500); |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
public function update_item(WP_REST_Request $request) |
| 164 |
{ |
| 165 |
try { |
| 166 |
$result = $this->service->update($this->getId($request), $this->getBody($request)); |
| 167 |
|
| 168 |
if (!$result) { |
| 169 |
return $this->error_response(__('Failed to update item type', 'yatra'), 500); |
| 170 |
} |
| 171 |
|
| 172 |
return $this->success_response([ |
| 173 |
'message' => __('Item type updated successfully', 'yatra'), |
| 174 |
]); |
| 175 |
} catch (\InvalidArgumentException $e) { |
| 176 |
return $this->validation_error($e->getMessage()); |
| 177 |
} catch (\Exception $e) { |
| 178 |
return $this->error_response($e->getMessage(), 500); |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
public function delete_item(WP_REST_Request $request) |
| 183 |
{ |
| 184 |
try { |
| 185 |
$result = $this->service->delete($this->getId($request)); |
| 186 |
|
| 187 |
if (!$result) { |
| 188 |
return $this->error_response(__('Failed to delete item type', 'yatra'), 500); |
| 189 |
} |
| 190 |
|
| 191 |
return $this->success_response([ |
| 192 |
'message' => __('Item type deleted successfully', 'yatra'), |
| 193 |
]); |
| 194 |
} catch (\Exception $e) { |
| 195 |
return $this->error_response($e->getMessage(), 500); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
private function prepareItem($item, ?BaseRepository $repository = null): array |
| 200 |
{ |
| 201 |
$prepared = (array) $item; |
| 202 |
|
| 203 |
if (isset($prepared['icon']) && is_string($prepared['icon'])) { |
| 204 |
$prepared['icon'] = maybe_unserialize($prepared['icon']); |
| 205 |
} |
| 206 |
if (isset($prepared['icon'])) { |
| 207 |
$prepared['icon'] = $this->convert_icon_attachment_id_to_url($prepared['icon']); |
| 208 |
} |
| 209 |
|
| 210 |
if (!empty($prepared['created_by'])) { |
| 211 |
$user = get_userdata((int) $prepared['created_by']); |
| 212 |
$prepared['created_by_name'] = $user ? esc_html($user->display_name) : null; |
| 213 |
} |
| 214 |
|
| 215 |
if (!empty($prepared['updated_by'])) { |
| 216 |
$user = get_userdata((int) $prepared['updated_by']); |
| 217 |
$prepared['updated_by_name'] = $user ? esc_html($user->display_name) : null; |
| 218 |
} |
| 219 |
|
| 220 |
// Add items count |
| 221 |
if ($repository && !empty($prepared['id'])) { |
| 222 |
$prepared['items_count'] = $repository->countItemsByType((int) $prepared['id']); |
| 223 |
} else { |
| 224 |
$prepared['items_count'] = 0; |
| 225 |
} |
| 226 |
|
| 227 |
return $prepared; |
| 228 |
} |
| 229 |
} |
| 230 |
|