false, 'ok' => false, 'database' => '', 'version' => '', 'error' => '', ); $response = wp_remote_get( Config::api_base() . '/health', array( 'timeout' => 4, 'redirection' => 0, 'headers' => array( 'Accept' => 'application/json' ), 'user-agent' => 'Brickmoat-WooCommerce/' . BRICKMOAT_VERSION, ) ); if ( is_wp_error( $response ) ) { $result['error'] = $response->get_error_message(); return $result; } $result = array_merge( $result, self::parse_health( wp_remote_retrieve_body( $response ) ) ); if ( ! $result['reachable'] && '' === $result['error'] ) { $result['error'] = sprintf( 'Unexpected answer (HTTP %d).', (int) wp_remote_retrieve_response_code( $response ) ); } return $result; } /** * Reads a health answer. "Reachable" means the body really came from the Brickmoat * API, not from whatever else happens to answer at that address. * * @param string $body Response body. * @return array{reachable:bool,ok:bool,database:string,version:string} */ public static function parse_health( $body ) { $data = json_decode( (string) $body, true ); if ( ! is_array( $data ) || ! isset( $data['service'] ) || self::SERVICE_NAME !== $data['service'] ) { return array( 'reachable' => false, 'ok' => false, 'database' => '', 'version' => '', ); } return array( 'reachable' => true, 'ok' => isset( $data['ok'] ) && true === $data['ok'], 'database' => isset( $data['database'] ) && is_string( $data['database'] ) ? $data['database'] : '', 'version' => isset( $data['version'] ) && is_string( $data['version'] ) ? $data['version'] : '', ); } /** * Calls a licensed route of the service. Never throws; every failure comes back as ok = false. * * @param string $method GET, POST or PUT. * @param string $path Path under the API base, starting with "/". * @param array|null $body JSON body, or null for none. * @param float $timeout Seconds. Below 1, the request is cut off after that many milliseconds. * @param string $key Licence key to use; the saved one when ''. * @return array{ok:bool,status:int,data:?array,code:string,message:string} */ public static function request( $method, $path, $body = null, $timeout = 5, $key = '' ) { $key = '' !== $key ? $key : Licence::key(); if ( '' === $key ) { return self::failure( 0, 'licence_missing', 'No licence key is saved.' ); } $args = array( 'method' => $method, 'timeout' => max( 1, (float) $timeout ), 'redirection' => 0, 'headers' => array( 'Accept' => 'application/json', 'X-BM-Licence' => $key, 'X-BM-Site' => untrailingslashit( home_url() ), ), 'user-agent' => 'Brickmoat-WooCommerce/' . BRICKMOAT_VERSION, ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $fast = $timeout < 1; if ( $fast ) { self::$timeout_ms = max( 50, (int) round( $timeout * 1000 ) ); add_action( 'http_api_curl', array( __CLASS__, 'apply_fast_timeout' ), 99 ); } try { $response = wp_remote_request( Config::api_base() . $path, $args ); } catch ( \Throwable $error ) { $response = new \WP_Error( 'brickmoat_http', $error->getMessage() ); } if ( $fast ) { remove_action( 'http_api_curl', array( __CLASS__, 'apply_fast_timeout' ), 99 ); self::$timeout_ms = 0; } if ( is_wp_error( $response ) ) { return self::failure( 0, 'unreachable', $response->get_error_message() ); } $status = (int) wp_remote_retrieve_response_code( $response ); $data = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $data = is_array( $data ) ? $data : null; if ( $status >= 200 && $status < 300 ) { return array( 'ok' => true, 'status' => $status, 'data' => $data, 'code' => '', 'message' => '', ); } if ( 304 === $status ) { return array( 'ok' => true, 'status' => 304, 'data' => null, 'code' => '', 'message' => '', ); } $code = isset( $data['error']['code'] ) && is_string( $data['error']['code'] ) ? $data['error']['code'] : 'http_' . $status; $message = isset( $data['error']['message'] ) && is_string( $data['error']['message'] ) ? $data['error']['message'] : ''; $failure = self::failure( $status, $code, $message ); // Some refusals carry details worth keeping (an ended licence comes with when it ended). $failure['data'] = $data; return $failure; } /** * A failed answer. * * @param int $status HTTP status, 0 when the service could not be reached. * @param string $code Error code. * @param string $message Error message (for the log, not for merchants). * @return array */ private static function failure( $status, $code, $message ) { return array( 'ok' => false, 'status' => (int) $status, 'data' => null, 'code' => (string) $code, 'message' => (string) $message, ); } /** * Sets a sub-second time limit on the request in progress. WordPress's HTTP API will not go below 1 second. * * @param resource|\CurlHandle $handle cURL handle. */ public static function apply_fast_timeout( $handle ) { if ( self::$timeout_ms > 0 && defined( 'CURLOPT_TIMEOUT_MS' ) ) { // phpcs:disable WordPress.WP.AlternativeFunctions.curl_curl_setopt -- Adjusting the handle WordPress's HTTP API created; there is no API for sub-second timeouts. curl_setopt( $handle, CURLOPT_NOSIGNAL, 1 ); curl_setopt( $handle, CURLOPT_CONNECTTIMEOUT_MS, self::$timeout_ms ); curl_setopt( $handle, CURLOPT_TIMEOUT_MS, self::$timeout_ms ); // phpcs:enable } } }