PluginProbe
Yatra – Travel Booking & Tour Operator Software / 3.0.2.6
Yatra – Travel Booking & Tour Operator Software v3.0.2.6
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 3.0.2.6, at app/Controllers/UsageTrackingController.php

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