Google_Sheet_Helpers::get_user_info()

Get the user info.


Return Return

(array) The user info.


Source Source

File: src/integrations/google-sheet/helpers/google-sheet-helpers.php

	public function get_user_info() {

		$profile_endpoint = 'https://www.googleapis.com/oauth2/v3/userinfo';

		$transient_key = '_uncannyowl_google_user_info';

		$user_info = array(
			'avatar_uri' => '',
			'name'       => '',
		);

		$saved_user_info = get_transient( $transient_key );

		if ( false === $saved_user_info ) {

			$google_sheet_options = get_option( '_uncannyowl_google_sheet_settings' );

			if ( false === $google_sheet_options ) {
				return $saved_user_info;
			}

			$access_token = $google_sheet_options['access_token'] ?? "";

			$headers = array(
				'Authorization'  => 'Bearer ' . $access_token,
				'Content-Length' => 0,
			);

			$request = wp_remote_get(
				$profile_endpoint,
				array(
					'headers' => $headers,
				)
			);

			if ( ! is_wp_error( $request ) ) {
				$response = json_decode( wp_remote_retrieve_body( $request ) );
				if ( ! isset( $response->error ) ) {
					$user_info['name']       = $response->name;
					$user_info['avatar_uri'] = $response->picture;
					$user_info['email']      = $response->email;
					set_transient( '_uncannyowl_google_user_info', $user_info, WEEK_IN_SECONDS );
				}
			}
		} else {

			return $saved_user_info;

		}

		return $user_info;

	}