plugin_name = $plugin_name; $this->version = $version; } /** * Make an API call to the Accessibility Assistant server. * * @since 1.0.0 * @param string $path The API endpoint path. * @param array $fields The data to send with the request. * @param string $type The request type (post or get). * @return array|mixed The JSON decoded response. */ public function call( $path, $fields, $type ) { $url = ASSISTANT_API_URL . $path; $apidata = array( 'body' => $fields, 'timeout' => 30, //'sslverify' => false, 'redirection' => '5', 'httpversion' => '1.0', 'blocking' => true, 'headers' => array(), 'cookies' => array() ); if ( $type == 'post' ) { $response = wp_remote_post( $url, $apidata ); } else { $response = wp_remote_get( $url, $apidata ); } if ( is_wp_error( $response ) ) { $msg = $response->get_error_message(); error_log( '[Accessibility Plugin] API call error to ' . $url . ': ' . $msg ); return array( 'status' => 0, 'error' => 'API request failed: ' . $msg, ); } $http_code = (int) wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( empty( $body ) ) { error_log( '[Accessibility Plugin] API response body empty for ' . $url . ' (HTTP ' . $http_code . ')' ); return array( 'status' => $http_code ? $http_code : 0, 'error' => 'API returned an empty response body.', 'http_code' => $http_code, ); } $decoded = json_decode( $body, true ); if ( json_last_error() !== JSON_ERROR_NONE ) { error_log( '[Accessibility Plugin] API invalid JSON for ' . $url . ' (HTTP ' . $http_code . '): ' . json_last_error_msg() ); return array( 'status' => $http_code ? $http_code : 0, 'error' => 'API returned invalid JSON: ' . json_last_error_msg(), 'http_code' => $http_code, 'raw' => substr( (string) $body, 0, 500 ), ); } // If server returns non-2xx, surface a generic error while preserving any payload. if ( $http_code < 200 || $http_code >= 300 ) { $err = 'API request failed with HTTP ' . $http_code . '.'; if ( is_array( $decoded ) && ! empty( $decoded['message'] ) && is_string( $decoded['message'] ) ) { $err = (string) $decoded['message']; } return array( 'status' => $http_code, 'error' => $err, 'http_code' => $http_code, 'data' => is_array( $decoded ) ? $decoded : array(), ); } return $decoded; } /** * Get Access Token for the scanner API. * * @since 1.0.0 * @return string|WP_Error The access token or WP_Error on failure. */ public function get_access_token() { $url = 'https://accessscanner.accessibilityassistant.com/generate-token'; $args = array( 'timeout' => 10, 'headers' => array( 'X-API-KEY' => '2542342990af6dc61257b52ef1070a5ca04d9ded77989e9600f08f2d2fb032ec', ), ); $response = wp_remote_get( $url, $args ); if ( is_wp_error( $response ) ) { error_log( 'WP Remote Get error in get_access_token: ' . $response->get_error_message() ); return $response; } $http_code = wp_remote_retrieve_response_code( $response ); if ( $http_code !== 200 ) { $body = wp_remote_retrieve_body( $response ); error_log( "API call failed with status $http_code and response: $body" ); return new WP_Error( 'api_error', 'API returned status ' . $http_code ); } $body = wp_remote_retrieve_body( $response ); $data = json_decode( $body, true ); if ( json_last_error() !== JSON_ERROR_NONE || empty( $data['access_token'] ) ) { return new WP_Error( 'no_token', 'Access token not found in API response' ); } return $data['access_token']; } /** * Run an accessibility audit for a given URL. * * @since 1.0.0 * @param string $url The URL to audit. * @return array|mixed The audit result. */ public function get_accessibility_audit( $url ) { $token = $this->get_access_token(); if ( is_wp_error( $token ) ) { return ['error' => 'Unable to retrieve token']; } $api_url = 'https://accessscanner.accessibilityassistant.com/single-audit'; $payload = wp_json_encode( ['url' => $url], JSON_UNESCAPED_SLASHES ); $args = array( 'body' => $payload, 'headers' => array( 'Authorization' => "Bearer $token", 'Content-Type' => 'application/json', ), 'timeout' => 60, // Scans might take longer 'sslverify' => false, ); $response = wp_remote_post( $api_url, $args ); $http_code = wp_remote_retrieve_response_code( $response ); $body = wp_remote_retrieve_body( $response ); if ( $http_code === 200 && $body ) { return json_decode( $body, true ); } return ['error' => 'Audit API request failed', 'status' => $http_code]; } }