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 / UsageTrackingController.php

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

235 lines 8.7 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
11 /**
12 * REST endpoints for opt-in usage telemetry (admin only).
13 */
14 class UsageTrackingController extends BaseController
15 {
16 protected string $rest_base = 'usage-tracking';
17
18 public function register_routes(): void
19 {
20 register_rest_route($this->namespace, '/' . $this->rest_base . '/status', [
21 [
22 'methods' => \WP_REST_Server::READABLE,
23 'callback' => [$this, 'get_status'],
24 'permission_callback' => [$this, 'check_permission'],
25 ],
26 ]);
27
28 register_rest_route($this->namespace, '/' . $this->rest_base . '/settings', [
29 [
30 'methods' => \WP_REST_Server::CREATABLE,
31 'callback' => [$this, 'update_settings'],
32 'permission_callback' => [$this, 'check_permission'],
33 ],
34 ]);
35
36 register_rest_route($this->namespace, '/' . $this->rest_base . '/send', [
37 [
38 'methods' => \WP_REST_Server::CREATABLE,
39 'callback' => [$this, 'send_now'],
40 'permission_callback' => [$this, 'check_permission'],
41 ],
42 ]);
43
44 register_rest_route($this->namespace, '/' . $this->rest_base . '/preview', [
45 [
46 'methods' => \WP_REST_Server::READABLE,
47 'callback' => [$this, 'preview_payload'],
48 'permission_callback' => [$this, 'check_permission'],
49 ],
50 ]);
51
52 register_rest_route($this->namespace, '/' . $this->rest_base . '/clear-cache', [
53 [
54 'methods' => \WP_REST_Server::CREATABLE,
55 'callback' => [$this, 'clear_cache'],
56 'permission_callback' => [$this, 'check_permission'],
57 ],
58 ]);
59
60 register_rest_route($this->namespace, '/' . $this->rest_base . '/delete-snapshots', [
61 [
62 'methods' => \WP_REST_Server::CREATABLE,
63 'callback' => [$this, 'delete_snapshots'],
64 'permission_callback' => [$this, 'check_permission'],
65 ],
66 ]);
67 }
68
69 public function check_permission(?WP_REST_Request $request = null): bool
70 {
71 unset($request);
72
73 return current_user_can('manage_options') || current_user_can('manage_yatra');
74 }
75
76 public function get_status(WP_REST_Request $request): WP_REST_Response
77 {
78 unset($request);
79 // Never fatal if telemetry files were not packaged correctly.
80 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
81 return new WP_REST_Response([
82 'success' => false,
83 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
84 ], 500);
85 }
86
87 /** @var \Yatra\Services\StatsUsage $u */
88 $u = \Yatra\Services\StatsUsage::instance();
89
90 return new WP_REST_Response([
91 'success' => true,
92 'data' => [
93 'enabled' => $u->is_enabled(),
94 'last_sync' => (int) get_option(\Yatra\Services\StatsUsage::OPT_LAST_SYNC, 0),
95 'next_scheduled' => $u->get_next_scheduled(),
96 'retry_count' => (int) get_option(\Yatra\Services\StatsUsage::OPT_RETRY_COUNT, 0),
97 'next_retry' => (int) get_option(\Yatra\Services\StatsUsage::OPT_NEXT_RETRY, 0),
98 'instance_id' => $u->ensure_instance_id(),
99 'last_send_error' => $u->get_last_send_error(),
100 ],
101 ]);
102 }
103
104 public function update_settings(WP_REST_Request $request): WP_REST_Response
105 {
106 $body = $request->get_json_params();
107 if (!is_array($body)) {
108 $body = [];
109 }
110 $enabled = !empty($body['enabled']);
111 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
112 return new WP_REST_Response([
113 'success' => false,
114 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
115 ], 500);
116 }
117 $u = \Yatra\Services\StatsUsage::instance();
118 if ($enabled) {
119 $u->enable(true);
120 } else {
121 $u->disable();
122 }
123
124 return new WP_REST_Response([
125 'success' => true,
126 'message' => __('Preference saved.', 'yatra'),
127 'data' => ['enabled' => $u->is_enabled()],
128 ]);
129 }
130
131 public function send_now(WP_REST_Request $request): WP_REST_Response
132 {
133 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
134 return new WP_REST_Response([
135 'success' => false,
136 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
137 ], 500);
138 }
139 $u = \Yatra\Services\StatsUsage::instance();
140 $body = $request->get_json_params();
141 $body = is_array($body) ? $body : [];
142 $force = !empty($body['force']);
143
144 if (!$u->is_enabled() && !$force) {
145 return new WP_REST_Response([
146 'success' => false,
147 'message' => __('Sharing is turned off. Enable it above, or use “Send now” for a one-time test send.', 'yatra'),
148 ], 400);
149 }
150
151 // force=true skips opt-in check (same permission as this endpoint — trusted admins only).
152 $ok = $u->sync(!$force);
153
154 $lastErr = $u->get_last_send_error();
155 $message = $ok
156 ? __('Sent successfully.', 'yatra')
157 : __('Send failed; will retry with backoff.', 'yatra');
158 if (!$ok && is_array($lastErr)) {
159 $hint = '';
160 if (!empty($lastErr['code'])) {
161 $hint .= ' HTTP ' . (string) $lastErr['code'];
162 }
163 if (!empty($lastErr['wp_error'])) {
164 $hint .= ($hint !== '' ? '' : '') . (string) $lastErr['wp_error'];
165 }
166 if ($hint === '' && !empty($lastErr['body'])) {
167 $hint = ' ' . (string) $lastErr['body'];
168 }
169 if ($hint !== '') {
170 $message .= ' ' . $hint;
171 }
172 }
173
174 return new WP_REST_Response([
175 'success' => $ok,
176 'message' => $message,
177 'detail' => $lastErr,
178 ], $ok ? 200 : 502);
179 }
180
181 public function preview_payload(WP_REST_Request $request): WP_REST_Response
182 {
183 unset($request);
184 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
185 return new WP_REST_Response([
186 'success' => false,
187 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
188 ], 500);
189 }
190 $u = \Yatra\Services\StatsUsage::instance();
191 $payload = $u->build_payload();
192
193 return new WP_REST_Response([
194 'success' => true,
195 'data' => [
196 'json' => wp_json_encode($payload, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES),
197 ],
198 ]);
199 }
200
201 public function clear_cache(WP_REST_Request $request): WP_REST_Response
202 {
203 unset($request);
204 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
205 return new WP_REST_Response([
206 'success' => false,
207 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
208 ], 500);
209 }
210 \Yatra\Services\StatsUsage::instance()->clear_local_cache();
211
212 return new WP_REST_Response([
213 'success' => true,
214 'message' => __('Local telemetry cache cleared.', 'yatra'),
215 ]);
216 }
217
218 public function delete_snapshots(WP_REST_Request $request): WP_REST_Response
219 {
220 unset($request);
221 if (!class_exists('\\Yatra\\Services\\StatsUsage')) {
222 return new WP_REST_Response([
223 'success' => false,
224 'message' => __('Usage tracking is unavailable because StatsUsage could not be loaded. Please ensure Yatra is deployed with app/Services/StatsUsage.php and vendor/autoload.php.', 'yatra'),
225 ], 500);
226 }
227 \Yatra\Services\StatsUsage::instance()->delete_snapshots();
228
229 return new WP_REST_Response([
230 'success' => true,
231 'message' => __('Local snapshots removed.', 'yatra'),
232 ]);
233 }
234 }
235