Api_Credentials_Screen::validate_credentials()

Summary

Validate API credentials before saving


Return

(void)


Source

File: src/BigCommerce/Settings/Screens/Api_Credentials_Screen.php

	public function validate_credentials() {
		if ( filter_input( INPUT_POST, 'option_page', FILTER_SANITIZE_STRING ) !== Api_Credentials_Screen::NAME ) {
			return;
		}
		$config = new Configuration();
		$config->setHost( untrailingslashit( filter_input( INPUT_POST, Api_Credentials::OPTION_STORE_URL, FILTER_SANITIZE_URL ) ) );
		$config->setClientId( filter_input( INPUT_POST, Api_Credentials::OPTION_CLIENT_ID, FILTER_SANITIZE_STRING ) );
		$config->setAccessToken( filter_input( INPUT_POST, Api_Credentials::OPTION_ACCESS_TOKEN, FILTER_SANITIZE_STRING ) );
		$config->setClientSecret( filter_input( INPUT_POST, Api_Credentials::OPTION_CLIENT_SECRET, FILTER_SANITIZE_STRING ) );
		/**
		 * This filter is documented in src/BigCommerce/Container/Api.php.
		 */
		$config->setCurlTimeout( apply_filters( 'bigcommerce/api/timeout', 15 ) );
		$client = new Base_Client( $config );
		$api = new CatalogApi( $client );

		try {
			// throws an exception on any non-2xx response
			$api->catalogSummaryGet();
		} catch ( \Exception $e ) {
			add_settings_error( self::NAME, 'submitted', __( 'Unable to connect to the BigCommerce API. Please re-enter your credentials.', 'bigcommerce' ), 'error' );
			set_transient( 'settings_errors', get_settings_errors(), 30 );

			// prevent saving of the options
			wp_safe_redirect( esc_url_raw( add_query_arg( [ 'settings-updated' => 1 ], $this->get_url() ) ), 303 );
			exit();
		}
	}


User Contributed Notes

You must log in before being able to contribute a note or feedback.