Skip to content

Commit 734a6b3

Browse files
gzioloclaude
andcommitted
Connectors: Remove per-provider boilerplate in favor of shared callbacks.
Replace six identical per-provider mask/sanitize wrapper functions with `_wp_connectors_mask_api_key` used directly and sanitize closures built from a provider map in `_wp_connectors_get_connectors()`. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 31ab71c commit 734a6b3

1 file changed

Lines changed: 22 additions & 124 deletions

File tree

src/wp-includes/connectors.php

Lines changed: 22 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -147,114 +147,6 @@ function _wp_connectors_get_real_api_key( string $option_name, callable $mask_ca
147147
return (string) $value;
148148
}
149149

150-
/**
151-
* Masks the Gemini API key on read.
152-
*
153-
* @since 7.0.0
154-
* @access private
155-
*
156-
* @param string $value The raw option value.
157-
* @return string Masked key or empty string.
158-
*/
159-
function _wp_connectors_mask_gemini_api_key( string $value ): string {
160-
if ( '' === $value ) {
161-
return $value;
162-
}
163-
164-
return _wp_connectors_mask_api_key( $value );
165-
}
166-
167-
/**
168-
* Sanitizes and validates the Gemini API key before saving.
169-
*
170-
* @since 7.0.0
171-
* @access private
172-
*
173-
* @param string $value The new value.
174-
* @return string The sanitized value, or empty string if the key is not valid.
175-
*/
176-
function _wp_connectors_sanitize_gemini_api_key( string $value ): string {
177-
$value = sanitize_text_field( $value );
178-
if ( '' === $value ) {
179-
return $value;
180-
}
181-
182-
$valid = _wp_connectors_is_api_key_valid( $value, 'google' );
183-
return true === $valid ? $value : '';
184-
}
185-
186-
/**
187-
* Masks the OpenAI API key on read.
188-
*
189-
* @since 7.0.0
190-
* @access private
191-
*
192-
* @param string $value The raw option value.
193-
* @return string Masked key or empty string.
194-
*/
195-
function _wp_connectors_mask_openai_api_key( string $value ): string {
196-
if ( '' === $value ) {
197-
return $value;
198-
}
199-
200-
return _wp_connectors_mask_api_key( $value );
201-
}
202-
203-
/**
204-
* Sanitizes and validates the OpenAI API key before saving.
205-
*
206-
* @since 7.0.0
207-
* @access private
208-
*
209-
* @param string $value The new value.
210-
* @return string The sanitized value, or empty string if the key is not valid.
211-
*/
212-
function _wp_connectors_sanitize_openai_api_key( string $value ): string {
213-
$value = sanitize_text_field( $value );
214-
if ( '' === $value ) {
215-
return $value;
216-
}
217-
218-
$valid = _wp_connectors_is_api_key_valid( $value, 'openai' );
219-
return true === $valid ? $value : '';
220-
}
221-
222-
/**
223-
* Masks the Anthropic API key on read.
224-
*
225-
* @since 7.0.0
226-
* @access private
227-
*
228-
* @param string $value The raw option value.
229-
* @return string Masked key or empty string.
230-
*/
231-
function _wp_connectors_mask_anthropic_api_key( string $value ): string {
232-
if ( '' === $value ) {
233-
return $value;
234-
}
235-
236-
return _wp_connectors_mask_api_key( $value );
237-
}
238-
239-
/**
240-
* Sanitizes and validates the Anthropic API key before saving.
241-
*
242-
* @since 7.0.0
243-
* @access private
244-
*
245-
* @param string $value The new value.
246-
* @return string The sanitized value, or empty string if the key is not valid.
247-
*/
248-
function _wp_connectors_sanitize_anthropic_api_key( string $value ): string {
249-
$value = sanitize_text_field( $value );
250-
if ( '' === $value ) {
251-
return $value;
252-
}
253-
254-
$valid = _wp_connectors_is_api_key_valid( $value, 'anthropic' );
255-
return true === $valid ? $value : '';
256-
}
257-
258150
/**
259151
* Gets the provider connectors.
260152
*
@@ -264,23 +156,29 @@ function _wp_connectors_sanitize_anthropic_api_key( string $value ): string {
264156
* @return array<string, array{provider: string, mask: callable, sanitize: callable}> Connectors.
265157
*/
266158
function _wp_connectors_get_connectors(): array {
267-
return array(
268-
'connectors_gemini_api_key' => array(
269-
'provider' => 'google',
270-
'mask' => '_wp_connectors_mask_gemini_api_key',
271-
'sanitize' => '_wp_connectors_sanitize_gemini_api_key',
272-
),
273-
'connectors_openai_api_key' => array(
274-
'provider' => 'openai',
275-
'mask' => '_wp_connectors_mask_openai_api_key',
276-
'sanitize' => '_wp_connectors_sanitize_openai_api_key',
277-
),
278-
'connectors_anthropic_api_key' => array(
279-
'provider' => 'anthropic',
280-
'mask' => '_wp_connectors_mask_anthropic_api_key',
281-
'sanitize' => '_wp_connectors_sanitize_anthropic_api_key',
282-
),
159+
$providers = array(
160+
'google' => 'connectors_gemini_api_key',
161+
'openai' => 'connectors_openai_api_key',
162+
'anthropic' => 'connectors_anthropic_api_key',
283163
);
164+
165+
$connectors = array();
166+
foreach ( $providers as $provider => $option_name ) {
167+
$connectors[ $option_name ] = array(
168+
'provider' => $provider,
169+
'mask' => '_wp_connectors_mask_api_key',
170+
'sanitize' => static function ( string $value ) use ( $provider ): string {
171+
$value = sanitize_text_field( $value );
172+
if ( '' === $value ) {
173+
return $value;
174+
}
175+
176+
$valid = _wp_connectors_is_api_key_valid( $value, $provider );
177+
return true === $valid ? $value : '';
178+
},
179+
);
180+
}
181+
return $connectors;
284182
}
285183

286184
/**

0 commit comments

Comments
 (0)