Skip to content

Commit a4192de

Browse files
gzioloclaude
andcommitted
Connectors: Remove wp_register_connector() wrapper in favor of direct registry usage.
Plugins now use $registry->register() directly in the wp_connectors_init action callback, which structurally enforces correct registration timing without needing a doing_action() guard. Co-Authored-By: Claude Opus 4.6 <[email protected]>
1 parent 64dcc37 commit a4192de

3 files changed

Lines changed: 19 additions & 140 deletions

File tree

src/wp-includes/class-wp-connector-registry.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,12 +40,8 @@ final class WP_Connector_Registry {
4040
/**
4141
* Registers a new connector.
4242
*
43-
* Do not use this method directly. Instead, use the `wp_register_connector()` function.
44-
*
4543
* @since 7.0.0
4644
*
47-
* @see wp_register_connector()
48-
*
4945
* @param string $id The unique connector identifier. Must contain only lowercase
5046
* alphanumeric characters and underscores.
5147
* @param array $args {

src/wp-includes/connectors.php

Lines changed: 18 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -10,76 +10,6 @@
1010
use WordPress\AiClient\AiClient;
1111
use WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication;
1212

13-
/**
14-
* Registers a new connector.
15-
*
16-
* Must be called during the `wp_connectors_init` action.
17-
*
18-
* Example:
19-
*
20-
* function my_plugin_register_connectors(): void {
21-
* wp_register_connector(
22-
* 'my_custom_ai',
23-
* array(
24-
* 'name' => __( 'My Custom AI', 'my-plugin' ),
25-
* 'description' => __( 'Custom AI provider integration.', 'my-plugin' ),
26-
* 'type' => 'ai_provider',
27-
* 'authentication' => array(
28-
* 'method' => 'api_key',
29-
* 'credentials_url' => 'https://example.com/api-keys',
30-
* ),
31-
* )
32-
* );
33-
* }
34-
* add_action( 'wp_connectors_init', 'my_plugin_register_connectors' );
35-
*
36-
* @since 7.0.0
37-
*
38-
* @see WP_Connector_Registry::register()
39-
*
40-
* @param string $id The unique connector identifier. Must contain only lowercase
41-
* alphanumeric characters and underscores.
42-
* @param array $args {
43-
* An associative array of arguments for the connector.
44-
*
45-
* @type string $name Required. The connector's display name.
46-
* @type string $description Optional. The connector's description. Default empty string.
47-
* @type string $type Required. The connector type. Currently, only 'ai_provider' is supported.
48-
* @type array $authentication {
49-
* Required. Authentication configuration.
50-
*
51-
* @type string $method Required. The authentication method: 'api_key' or 'none'.
52-
* @type string|null $credentials_url Optional. URL where users can obtain API credentials.
53-
* }
54-
* @type array $plugin Optional. Plugin data for install/activate UI.
55-
* @type string $slug The WordPress.org plugin slug.
56-
* }
57-
* }
58-
* @return array|null The registered connector data on success, null on failure.
59-
*/
60-
function wp_register_connector( string $id, array $args ): ?array {
61-
if ( ! doing_action( 'wp_connectors_init' ) ) {
62-
_doing_it_wrong(
63-
__FUNCTION__,
64-
sprintf(
65-
/* translators: 1: wp_connectors_init, 2: string value of the connector ID. */
66-
__( 'Connectors must be registered on the %1$s action. The connector %2$s was not registered.' ),
67-
'<code>wp_connectors_init</code>',
68-
'<code>' . esc_html( $id ) . '</code>'
69-
),
70-
'7.0.0'
71-
);
72-
return null;
73-
}
74-
75-
$registry = WP_Connector_Registry::get_instance();
76-
if ( null === $registry ) {
77-
return null;
78-
}
79-
80-
return $registry->register( $id, $args );
81-
}
82-
8313
/**
8414
* Checks if a connector is registered.
8515
*
@@ -244,7 +174,24 @@ function _wp_connectors_init(): void {
244174
* Fires when the connector registry is ready for plugins to register connectors.
245175
*
246176
* Default connectors have already been registered at this point and cannot be
247-
* unhooked. Use `wp_register_connector()` within this action to add new connectors.
177+
* unhooked. Use `$registry->register()` within this action to add new connectors.
178+
*
179+
* Example usage:
180+
*
181+
* add_action( 'wp_connectors_init', function ( WP_Connector_Registry $registry ) {
182+
* $registry->register(
183+
* 'my_custom_ai',
184+
* array(
185+
* 'name' => __( 'My Custom AI', 'my-plugin' ),
186+
* 'description' => __( 'Custom AI provider integration.', 'my-plugin' ),
187+
* 'type' => 'ai_provider',
188+
* 'authentication' => array(
189+
* 'method' => 'api_key',
190+
* 'credentials_url' => 'https://example.com/api-keys',
191+
* ),
192+
* )
193+
* );
194+
* } );
248195
*
249196
* @since 7.0.0
250197
*

tests/phpunit/tests/connectors/wpRegisterConnector.php

Lines changed: 1 addition & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,14 @@
11
<?php
22
/**
3-
* Tests for wp_register_connector() and companion functions.
3+
* Tests for connector registration companion functions.
44
*
55
* @group connectors
6-
* @covers ::wp_register_connector
76
* @covers ::wp_is_connector_registered
87
* @covers ::wp_get_connector
98
* @covers ::wp_get_connectors
109
*/
1110
class Tests_Connectors_WpRegisterConnector extends WP_UnitTestCase {
1211

13-
/**
14-
* Default valid connector args for testing.
15-
*
16-
* @var array
17-
*/
18-
private static $default_args = array();
19-
20-
/**
21-
* Set up before class.
22-
*/
23-
public static function set_up_before_class() {
24-
parent::set_up_before_class();
25-
26-
self::$default_args = array(
27-
'name' => 'Test Connector',
28-
'description' => 'A test connector.',
29-
'type' => 'ai_provider',
30-
'authentication' => array(
31-
'method' => 'api_key',
32-
'credentials_url' => 'https://example.com/keys',
33-
),
34-
);
35-
}
36-
37-
/**
38-
* Helper to simulate the wp_connectors_init action for registration.
39-
*
40-
* @param callable $callback The registration callback to run.
41-
*/
42-
private function simulate_doing_wp_connectors_init_action( callable $callback ): void {
43-
global $wp_current_filter;
44-
$wp_current_filter[] = 'wp_connectors_init';
45-
$callback();
46-
array_pop( $wp_current_filter );
47-
}
48-
49-
/**
50-
* @ticket 64791
51-
*/
52-
public function test_register_fails_outside_action() {
53-
$this->setExpectedIncorrectUsage( 'wp_register_connector' );
54-
55-
$result = wp_register_connector( 'outside_action', self::$default_args );
56-
57-
$this->assertNull( $result );
58-
}
59-
60-
/**
61-
* @ticket 64791
62-
*/
63-
public function test_register_succeeds_during_action() {
64-
$result = null;
65-
66-
$this->simulate_doing_wp_connectors_init_action(
67-
function () use ( &$result ) {
68-
$result = wp_register_connector( 'during_action', self::$default_args );
69-
}
70-
);
71-
72-
$this->assertIsArray( $result );
73-
$this->assertSame( 'Test Connector', $result['name'] );
74-
}
75-
7612
/**
7713
* @ticket 64791
7814
*/

0 commit comments

Comments
 (0)