Skip to content

Commit 0ad2774

Browse files
Connectors: Replace plugin.slug with plugin.file in connector registration.
Use the plugin's main file path relative to the plugins directory (e.g. `akismet/akismet.php` or `hello.php`) instead of the WordPress.org slug to identify a connector's associated plugin. This lets `_wp_connectors_get_connector_script_module_data()` check plugin status with `file_exists()` and `is_plugin_active()` directly, removing the `get_plugins()` slug-to-file mapping that was previously needed. Props jorgefilipecosta, mukesh27, gziolo. Fixes #65002. git-svn-id: https://develop.svn.wordpress.org/branches/7.0@62195 602fd350-edb4-49c9-b593-d223f7449a82
1 parent af57e89 commit 0ad2774

3 files changed

Lines changed: 23 additions & 30 deletions

File tree

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* env_var_name?: non-empty-string
4141
* },
4242
* plugin?: array{
43-
* slug: non-empty-string
43+
* file: non-empty-string
4444
* }
4545
* }
4646
*/
@@ -109,7 +109,8 @@ final class WP_Connector_Registry {
109109
* @type array $plugin {
110110
* Optional. Plugin data for install/activate UI.
111111
*
112-
* @type string $slug The WordPress.org plugin slug.
112+
* @type string $file The plugin's main file path relative to the plugins
113+
* directory (e.g. 'akismet/akismet.php' or 'hello.php').
113114
* }
114115
* }
115116
* @return array|null The registered connector data on success, null on failure.
@@ -242,8 +243,8 @@ public function register( string $id, array $args ): ?array {
242243
}
243244
}
244245

245-
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) {
246-
$connector['plugin'] = $args['plugin'];
246+
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
247+
$connector['plugin'] = array( 'file' => $args['plugin']['file'] );
247248
}
248249

249250
$this->registered_connectors[ $id ] = $connector;

src/wp-includes/connectors.php

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ function wp_is_connector_registered( string $id ): bool {
5858
* @type array $plugin {
5959
* Optional. Plugin data for install/activate UI.
6060
*
61-
* @type string $slug The WordPress.org plugin slug.
61+
* @type string $file The plugin's main file path relative to the plugins
62+
* directory (e.g. 'akismet/akismet.php' or 'hello.php').
6263
* }
6364
* }
6465
* @phpstan-return ?array{
@@ -74,7 +75,7 @@ function wp_is_connector_registered( string $id ): bool {
7475
* env_var_name?: non-empty-string
7576
* },
7677
* plugin?: array{
77-
* slug: non-empty-string
78+
* file: non-empty-string
7879
* }
7980
* }
8081
*/
@@ -118,7 +119,8 @@ function wp_get_connector( string $id ): ?array {
118119
* @type array $plugin {
119120
* Optional. Plugin data for install/activate UI.
120121
*
121-
* @type string $slug The WordPress.org plugin slug.
122+
* @type string $file The plugin's main file path relative to the plugins
123+
* directory (e.g. 'akismet/akismet.php' or 'hello.php').
122124
* }
123125
* }
124126
* }
@@ -135,7 +137,7 @@ function wp_get_connector( string $id ): ?array {
135137
* env_var_name?: non-empty-string
136138
* },
137139
* plugin?: array{
138-
* slug: non-empty-string
140+
* file: non-empty-string
139141
* }
140142
* }>
141143
*/
@@ -256,7 +258,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
256258
'description' => __( 'Text generation with Claude.' ),
257259
'type' => 'ai_provider',
258260
'plugin' => array(
259-
'slug' => 'ai-provider-for-anthropic',
261+
'file' => 'ai-provider-for-anthropic/plugin.php',
260262
),
261263
'authentication' => array(
262264
'method' => 'api_key',
@@ -268,7 +270,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
268270
'description' => __( 'Text and image generation with Gemini and Imagen.' ),
269271
'type' => 'ai_provider',
270272
'plugin' => array(
271-
'slug' => 'ai-provider-for-google',
273+
'file' => 'ai-provider-for-google/plugin.php',
272274
),
273275
'authentication' => array(
274276
'method' => 'api_key',
@@ -280,7 +282,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
280282
'description' => __( 'Text and image generation with GPT and Dall-E.' ),
281283
'type' => 'ai_provider',
282284
'plugin' => array(
283-
'slug' => 'ai-provider-for-openai',
285+
'file' => 'ai-provider-for-openai/plugin.php',
284286
),
285287
'authentication' => array(
286288
'method' => 'api_key',
@@ -636,15 +638,9 @@ function _wp_connectors_pass_default_keys_to_ai_client(): void {
636638
function _wp_connectors_get_connector_script_module_data( array $data ): array {
637639
$registry = AiClient::defaultRegistry();
638640

639-
// Build a slug-to-file map for plugin installation status.
640-
if ( ! function_exists( 'get_plugins' ) ) {
641+
if ( ! function_exists( 'is_plugin_active' ) ) {
641642
require_once ABSPATH . 'wp-admin/includes/plugin.php';
642643
}
643-
$plugin_files_by_slug = array();
644-
foreach ( array_keys( get_plugins() ) as $plugin_file ) {
645-
$slug = str_contains( $plugin_file, '/' ) ? dirname( $plugin_file ) : str_replace( '.php', '', $plugin_file );
646-
$plugin_files_by_slug[ $slug ] = $plugin_file;
647-
}
648644

649645
$connectors = array();
650646
foreach ( wp_get_connectors() as $connector_id => $connector_data ) {
@@ -676,18 +672,14 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
676672
'authentication' => $auth_out,
677673
);
678674

679-
if ( ! empty( $connector_data['plugin']['slug'] ) ) {
680-
$plugin_slug = $connector_data['plugin']['slug'];
681-
$plugin_file = $plugin_files_by_slug[ $plugin_slug ] ?? null;
682-
683-
$is_installed = null !== $plugin_file;
684-
$is_activated = $is_installed && is_plugin_active( $plugin_file );
675+
if ( ! empty( $connector_data['plugin']['file'] ) ) {
676+
$file = $connector_data['plugin']['file'];
677+
$is_installed = file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) );
678+
$is_activated = $is_installed && is_plugin_active( $file );
685679

686680
$connector_out['plugin'] = array(
687-
'slug' => $plugin_slug,
688-
'pluginFile' => $is_installed
689-
? ( str_ends_with( $plugin_file, '.php' ) ? substr( $plugin_file, 0, -4 ) : $plugin_file )
690-
: null,
681+
'file' => $file,
682+
'isInstalled' => $is_installed,
691683
'isActivated' => $is_activated,
692684
);
693685
}

tests/phpunit/tests/connectors/wpConnectorRegistry.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,12 @@ public function test_register_omits_logo_url_when_empty() {
294294
*/
295295
public function test_register_includes_plugin_data() {
296296
$args = self::$default_args;
297-
$args['plugin'] = array( 'slug' => 'my-plugin' );
297+
$args['plugin'] = array( 'file' => 'my-plugin/my-plugin.php' );
298298

299299
$result = $this->registry->register( 'with-plugin', $args );
300300

301301
$this->assertArrayHasKey( 'plugin', $result );
302-
$this->assertSame( array( 'slug' => 'my-plugin' ), $result['plugin'] );
302+
$this->assertSame( array( 'file' => 'my-plugin/my-plugin.php' ), $result['plugin'] );
303303
}
304304

305305
/**

0 commit comments

Comments
 (0)