forked from WordPress/wordpress-develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnectors.php
More file actions
369 lines (330 loc) · 9.33 KB
/
connectors.php
File metadata and controls
369 lines (330 loc) · 9.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<?php
/**
* Connectors API.
*
* @package WordPress
* @subpackage Connectors
* @since 7.0.0
*/
use WordPress\AiClient\AiClient;
use WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication;
/**
* Registers the Connectors menu item under Settings.
*
* @since 7.0.0
* @access private
*/
function _wp_connectors_add_settings_menu_item(): void {
if ( ! class_exists( '\WordPress\AiClient\AiClient' ) || ! function_exists( 'wp_connectors_wp_admin_render_page' ) ) {
return;
}
add_submenu_page(
'options-general.php',
__( 'Connectors' ),
__( 'Connectors' ),
'manage_options',
'connectors-wp-admin',
'wp_connectors_wp_admin_render_page',
1
);
}
add_action( 'admin_menu', '_wp_connectors_add_settings_menu_item' );
/**
* Masks an API key, showing only the last 4 characters.
*
* @since 7.0.0
* @access private
*
* @param string $key The API key to mask.
* @return string The masked key, e.g. "************fj39".
*/
function _wp_connectors_mask_api_key( string $key ): string {
if ( strlen( $key ) <= 4 ) {
return $key;
}
return str_repeat( "\u{2022}", min( strlen( $key ) - 4, 16 ) ) . substr( $key, -4 );
}
/**
* Checks whether an API key is valid for a given provider.
*
* @since 7.0.0
* @access private
*
* @param string $key The API key to check.
* @param string $provider_id The WP AI client provider ID.
* @return bool|null True if valid, false if invalid, null if unable to determine.
*/
function _wp_connectors_is_api_key_valid( string $key, string $provider_id ): ?bool {
try {
$registry = AiClient::defaultRegistry();
if ( ! $registry->hasProvider( $provider_id ) ) {
return null;
}
$registry->setProviderRequestAuthentication(
$provider_id,
new ApiKeyRequestAuthentication( $key )
);
return $registry->isProviderConfigured( $provider_id );
} catch ( \Error $e ) {
return null;
}
}
/**
* Sets API key authentication for a provider in the WP AI Client registry.
*
* @since 7.0.0
* @access private
*
* @param string $key The API key.
* @param string $provider_id The WP AI client provider ID.
*/
function _wp_connectors_set_provider_api_key( string $key, string $provider_id ): void {
try {
$registry = AiClient::defaultRegistry();
if ( ! $registry->hasProvider( $provider_id ) ) {
return;
}
$registry->setProviderRequestAuthentication(
$provider_id,
new ApiKeyRequestAuthentication( $key )
);
} catch ( \Error $e ) {
// WP AI Client not available.
}
}
/**
* Retrieves the real (unmasked) value of a connector API key.
*
* Temporarily removes the masking filter, reads the option, then re-adds it.
*
* @since 7.0.0
* @access private
*
* @param string $option_name The option name for the API key.
* @param callable $mask_callback The mask filter function.
* @return string The real API key value.
*/
function _wp_connectors_get_real_api_key( string $option_name, callable $mask_callback ): string {
remove_filter( "option_{$option_name}", $mask_callback );
$value = get_option( $option_name, '' );
add_filter( "option_{$option_name}", $mask_callback );
return (string) $value;
}
/**
* Masks the Gemini API key on read.
*
* @since 7.0.0
* @access private
*
* @param string $value The raw option value.
* @return string Masked key or empty string.
*/
function _wp_connectors_mask_gemini_api_key( string $value ): string {
if ( '' === $value ) {
return $value;
}
return _wp_connectors_mask_api_key( $value );
}
/**
* Sanitizes and validates the Gemini API key before saving.
*
* @since 7.0.0
* @access private
*
* @param string $value The new value.
* @return string The sanitized value, or empty string if the key is not valid.
*/
function _wp_connectors_sanitize_gemini_api_key( string $value ): string {
$value = sanitize_text_field( $value );
if ( '' === $value ) {
return $value;
}
$valid = _wp_connectors_is_api_key_valid( $value, 'google' );
return true === $valid ? $value : '';
}
/**
* Masks the OpenAI API key on read.
*
* @since 7.0.0
* @access private
*
* @param string $value The raw option value.
* @return string Masked key or empty string.
*/
function _wp_connectors_mask_openai_api_key( string $value ): string {
if ( '' === $value ) {
return $value;
}
return _wp_connectors_mask_api_key( $value );
}
/**
* Sanitizes and validates the OpenAI API key before saving.
*
* @since 7.0.0
* @access private
*
* @param string $value The new value.
* @return string The sanitized value, or empty string if the key is not valid.
*/
function _wp_connectors_sanitize_openai_api_key( string $value ): string {
$value = sanitize_text_field( $value );
if ( '' === $value ) {
return $value;
}
$valid = _wp_connectors_is_api_key_valid( $value, 'openai' );
return true === $valid ? $value : '';
}
/**
* Masks the Anthropic API key on read.
*
* @since 7.0.0
* @access private
*
* @param string $value The raw option value.
* @return string Masked key or empty string.
*/
function _wp_connectors_mask_anthropic_api_key( string $value ): string {
if ( '' === $value ) {
return $value;
}
return _wp_connectors_mask_api_key( $value );
}
/**
* Sanitizes and validates the Anthropic API key before saving.
*
* @since 7.0.0
* @access private
*
* @param string $value The new value.
* @return string The sanitized value, or empty string if the key is not valid.
*/
function _wp_connectors_sanitize_anthropic_api_key( string $value ): string {
$value = sanitize_text_field( $value );
if ( '' === $value ) {
return $value;
}
$valid = _wp_connectors_is_api_key_valid( $value, 'anthropic' );
return true === $valid ? $value : '';
}
/**
* Gets the provider connectors.
*
* @since 7.0.0
* @access private
*
* @return array<string, array{provider: string, mask: callable, sanitize: callable}> Connectors.
*/
function _wp_connectors_get_connectors(): array {
return array(
'connectors_gemini_api_key' => array(
'provider' => 'google',
'mask' => '_wp_connectors_mask_gemini_api_key',
'sanitize' => '_wp_connectors_sanitize_gemini_api_key',
),
'connectors_openai_api_key' => array(
'provider' => 'openai',
'mask' => '_wp_connectors_mask_openai_api_key',
'sanitize' => '_wp_connectors_sanitize_openai_api_key',
),
'connectors_anthropic_api_key' => array(
'provider' => 'anthropic',
'mask' => '_wp_connectors_mask_anthropic_api_key',
'sanitize' => '_wp_connectors_sanitize_anthropic_api_key',
),
);
}
/**
* Validates connector API keys in the REST response when explicitly requested.
*
* Runs on `rest_post_dispatch` for `/wp/v2/settings` requests that include connector
* fields via `_fields`. For each requested connector field, it validates the unmasked
* key against the provider and replaces the response value with `invalid_key` if
* validation fails.
*
* @since 7.0.0
* @access private
*
* @param WP_REST_Response $response The response object.
* @param WP_REST_Server $server The server instance.
* @param WP_REST_Request $request The request object.
* @return WP_REST_Response The potentially modified response.
*/
function _wp_connectors_validate_keys_in_rest( WP_REST_Response $response, WP_REST_Server $server, WP_REST_Request $request ): WP_REST_Response {
if ( '/wp/v2/settings' !== $request->get_route() ) {
return $response;
}
if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) {
return $response;
}
$fields = $request->get_param( '_fields' );
if ( ! $fields ) {
return $response;
}
if ( is_array( $fields ) ) {
$requested = $fields;
} else {
$requested = array_map( 'trim', explode( ',', $fields ) );
}
$data = $response->get_data();
if ( ! is_array( $data ) ) {
return $response;
}
foreach ( _wp_connectors_get_connectors() as $option_name => $config ) {
if ( ! in_array( $option_name, $requested, true ) ) {
continue;
}
$real_key = _wp_connectors_get_real_api_key( $option_name, $config['mask'] );
if ( '' === $real_key ) {
continue;
}
if ( true !== _wp_connectors_is_api_key_valid( $real_key, $config['provider'] ) ) {
$data[ $option_name ] = 'invalid_key';
}
}
$response->set_data( $data );
return $response;
}
add_filter( 'rest_post_dispatch', '_wp_connectors_validate_keys_in_rest', 10, 3 );
/**
* Registers default connector settings and mask/sanitize filters.
*
* @since 7.0.0
* @access private
*/
function _wp_register_default_connector_settings(): void {
if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) {
return;
}
foreach ( _wp_connectors_get_connectors() as $option_name => $config ) {
register_setting(
'connectors',
$option_name,
array(
'type' => 'string',
'default' => '',
'show_in_rest' => true,
'sanitize_callback' => $config['sanitize'],
)
);
add_filter( "option_{$option_name}", $config['mask'] );
}
}
add_action( 'init', '_wp_register_default_connector_settings' );
/**
* Passes stored connector API keys to the WP AI client.
*
* @since 7.0.0
* @access private
*/
function _wp_connectors_pass_default_keys_to_ai_client(): void {
if ( ! class_exists( '\WordPress\AiClient\AiClient' ) ) {
return;
}
foreach ( _wp_connectors_get_connectors() as $option_name => $config ) {
$api_key = _wp_connectors_get_real_api_key( $option_name, $config['mask'] );
if ( '' !== $api_key ) {
_wp_connectors_set_provider_api_key( $api_key, $config['provider'] );
}
}
}
add_action( 'init', '_wp_connectors_pass_default_keys_to_ai_client' );