}|\WP_Error */ public static function request( $method, $path, $body = null, $env = null, $timeout = 20 ) { $token = Connection::token( $env ); if ( null === $token ) { return new \WP_Error( 'thridify_not_connected', __( 'This site is not connected to Thridify.', 'thridify' ) ); } $args = array( 'method' => $method, 'timeout' => $timeout, 'headers' => array( 'Authorization' => 'Bearer ' . $token, 'Accept' => 'application/json', 'User-Agent' => 'thridify-woocommerce/' . THRIDIFY_VERSION . '; ' . home_url( '/' ), ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $response = wp_remote_request( Env::host( 'api', $env ) . '/v1/platform/link' . $path, $args ); if ( is_wp_error( $response ) ) { return $response; } $status = (int) wp_remote_retrieve_response_code( $response ); $decoded = json_decode( (string) wp_remote_retrieve_body( $response ), true ); $decoded = is_array( $decoded ) ? $decoded : array(); if ( 401 === $status ) { Connection::mark_reconnect( $env ); } return array( 'status' => $status, 'body' => $decoded, ); } /** * A short human message for a failed call. * * @param array|\WP_Error $result Result of request(). * @return string */ public static function error_message( $result ) { if ( is_wp_error( $result ) ) { return $result->get_error_message(); } if ( 401 === $result['status'] ) { return __( 'Thridify no longer accepts this connection. Connect the site again.', 'thridify' ); } if ( ! empty( $result['body']['error'] ) && is_string( $result['body']['error'] ) ) { return $result['body']['error']; } /* translators: %d: HTTP status code. */ return sprintf( __( 'Thridify answered with HTTP %d.', 'thridify' ), (int) $result['status'] ); } /** * Whether a result is a 2xx answer. * * @param array|\WP_Error $result Result of request(). * @return bool */ public static function ok( $result ) { return ! is_wp_error( $result ) && $result['status'] >= 200 && $result['status'] < 300; } /** * Refresh and cache the health answer (account, team, sandbox access). * * @param string|null $env Environment. * @return array|\WP_Error */ public static function refresh_health( $env = null ) { $result = self::request( 'GET', '/health', null, $env, 15 ); if ( ! self::ok( $result ) ) { return is_wp_error( $result ) ? $result : new \WP_Error( 'thridify_health', self::error_message( $result ) ); } $health = $result['body']; $account = isset( $health['accountHash'] ) ? (string) $health['accountHash'] : ''; Connection::merge( array( 'status' => 'connected', 'account' => preg_match( '/^[a-f0-9]{32}$/', $account ) ? $account : '', 'health' => array( 'teamName' => isset( $health['teamName'] ) ? (string) $health['teamName'] : '', 'accountEmail' => isset( $health['accountEmail'] ) ? (string) $health['accountEmail'] : '', 'accountName' => isset( $health['accountName'] ) ? (string) $health['accountName'] : '', 'linkedSite' => isset( $health['linkedSite'] ) ? (string) $health['linkedSite'] : '', 'sandboxAccess' => ! empty( $health['sandboxAccess'] ), ), 'checked_at' => time(), ), $env ); return Connection::health( $env ); } }