|null $body JSON body, or null. * @param string $site Which address to send: Api::SITE_LINKED or Api::SITE_CURRENT. * @return array{ok:bool,status:int,data:mixed,error:string} */ public static function request( string $method, string $path, ?array $body = null, string $site = self::SITE_LINKED ): array { if ( ! self::context_allowed() ) { return self::fail( 0, __( 'Outercite is only contacted from admin screens.', 'outercite' ) ); } if ( empty( Connection::get()['access_token'] ) ) { return self::fail( 401, __( 'This site is not signed in to Outercite.', 'outercite' ) ); } $host = self::SITE_CURRENT === $site ? Site::current_host() : ( Site::linked_host() ?? Site::current_host() ); $result = self::send( $method, $path, $body, $host ); // An expired or revoked token: refresh once and retry, unless this is a copy. // Revoked only when a fresh token is refused too; a refresh that could not // run (an outage, another request refreshing) leaves the sign-in alone. if ( 401 === $result['status'] && ! Site::is_staging() && Connection::refresh() ) { $result = self::send( $method, $path, $body, $host ); if ( 401 === $result['status'] ) { Connection::mark_revoked(); } } return $result; } /** * Send one request. * * @param string $method HTTP method. * @param string $path API path, starting with a slash. * @param array|null $body JSON body, or null for none. * @param string $host Site address for the site header. * @return array{ok:bool,status:int,data:mixed,error:string} */ private static function send( string $method, string $path, ?array $body, string $host ): array { $token = Connection::access_token(); if ( ! $token ) { return self::fail( 401, __( 'This site is not signed in to Outercite.', 'outercite' ) ); } $args = array( 'method' => $method, 'timeout' => 12, 'user-agent' => 'Outercite-WordPress/' . OUTERCITE_VERSION . '; ' . home_url(), 'headers' => array( 'Authorization' => 'Bearer ' . $token, Site::HEADER => $host, 'Accept' => 'application/json', ), ); if ( null !== $body ) { $args['headers']['Content-Type'] = 'application/json'; $args['body'] = wp_json_encode( $body ); } $res = wp_remote_request( untrailingslashit( OUTERCITE_API_URL ) . $path, $args ); if ( is_wp_error( $res ) ) { return self::fail( 0, __( 'Could not reach Outercite. Try again in a moment.', 'outercite' ) ); } $status = (int) wp_remote_retrieve_response_code( $res ); $json = json_decode( wp_remote_retrieve_body( $res ), true ); if ( $status >= 200 && $status < 300 ) { return array( 'ok' => true, 'status' => $status, 'data' => is_array( $json ) && array_key_exists( 'data', $json ) ? $json['data'] : null, 'error' => '', ); } $msg = is_array( $json ) && isset( $json['error']['message'] ) ? (string) $json['error']['message'] : sprintf( /* translators: %d: HTTP status code */ __( 'Outercite answered with an error (%d).', 'outercite' ), $status ); return self::fail( $status, $msg ); } /** * A failed result. * * @param int $status HTTP status, or 0 when Outercite was unreachable. * @param string $message Message for the user. * @return array{ok:bool,status:int,data:mixed,error:string} */ private static function fail( int $status, string $message ): array { return array( 'ok' => false, 'status' => $status, 'data' => null, 'error' => $message, ); } }