fix: surface flush() errors in admin connection status

When WP-Cron's automatic queue flush fails (network error, wrong token,
non-200 response), update connection_ok=false and last_error with the
reason so the admin dot turns red and shows the exact failure message.
Previously failures were silent.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-03-09 20:35:14 +01:00
parent f1c32e5060
commit 01a15007cb

View File

@@ -349,11 +349,27 @@ class SmartHoneypotAPIClient {
] ]
); );
if (!is_wp_error($response) && wp_remote_retrieve_response_code($response) === 200) { if (is_wp_error($response)) {
update_option(self::OPT_QUEUE, $queue, false); $s['connection_ok'] = false;
$s['last_sync'] = time(); $s['last_error'] = 'Flush failed: ' . $response->get_error_message();
$s['sent_total'] = ($s['sent_total'] ?? 0) + count($batch);
update_option(self::OPT_SETTINGS, $s); update_option(self::OPT_SETTINGS, $s);
error_log('[Honeypot] flush() wp_error: ' . $response->get_error_message());
return;
}
$code = wp_remote_retrieve_response_code($response);
if ($code === 200) {
update_option(self::OPT_QUEUE, $queue, false);
$s['last_sync'] = time();
$s['sent_total'] = ($s['sent_total'] ?? 0) + count($batch);
$s['last_error'] = '';
$s['connection_ok'] = true;
update_option(self::OPT_SETTINGS, $s);
} else {
$s['connection_ok'] = false;
$s['last_error'] = "Flush failed: API returned HTTP {$code}. Check URL and token.";
update_option(self::OPT_SETTINGS, $s);
error_log("[Honeypot] flush() API returned HTTP {$code}");
} }
} }