Skip to content

Commit 2b6427b

Browse files
gzioloclaude
andcommitted
Connectors: Improve wp_connectors_settings filter PHPDoc and test cleanup.
Use structured PHPDoc for the `$connectors` param in `apply_filters()` to mirror the function's return documentation. In tests, use closure references for filter removal before assertions to prevent leaking to other tests. Follow-up to [555bb8a]. See #64791. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 2a7e622 commit 2b6427b

File tree

2 files changed

+84
-85
lines changed

2 files changed

+84
-85
lines changed

src/wp-includes/connectors.php

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,27 @@ function _wp_connectors_get_connector_settings(): array {
212212
*
213213
* @since 7.0.0
214214
*
215-
* @param array $connectors Connector settings keyed by connector ID.
216-
* Each entry is an array with keys: name, description,
217-
* type, authentication (with method, and optionally
218-
* credentials_url), and optionally plugin.
215+
* @param array $connectors {
216+
* Connector settings keyed by connector ID.
217+
*
218+
* @type array ...$0 {
219+
* Data for a single connector.
220+
*
221+
* @type string $name The connector's display name.
222+
* @type string $description The connector's description.
223+
* @type string $type The connector type. Currently, only 'ai_provider' is supported.
224+
* @type array $plugin Optional. Plugin data for install/activate UI.
225+
* @type string $slug The WordPress.org plugin slug.
226+
* }
227+
* @type array $authentication {
228+
* Authentication configuration. When method is 'api_key', includes
229+
* credentials_url. When 'none', only method is present.
230+
*
231+
* @type string $method The authentication method: 'api_key' or 'none'.
232+
* @type string|null $credentials_url Optional. URL where users can obtain API credentials.
233+
* }
234+
* }
235+
* }
219236
*/
220237
$connectors = apply_filters( 'wp_connectors_settings', $connectors );
221238

tests/phpunit/tests/connectors/wpConnectorsGetConnectorSettings.php

Lines changed: 63 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -117,158 +117,140 @@ public function test_includes_registered_provider_from_registry() {
117117
}
118118

119119
/**
120-
* @ticket 64730
120+
* @ticket 64791
121121
*/
122122
public function test_filter_can_add_new_connector() {
123-
add_filter(
124-
'wp_connectors_settings',
125-
static function ( $connectors ) {
126-
$connectors['my_email_service'] = array(
127-
'name' => 'My Email Service',
128-
'description' => 'Send transactional emails.',
129-
'type' => 'email_service',
130-
'authentication' => array( 'method' => 'none' ),
131-
);
132-
return $connectors;
133-
}
134-
);
123+
$callback = static function ( $connectors ) {
124+
$connectors['my_email_service'] = array(
125+
'name' => 'My Email Service',
126+
'description' => 'Send transactional emails.',
127+
'type' => 'email_service',
128+
'authentication' => array( 'method' => 'none' ),
129+
);
130+
return $connectors;
131+
};
132+
add_filter( 'wp_connectors_settings', $callback );
135133

136134
$connectors = _wp_connectors_get_connector_settings();
135+
remove_filter( 'wp_connectors_settings', $callback );
137136

138137
$this->assertArrayHasKey( 'my_email_service', $connectors );
139138
$this->assertSame( 'My Email Service', $connectors['my_email_service']['name'] );
140139
$this->assertSame( 'email_service', $connectors['my_email_service']['type'] );
141140
$this->assertSame( 'none', $connectors['my_email_service']['authentication']['method'] );
142-
143-
remove_all_filters( 'wp_connectors_settings' );
144141
}
145142

146143
/**
147-
* @ticket 64730
144+
* @ticket 64791
148145
*/
149146
public function test_filter_can_modify_existing_connector() {
150-
add_filter(
151-
'wp_connectors_settings',
152-
static function ( $connectors ) {
153-
$connectors['google']['description'] = 'Custom description for Google.';
154-
return $connectors;
155-
}
156-
);
147+
$callback = static function ( $connectors ) {
148+
$connectors['google']['description'] = 'Custom description for Google.';
149+
return $connectors;
150+
};
151+
add_filter( 'wp_connectors_settings', $callback );
157152

158153
$connectors = _wp_connectors_get_connector_settings();
154+
remove_filter( 'wp_connectors_settings', $callback );
159155

160156
$this->assertSame( 'Custom description for Google.', $connectors['google']['description'] );
161-
162-
remove_all_filters( 'wp_connectors_settings' );
163157
}
164158

