getPostOrGetSanitize('page', ''); $subpage = $requestReader->getPostOrGetSanitize('subpage', ''); $currentHash = $requestReader->getPostOrGetSanitize('currentHash', ''); $isPluginAdmin = false; $context = array( 'action' => 'ajaxRefreshStatsDashboard', 'page' => $page, 'subpage' => $subpage, 'request_uri' => array_key_exists('REQUEST_URI', $_SERVER) ? $_SERVER['REQUEST_URI'] : '', 'user_id' => function_exists('get_current_user_id') ? get_current_user_id() : 0, ); $context = ABJ_404_Solution_AjaxAdminEndpointSupport::startAjaxDebugContext($context, 'Ajax_RefreshStatsDashboard::handle'); try { if (!ABJ_404_Solution_AjaxAdminEndpointSupport::requireAdminWithNonceOrRespond( 'abj404_refreshStatsDashboard', $context, 'ajaxRefreshStatsDashboard' )) { return; } $isPluginAdmin = true; if (ABJ_404_Solution_Ajax_Php::consumeRateLimit('refresh_stats_dashboard', 30, 60)) { ABJ_404_Solution_AjaxAdminEndpointSupport::safeLogAjaxFailure('AJAX rate limit in ajaxRefreshStatsDashboard.', $context); ABJ_404_Solution_AjaxAdminEndpointSupport::markAjaxResponseSent(); $payload = ABJ_404_Solution_AjaxErrorEnvelope::build('Rate limit exceeded. Please try again later.', null, false); ABJ_404_Solution_AjaxAdminEndpointSupport::getAndClearAjaxBufferedOutput(); ABJ_404_Solution_AjaxResponseEmitter::sendJsonResponseAndExit($payload, 429); return; } $snapshot = $statsRepository->refreshStatsDashboardSnapshot(false); $newHash = $snapshot['hash']; $state = $snapshot['state'] ?? 'ready'; $pending = $state !== 'ready'; $hasUpdate = (!$pending && $newHash !== '' && ($currentHash === '' || $newHash !== $currentHash)); if ($state === 'pending') { abj_service('stats_rollup')->armAuthenticatedAjaxContinuation(); } $response = array( 'hasUpdate' => $hasUpdate, 'hash' => $newHash, 'refreshedAt' => intval($snapshot['refreshed_at']), 'state' => $state, 'generation' => $snapshot['generation'] ?? '', 'publishedGeneration' => $snapshot['published_generation'] ?? '', ); if ($state === 'unavailable' && isset($snapshot['error'])) { $response['error'] = $snapshot['error']; } ABJ_404_Solution_AjaxAdminEndpointSupport::markAjaxResponseSent(); ABJ_404_Solution_AjaxAdminEndpointSupport::getAndClearAjaxBufferedOutput(); ABJ_404_Solution_AjaxResponseEmitter::sendJsonResponseAndExit($response, 200); return; } catch (Throwable $e) { $isPluginAdmin = ABJ_404_Solution_AjaxAdminEndpointSupport::resolveIsPluginAdminFallback($isPluginAdmin); $details = array( 'exception' => array( 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'trace' => $e->getTraceAsString(), ), 'context' => $context, ); ABJ_404_Solution_AjaxAdminEndpointSupport::safeLogAjaxFailure('AJAX exception in ajaxRefreshStatsDashboard.', $details, $e); $capturedOutput = ABJ_404_Solution_AjaxAdminEndpointSupport::getAndClearAjaxBufferedOutput(); if ($capturedOutput !== '') { $details['buffered_output'] = substr($capturedOutput, 0, 8000); } ABJ_404_Solution_AjaxAdminEndpointSupport::markAjaxResponseSent(); $payload = ABJ_404_Solution_AjaxErrorEnvelope::build( 'Server error while refreshing stats.', $details, $isPluginAdmin ); ABJ_404_Solution_AjaxResponseEmitter::sendJsonResponseAndExit($payload, 500); return; } } }