Skip to content

Commit

Permalink
Only hide key option if not in the env
Browse files Browse the repository at this point in the history
  • Loading branch information
jhnstn committed Aug 30, 2024
1 parent 5cb0799 commit 9d995d0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 14 deletions.
2 changes: 1 addition & 1 deletion plugin/admin/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ function wpcloud_settings_init(): void {
);

// Only show the API key field if it's not set in the environment.
if ( ! wpcloud_get_api_key() ) {
if ( ! wpcloud_get_api_key_from_env() ) {
add_settings_field(
'wpcloud_field_api_key',
__( 'API Key', 'wpcloud' ),
Expand Down
2 changes: 1 addition & 1 deletion plugin/includes/class-wpcloud-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ public function set( $args ) {
$options = get_option( 'wpcloud_settings' );

// Only allow changing the API key if it's not set in the environment.
if ( ! wpcloud_get_api_key() ) {
if ( ! wpcloud_get_api_key_from_env() ) {
if ( 'api_key' === $key && isset( $options['wpcloud_api_key'] ) ) {
WP_CLI::confirm( 'Are you sure you want to change the API key?' );
}
Expand Down
7 changes: 6 additions & 1 deletion plugin/includes/wpcloud-client.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ function wpcloud_get_client_name(): mixed {
* @return string|null Client API Key on success. WP_Error on error.
*/
function wpcloud_get_client_api_key(): mixed {
return wpcloud_get_api_key();
$api_key = wpcloud_get_api_key_from_env();
if ( $api_key ) {
return $api_key;
}
$settings = get_option( 'wpcloud_settings', array() );
return $settings['wpcloud_api_key'] ?? null;
}

/**
Expand Down
14 changes: 3 additions & 11 deletions plugin/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,24 +37,16 @@ function (): void {
}

/**
* Get the api key.
* Get the api key from the environment.
*/
function wpcloud_get_api_key(): string {

// Try to find the key in the ENV or config.
function wpcloud_get_api_key_from_env(): string {
$api_key = getenv( 'WP_CLOUD_API_KEY' );
if ( defined( 'WP_CLOUD_API_KEY' ) ) {
$api_key = WP_CLOUD_API_KEY;
}

$api_key = apply_filters( 'wpcloud_api_key', $api_key );

if ( $api_key ) {
return $api_key;
}
// Else check the local options.
$settings = get_option( 'wpcloud_settings', array() );
return $settings['wpcloud_api_key'] ?? '';
return $api_key ? $api_key : '';
}

/**
Expand Down

0 comments on commit 9d995d0

Please sign in to comment.