Mailchimp_Helpers::validate_oauth_tokens()

Callback function for OAuth redirect verification.


Source Source

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

	public function validate_oauth_tokens() {

		if ( ! empty( $_GET['automator_api_message'] ) && isset( $_REQUEST['tab'] ) && $this->setting_tab == $_REQUEST['tab'] ) {
			try {
				if ( ! empty( $_GET['automator_api_message'] ) ) {
					global $uncanny_automator;
					$secret = get_transient( 'automator_api_mailchimp_authorize_nonce' );
					$tokens = Automator_Helpers_Recipe::automator_api_decode_message( $_GET['automator_api_message'], $secret );

					if ( ! empty( $tokens['access_token'] ) ) {

						$user_info = array(
							'email'      => '',
							'avatar'     => '',
							'login_name' => '',
						);

						if ( isset( $tokens['login'] ) ) {
							$user_info['email']      = $tokens['login']->email ?? '';
							$user_info['avatar']     = $tokens['login']->avatar ?? '';
							$user_info['login_name'] = $tokens['login']->login_name ?? '';
						}

						// Update user info settings.
						update_option( '_uncannyowl_mailchimp_settings_user_info', $user_info );

						// On success
						update_option( '_uncannyowl_mailchimp_settings', $tokens );
						delete_option( '_uncannyowl_mailchimp_settings_expired' );
						//set the transient
						set_transient( '_uncannyowl_mailchimp_settings', $tokens['access_token'] . '|' . $tokens['dc'], 60 * 50 );
						wp_safe_redirect( admin_url( 'edit.php?post_type=uo-recipe&page=uncanny-automator-settings&tab=' . $this->setting_tab . '&connect=1' ) );
						die;

					} else {
						// On Error
						wp_safe_redirect( admin_url( 'edit.php?post_type=uo-recipe&page=uncanny-automator-settings&tab=' . $this->setting_tab . '&connect=2' ) );
						die;
					}
				}
			} catch ( \Exception $e ) {
				// On Error
				wp_safe_redirect( admin_url( 'edit.php?post_type=uo-recipe&page=uncanny-automator-settings&tab=' . $this->setting_tab . '&connect=2' ) );
				die;
			}
		}
	}