'forbidden' ), 403 ); } check_ajax_referer( 'accessibit_scan', 'nonce' ); // The screen renders no scan panels on a copy of another site's // database; this is the same refusal for a request that arrives anyway, // as the REST proxy makes (ACC-411). if ( Accessibit_Settings::is_binding_stale() ) { wp_send_json_error( array( 'code' => 'not_connected' ), 409 ); } } /** * The API client, built from the stored credentials. * * @return Accessibit_Api_Client */ private static function client(): Accessibit_Api_Client { return Accessibit_Api_Client::from_settings(); } /** * Return the connected site's latest scan summary. */ public static function summary(): void { self::guard(); $result = self::client()->summary( Accessibit_Settings::get_site_id() ); $result['ok'] ? wp_send_json_success( $result['data'] ) : wp_send_json_error( array( 'code' => $result['code'] ), 400 ); } /** * Start the free homepage scan (ACC-409). * * A domain that has already used its one free scan answers 200 * "already_used", so this succeeds either way and the caller goes straight * on to reading the result. * * The response body is NOT forwarded. It used to be, which handed * the browser the `jobId` of an AuditJob the dashboard would then serve the * full issue list for — the opposite of the five-issue cap this scan exists * to apply. The id has since been removed from the API response as well; this * is the half that does not depend on which API version answers. */ public static function free_scan_start(): void { self::guard(); // `determine_locale()`, the same lever the admin screens' own strings and // dates follow (`Accessibit_Admin::js_locale`). The scan's issue text and // the sentence introducing it should not be able to disagree about which // language this administrator reads. $result = self::client()->start_free_scan( Accessibit_Settings::get_site_id(), determine_locale() ); if ( ! $result['ok'] ) { // `retryAfterMs` travels with the refusal, like `jobId` does on the // paid scan's 409. The API answers 429 `free_scan_retry_too_soon` // with how long is left of the retry backoff, and forwarding only // `code` threw it away — so the screen could say nothing better than // "AccessiBit is busy just now", for a wait of up to ten minutes that // it knew the length of. Nothing else in the error body is passed on: // the reason the plugin needs is the code, and a refusal is not a // place to widen what reaches the browser. // Top level, beside `error`, not inside it: v1's `fail()` spreads its // extras over the response root (`{ error: {code, message}, ... }`). $retry_after = $result['data']['retryAfterMs'] ?? null; wp_send_json_error( array( 'code' => $result['code'], 'retryAfterMs' => is_numeric( $retry_after ) ? (int) $retry_after : null, ), 400 ); } wp_send_json_success( array( 'status' => $result['data']['status'] ?? 'accepted' ) ); } /** * Read the free scan's progress and, once it is done, its preview. * * The payload is passed through untouched. The service has already limited * it to the issues we are willing to show; the plugin neither filters nor * holds anything back, which is what keeps it inside WordPress.org's rule * against shipping locked functionality. */ public static function free_scan_result(): void { self::guard(); $result = self::client()->free_scan( Accessibit_Settings::get_site_id() ); if ( ! $result['ok'] ) { // `retryAfterMs` is forwarded here as well as on the start call, for // the same reason and so the screen behaves the same whichever call // hit the refusal: a 503 `scanner_busy` carries the wait, and // forwarding only the code left the panel saying "reload this page in // a minute" while holding the number of seconds in its hand. $retry_after = $result['data']['retryAfterMs'] ?? null; wp_send_json_error( array( 'code' => $result['code'], 'retryAfterMs' => is_numeric( $retry_after ) ? (int) $retry_after : null, ), 400 ); } wp_send_json_success( $result['data'] ); } }