PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.7
Yatra – Travel Booking & Tour Operator Software v3.0.2.7
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 / ModuleController.php

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

135 lines 3.9 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\Core\Modules\ModuleManager;
11
12 class ModuleController extends BaseController
13 {
14 public function register_routes(): void
15 {
16 $namespace = 'yatra/v1';
17
18 register_rest_route($namespace, '/modules', [
19 [
20 'methods' => \WP_REST_Server::READABLE,
21 'callback' => [$this, 'get_modules'],
22 'permission_callback' => [$this, 'check_permission'],
23 ],
24 ]);
25
26 register_rest_route($namespace, '/modules/(?P<slug>[a-z0-9_\-]+)/toggle', [
27 [
28 'methods' => \WP_REST_Server::CREATABLE,
29 'callback' => [$this, 'toggle_module'],
30 'permission_callback' => [$this, 'check_permission'],
31 'args' => [
32 'slug' => [
33 'required' => true,
34 'sanitize_callback' => 'sanitize_key',
35 ],
36 'enabled' => [
37 'required' => true,
38 'validate_callback' => function ($value) {
39 return is_bool($value) || $value === 'true' || $value === 'false' || $value === 1 || $value === 0;
40 },
41 ],
42 ],
43 ],
44 ]);
45
46 register_rest_route($namespace, '/modules/bulk-toggle', [
47 [
48 'methods' => \WP_REST_Server::CREATABLE,
49 'callback' => [$this, 'bulk_toggle'],
50 'permission_callback' => [$this, 'check_permission'],
51 ],
52 ]);
53 }
54
55 public function check_permission(?WP_REST_Request $request = null): bool
56 {
57 if (!is_user_logged_in()) {
58 return false;
59 }
60
61 if (current_user_can('manage_options')) {
62 return true;
63 }
64
65 return current_user_can('yatra_edit_trips');
66 }
67
68 public function get_modules(): WP_REST_Response
69 {
70 return $this->success_response([
71 'data' => ModuleManager::getModules(),
72 ]);
73 }
74
75 public function toggle_module(WP_REST_Request $request)
76 {
77 $slug = sanitize_key($request->get_param('slug'));
78 $enabled = filter_var($request->get_param('enabled'), FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
79
80 if ($enabled === null) {
81 return $this->error_response(__('Enabled flag is required.', 'yatra'), 400);
82 }
83
84 $modules = ModuleManager::getModules();
85 $exists = array_filter($modules, static fn ($module) => $module['slug'] === $slug);
86
87 if (empty($exists)) {
88 return $this->error_response(__('Module not found.', 'yatra'), 404);
89 }
90
91 $updated = ModuleManager::setModuleStatus($slug, (bool) $enabled);
92
93 return $this->success_response([
94 'data' => $updated,
95 ]);
96 }
97
98 public function bulk_toggle(WP_REST_Request $request)
99 {
100 $items = $request->get_param('items');
101 if (!is_array($items) || empty($items)) {
102 return $this->error_response(__('No module changes supplied.', 'yatra'), 400);
103 }
104
105 $sanitized = [];
106 foreach ($items as $item) {
107 if (empty($item['slug'])) {
108 continue;
109 }
110
111 $enabled = filter_var($item['enabled'] ?? false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
112 if ($enabled === null) {
113 continue;
114 }
115
116 $sanitized[] = [
117 'slug' => sanitize_key($item['slug']),
118 'enabled' => (bool) $enabled,
119 ];
120 }
121
122 if (empty($sanitized)) {
123 return $this->error_response(__('No valid module changes supplied.', 'yatra'), 400);
124 }
125
126 $updated = ModuleManager::setMultipleStatuses($sanitized);
127
128 return $this->success_response([
129 'data' => $updated,
130 ]);
131 }
132 }
133
134
135