Gototraining_Helpers::validate_oauth_tokens()

Callback function for OAuth redirect verification.


Source Source

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

	public function validate_oauth_tokens() {

		if ( ! automator_filter_has_var( 'state' ) || $this->setting_tab !== automator_filter_input( 'state' ) ) {
			return;
		}

		if ( ! automator_filter_has_var( 'code' ) ) {
			return;
		}

		$consumer_key    = trim( get_option( 'uap_automator_gtt_api_consumer_key', '' ) );
		$consumer_secret = trim( get_option( 'uap_automator_gtt_api_consumer_secret', '' ) );

		$code = wp_unslash( automator_filter_input( 'code' ) );

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

		$connect = 2;

		try {

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

			if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
				throw new \Exception( __( 'Error validating Oauth tokens', 'uncanny-automator' ) );
			}

			$jsondata = array();
			
			//lets get the response and decode it
			$jsondata = json_decode( $response['body'], true );

			// Update the options.
			update_option( '_uncannyowl_gtt_settings', $jsondata );
			delete_option( '_uncannyowl_gtt_settings_expired' );

			// Set the transient.
			set_transient( '_uncannyowl_gtt_settings', $jsondata['access_token'] . '|' . $jsondata['organizer_key'], 60 * 50 );

			$connect = 1;

		} catch ( \Exception $e ) {
			automator_log( $e->getMessage() );
		}
		
		wp_safe_redirect( automator_get_premium_integrations_settings_url( 'go-to-training' ) . '&connect=' . $connect );
		die;
		
	}