Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Donate link: https://scanfully.com
Tags: monitoring, site health, broken links, broken media, activity log
Requires at least: 6.0
Tested up to: 7.1.0
Stable tag: 1.9.1
Stable tag: 1.10.0
License: GPLv3 or later
License URI: http://www.gnu.org/licenses/gpl-3.0.html
Requires PHP: 7.4
Expand Down Expand Up @@ -216,6 +216,8 @@ Once connected, the plugin sends data to the Scanfully API at api.scanfully.com
* For accounts with elevated rights only: their email address with their user events, and the IP address and browser of their logins and of the first use of an application password.
* Failed logins as counts per hour, with the accounts with elevated rights that were tried, but without IP addresses or the usernames tried.
* When WooCommerce is active: the shop details a test checkout needs, such as the product, shipping method and checkout page, on connect, once a day and when they change. Scanfully only places test orders when WooCommerce checkout monitoring is on for the site, and the plugin cancels and removes them.
* When the plugin is deactivated, disconnected or deleted: that it happened, and the display name of the person who did it.
* When Scanfully asks the plugin to sync: the plugin's version, when its last request to Scanfully succeeded, and the time, status and error message of its last failed request.

Email deliverability checks are on by default: the plugin sends test emails to a Scanfully address through your site's own mail setup. You can turn them off under Settings > Scanfully.

Expand All @@ -226,6 +228,12 @@ Scanfully's [terms and conditions](https://scanfully.com/terms/) and [privacy po

== Changelog ==

= 1.10.0: November 2, 2026 =
* Added: the plugin tells Scanfully when it is deactivated, disconnected or deleted, and by whom, so Scanfully can say why a site stopped reporting
* Added: when Scanfully asks the plugin to sync, the plugin answers with its version and how its last requests to Scanfully went
* Fixed: wp-admin no longer asks to reconnect right after activating the plugin, and the first email ping goes about a minute after it
* Fixed: a request Scanfully answers with 400 or 429 no longer counts as the plugin failing to reach Scanfully

