Skip to content

Commit 33f9834

Browse files
Connectors: Simplify auto-registration gate using is_active
Collapse the AI/non-AI branches in _wp_register_default_connector_settings() into a single is_active callback check. AI providers already get an is_active callback wired up at registration that delegates to the AI Client registry, so one path covers both cases. Add tests for is_active returning true (setting registered) and false (setting skipped).
1 parent 5bb886d commit 33f9834

2 files changed

Lines changed: 61 additions & 13 deletions

File tree

src/wp-includes/connectors.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -549,10 +549,9 @@ function _wp_connectors_rest_settings_dispatch( WP_REST_Response $response, WP_R
549549
* @access private
550550
*/
551551
function _wp_register_default_connector_settings(): void {
552-
$ai_registry = AiClient::defaultRegistry();
553552
$registered_settings = get_registered_settings();
554553

555-
foreach ( wp_get_connectors() as $connector_id => $connector_data ) {
554+
foreach ( wp_get_connectors() as $connector_data ) {
556555
$auth = $connector_data['authentication'];
557556
if ( 'api_key' !== $auth['method'] || empty( $auth['setting_name'] ) ) {
558557
continue;
@@ -563,19 +562,12 @@ function _wp_register_default_connector_settings(): void {
563562
continue;
564563
}
565564

566-
if ( 'ai_provider' === $connector_data['type'] ) {
567-
// For AI providers, skip if the provider is not in the AI Client registry.
568-
if ( ! $ai_registry->hasProvider( $connector_id ) ) {
565+
if ( ! isset( $connector_data['plugin']['is_active'] ) || ! is_callable( $connector_data['plugin']['is_active'] ) ) {
569566
continue;
570567
}
571-
} else {
572-
if ( ! isset( $connector_data['plugin']['is_active'] ) || ! is_callable( $connector_data['plugin']['is_active'] ) ) {
573-
continue;
574-
}
575568

576-
if ( ! call_user_func( $connector_data['plugin']['is_active'] ) ) {
577-
continue;
578-
}
569+
if ( ! call_user_func( $connector_data['plugin']['is_active'] ) ) {
570+
continue;
579571
}
580572

581573
register_setting(

tests/phpunit/tests/connectors/wpRegisterDefaultConnectorSettings.php

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function tear_down(): void {
4444
}
4545

4646
/**
47-
* @ticket 64730
47+
* @ticket 65099
4848
*/
4949
public function test_non_ai_connector_skipped_when_is_active_missing(): void {
5050
WP_Connector_Registry::get_instance()->register(
@@ -64,4 +64,60 @@ public function test_non_ai_connector_skipped_when_is_active_missing(): void {
6464

6565
$this->assertArrayNotHasKey( self::SETTING_NAME, get_registered_settings() );
6666
}
67+
68+
/**
69+
* @ticket 65099
70+
*/
71+
public function test_non_ai_connector_skipped_when_is_active_returns_false(): void {
72+
WP_Connector_Registry::get_instance()->register(
73+
self::CONNECTOR_ID,
74+
array(
75+
'name' => 'Test Non-AI Connector',
76+
'description' => '',
77+
'type' => 'spam_filtering',
78+
'authentication' => array(
79+
'method' => 'api_key',
80+
'setting_name' => self::SETTING_NAME,
81+
),
82+
'plugin' => array(
83+
'file' => 'test/test.php',
84+
'is_active' => static function (): bool {
85+
return false;
86+
},
87+
),
88+
)
89+
);
90+
91+
_wp_register_default_connector_settings();
92+
93+
$this->assertArrayNotHasKey( self::SETTING_NAME, get_registered_settings() );
94+
}
95+
96+
/**
97+
* @ticket 65099
98+
*/
99+
public function test_non_ai_connector_registers_setting_when_is_active_returns_true(): void {
100+
WP_Connector_Registry::get_instance()->register(
101+
self::CONNECTOR_ID,
102+
array(
103+
'name' => 'Test Non-AI Connector',
104+
'description' => '',
105+
'type' => 'spam_filtering',
106+
'authentication' => array(
107+
'method' => 'api_key',
108+
'setting_name' => self::SETTING_NAME,
109+
),
110+
'plugin' => array(
111+
'file' => 'test/test.php',
112+
'is_active' => static function (): bool {
113+
return true;
114+
},
115+
),
116+
)
117+
);
118+
119+
_wp_register_default_connector_settings();
120+
121+
$this->assertArrayHasKey( self::SETTING_NAME, get_registered_settings() );
122+
}
67123
}

0 commit comments

Comments
 (0)