Skip to content

Commit 598847d

Browse files
committed
Connector Registry: Add default is_active callback and enhance tests for plugin registration
1 parent f67a6cc commit 598847d

3 files changed

Lines changed: 53 additions & 16 deletions

File tree

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ final class WP_Connector_Registry {
114114
* directory (e.g. 'my-plugin/my-plugin.php' or 'hello.php').
115115
* @type callable $is_active Optional callback to determine whether the plugin
116116
* is active. Receives no arguments and must return bool.
117+
* Defaults to `__return_true`.
117118
* }
118119
* }
119120
* @return array|null The registered connector data on success, null on failure.
@@ -261,6 +262,8 @@ public function register( string $id, array $args ): ?array {
261262
}
262263

263264
$connector['plugin']['is_active'] = $args['plugin']['is_active'];
265+
} else {
266+
$connector['plugin']['is_active'] = '__return_true';
264267
}
265268
}
266269

src/wp-includes/connectors.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -638,10 +638,6 @@ function _wp_connectors_pass_default_keys_to_ai_client(): void {
638638
function _wp_connectors_get_connector_script_module_data( array $data ): array {
639639
$registry = AiClient::defaultRegistry();
640640

641-
if ( ! function_exists( 'is_plugin_active' ) ) {
642-
require_once ABSPATH . 'wp-admin/includes/plugin.php';
643-
}
644-
645641
$connectors = array();
646642
foreach ( wp_get_connectors() as $connector_id => $connector_data ) {
647643
$auth = $connector_data['authentication'];
@@ -674,17 +670,7 @@ function _wp_connectors_get_connector_script_module_data( array $data ): array {
674670

675671
if ( ! empty( $connector_data['plugin']['file'] ) ) {
676672
$file = $connector_data['plugin']['file'];
677-
$is_installed = false;
678-
$is_activated = false;
679-
680-
if ( ! empty( $connector_data['plugin']['is_active'] ) && is_callable( $connector_data['plugin']['is_active'] ) ) {
681-
$is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] );
682-
}
683-
684-
if ( ! $is_activated ) {
685-
$is_activated = is_plugin_active( $file );
686-
}
687-
673+
$is_activated = (bool) call_user_func( $connector_data['plugin']['is_active'] );
688674
$is_installed = $is_activated || file_exists( WP_PLUGIN_DIR . '/' . $file );
689675

690676
$connector_out['plugin'] = array(

tests/phpunit/tests/connectors/wpConnectorRegistry.php

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,55 @@ public function test_register_includes_plugin_data() {
299299
$result = $this->registry->register( 'with-plugin', $args );
300300

301301
$this->assertArrayHasKey( 'plugin', $result );
302-
$this->assertSame( array( 'file' => 'my-plugin/my-plugin.php' ), $result['plugin'] );
302+
$this->assertSame( 'my-plugin/my-plugin.php', $result['plugin']['file'] );
303+
}
304+
305+
/**
306+
* @ticket 65020
307+
*/
308+
public function test_register_stores_plugin_is_active_callback() {
309+
$args = self::$default_args;
310+
$args['plugin'] = array(
311+
'file' => 'my-plugin/my-plugin.php',
312+
'is_active' => '__return_true',
313+
);
314+
315+
$result = $this->registry->register( 'with-callback', $args );
316+
317+
$this->assertIsArray( $result );
318+
$this->assertArrayHasKey( 'is_active', $result['plugin'] );
319+
$this->assertIsCallable( $result['plugin']['is_active'] );
320+
}
321+
322+
/**
323+
* @ticket 65020
324+
*/
325+
public function test_register_rejects_non_callable_plugin_is_active() {
326+
$this->setExpectedIncorrectUsage( 'WP_Connector_Registry::register' );
327+
328+
$args = self::$default_args;
329+
$args['plugin'] = array(
330+
'file' => 'my-plugin/my-plugin.php',
331+
'is_active' => 'not_a_real_function_name',
332+
);
333+
334+
$result = $this->registry->register( 'bad-callback', $args );
335+
336+
$this->assertNull( $result );
337+
}
338+
339+
/**
340+
* @ticket 65020
341+
*/
342+
public function test_register_defaults_plugin_is_active_to_return_true() {
343+
$args = self::$default_args;
344+
$args['plugin'] = array( 'file' => 'my-plugin/my-plugin.php' );
345+
346+
$result = $this->registry->register( 'default-callback', $args );
347+
348+
$this->assertIsArray( $result );
349+
$this->assertArrayHasKey( 'is_active', $result['plugin'] );
350+
$this->assertSame( '__return_true', $result['plugin']['is_active'] );
303351
}
304352

305353
/**

0 commit comments

Comments
 (0)