= 1.9.1: October 27, 2026 =
* Fixed: the WooCommerce checkout test reported a false failure on shops with Mollie
* Fixed: test orders of the WooCommerce checkout test no longer email the shop a cancelled order notice, which WooCommerce 11 started sending, and no other WooCommerce email is sent about a test order
Expand Down
19 changes: 17 additions & 2 deletions scanfully.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*
* @wordpress-plugin
* Plugin Name: Scanfully
* Version: 1.9.1
* Version: 1.10.0
* Plugin URI: https://scanfully.com/wp-plugin
* Description: Scanfully is your favorite WordPress performance and health monitoring tool.
* Author: Scanfully
Expand Down Expand Up @@ -68,7 +68,7 @@ function Scanfully(): \Scanfully\Main { // phpcs:ignore WordPress.NamingConventi
function () {
// meta.
define( 'SCANFULLY_PLUGIN_FILE', __FILE__ );
define( 'SCANFULLY_VERSION', '1.9.1' );
define( 'SCANFULLY_VERSION', '1.10.0' );

// boot.
require __DIR__ . '/vendor/autoload.php';
Expand All @@ -77,10 +77,25 @@ function () {
20
);

// register activation hook.
register_activation_hook(
__FILE__,
function () {
// The connection was last used before the deactivation, so its first
// report needs a moment before anything calls it stale.
set_transient( 'scanfully_recently_activated', 1, 5 * MINUTE_IN_SECONDS );
}
);

// register deactivation hook.
register_deactivation_hook(
__FILE__,
function () {
// Tell Scanfully first, so the site's owner learns why the plugin
// stopped reporting.
if ( class_exists( '\\Scanfully\\Connect\\Controller' ) ) {
\Scanfully\Connect\Controller::report_deactivated();
}
\Scanfully\Cron\Controller::clear_scheduled_events();
}
);
111 changes: 99 additions & 12 deletions src/API/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@
*/
abstract class Request {

/**
* Option holding the plugin's last failed request to Scanfully: when,
* the HTTP status (0 when no answer came) and the error. The sync route
* answers it to the API, so the site's owner learns why the plugin
* stopped reporting. Written only on a failure, never autoloaded.
*/
public const OPTION_LAST_ERROR = 'scanfully_connect_last_request_error';

/**
* The longest error text kept.
*/
private const MAX_ERROR_LENGTH = 200;

/**
* Send the request to the API.
*
Expand Down Expand Up @@ -53,28 +66,41 @@ public function do_request( string $endpoint, array $data ): ?int {

$response = wp_remote_post( $this->get_url( $endpoint ), $request_args );
if ( is_wp_error( $response ) ) {
self::record_failure( 0, self::error_message( $response ) );
return null;
}

// Only update last_used when we can confirm a successful response.
$status = (int) wp_remote_retrieve_response_code( $response );
if ( $status >= 200 && $status < 300 ) {
// A successful request proves the connection works, so clear any
// stale refresh-failure state that would otherwise keep the
// broken-connection notice showing while data is flowing.
\Scanfully\Cron\Controller::clear_refresh_failures();
try {
$now = new \DateTime();
$now->setTimezone( new \DateTimeZone( 'UTC' ) );
OptionController::set_option( 'last_used', $now->format( \Scanfully\Connect\Controller::DATE_FORMAT ) );
} catch ( \Exception $e ) {
// do nothing for now, just don't break the plugin.
}
self::record_success();
} else {
self::record_failure( $status, '' );
}

return $status;
}

/**
* Keep that a request to Scanfully worked: it proves the connection
* works, so any stale refresh-failure state that would keep the
* broken-connection notice showing goes, and last_used says when. The
* sync route answers last_used to the API as the last success, next to
* the last failure, so every kind of request records both.
*
* @return void
*/
private static function record_success(): void {
\Scanfully\Cron\Controller::clear_refresh_failures();
try {
$now = new \DateTime();
$now->setTimezone( new \DateTimeZone( 'UTC' ) );
OptionController::set_option( 'last_used', $now->format( \Scanfully\Connect\Controller::DATE_FORMAT ) );
} catch ( \Exception $e ) {
// do nothing for now, just don't break the plugin.
}
}

/**
* Send the request to the API and return the parsed response.
*
Expand Down Expand Up @@ -164,16 +190,20 @@ public function do_get_request( string $endpoint, array $query = [], int $timeou
*/
private function process_response( $response ): ?array {
if ( is_wp_error( $response ) ) {
self::record_failure( 0, self::error_message( $response ) );
return null;
}
$status = (int) wp_remote_retrieve_response_code( $response );
$raw_body = wp_remote_retrieve_body( $response );
if ( $status < 200 || $status >= 300 ) {
self::record_failure( $status, '' );
}

// A successful request proves the connection works, so clear any stale
// refresh-failure state that would otherwise keep the broken-connection
// notice showing while the plugin is communicating fine.
if ( $status >= 200 && $status < 300 ) {
\Scanfully\Cron\Controller::clear_refresh_failures();
self::record_success();
}

$decoded = null;
Expand All @@ -190,6 +220,63 @@ private function process_response( $response ): ?array {
];
}

/**
* Keep a failed request to Scanfully: no answer at all, with the
* transport's error, or an answer that says it did not get through.
* Scanfully answering 400 or 429 about one request does not count: the
* connection works, and the API would tell the site's owner it doesn't.
*
* @param int $status The HTTP status; 0 when no answer came.
* @param string $message The error of a request that got no answer.
*
* @return void
*/
private static function record_failure( int $status, string $message ): void {
if ( $status >= 400 && $status < 500 && ! in_array( $status, [ 401, 403, 407 ], true ) ) {
return;
}

update_option(
self::OPTION_LAST_ERROR,
[
'at' => time(),
'status' => $status,
'message' => mb_substr( trim( $message ), 0, self::MAX_ERROR_LENGTH ),
],
false
);
}

/**
* The message of a transport error, or '' when it holds none.
*
* @param mixed $error What is_wp_error() took for an error.
*
* @return string
*/
private static function error_message( $error ): string {
return $error instanceof \WP_Error ? $error->get_error_message() : '';
}

/**
* The plugin's last failed request to Scanfully, or null when none
* failed since the site connected.
*
* @return array{at: int, status: int, message: string}|null
*/
public static function last_error(): ?array {
$error = get_option( self::OPTION_LAST_ERROR );
if ( ! is_array( $error ) || ! isset( $error['at'] ) ) {
return null;
}

return [
'at' => (int) $error['at'],
'status' => (int) ( $error['status'] ?? 0 ),
'message' => (string) ( $error['message'] ?? '' ),
];
}

/**
* Get the auth headers for the request.
*
Expand Down
5 changes: 5 additions & 0 deletions src/Connect/AdminNotice.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ private static function is_connection_stale( \Scanfully\Options\Options $options
return true;
}

// Just activated: it hasn't had the chance to report yet.
if ( get_transient( 'scanfully_recently_activated' ) ) {
return false;
}

$reference_date = $options->last_used;

// Fall back to date_connected if last_used is empty.
Expand Down
40 changes: 37 additions & 3 deletions src/Connect/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,21 +427,55 @@ public static function refresh_access_token( string $refresh_token, string $site
* a database dump) stays valid on the API side until it expires.
*
* Best effort: a failure never blocks the disconnect, and the timeout is
* short so an unreachable API doesn't hold up the admin.
* short so an unreachable API doesn't hold up the admin. It says the
* site was disconnected, and by whom, so the site's owner learns why the
* plugin stopped reporting.
*
* @return void
*/
public static function revoke_tokens(): void {
self::tell_connection( 'revoke', [ 'reason' => 'disconnected' ] );
}

/**
* Tell the API that the plugin is being deactivated, and by whom, so the
* site's owner learns why the plugin stopped reporting. Called from the
* deactivation hook, before the jobs are cancelled. The tokens stay:
* activating the plugin again lets it report with them.
*
* @return void
*/
public static function report_deactivated(): void {
self::tell_connection( 'deactivated', [] );
}

/**
* Send a request about the connection itself to the API: the revoke or
* the deactivation, with who did it. Best effort with a short timeout:
* it never blocks what the user is doing.
*
* @param string $action The connect route: revoke or deactivated.
* @param array $body What the request says, besides who did it.
*
* @return void
*/
private static function tell_connection( string $action, array $body ): void {
$site_id = OptionsController::get_option( 'site_id' );
$access_token = OptionsController::get_option( 'access_token' );
if ( '' === $site_id || '' === $access_token ) {
return;
}

$body['by'] = \Scanfully\Events\Event::safe_display_name( wp_get_current_user() );

wp_remote_post(
Main::get_api_url() . '/sites/' . rawurlencode( $site_id ) . '/connect/revoke',
Main::get_api_url() . '/sites/' . rawurlencode( $site_id ) . '/connect/' . $action,
[
'headers' => [ 'Authorization' => sprintf( 'Bearer %s', $access_token ) ],
'headers' => [
'Authorization' => sprintf( 'Bearer %s', $access_token ),
'Content-Type' => 'application/json',
],
'body' => wp_json_encode( $body ),
'timeout' => 5,
'blocking' => true,
'httpversion' => '1.0',
Expand Down
7 changes: 4 additions & 3 deletions src/Cron/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -198,10 +198,11 @@ public static function schedule_events(): void {
// Email deliverability runs as a self-scheduling single action (each run
// arms the next via EmailHealth\Controller::schedule_next_ping) so the
// cadence can adapt without the duplicate pending actions a recurring
// action produced. This only bootstraps the chain, or heals it if it stalls.
// action produced. This only bootstraps the chain, or heals it if it stalls,
// so the first ping goes soon: after activating the plugin again, the Email
// page should not wait a whole interval for it.
if ( ! as_has_scheduled_action( self::ACTION_EMAIL_DELIVERABILITY_PING, [], self::AS_GROUP ) ) {
$interval = \Scanfully\EmailHealth\Controller::current_interval_seconds();
as_schedule_single_action( time() + $interval, self::ACTION_EMAIL_DELIVERABILITY_PING, [], self::AS_GROUP, true );
as_schedule_single_action( time() + MINUTE_IN_SECONDS, self::ACTION_EMAIL_DELIVERABILITY_PING, [], self::AS_GROUP, true );
}

if ( ! as_has_scheduled_action( self::ACTION_SYNC_WOOCHECKOUT_CONFIG, [], self::AS_GROUP ) ) {
Expand Down
5 changes: 3 additions & 2 deletions src/Events/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,14 @@ protected function get_actor( array $args ): ?object {
* A user's display name, safe to send. WordPress defaults the display
* name to the login, which is often an email address; an email address
* is replaced by the user's first and last name, or left empty, so no
* email address leaves the site.
* email address leaves the site. Public, as the connection's own
* requests name who deactivated or disconnected the plugin the same way.
*
* @param object $user The user (a WP_User).
*
* @return string
*/
protected static function safe_display_name( object $user ): string {
public static function safe_display_name( object $user ): string {
$name = trim( (string) ( $user->display_name ?? '' ) );
if ( ! self::looks_like_email( $name ) ) {
return $name;
Expand Down
1 change: 1 addition & 0 deletions src/Options/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ public static function clear() {
delete_option( self::$db_prefix . 'expires' );
delete_option( self::$db_prefix . 'last_used' );
delete_option( self::$db_prefix . 'date_connected' );
delete_option( self::$db_prefix . 'last_request_error' );

// Email deliverability sub-feature: per-site secret + cached state.
delete_option( self::$db_prefix . 'email_deliverability_enabled' );
Expand Down
29 changes: 27 additions & 2 deletions src/Sync/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public static function handle_sync(): \WP_REST_Response {
if ( $last_run > 0 ) {
$retry_after = max( 1, self::RATE_LIMIT_WINDOW - ( time() - $last_run ) );

$response = new \WP_REST_Response( [ 'status' => 'rate_limited' ], 429 );
$response = new \WP_REST_Response( array_merge( [ 'status' => 'rate_limited' ], self::diagnostics() ), 429 );
$response->header( 'Retry-After', (string) $retry_after );

return $response;
Expand All @@ -140,7 +140,32 @@ public static function handle_sync(): \WP_REST_Response {
self::schedule_syncs();
self::run_queue_now();

return new \WP_REST_Response( [ 'status' => 'scheduled' ], 202 );
return new \WP_REST_Response( array_merge( [ 'status' => 'scheduled' ], self::diagnostics() ), 202 );
}

/**
* What the plugin tells the API about itself in the answer: its version,
* when its last request to Scanfully worked and its last one that
* failed. The answer comes from this web request, so it gets through
* when the plugin's scheduled jobs do not run. Only the token holder (the
* API) gets it, and it holds no site data.
*
* @return array
*/
private static function diagnostics(): array {
$last_used = Options\Controller::get_option( 'last_used' );
$last_error = \Scanfully\API\Request::last_error();
$used_at = '' === $last_used ? false : strtotime( $last_used . ' UTC' );

return [
'version' => defined( 'SCANFULLY_VERSION' ) ? SCANFULLY_VERSION : '',
'last_success_at' => false === $used_at ? null : gmdate( 'c', $used_at ),
'last_error' => null === $last_error ? null : [
'at' => gmdate( 'c', $last_error['at'] ),
'status' => $last_error['status'],
'message' => $last_error['message'],
],
];
}

/**
Expand Down
Loading
Loading