Skip to content

Commit

Permalink
feat: add support for API key configuration via PHP constant
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaucau committed Jul 9, 2024
1 parent 1fe65ad commit 96ed5b0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
37 changes: 25 additions & 12 deletions includes/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ public function register_settings(): void {
[
'type' => 'array',
'sanitize_callback' => function ( $input ) {
$input['api_key'] = isset( $input['api_key'] ) ? sanitize_text_field( $input['api_key'] ) : null;
if ( ! defined( 'ACPL_ALT_GENERATOR_OPENAI_API_KEY' ) ) {
$input['api_key'] = isset( $input['api_key'] ) ? sanitize_text_field( $input['api_key'] ) : null;
}
$input['auto_generate'] = isset( $input['auto_generate'] ) && $input['auto_generate'];
$input['detail'] = isset( $input['detail'] ) ? sanitize_text_field( $input['detail'] ) : 'low';

Expand Down Expand Up @@ -56,27 +58,38 @@ function () {
__( 'OpenAI API Key', 'alt-text-generator-gpt-vision' ),
function () use ( $options ) {
printf(
'<input type="password" id="openai_api_key" name="%1$s[api_key]" value="%2$s" class="regular-text" placeholder="sk-..." autocomplete="off"/>',
'<input type="password" id="openai_api_key" name="%1$s[api_key]" value="%2$s" class="regular-text" placeholder="sk-..." autocomplete="off" %3$s/>',
esc_attr( AltGeneratorPlugin::OPTION_NAME ),
esc_attr( $options['api_key'] ?? '' )
esc_attr( defined( 'ACPL_ALT_GENERATOR_OPENAI_API_KEY' ) ? '' : ( $options['api_key'] ?? '' ) ),
disabled( defined( 'ACPL_ALT_GENERATOR_OPENAI_API_KEY' ), true, false )
);

if ( defined( 'ACPL_ALT_GENERATOR_OPENAI_API_KEY' ) ) {
$description = __(
'The API key is currently set using the <code>ACPL_ALT_GENERATOR_OPENAI_API_KEY</code> constant in PHP. This field will remain disabled until the constant is removed.',
'alt-text-generator-gpt-vision'
);
} else {
$description = sprintf(
// translators: %s is for link attributes.
__(
'Enter your OpenAI API key here. You can find it in your <a href="https://platform.openai.com/api-keys" %s>OpenAI account settings</a>.',
'alt-text-generator-gpt-vision'
),
'target="_blank" rel="noopener noreferrer"'
);
}

echo '<p class="description">' .
wp_kses(
sprintf(
// translators: %s is for link attributes.
__(
'Enter your OpenAI API key here. You can find it in your <a href="https://platform.openai.com/api-keys" %s>OpenAI account settings</a>.',
'alt-text-generator-gpt-vision'
),
'target="_blank" rel="noopener noreferrer"'
),
$description,
[
'a' => [
'a' => [
'href' => [],
'target' => [],
'rel' => [],
],
'code' => [],
]
)
. '</p>';
Expand Down
7 changes: 6 additions & 1 deletion includes/AltGeneratorPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,12 @@ public function __construct() {
}

public static function get_options(): array|false {
return get_option( self::OPTION_NAME );
$options = get_option( self::OPTION_NAME );
if ( defined( 'ACPL_ALT_GENERATOR_OPENAI_API_KEY' ) ) {
$options['api_key'] = ACPL_ALT_GENERATOR_OPENAI_API_KEY;
}

return $options;
}

public static function error_log( WP_Error $error ): WP_Error {
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ By using this plugin, you agree to OpenAI's terms and acknowledge that you have
2. Activate the plugin through the 'Plugins' menu in WordPress.
3. Go to `Settings -> Media` to configure the plugin settings.
4. Enter your OpenAI API key (you can find it in your [OpenAI account settings](https://platform.openai.com/account/api-keys)).
5. Alternatively, you can set your API key by defining the `ACPL_ALT_GENERATOR_OPENAI_API_KEY` constant in your wp-config.php file: `define('ACPL_ALT_GENERATOR_OPENAI_API_KEY', 'your-api-key-here');`. When the constant is defined, the API key field in the plugin settings will be disabled.

== Frequently Asked Questions ==

Expand Down

0 comments on commit 96ed5b0

Please sign in to comment.