_wp_connectors_is_ai_api_key_valid()WP 7.0.0

Checks whether an API key is valid for a given provider.

Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.

No Hooks.

Returns

true|false|null. True if valid, false if invalid, null if unable to determine.

Usage

_wp_connectors_is_ai_api_key_valid( $key, $provider_id ): ?bool;
$key(string) (required)
The API key to check.
$provider_id(string) (required)
The WP AI client provider ID.

Changelog

Since 7.0.0 Introduced.

_wp_connectors_is_ai_api_key_valid() code WP 7.0

function _wp_connectors_is_ai_api_key_valid( string $key, string $provider_id ): ?bool {
	try {
		$registry = AiClient::defaultRegistry();

		if ( ! $registry->hasProvider( $provider_id ) ) {
			_doing_it_wrong(
				__FUNCTION__,
				sprintf(
					/* translators: %s: AI provider ID. */
					__( 'The provider "%s" is not registered in the AI client registry.' ),
					$provider_id
				),
				'7.0.0'
			);
			return null;
		}

		$registry->setProviderRequestAuthentication(
			$provider_id,
			new ApiKeyRequestAuthentication( $key )
		);

		return $registry->isProviderConfigured( $provider_id );
	} catch ( Exception $e ) {
		wp_trigger_error( __FUNCTION__, $e->getMessage() );
		return null;
	}
}