_wp_connectors_get_api_key_source()WP 7.0.0

Determines the source of an API key for a given connector.

Checks in order: environment variable, PHP constant, database. Environment variable and constant are only checked when their respective names are provided.

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

String. The key source: 'env', 'constant', 'database', or 'none'.

Usage

_wp_connectors_get_api_key_source( $setting_name, $env_var_name, $constant_name ): string;
$setting_name(string) (required)
The option name for the API key (e.g., 'connectors_spam_filtering_my_plugin_api_key').
$env_var_name(string)
Environment variable name to check (e.g., 'MY_PLUGIN_API_KEY').
Default: ''
$constant_name(string)
PHP constant name to check (e.g., 'MY_PLUGIN_API_KEY').
Default: ''

Changelog

Since 7.0.0 Introduced.

_wp_connectors_get_api_key_source() code WP 7.0

function _wp_connectors_get_api_key_source( string $setting_name, string $env_var_name = '', string $constant_name = '' ): string {
	// Check environment variable first.
	if ( '' !== $env_var_name ) {
		$env_value = getenv( $env_var_name );
		if ( false !== $env_value && '' !== $env_value ) {
			return 'env';
		}
	}

	// Check PHP constant.
	if ( '' !== $constant_name && defined( $constant_name ) ) {
		$const_value = constant( $constant_name );
		if ( is_string( $const_value ) && '' !== $const_value ) {
			return 'constant';
		}
	}

	// Check database.
	$db_value = get_option( $setting_name, '' );
	if ( '' !== $db_value ) {
		return 'database';
	}

	return 'none';
}