array( 'GET' => 'rest_summary' ), '/scan/free' => array( 'GET' => 'rest_free_scan', 'POST' => 'rest_start_free_scan', ), ); foreach ( $routes as $route => $methods ) { $endpoints = array(); foreach ( $methods as $verb => $handler ) { $endpoints[] = array( 'methods' => $verb, 'permission_callback' => array( 'Accessibit_Rest', 'permission' ), 'callback' => array( __CLASS__, $handler ), ); } register_rest_route( Accessibit_Rest::NAMESPACE_V1, $route, $endpoints ); } } /** * `GET /scan/summary`: the site's latest full audit. * * @return WP_REST_Response */ public static function rest_summary(): WP_REST_Response { return self::respond( self::bound() ? self::rest_answer( self::client()->summary( Accessibit_Settings::get_site_id() ) ) : self::not_connected() ); } /** * `GET /scan/free`: the free homepage scan's progress or preview, as the * service capped it. * * @return WP_REST_Response */ public static function rest_free_scan(): WP_REST_Response { return self::respond( self::bound() ? self::rest_answer( self::client()->free_scan( Accessibit_Settings::get_site_id() ) ) : self::not_connected() ); } /** * `POST /scan/free`: start the free homepage scan. Answers the status * alone, never the service's whole body (see `free_scan_start()`). * * @return WP_REST_Response */ public static function rest_start_free_scan(): WP_REST_Response { if ( ! self::bound() ) { return self::respond( self::not_connected() ); } $result = self::client()->start_free_scan( Accessibit_Settings::get_site_id(), determine_locale() ); $answer = self::rest_answer( $result ); if ( 200 === $answer['status'] ) { $answer['body'] = array( 'status' => $result['data']['status'] ?? 'accepted' ); } return self::respond( $answer ); } /** * Whether this install is bound to a site, and to its own: a copy of * another site's database must not read the original's figures (ACC-411). * * @return bool */ private static function bound(): bool { return Accessibit_Settings::is_site_linked() && ! Accessibit_Settings::is_binding_stale(); } /** * The proxy's own refusal for an install that is not connected. * * @return array{status: int, body: array} */ private static function not_connected(): array { return array( 'status' => 409, 'body' => array( 'error' => array( 'code' => 'accessibit_not_connected', 'message' => 'This site is not connected to AccessiBit.', ), ), ); } /** * A REST answer from an API client result: the data as it came, or the * refusal's code with the wait it carried (`retryAfterMs`), and nothing * else of the service's error body. * * Pure, for the unit suite. * * @param array $result An `Accessibit_Api_Client` result. * @return array{status: int, body: array} */ public static function rest_answer( array $result ): array { if ( ! empty( $result['ok'] ) ) { return array( 'status' => 200, 'body' => is_array( $result['data'] ?? null ) ? $result['data'] : array(), ); } $code = isset( $result['code'] ) && is_string( $result['code'] ) && preg_match( '/^[a-z0-9_]{1,64}$/', $result['code'] ) ? $result['code'] : 'request_failed'; $retry_after = $result['data']['retryAfterMs'] ?? null; return array( // One status for every refusal, as the admin-ajax handlers answer: // the app words them by code. Not the service's 401 or 403, which // on a REST route say the administrator's own WordPress session // failed, nor a 5xx, whose body some hosts replace with their own // error page. 'status' => 400, 'body' => array( 'error' => array( 'code' => $code, 'message' => 'AccessiBit refused the request', 'retryAfterMs' => is_numeric( $retry_after ) ? (int) $retry_after : null, ), ), ); } /** * An answer as a REST response nothing in between should keep. * * @param array{status: int, body: array} $answer From one of the handlers. * @return WP_REST_Response */ private static function respond( array $answer ): WP_REST_Response { $response = new WP_REST_Response( $answer['body'], $answer['status'] ); $response->header( 'Cache-Control', 'no-store' ); return $response; } /** * Refuse anyone who is not an administrator with a valid nonce. * * Both halves end the request themselves, so a handler can call this and then * assume it is authorised. */ private static function guard(): void { if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'code' => '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'] ); } }