Skip to content

Commit e87d603

Browse files
Apply feedback and pass a correct is_active hook for the ai connectors
1 parent 31eb00b commit e87d603

3 files changed

Lines changed: 29 additions & 9 deletions

File tree

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,9 @@ final class WP_Connector_Registry {
110110
* @type array $plugin {
111111
* Optional. Plugin data for install/activate UI.
112112
*
113-
* @type string $file The plugin's main file path relative to the plugins
114-
* directory (e.g. 'my-plugin/my-plugin.php' or 'hello.php').
113+
* @type string $file Optional. The plugin's main file path relative to the
114+
* plugins directory (e.g. 'my-plugin/my-plugin.php' or
115+
* 'hello.php').
115116
* @type callable $is_active Optional callback to determine whether the plugin
116117
* is active. Receives no arguments and must return bool.
117118
* Defaults to `__return_true`.
@@ -247,8 +248,12 @@ public function register( string $id, array $args ): ?array {
247248
}
248249
}
249250

250-
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) && ! empty( $args['plugin']['file'] ) ) {
251-
$connector['plugin'] = array( 'file' => $args['plugin']['file'] );
251+
$connector['plugin'] = array();
252+
253+
if ( ! empty( $args['plugin'] ) && is_array( $args['plugin'] ) ) {
254+
if ( ! empty( $args['plugin']['file'] ) ) {
255+
$connector['plugin']['file'] = $args['plugin']['file'];
256+
}
252257

253258
if ( isset( $args['plugin']['is_active'] ) ) {
254259
if ( ! is_callable( $args['plugin']['is_active'] ) ) {
@@ -262,11 +267,13 @@ public function register( string $id, array $args ): ?array {
262267
}
263268

264269
$connector['plugin']['is_active'] = $args['plugin']['is_active'];
265-
} else {
266-
$connector['plugin']['is_active'] = '__return_true';
267270
}
268271
}
269272

273+
if ( ! isset( $connector['plugin']['is_active'] ) ) {
274+
$connector['plugin']['is_active'] = '__return_true';
275+
}
276+
270277
$this->registered_connectors[ $id ] = $connector;
271278
return $connector;
272279
}

src/wp-includes/connectors.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,17 @@ function _wp_connectors_register_default_ai_providers( WP_Connector_Registry $re
367367
}
368368
}
369369
}
370+
371+
if ( ! isset( $args['plugin']['is_active'] ) ) {
372+
$args['plugin']['is_active'] = static function () use ( $ai_registry, $id ): bool {
373+
try {
374+
return $ai_registry->hasProvider( $id );
375+
} catch ( Exception $e ) {
376+
return false;
377+
}
378+
};
379+
}
380+
370381
$registry->register( $id, $args );
371382
}
372383
}
@@ -671,7 +682,7 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
671682
if ( ! empty( $connector_data['plugin']['file'] ) ) {
672683
$file = $connector_data['plugin']['file'];
673684
$is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] );
674-
$is_installed = $is_activated || file_exists( WP_PLUGIN_DIR . '/' . $file );
685+
$is_installed = $is_activated || file_exists( wp_normalize_path( WP_PLUGIN_DIR . '/' . $file ) );
675686

676687
$connector_out['plugin'] = array(
677688
'file' => $file,

tests/phpunit/tests/connectors/wpConnectorRegistry.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -353,10 +353,12 @@ public function test_register_defaults_plugin_is_active_to_return_true() {
353353
/**
354354
* @ticket 64791
355355
*/
356-
public function test_register_omits_plugin_when_not_provided() {
356+
public function test_register_defaults_plugin_when_not_provided() {
357357
$result = $this->registry->register( 'no-plugin', self::$default_args );
358358

359-
$this->assertArrayNotHasKey( 'plugin', $result );
359+
$this->assertArrayHasKey( 'plugin', $result );
360+
$this->assertArrayNotHasKey( 'file', $result['plugin'] );
361+
$this->assertSame( '__return_true', $result['plugin']['is_active'] );
360362
}
361363

362364
/**

0 commit comments

Comments
 (0)