Skip to content

Commit 0186161

Browse files
gzioloclaude
andcommitted
Connectors: Add plugin data, rename validation function, and update provider names.
- Add plugin sub-object with WordPress.org slugs to featured connector definitions for install/activate UI support. - Expose plugin data in script module output for the frontend. - Rename _wp_connectors_is_api_key_valid to _wp_connectors_is_ai_api_key_valid to reflect AI-provider scope. - Update provider names and descriptions to match Gutenberg PR #76014. - Add type check for 'ai_provider' in REST validation function. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 3c8dad3 commit 0186161

3 files changed

Lines changed: 37 additions & 19 deletions

File tree

src/wp-includes/connectors.php

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ function _wp_connectors_mask_api_key( string $key ): string {
6060
* @param string $provider_id The WP AI client provider ID.
6161
* @return bool|null True if valid, false if invalid, null if unable to determine.
6262
*/
63-
function _wp_connectors_is_api_key_valid( string $key, string $provider_id ): ?bool {
63+
function _wp_connectors_is_ai_api_key_valid( string $key, string $provider_id ): ?bool {
6464
try {
6565
$registry = AiClient::defaultRegistry();
6666

@@ -122,7 +122,10 @@ function _wp_connectors_get_real_api_key( string $option_name, callable $mask_ca
122122
*
123123
* @type string $name The connector's display name.
124124
* @type string $description The connector's description.
125-
* @type string $type The connector type: 'ai_provider'.
125+
* @type string $type The connector type. Currently, only 'ai_provider' is supported.
126+
* @type array $plugin Optional. Plugin data for install/activate UI.
127+
* @type string $slug The WordPress.org plugin slug.
128+
* }
126129
* @type array $authentication {
127130
* Authentication configuration. When method is 'api_key', includes
128131
* credentials_url and setting_name. When 'none', only method is present.
@@ -137,27 +140,36 @@ function _wp_connectors_get_real_api_key( string $option_name, callable $mask_ca
137140
function _wp_connectors_get_connector_settings(): array {
138141
$connectors = array(
139142
'google' => array(
140-
'name' => 'Gemini',
141-
'description' => __( 'Content generation, translation, and vision with Google\'s Gemini.' ),
143+
'name' => 'Google',
144+
'description' => __( 'Text and image generation with Gemini and Imagen.' ),
142145
'type' => 'ai_provider',
146+
'plugin' => array(
147+
'slug' => 'ai-provider-for-google',
148+
),
143149
'authentication' => array(
144150
'method' => 'api_key',
145151
'credentials_url' => 'https://aistudio.google.com/api-keys',
146152
),
147153
),
148154
'openai' => array(
149155
'name' => 'OpenAI',
150-
'description' => __( 'Text, image, and code generation with GPT and DALL-E.' ),
156+
'description' => __( 'Text and image generation with GPT and Dall-E.' ),
151157
'type' => 'ai_provider',
158+
'plugin' => array(
159+
'slug' => 'ai-provider-for-openai',
160+
),
152161
'authentication' => array(
153162
'method' => 'api_key',
154163
'credentials_url' => 'https://platform.openai.com/api-keys',
155164
),
156165
),
157166
'anthropic' => array(
158-
'name' => 'Claude',
159-
'description' => __( 'Writing, research, and analysis with Claude.' ),
167+
'name' => 'Anthropic',
168+
'description' => __( 'Text generation with Claude.' ),
160169
'type' => 'ai_provider',
170+
'plugin' => array(
171+
'slug' => 'ai-provider-for-anthropic',
172+
),
161173
'authentication' => array(
162174
'method' => 'api_key',
163175
'credentials_url' => 'https://platform.claude.com/settings/keys',
@@ -259,7 +271,7 @@ function _wp_connectors_validate_keys_in_rest( WP_REST_Response $response, WP_RE
259271

260272
foreach ( _wp_connectors_get_connector_settings() as $connector_id => $connector_data ) {
261273
$auth = $connector_data['authentication'];
262-
if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
274+
if ( 'ai_provider' !== $connector_data['type'] ||'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
263275
continue;
264276
}
265277

@@ -273,7 +285,7 @@ function _wp_connectors_validate_keys_in_rest( WP_REST_Response $response, WP_RE
273285
continue;
274286
}
275287

276-
if ( true !== _wp_connectors_is_api_key_valid( $real_key, $connector_id ) ) {
288+
if ( true !== _wp_connectors_is_ai_api_key_valid( $real_key, $connector_id ) ) {
277289
$data[ $setting_name ] = 'invalid_key';
278290
}
279291
}
@@ -320,7 +332,7 @@ function _wp_register_default_connector_settings(): void {
320332
return $value;
321333
}
322334

323-
$valid = _wp_connectors_is_api_key_valid( $value, $connector_id );
335+
$valid = _wp_connectors_is_ai_api_key_valid( $value, $connector_id );
324336
return true === $valid ? $value : '';
325337
},
326338
)
@@ -385,12 +397,18 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
385397
$auth_out['credentialsUrl'] = $auth['credentials_url'] ?? null;
386398
}
387399

388-
$connectors[ $connector_id ] = array(
400+
$connector_out = array(
389401
'name' => $connector_data['name'],
390402
'description' => $connector_data['description'],
391403
'type' => $connector_data['type'],
392404
'authentication' => $auth_out,
393405
);
406+
407+
if ( ! empty( $connector_data['plugin'] ) ) {
408+
$connector_out['plugin'] = $connector_data['plugin'];
409+
}
410+
411+
$connectors[ $connector_id ] = $connector_out;
394412
}
395413
$data['connectors'] = $connectors;
396414
return $data;

tests/phpunit/tests/connectors/wpConnectorsGetProviderSettings.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function test_api_key_connectors_have_setting_name_and_credentials_url()
8888
public function test_featured_provider_names_match_expected() {
8989
$connectors = _wp_connectors_get_connector_settings();
9090

91-
$this->assertSame( 'Gemini', $connectors['google']['name'] );
91+
$this->assertSame( 'Google', $connectors['google']['name'] );
9292
$this->assertSame( 'OpenAI', $connectors['openai']['name'] );
93-
$this->assertSame( 'Claude', $connectors['anthropic']['name'] );
93+
$this->assertSame( 'Anthropic', $connectors['anthropic']['name'] );
9494
}
9595

9696
/**

tests/phpunit/tests/connectors/wpConnectorsIsApiKeyValid.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
require_once dirname( __DIR__, 2 ) . '/includes/wp-ai-client-mock-provider-trait.php';
44

55
/**
6-
* Tests for _wp_connectors_is_api_key_valid().
6+
* Tests for _wp_connectors_is_ai_api_key_valid().
77
*
88
* @group connectors
9-
* @covers ::_wp_connectors_is_api_key_valid
9+
* @covers ::_wp_connectors_is_ai_api_key_valid
1010
*/
1111
class Tests_Connectors_WpConnectorsIsApiKeyValid extends WP_UnitTestCase {
1212

@@ -34,9 +34,9 @@ public function set_up() {
3434
* @ticket 64730
3535
*/
3636
public function test_unregistered_provider_returns_null() {
37-
$this->setExpectedIncorrectUsage( '_wp_connectors_is_api_key_valid' );
37+
$this->setExpectedIncorrectUsage( '_wp_connectors_is_ai_api_key_valid' );
3838

39-
$result = _wp_connectors_is_api_key_valid( 'test-key', 'nonexistent_provider' );
39+
$result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'nonexistent_provider' );
4040

4141
$this->assertNull( $result );
4242
}
@@ -49,7 +49,7 @@ public function test_unregistered_provider_returns_null() {
4949
public function test_configured_provider_returns_true() {
5050
self::set_mock_provider_configured( true );
5151

52-
$result = _wp_connectors_is_api_key_valid( 'test-key', 'mock_connectors_test' );
52+
$result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock_connectors_test' );
5353

5454
$this->assertTrue( $result );
5555
}
@@ -62,7 +62,7 @@ public function test_configured_provider_returns_true() {
6262
public function test_unconfigured_provider_returns_false() {
6363
self::set_mock_provider_configured( false );
6464

65-
$result = _wp_connectors_is_api_key_valid( 'test-key', 'mock_connectors_test' );
65+
$result = _wp_connectors_is_ai_api_key_valid( 'test-key', 'mock_connectors_test' );
6666

6767
$this->assertFalse( $result );
6868
}

0 commit comments

Comments
 (0)