Skip to content

Commit b82b63a

Browse files
Copilotswissspidy
andcommitted
refactor: replace DB query with get_option() loop; remove mask_api_key()
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
1 parent 5940d57 commit b82b63a

1 file changed

Lines changed: 23 additions & 49 deletions

File tree

src/Credentials_Command.php

Lines changed: 23 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public function list_( $args, $assoc_args ) {
7979
foreach ( $credentials as $provider => $api_key ) {
8080
$items[] = array(
8181
'provider' => $provider,
82-
'api_key' => $this->mask_api_key( $api_key ?? '' ),
82+
'api_key' => $api_key,
8383
);
8484
}
8585

@@ -119,15 +119,17 @@ public function list_( $args, $assoc_args ) {
119119
public function get( $args, $assoc_args ) {
120120
list( $provider ) = $args;
121121

122-
$credentials = $this->get_all_credentials();
122+
$option_name = self::OPTION_PREFIX . $provider . self::OPTION_SUFFIX;
123+
$raw_key = get_option( $option_name, '' );
124+
$api_key = is_string( $raw_key ) ? $raw_key : '';
123125

124-
if ( ! isset( $credentials[ $provider ] ) ) {
126+
if ( '' === $api_key ) {
125127
WP_CLI::error( sprintf( 'Credentials for provider "%s" not found.', $provider ) );
126128
}
127129

128130
$data = array(
129131
'provider' => $provider,
130-
'api_key' => $this->mask_api_key( $credentials[ $provider ] ?? '' ),
132+
'api_key' => $api_key,
131133
);
132134

133135
$format = $assoc_args['format'] ?? 'json';
@@ -201,13 +203,15 @@ public function set( $args, $assoc_args ) {
201203
public function delete( $args, $assoc_args ) {
202204
list( $provider ) = $args;
203205

204-
$credentials = $this->get_all_credentials();
206+
$option_name = self::OPTION_PREFIX . $provider . self::OPTION_SUFFIX;
207+
$raw_key = get_option( $option_name, '' );
208+
$api_key = is_string( $raw_key ) ? $raw_key : '';
205209

206-
if ( ! isset( $credentials[ $provider ] ) ) {
210+
if ( '' === $api_key ) {
207211
WP_CLI::error( sprintf( 'Credentials for provider "%s" not found.', $provider ) );
208212
}
209213

210-
delete_option( self::OPTION_PREFIX . $provider . self::OPTION_SUFFIX );
214+
delete_option( $option_name );
211215

212216
WP_CLI::success( sprintf( 'Credentials for provider "%s" have been deleted.', $provider ) );
213217
}
@@ -218,60 +222,30 @@ public function delete( $args, $assoc_args ) {
218222
* @return array<string, string>
219223
*/
220224
private function get_all_credentials() {
221-
global $wpdb;
222-
223-
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching
224-
$results = $wpdb->get_results(
225-
$wpdb->prepare(
226-
"SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name LIKE %s",
227-
$wpdb->esc_like( self::OPTION_PREFIX ) . '%' . $wpdb->esc_like( self::OPTION_SUFFIX )
228-
),
229-
ARRAY_A
230-
);
231-
232-
if ( ! is_array( $results ) ) {
225+
if ( ! function_exists( '_wp_connectors_get_connector_settings' ) ) {
233226
return array();
234227
}
235228

236-
$credentials = array();
237-
$prefix_length = strlen( self::OPTION_PREFIX );
238-
$suffix_length = strlen( self::OPTION_SUFFIX );
229+
$credentials = array();
239230

240-
foreach ( $results as $row ) {
241-
if ( ! is_array( $row ) ) {
231+
foreach ( _wp_connectors_get_connector_settings() as $connector_id => $connector_data ) {
232+
if ( ! isset( $connector_data['authentication'] ) ) {
242233
continue;
243234
}
244-
/** @var array<string, string> $row */
245-
$option_name = $row['option_name'];
246-
if ( strlen( $option_name ) <= $prefix_length + $suffix_length ) {
235+
$auth = $connector_data['authentication'];
236+
if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
247237
continue;
248238
}
249-
$provider = substr( $option_name, $prefix_length, -$suffix_length );
250-
$credentials[ $provider ] = $row['option_value'];
239+
240+
$raw = get_option( $auth['setting_name'], '' );
241+
$value = is_string( $raw ) ? $raw : '';
242+
if ( '' !== $value ) {
243+
$credentials[ $connector_id ] = $value;
244+
}
251245
}
252246

253247
ksort( $credentials );
254248

255249
return $credentials;
256250
}
257-
258-
/**
259-
* Masks an API key for display purposes.
260-
*
261-
* Uses the same logic as WordPress core's `_wp_connectors_mask_api_key()`.
262-
*
263-
* @param string $api_key The API key to mask.
264-
* @return string
265-
*/
266-
private function mask_api_key( $api_key ) {
267-
if ( function_exists( '_wp_connectors_mask_api_key' ) ) {
268-
return _wp_connectors_mask_api_key( $api_key );
269-
}
270-
271-
if ( strlen( $api_key ) <= 4 ) {
272-
return $api_key;
273-
}
274-
275-
return str_repeat( "\u{2022}", min( strlen( $api_key ) - 4, 16 ) ) . substr( $api_key, -4 );
276-
}
277251
}

0 commit comments

Comments
 (0)