2424 */
2525class 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