Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions src/wp-includes/class-wp-connector-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* env_var_name?: non-empty-string
* },
* plugin?: array{
* slug: non-empty-string
* file: non-empty-string
* }
* }
*/
Expand Down Expand Up @@ -109,7 +109,8 @@ final class WP_Connector_Registry {
* @type array $plugin {
* Optional. Plugin data for install/activate UI.
*
* @type string $slug The WordPress.org plugin slug.
* @type string $file The plugin's main file path relative to the plugins
* directory (e.g. 'akismet/akismet.php' or 'hello.php').
* }
* }
* @return array|null The registered connector data on success, null on failure.
Expand Down Expand Up @@ -242,8 +243,8 @@ public function register( string $id, array $args ): ?array {
}
}

if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) {
$connector['plugin'] = $args['plugin'];
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
$connector['plugin'] = array( 'file' => $args['plugin']['file'] );
}

$this->registered_connectors[ $id ] = $connector;
Expand Down
40 changes: 16 additions & 24 deletions src/wp-includes/connectors.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ function wp_is_connector_registered( string $id ): bool {
* @type array $plugin {
* Optional. Plugin data for install/activate UI.
*
* @type string $slug The WordPress.org plugin slug.
* @type string $file The plugin's main file path relative to the plugins
* directory (e.g. 'akismet/akismet.php' or 'hello.php').
* }
* }
* @phpstan-return ?array{
Expand All @@ -74,7 +75,7 @@ function wp_is_connector_registered( string $id ): bool {
* env_var_name?: non-empty-string
* },
* plugin?: array{
* slug: non-empty-string
* file: non-empty-string
* }
* }
*/
Expand Down Expand Up @@ -118,7 +119,8 @@ function wp_get_connector( string $id ): ?array {
* @type array $plugin {
* Optional. Plugin data for install/activate UI.
*
* @type string $slug The WordPress.org plugin slug.
* @type string $file The plugin's main file path relative to the plugins
* directory (e.g. 'akismet/akismet.php').
* }
* }
* }
Expand All @@ -135,7 +137,7 @@ function wp_get_connector( string $id ): ?array {
* env_var_name?: non-empty-string
* },
* plugin?: array{
* slug: non-empty-string
* file: non-empty-string
* }
* }>
*/
Expand Down Expand Up @@ -256,7 +258,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
'description' => __( 'Text generation with Claude.' ),
'type' => 'ai_provider',
'plugin' => array(
'slug' => 'ai-provider-for-anthropic',
'file' => 'ai-provider-for-anthropic/plugin.php',
),
'authentication' => array(
'method' => 'api_key',
Expand All @@ -268,7 +270,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
'description' => __( 'Text and image generation with Gemini and Imagen.' ),
'type' => 'ai_provider',
'plugin' => array(
'slug' => 'ai-provider-for-google',
'file' => 'ai-provider-for-google/plugin.php',
),
'authentication' => array(
'method' => 'api_key',
Expand All @@ -280,7 +282,7 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
'description' => __( 'Text and image generation with GPT and Dall-E.' ),
'type' => 'ai_provider',
'plugin' => array(
'slug' => 'ai-provider-for-openai',
'file' => 'ai-provider-for-openai/plugin.php',
),
'authentication' => array(
'method' => 'api_key',
Expand Down Expand Up @@ -636,15 +638,9 @@ function _wp_connectors_pass_default_keys_to_ai_client(): void {
function _wp_connectors_get_connector_script_module_data( array $data ): array {
$registry = AiClient::defaultRegistry();

// Build a slug-to-file map for plugin installation status.
if ( ! function_exists( 'get_plugins' ) ) {
if ( ! function_exists( 'is_plugin_active' ) ) {
require_once ABSPATH . 'wp-admin/includes/plugin.php';
}
$plugin_files_by_slug = array();
foreach ( array_keys( get_plugins() ) as $plugin_file ) {
$slug = str_contains( $plugin_file, '/' ) ? dirname( $plugin_file ) : str_replace( '.php', '', $plugin_file );
$plugin_files_by_slug[ $slug ] = $plugin_file;
}

$connectors = array();
foreach ( wp_get_connectors() as $connector_id => $connector_data ) {
Expand Down Expand Up @@ -676,18 +672,14 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
'authentication' => $auth_out,
);

if ( ! empty( $connector_data['plugin']['slug'] ) ) {
$plugin_slug = $connector_data['plugin']['slug'];
$plugin_file = $plugin_files_by_slug[ $plugin_slug ] ?? null;

$is_installed = null !== $plugin_file;
$is_activated = $is_installed && is_plugin_active( $plugin_file );
if ( ! empty( $connector_data['plugin']['file'] ) ) {
$file = $connector_data['plugin']['file'];
$is_installed = file_exists( WP_PLUGIN_DIR . '/' . $file );
$is_activated = $is_installed && is_plugin_active( $file );

$connector_out['plugin'] = array(
'slug' => $plugin_slug,
'pluginFile' => $is_installed
? ( str_ends_with( $plugin_file, '.php' ) ? substr( $plugin_file, 0, -4 ) : $plugin_file )
: null,
'file' => $file,
'isInstalled' => $is_installed,
'isActivated' => $is_activated,
);
}
Expand Down
4 changes: 2 additions & 2 deletions tests/phpunit/tests/connectors/wpConnectorRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,12 +294,12 @@ public function test_register_omits_logo_url_when_empty() {
*/
public function test_register_includes_plugin_data() {
$args = self::$default_args;
$args['plugin'] = array( 'slug' => 'my-plugin' );
$args['plugin'] = array( 'file' => 'my-plugin/my-plugin.php' );

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

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

/**
Expand Down
Loading