PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.9
Yatra – Travel Booking & Tour Operator Software v3.0.9
3.0.15 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 All 83 releases
yatra / app / Controllers / BlockEditorChoicesController.php

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

120 lines 4.1 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_Server;
9
10 /**
11 * Read-only taxonomy option lists for the block editor (id + label).
12 *
13 * Separate from canonical CRUD routes so anyone who can edit posts can pick classifications
14 * without requiring yatra_view_trips.
15 */
16 final class BlockEditorChoicesController
17 {
18 private string $namespace = 'yatra/v1';
19
20 public function register_routes(): void
21 {
22 register_rest_route(
23 $this->namespace,
24 '/block-editor/taxonomy-choices',
25 [
26 'methods' => WP_REST_Server::READABLE,
27 'callback' => [$this, 'get_taxonomy_choices'],
28 'permission_callback' => static function (): bool {
29 return current_user_can('edit_posts');
30 },
31 'args' => [
32 'taxonomy' => [
33 'required' => true,
34 'enum' => ['destination', 'activity', 'trip_category', 'difficulty'],
35 'sanitize_callback' => 'sanitize_key',
36 ],
37 ],
38 ]
39 );
40 }
41
42 /**
43 * @return \WP_REST_Response|\WP_Error
44 */
45 public function get_taxonomy_choices(WP_REST_Request $request)
46 {
47 $taxonomy = (string) $request->get_param('taxonomy');
48
49 $args = [
50 'limit' => 500,
51 'offset' => 0,
52 'order_by' => 'name',
53 'order' => 'ASC',
54 'where' => ['status' => 'publish'],
55 ];
56
57 $items = [];
58
59 try {
60 switch ($taxonomy) {
61 case 'destination':
62 foreach ((new \Yatra\Services\DestinationService())->getAll($args) as $row) {
63 $id = isset($row->id) ? (int) $row->id : 0;
64 if ($id > 0) {
65 $items[] = [
66 'id' => $id,
67 'name' => (string) ($row->name ?? ''),
68 ];
69 }
70 }
71 break;
72 case 'activity':
73 foreach ((new \Yatra\Services\ActivityService())->getAll($args) as $row) {
74 $id = isset($row->id) ? (int) $row->id : 0;
75 if ($id > 0) {
76 $items[] = [
77 'id' => $id,
78 'name' => (string) ($row->name ?? ''),
79 ];
80 }
81 }
82 break;
83 case 'trip_category':
84 foreach ((new \Yatra\Services\CategoryService())->getAll($args) as $row) {
85 $id = isset($row->id) ? (int) $row->id : 0;
86 if ($id > 0) {
87 $items[] = [
88 'id' => $id,
89 'name' => (string) ($row->name ?? ''),
90 ];
91 }
92 }
93 break;
94 case 'difficulty':
95 $rows = (new \Yatra\Repositories\DifficultyLevelRepository())->getPublished() ?: [];
96 foreach ($rows as $row) {
97 $id = isset($row->id) ? (int) $row->id : 0;
98 if ($id > 0) {
99 $items[] = [
100 'id' => $id,
101 'name' => (string) ($row->name ?? ''),
102 ];
103 }
104 }
105 break;
106 default:
107 return new \WP_Error('invalid_taxonomy', 'Invalid taxonomy', ['status' => 400]);
108 }
109 } catch (\Throwable $e) {
110 return new \WP_Error('choices_failed', 'Could not load choices', ['status' => 500]);
111 }
112
113 usort($items, static function (array $a, array $b): int {
114 return strcasecmp((string) $a['name'], (string) $b['name']);
115 });
116
117 return rest_ensure_response(['items' => $items]);
118 }
119 }
120