165159
/**
166-
* @ticket 64730
160+
* @ticket 64791
167161
*/
168162
public function test_filter_can_remove_connector() {
169-
add_filter(
170-
'wp_connectors_settings',
171-
static function ( $connectors ) {
172-
unset( $connectors['openai'] );
173-
return $connectors;
174-
}
175-
);
163+
$callback = static function ( $connectors ) {
164+
unset( $connectors['openai'] );
165+
return $connectors;
166+
};
167+
add_filter( 'wp_connectors_settings', $callback );
176168

177169
$connectors = _wp_connectors_get_connector_settings();
170+
remove_filter( 'wp_connectors_settings', $callback );
178171

179172
$this->assertArrayNotHasKey( 'openai', $connectors );
180173
// Other connectors remain.
181174
$this->assertArrayHasKey( 'google', $connectors );
182175
$this->assertArrayHasKey( 'anthropic', $connectors );
183-
184-
remove_all_filters( 'wp_connectors_settings' );
185176
}
186177

187178
/**
188-
* @ticket 64730
179+
* @ticket 64791
189180
*/
190181
public function test_filter_added_api_key_connector_gets_setting_name() {
191-
add_filter(
192-
'wp_connectors_settings',
193-
static function ( $connectors ) {
194-
$connectors['custom_ai'] = array(
195-
'name' => 'Custom AI',
196-
'description' => 'A custom AI provider.',
197-
'type' => 'ai_provider',
198-
'authentication' => array(
199-
'method' => 'api_key',
200-
'credentials_url' => 'https://example.com/keys',
201-
),
202-
);
203-
return $connectors;
204-
}
205-
);
182+
$callback = static function ( $connectors ) {
183+
$connectors['custom_ai'] = array(
184+
'name' => 'Custom AI',
185+
'description' => 'A custom AI provider.',
186+
'type' => 'ai_provider',
187+
'authentication' => array(
188+
'method' => 'api_key',
189+
'credentials_url' => 'https://example.com/keys',
190+
),
191+
);
192+
return $connectors;
193+
};
194+
add_filter( 'wp_connectors_settings', $callback );
206195

207196
$connectors = _wp_connectors_get_connector_settings();
197+
remove_filter( 'wp_connectors_settings', $callback );
208198

209199
$this->assertArrayHasKey( 'custom_ai', $connectors );
210200
$this->assertSame(
211201
'connectors_ai_custom_ai_api_key',
212202
$connectors['custom_ai']['authentication']['setting_name'],
213203
'Connectors added via the filter with api_key auth should receive a setting_name automatically.'
214204
);
215-
216-
remove_all_filters( 'wp_connectors_settings' );
217205
}
218206

219207
/**
220-
* @ticket 64730
208+
* @ticket 64791
221209
*/
222210
public function test_filter_added_non_ai_api_key_connector_does_not_get_setting_name() {
223-
add_filter(
224-
'wp_connectors_settings',
225-
static function ( $connectors ) {
226-
$connectors['my_crm'] = array(
227-
'name' => 'My CRM',
228-
'description' => 'CRM integration.',
229-
'type' => 'crm',
230-
'authentication' => array(
231-
'method' => 'api_key',
232-
'credentials_url' => 'https://example.com/crm-keys',
233-
),
234-
);
235-
return $connectors;
236-
}
237-
);
211+
$callback = static function ( $connectors ) {
212+
$connectors['my_crm'] = array(
213+
'name' => 'My CRM',
214+
'description' => 'CRM integration.',
215+
'type' => 'crm',
216+
'authentication' => array(
217+
'method' => 'api_key',
218+
'credentials_url' => 'https://example.com/crm-keys',
219+
),
220+
);
221+
return $connectors;
222+
};
223+
add_filter( 'wp_connectors_settings', $callback );
238224

239225
$connectors = _wp_connectors_get_connector_settings();
226+
remove_filter( 'wp_connectors_settings', $callback );
240227

241228
$this->assertArrayHasKey( 'my_crm', $connectors );
242229
$this->assertArrayNotHasKey(
243230
'setting_name',
244231
$connectors['my_crm']['authentication'],
245232
'Non-AI connectors should not receive an auto-generated setting_name.'
246233
);
247-
248-
remove_all_filters( 'wp_connectors_settings' );
249234
}
250235

251236
/**
252-
* @ticket 64730
237+
* @ticket 64791
253238
*/
254239
public function test_filter_receives_all_default_connectors() {
255240
$received = null;
256241

257-
add_filter(
258-
'wp_connectors_settings',
259-
static function ( $connectors ) use ( &$received ) {
260-
$received = $connectors;
261-
return $connectors;
262-
}
263-
);
242+
$callback = static function ( $connectors ) use ( &$received ) {
243+
$received = $connectors;
244+
return $connectors;
245+
};
246+
add_filter( 'wp_connectors_settings', $callback );
264247

265248
_wp_connectors_get_connector_settings();
249+
remove_filter( 'wp_connectors_settings', $callback );
266250

267251
$this->assertArrayHasKey( 'google', $received );
268252
$this->assertArrayHasKey( 'openai', $received );
269253
$this->assertArrayHasKey( 'anthropic', $received );
270254
$this->assertArrayHasKey( 'mock_connectors_test', $received );
271-
272-
remove_all_filters( 'wp_connectors_settings' );
273255
}
274256
}

0 commit comments

Comments
 (0)