PluginProbe
Yatra – Travel Booking & Tour Operator Software / trunk
Yatra – Travel Booking & Tour Operator Software vtrunk
3.0.14 3.0.14.1 3.0.14.2 3.0.12 3.0.13 3.0.11 3.0.10 3.0.9 3.0.8 3.0.7 3.0.6 3.0.5 3.0.5.1 3.0.4 3.0.3 3.0.2.9 3.0.2.7 3.0.2.8 3.0.2.6 trunk 1.0.0 2.0.0 2.0.1 2.0.10 2.0.11 All 82 releases
yatra / app / Controllers / DifficultyLevelController.php

DifficultyLevelController.php in Yatra – Travel Booking & Tour Operator Software trunk, at app/Controllers/DifficultyLevelController.php

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