Salsox first.', 'salsox' ) ); } $args = array( 'method' => $method, 'timeout' => 'POST' === $method ? 30 : 15, 'headers' => array( 'Authorization' => 'Bearer ' . $key, 'Accept' => 'application/json', 'User-Agent' => 'Salsox-WordPress/' . SALSOX_VERSION . '; ' . home_url( '/' ), ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $response = wp_remote_request( untrailingslashit( SALSOX_SERVER ) . '/api/v1' . $path, $args ); if ( is_wp_error( $response ) ) { return new WP_Error( 'salsox_unreachable', __( 'Salsox could not be reached. Check that this server can make outgoing HTTPS requests, then try again.', 'salsox' ) ); } $status = (int) wp_remote_retrieve_response_code( $response ); if ( 204 === $status ) { return array(); } $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $status >= 200 && $status < 300 && is_array( $data ) ) { return $data; } /* * A rejected key gets the plugin's own sentence. Salsox's is written for * developers ("Send a valid API key as `Authorization: Bearer `."), * and sanitize_text_field() strips "" from it as if it were a tag, * which left the site owner reading "… Bearer `." */ if ( 401 === $status ) { return new WP_Error( 'salsox_unauthorized', __( 'Salsox did not accept this API key. It may have been revoked or copied incompletely. Create a new key on the Developers page in Salsox and paste it here.', 'salsox' ), array( 'status' => 401 ) ); } if ( is_array( $data ) && isset( $data['error']['code'], $data['error']['message'] ) ) { return new WP_Error( 'salsox_' . sanitize_key( $data['error']['code'] ), sanitize_text_field( $data['error']['message'] ), array( 'status' => $status ) ); } /* translators: %d: HTTP status code. */ return new WP_Error( 'salsox_http', sprintf( __( 'Salsox answered with an unexpected response (HTTP %d).', 'salsox' ), $status ), array( 'status' => $status ) ); } /** * The account the key belongs to: id, email, name and plan. * * @param string|null $key A key to test instead of the saved one. * @return array|WP_Error */ public static function me( $key = null ) { return self::request( 'GET', '/me', null, $key ); } /** * The templates a form can send, cached for ten minutes. * * @param bool $fresh Skip the cache. * @return array|WP_Error { data: library templates, saved: saved templates } */ public static function templates( $fresh = false ) { if ( ! $fresh ) { $cached = get_transient( self::TEMPLATES_CACHE ); if ( is_array( $cached ) ) { return $cached; } } $result = self::request( 'GET', '/templates' ); if ( ! is_wp_error( $result ) ) { set_transient( self::TEMPLATES_CACHE, $result, 10 * MINUTE_IN_SECONDS ); } return $result; } /** * Creates and sends a document. * * @param array $body The POST /api/v1/documents body. * @return array|WP_Error The document, with `undelivered` and `not_emailed` counts. */ public static function send_document( array $body ) { return self::request( 'POST', '/documents', $body ); } /** * The account's latest documents, newest first. * * @param int $limit Up to 100. * @return array|WP_Error { data, next_cursor } */ public static function documents( $limit = 100 ) { return self::request( 'GET', '/documents?limit=' . absint( $limit ) ); } /** * Revokes the saved key at Salsox. * * @return array|WP_Error */ public static function revoke_key() { return self::request( 'DELETE', '/keys/current' ); } }