Skip to content

Commit be5d975

Browse files
authored
Merge pull request #7 from wp-cli/copilot/add-wordpress-7-0-connectors
2 parents b55e8d9 + aee3013 commit be5d975

2 files changed

Lines changed: 68 additions & 53 deletions

File tree

‎features/credentials.feature‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ Feature: Manage AI provider credentials
3333
When I run `wp ai credentials list --format=json`
3434
Then STDOUT should be JSON containing:
3535
"""
36-
[{"provider":"openai","api_key":"sk-*********6789"}]
36+
[{"provider":"openai","api_key":"••••••••••••6789"}]
3737
"""
3838

3939
@require-wp-7.0
@@ -51,7 +51,7 @@ Feature: Manage AI provider credentials
5151
"""
5252
And STDOUT should contain:
5353
"""
54-
"api_key":"sk-**********-123"
54+
"api_key":"\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022\u2022-123"
5555
"""
5656

5757
@require-wp-7.0
@@ -80,7 +80,7 @@ Feature: Manage AI provider credentials
8080
When I try `wp ai credentials get nonexistent`
8181
Then STDERR should contain:
8282
"""
83-
Error: Credentials for provider "nonexistent" not found.
83+
Error: Provider "nonexistent" is not a supported AI connector.
8484
"""
8585
And the return code should be 1
8686

@@ -100,8 +100,8 @@ Feature: Manage AI provider credentials
100100
And I run `wp ai credentials list`
101101
Then STDOUT should be a table containing rows:
102102
| provider | api_key |
103-
| openai | sk-*****i123 |
104-
| anthropic | sk-*******-456 |
103+
| openai | ••••••••i123 |
104+
| anthropic | ••••••••••-456 |
105105

106106
@require-wp-7.0
107107
Scenario: Update existing credentials
@@ -120,5 +120,5 @@ Feature: Manage AI provider credentials
120120
When I run `wp ai credentials get openai --format=json`
121121
Then STDOUT should contain:
122122
"""
123-
"api_key":"new****-456"
123+
"api_key":"\u2022\u2022\u2022\u2022\u2022\u2022\u2022-456"
124124
"""

‎src/Credentials_Command.php‎

Lines changed: 62 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,6 @@
2424
*/
2525
class Credentials_Command extends WP_CLI_Command {
2626

27-
/**
28-
* The option name where credentials are stored.
29-
*/
30-
const OPTION_NAME = 'wp_ai_client_provider_credentials';
31-
3227
/**
3328
* Lists all stored AI provider credentials.
3429
*
@@ -52,7 +47,7 @@ class Credentials_Command extends WP_CLI_Command {
5247
* +----------+----------+
5348
* | provider | api_key |
5449
* +----------+----------+
55-
* | openai | sk-***** |
50+
* | openai | ••••••• |
5651
* +----------+----------+
5752
*
5853
* @subcommand list
@@ -74,7 +69,7 @@ public function list_( $args, $assoc_args ) {
7469
foreach ( $credentials as $provider => $api_key ) {
7570
$items[] = array(
7671
'provider' => $provider,
77-
'api_key' => $this->mask_api_key( $api_key ?? '' ),
72+
'api_key' => $api_key,
7873
);
7974
}
8075

@@ -103,7 +98,7 @@ public function list_( $args, $assoc_args ) {
10398
*
10499
* # Get OpenAI credentials
105100
* $ wp ai credentials get openai
106-
* {"provider":"openai","api_key":"sk-*****"}
101+
* {"provider":"openai","api_key":"••••••••••••6789"}
107102
*
108103
* @when after_wp_load
109104
*
@@ -114,15 +109,17 @@ public function list_( $args, $assoc_args ) {
114109
public function get( $args, $assoc_args ) {
115110
list( $provider ) = $args;
116111

117-
$credentials = $this->get_all_credentials();
112+
$option_name = $this->get_connector_setting_name( $provider );
113+
$raw_key = get_option( $option_name, '' );
114+
$api_key = is_string( $raw_key ) ? $raw_key : '';
118115

119-
if ( ! isset( $credentials[ $provider ] ) ) {
116+
if ( '' === $api_key ) {
120117
WP_CLI::error( sprintf( 'Credentials for provider "%s" not found.', $provider ) );
121118
}
122119

123120
$data = array(
124121
'provider' => $provider,
125-
'api_key' => $this->mask_api_key( $credentials[ $provider ] ?? '' ),
122+
'api_key' => $api_key,
126123
);
127124

128125
$format = $assoc_args['format'] ?? 'json';
@@ -164,11 +161,11 @@ public function set( $args, $assoc_args ) {
164161
list( $provider ) = $args;
165162

166163
$api_key = $assoc_args['api-key'];
167-
$credentials = $this->get_all_credentials();
168-
169-
$credentials[ $provider ] = $api_key;
164+
$option_name = $this->get_connector_setting_name( $provider );
170165

171-
$this->save_all_credentials( $credentials );
166+
// Remove any sanitize callback to bypass provider-side validation (e.g., live API checks).
167+
remove_all_filters( "sanitize_option_{$option_name}" );
168+
update_option( $option_name, $api_key, false );
172169

173170
WP_CLI::success( sprintf( 'Credentials for provider "%s" have been saved.', $provider ) );
174171
}
@@ -196,69 +193,87 @@ public function set( $args, $assoc_args ) {
196193
public function delete( $args, $assoc_args ) {
197194
list( $provider ) = $args;
198195

199-
$credentials = $this->get_all_credentials();
196+
$option_name = $this->get_connector_setting_name( $provider );
197+
$raw_key = get_option( $option_name, '' );
198+
$api_key = is_string( $raw_key ) ? $raw_key : '';
200199

201-
if ( ! isset( $credentials[ $provider ] ) ) {
200+
if ( '' === $api_key ) {
202201
WP_CLI::error( sprintf( 'Credentials for provider "%s" not found.', $provider ) );
203202
}
204203

205-
unset( $credentials[ $provider ] );
206-
$this->save_all_credentials( $credentials );
204+
delete_option( $option_name );
207205

208206
WP_CLI::success( sprintf( 'Credentials for provider "%s" have been deleted.', $provider ) );
209207
}
210208

211209
/**
212-
* Gets all credentials from the database.
210+
* Gets the option name for a provider's API key from the connector registry.
213211
*
214-
* @return array<string, string>
212+
* @param string $provider The connector/provider ID.
213+
* @return string The option name.
215214
*/
216-
private function get_all_credentials() {
217-
$credentials = get_option( self::OPTION_NAME, array() );
215+
private function get_connector_setting_name( string $provider ): string {
216+
if ( ! function_exists( '_wp_connectors_get_connector_settings' ) ) {
217+
WP_CLI::error( 'Requires WordPress 7.0 or greater.' );
218+
}
218219

219-
if ( ! is_array( $credentials ) ) {
220-
return array();
220+
$settings = _wp_connectors_get_connector_settings();
221+
222+
if ( ! isset( $settings[ $provider ] ) ) {
223+
WP_CLI::error( sprintf( 'Provider "%s" is not a supported AI connector.', $provider ) );
221224
}
222225

223-
/**
224-
* @var array<string, string> $credentials
225-
*/
226+
$setting_name = $this->get_api_key_setting_name( $settings[ $provider ]['authentication'] ?? [] );
226227

227-
return $credentials;
228+
if ( null === $setting_name ) {
229+
WP_CLI::error( sprintf( 'Provider "%s" does not support API key authentication.', $provider ) );
230+
}
231+
232+
return $setting_name;
228233
}
229234

230235
/**
231-
* Saves all credentials to the database.
236+
* Returns the option/setting name if the given authentication config is an API key type, or null otherwise.
232237
*
233-
* @param array<string, string> $credentials The credentials to save.
234-
* @return bool
238+
* @param mixed $auth Authentication config from the connector registry.
239+
* @return string|null
235240
*/
236-
private function save_all_credentials( $credentials ) {
237-
if ( empty( $credentials ) ) {
238-
return delete_option( self::OPTION_NAME );
241+
private function get_api_key_setting_name( $auth ): ?string {
242+
if ( ! is_array( $auth ) || ! isset( $auth['method'] ) || 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
243+
return null;
239244
}
240245

241-
return update_option( self::OPTION_NAME, $credentials, false );
246+
return (string) $auth['setting_name'];
242247
}
243248

244249
/**
245-
* Masks an API key for display purposes.
250+
* Gets all credentials from the database.
246251
*
247-
* @param string $api_key The API key to mask.
248-
* @return string
252+
* @return array<string, string>
249253
*/
250-
private function mask_api_key( $api_key ) {
251-
if ( empty( $api_key ) ) {
252-
return '';
254+
private function get_all_credentials() {
255+
if ( ! function_exists( '_wp_connectors_get_connector_settings' ) ) {
256+
return array();
253257
}
254258

255-
$length = strlen( $api_key );
259+
$credentials = array();
256260

257-
if ( $length <= 8 ) {
258-
return str_repeat( '*', $length );
261+
foreach ( _wp_connectors_get_connector_settings() as $connector_id => $connector_data ) {
262+
$setting_name = $this->get_api_key_setting_name( $connector_data['authentication'] ?? [] );
263+
264+
if ( null === $setting_name ) {
265+
continue;
266+
}
267+
268+
$raw = get_option( $setting_name, '' );
269+
$value = is_string( $raw ) ? $raw : '';
270+
if ( '' !== $value ) {
271+
$credentials[ $connector_id ] = $value;
272+
}
259273
}
260274

261-
// Show first 3 and last 4 characters
262-
return substr( $api_key, 0, 3 ) . str_repeat( '*', min( 10, $length - 7 ) ) . substr( $api_key, -4 );
275+
ksort( $credentials );
276+
277+
return $credentials;
263278
}
264279
}

0 commit comments

Comments
 (0)