|
10 | 10 | use WordPress\AiClient\AiClient; |
11 | 11 | use WordPress\AiClient\Providers\Http\DTO\ApiKeyRequestAuthentication; |
12 | 12 |
|
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 | | - |
83 | 13 | /** |
84 | 14 | * Checks if a connector is registered. |
85 | 15 | * |
@@ -244,7 +174,24 @@ function _wp_connectors_init(): void { |
244 | 174 | * Fires when the connector registry is ready for plugins to register connectors. |
245 | 175 | * |
246 | 176 | * 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 | + * } ); |
248 | 195 | * |
249 | 196 | * @since 7.0.0 |
250 | 197 | * |
|
0 commit comments