Gotowebinar_Helpers::get_webinar_token()

To get webinar access token and organizer key


Return Return

(array)


Source Source

File: src/integrations/gotowebinar/helpers/gotowebinar-helpers.php

	public function get_webinar_token() {

		$get_transient = get_transient( '_uncannyowl_gtw_settings' );

		if ( false !== $get_transient ) {

			$tokens = explode( '|', $get_transient );

			return array( $tokens[0], $tokens[1] );

		} 

		$oauth_settings        = get_option( '_uncannyowl_gtw_settings' );
		$current_refresh_token = isset( $oauth_settings['refresh_token'] ) ? $oauth_settings['refresh_token'] : '';

		if ( empty( $current_refresh_token ) ) {
			update_option( '_uncannyowl_gtw_settings_expired', true );
			throw new \Exception( __( 'GoTo Webinar credentails have expired.', 'uncanny-automator' ) );
		}

		$consumer_key    = trim( get_option( 'uap_automator_gtw_api_consumer_key', '' ) );
		$consumer_secret = trim( get_option( 'uap_automator_gtw_api_consumer_secret', '' ) );

		$params = array(
			'method' => 'POST',
			'url' => 'https://api.getgo.com/oauth/v2/token',
			'headers' => array(
				'Authorization' => 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret ), // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
				'Content-Type'  => 'application/x-www-form-urlencoded; charset=utf-8',
			),
			'body'    => array(
				'refresh_token' => $current_refresh_token,
				'grant_type'    => 'refresh_token',
			),
		);

		$response = $this->remote_request( $params );

		if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
			update_option( '_uncannyowl_gtw_settings', array() );
			update_option( '_uncannyowl_gtw_settings_expired', true );
			throw new \Exception( __( 'GoTo Webinar credentails have expired.', 'uncanny-automator' ) );
		}

		//get new access token and refresh token
		$jsondata = json_decode( $response['body'], true );

		$tokens_info                  = array();
		$tokens_info['access_token']  = $jsondata['access_token'];
		$tokens_info['refresh_token'] = $jsondata['refresh_token'];
		$tokens_info['organizer_key'] = $jsondata['organizer_key'];
		$tokens_info['account_key']   = $jsondata['account_key'];

		update_option( '_uncannyowl_gtw_settings', $tokens_info );
		set_transient( '_uncannyowl_gtw_settings', $tokens_info['access_token'] . '|' . $tokens_info['organizer_key'], 60 * 50 );
		delete_option( '_uncannyowl_gtw_settings_expired' );

		//return the array
		return array( $tokens_info['access_token'], $tokens_info['organizer_key'] );

	}