self::TIMEOUT, 'redirection' => 0, 'headers' => array( 'Content-Type' => 'application/json; charset=utf-8', 'Accept' => 'application/json', 'X-Ascue-Site-Key' => $site_key, 'User-Agent' => 'Ascue-WordPress/' . ASCUE_VERSION . '; WordPress/' . get_bloginfo( 'version' ), ), 'body' => $body, 'data_format' => 'body', ) ); if ( is_wp_error( $response ) ) { return self::result( false, 0, $response->get_error_message(), '' ); } $status = (int) wp_remote_retrieve_response_code( $response ); if ( 202 === $status ) { return self::result( true, $status, 'HTTP 202', '' ); } // Errors are { "error": "", "message": "" }. $error_message = ''; $decoded = json_decode( (string) wp_remote_retrieve_body( $response ), true ); if ( is_array( $decoded ) && isset( $decoded['message'] ) && is_string( $decoded['message'] ) ) { $error_message = sanitize_text_field( $decoded['message'] ); } return self::result( false, $status, 'HTTP ' . $status, $error_message ); } /** * Builds a result array. * * @param bool $accepted Whether Ascue accepted the message. * @param int $status HTTP status, or 0. * @param string $detail Short description. * @param string $error_message Message from the API's error body, if any. * @return array{accepted:bool,status:int,detail:string,error_message:string} */ private static function result( $accepted, $status, $detail, $error_message ) { return array( 'accepted' => $accepted, 'status' => $status, 'detail' => $detail, 'error_message' => $error_message, ); } }