wp_connectors_get_application_password_credentials()
Resolves application-password credentials for a connector.
Checks in order: environment variable, PHP constant, database. The environment variable and constant are only checked when their respective names are provided, and must contain the credentials as a single username:password string. A non-empty environment variable or constant that cannot be parsed as username:password is reported with _doing_it_wrong() and ignored, so resolution falls through to the next source.
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
array{username: string, password: string, source: string}. Resolved credentials and their source: 'env', 'constant', 'database', or 'none'.
Usage
wp_connectors_get_application_password_credentials( $auth ): array;
- $auth(array) (required)
- The connector's authentication configuration.
Changelog
| Since 7.1.0 | Introduced. |
wp_connectors_get_application_password_credentials() wp connectors get application password credentials code WP 7.1
function wp_connectors_get_application_password_credentials( array $auth ): array {
// Check environment variable first.
$env_var_name = $auth['env_var_name'] ?? '';
if ( '' !== $env_var_name ) {
$env_value = getenv( $env_var_name );
if ( false !== $env_value && '' !== $env_value ) {
$credentials = wp_connectors_parse_application_password_credentials( $env_value );
if ( '' !== $credentials['username'] && '' !== $credentials['password'] ) {
$credentials['source'] = 'env';
return $credentials;
}
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: Environment variable name. */
__( 'The %s environment variable must contain application password credentials in "username:password" format.' ),
esc_html( $env_var_name )
),
'7.1.0'
);
}
}
// Check PHP constant.
$constant_name = $auth['constant_name'] ?? '';
if ( '' !== $constant_name && defined( $constant_name ) ) {
$const_value = constant( $constant_name );
if ( is_string( $const_value ) && '' !== $const_value ) {
$credentials = wp_connectors_parse_application_password_credentials( $const_value );
if ( '' !== $credentials['username'] && '' !== $credentials['password'] ) {
$credentials['source'] = 'constant';
return $credentials;
}
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: PHP constant name. */
__( 'The %s constant must contain application password credentials in "username:password" format.' ),
esc_html( $constant_name )
),
'7.1.0'
);
}
}
// Check database.
$stored = get_option( $auth['setting_name'] ?? '', array() );
$username = is_array( $stored ) && isset( $stored['username'] ) && is_string( $stored['username'] ) ? $stored['username'] : '';
$password = is_array( $stored ) && isset( $stored['password'] ) && is_string( $stored['password'] ) ? $stored['password'] : '';
return array(
'username' => $username,
'password' => $password,
'source' => '' !== $username && '' !== $password ? 'database' : 'none',
);
}