site id, 'secret' => site secret, 'connected' => bool ]. * * @return array */ public static function site() { $site = get_option( self::OPTION, array() ); return is_array( $site ) ? $site : array(); } /** * Whether the site is registered with the service. * * @return bool */ public static function is_registered() { $site = self::site(); return ! empty( $site['id'] ) && ! empty( $site['secret'] ); } /** * Whether the merchant completed the connection. * * @return bool */ public static function is_connected() { $site = self::site(); return self::is_registered() && ! empty( $site['connected'] ); } /** * Base URL of the service. * * @return string */ public static function server_url() { return untrailingslashit( EAZIGN_SERVER_URL ); } /** * Calls the API. * * @param string $method HTTP method. * @param string $path Path below /woo/v1/, may contain a query string. * @param array|null $body JSON body. * @param array $options signed (bool, default true), timeout (seconds). * @return array|WP_Error Decoded JSON answer. */ public static function request( $method, $path, $body = null, $options = array() ) { $options = wp_parse_args( $options, array( 'signed' => true, 'timeout' => 30, ) ); $method = strtoupper( $method ); $path = '/woo/v1/' . ltrim( $path, '/' ); $payload = null === $body ? '' : wp_json_encode( $body ); $headers = array( 'Accept' => 'application/json' ); if ( '' !== $payload ) { $headers['Content-Type'] = 'application/json'; } if ( $options['signed'] ) { $site = self::site(); if ( empty( $site['id'] ) || empty( $site['secret'] ) ) { return new WP_Error( 'eazign_not_connected', __( 'This shop is not connected to Eazign yet.', 'eazign' ) ); } $time = (string) time(); $headers['X-Eazign-Site'] = $site['id']; $headers['X-Eazign-Time'] = $time; $headers['X-Eazign-Signature'] = hash_hmac( 'sha256', $time . "\n" . $method . "\n" . $path . "\n" . hash( 'sha256', $payload ), $site['secret'] ); } $response = wp_remote_request( self::server_url() . $path, array( 'method' => $method, 'headers' => $headers, 'body' => '' === $payload ? null : $payload, 'timeout' => $options['timeout'], ) ); if ( is_wp_error( $response ) ) { return $response; } $code = wp_remote_retrieve_response_code( $response ); $data = json_decode( wp_remote_retrieve_body( $response ), true ); if ( $code >= 400 || ! is_array( $data ) ) { $message = is_array( $data ) && ! empty( $data['error'] ) ? $data['error'] : sprintf( /* translators: %d: HTTP status code */ __( 'Eazign service error (HTTP %d)', 'eazign' ), $code ); return new WP_Error( 'eazign_http_' . $code, $message, array( 'status' => $code ) ); } return $data; } /** * Shortcuts. * * @param string $path Path below /woo/v1/. * @return array|WP_Error */ public static function get( $path ) { return self::request( 'GET', $path ); } /** * POST shortcut. * * @param string $path Path below /woo/v1/. * @param array $body JSON body. * @param array $options See request(). * @return array|WP_Error */ public static function post( $path, $body = array(), $options = array() ) { return self::request( 'POST', $path, $body, $options ); } }