diff --git a/src/wp-includes/connectors.php b/src/wp-includes/connectors.php index 9e7db2cef8f68..b5f2354ddd93a 100644 --- a/src/wp-includes/connectors.php +++ b/src/wp-includes/connectors.php @@ -549,10 +549,9 @@ function _wp_connectors_rest_settings_dispatch( WP_REST_Response $response, WP_R * @access private */ function _wp_register_default_connector_settings(): void { - $ai_registry = AiClient::defaultRegistry(); $registered_settings = get_registered_settings(); - foreach ( wp_get_connectors() as $connector_id => $connector_data ) { + foreach ( wp_get_connectors() as $connector_data ) { $auth = $connector_data['authentication']; if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) { continue; @@ -563,8 +562,11 @@ function _wp_register_default_connector_settings(): void { continue; } - // For AI providers, skip if the provider is not in the AI Client registry. - if ( 'ai_provider' === $connector_data['type'] && ! $ai_registry->hasProvider( $connector_id ) ) { + if ( ! isset( $connector_data['plugin']['is_active'] ) || ! is_callable( $connector_data['plugin']['is_active'] ) ) { + continue; + } + + if ( ! call_user_func( $connector_data['plugin']['is_active'] ) ) { continue; } diff --git a/tests/phpunit/tests/connectors/wpRegisterDefaultConnectorSettings.php b/tests/phpunit/tests/connectors/wpRegisterDefaultConnectorSettings.php new file mode 100644 index 0000000000000..ba16fed864aed --- /dev/null +++ b/tests/phpunit/tests/connectors/wpRegisterDefaultConnectorSettings.php @@ -0,0 +1,101 @@ +original_registered_settings = $wp_registered_settings; + } + + /** + * Removes the test connector and restores registered settings. + */ + public function tear_down(): void { + $registry = WP_Connector_Registry::get_instance(); + if ( null !== $registry && $registry->is_registered( self::CONNECTOR_ID ) ) { + $registry->unregister( self::CONNECTOR_ID ); + } + + global $wp_registered_settings; + $wp_registered_settings = $this->original_registered_settings; + + parent::tear_down(); + } + + /** + * @ticket 65099 + */ + public function test_non_ai_connector_skipped_when_is_active_returns_false(): void { + WP_Connector_Registry::get_instance()->register( + self::CONNECTOR_ID, + array( + 'name' => 'Test Non-AI Connector', + 'description' => '', + 'type' => 'spam_filtering', + 'authentication' => array( + 'method' => 'api_key', + 'setting_name' => self::SETTING_NAME, + ), + 'plugin' => array( + 'file' => 'test/test.php', + 'is_active' => static function (): bool { + return false; + }, + ), + ) + ); + + _wp_register_default_connector_settings(); + + $this->assertArrayNotHasKey( self::SETTING_NAME, get_registered_settings() ); + } + + /** + * @ticket 65099 + */ + public function test_non_ai_connector_registers_setting_when_is_active_returns_true(): void { + WP_Connector_Registry::get_instance()->register( + self::CONNECTOR_ID, + array( + 'name' => 'Test Non-AI Connector', + 'description' => '', + 'type' => 'spam_filtering', + 'authentication' => array( + 'method' => 'api_key', + 'setting_name' => self::SETTING_NAME, + ), + 'plugin' => array( + 'file' => 'test/test.php', + 'is_active' => static function (): bool { + return true; + }, + ), + ) + ); + + _wp_register_default_connector_settings(); + + $this->assertArrayHasKey( self::SETTING_NAME, get_registered_settings() ); + } +}