| @@ -6,9 +6,8 @@ | ||
| 6 | 6 | |
| 7 | 7 | use WP_REST_Request; |
| 8 | 8 | use WP_REST_Response; |
| 9 | 9 | use WP_Error; |
| 10 | -use Yatra\Admin\StatsUsage; | |
| 11 | 10 | |
| 12 | 11 | /** |
| 13 | 12 | * REST endpoints for opt-in usage telemetry (admin only). |
| 14 | 13 | */ |
| @@ -76,18 +75,27 @@ | ||
| 76 | 75 | |
| 77 | 76 | public function get_status(WP_REST_Request $request): WP_REST_Response |
| 78 | 77 | { |
| 79 | 78 | unset($request); |
| 80 | - $u = StatsUsage::instance(); | |
| 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 | + } | |
| 81 | 86 | |
| 87 | + /** @var \Yatra\Services\StatsUsage $u */ | |
| 88 | + $u = \Yatra\Services\StatsUsage::instance(); | |
| 89 | + | |
| 82 | 90 | return new WP_REST_Response([ |
| 83 | 91 | 'success' => true, |
| 84 | 92 | 'data' => [ |
| 85 | 93 | 'enabled' => $u->is_enabled(), |
| 86 | - 'last_sync' => (int) get_option(StatsUsage::OPT_LAST_SYNC, 0), | |
| 94 | + 'last_sync' => (int) get_option(\Yatra\Services\StatsUsage::OPT_LAST_SYNC, 0), | |
| 87 | 95 | '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), | |
| 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), | |
| 90 | 98 | 'instance_id' => $u->ensure_instance_id(), |
| 91 | 99 | 'last_send_error' => $u->get_last_send_error(), |
| 92 | 100 | ], |
| 93 | 101 | ]); |
| @@ -99,9 +107,15 @@ | ||
| 99 | 107 | if (!is_array($body)) { |
| 100 | 108 | $body = []; |
| 101 | 109 | } |
| 102 | 110 | $enabled = !empty($body['enabled']); |
| 103 | - $u = StatsUsage::instance(); | |
| 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(); | |
| 104 | 118 | if ($enabled) { |
| 105 | 119 | $u->enable(true); |
| 106 | 120 | } else { |
| 107 | 121 | $u->disable(); |
| @@ -115,9 +129,15 @@ | ||
| 115 | 129 | } |
| 116 | 130 | |
| 117 | 131 | public function send_now(WP_REST_Request $request): WP_REST_Response |
| 118 | 132 | { |
| 119 | - $u = StatsUsage::instance(); | |
| 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(); | |
| 120 | 140 | $body = $request->get_json_params(); |
| 121 | 141 | $body = is_array($body) ? $body : []; |
| 122 | 142 | $force = !empty($body['force']); |
| 123 | 143 | |
| @@ -160,9 +180,15 @@ | ||
| 160 | 180 | |
| 161 | 181 | public function preview_payload(WP_REST_Request $request): WP_REST_Response |
| 162 | 182 | { |
| 163 | 183 | unset($request); |
| 164 | - $u = StatsUsage::instance(); | |
| 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(); | |
| 165 | 191 | $payload = $u->build_payload(); |
| 166 | 192 | |
| 167 | 193 | return new WP_REST_Response([ |
| 168 | 194 | 'success' => true, |
| @@ -174,9 +200,15 @@ | ||
| 174 | 200 | |
| 175 | 201 | public function clear_cache(WP_REST_Request $request): WP_REST_Response |
| 176 | 202 | { |
| 177 | 203 | unset($request); |
| 178 | - StatsUsage::instance()->clear_local_cache(); | |
| 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(); | |
| 179 | 211 | |
| 180 | 212 | return new WP_REST_Response([ |
| 181 | 213 | 'success' => true, |
| 182 | 214 | 'message' => __('Local telemetry cache cleared.', 'yatra'), |
| @@ -185,9 +217,15 @@ | ||
| 185 | 217 | |
| 186 | 218 | public function delete_snapshots(WP_REST_Request $request): WP_REST_Response |
| 187 | 219 | { |
| 188 | 220 | unset($request); |
| 189 | - StatsUsage::instance()->delete_snapshots(); | |
| 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(); | |
| 190 | 228 | |
| 191 | 229 | return new WP_REST_Response([ |
| 192 | 230 | 'success' => true, |
| 193 | 231 | 'message' => __('Local snapshots removed.', 'yatra'), |