Skip to content

Connectors: Make the API more flexible for custom connector types#11378

Open
gziolo wants to merge 17 commits intoWordPress:trunkfrom
gziolo:add/connector-setting-name-validation
Open

Connectors: Make the API more flexible for custom connector types#11378
gziolo wants to merge 17 commits intoWordPress:trunkfrom
gziolo:add/connector-setting-name-validation

Conversation

@gziolo
Copy link
Copy Markdown
Member

@gziolo gziolo commented Mar 27, 2026

Summary

  • Validate setting_name, constant_name, and env_var_name in connector registration — reject invalid values with _doing_it_wrong() instead of silently falling back.
  • Update PHPDoc to reflect current behavior — widen type from literal 'ai_provider' to non-empty-string, document new authentication fields, use Anthropic and non-AI examples throughout.
  • Change auto-generated setting_name pattern from connectors_ai_{$id}_api_key to connectors_{$type}_{$id}_api_key so it works for any connector type. Built-in AI providers infer their names using the existing connectors_ai_{$id}_api_key convention.
  • Add constant_name and env_var_name as optional authentication fields, allowing connectors to declare explicit PHP constant and environment variable names for API key lookup. AI providers auto-generate these using the {CONSTANT_CASE_ID}_API_KEY convention.
  • Refactor _wp_connectors_get_api_key_source() to accept explicit env_var_name and constant_name parameters instead of deriving them from the provider ID. Environment variable and constant checks are skipped when not provided.
  • Generalize REST dispatch, settings registration, and script module data to work with all connector types, not just ai_provider. Settings registration skips already-registered settings. Non-AI connectors determine isConnected based on key source.

Test plan

  • Verify existing AI provider connectors retain their connectors_ai_{id}_api_key setting names
  • Register a custom non-AI connector type and verify auto-generated setting name uses connectors_{type}_{id}_api_key
  • Verify constant_name and env_var_name are stored when provided and omitted when not
  • Verify invalid setting_name, constant_name, and env_var_name values trigger _doing_it_wrong()
  • Verify API key masking works for non-AI connector types in REST responses
  • Verify settings registration skips already-registered settings
  • Run full connector test suite: npm run test:php -- --filter=Tests_Connectors

Trac ticket: https://core.trac.wordpress.org/ticket/64957
See also: WordPress/gutenberg#76828

🤖 Generated with Claude Code

gziolo and others added 6 commits March 27, 2026 14:49
When `setting_name` is explicitly provided during connector registration,
validate that it is a non-empty string. This prevents silent fallback to
an auto-generated name when an invalid value is passed, which could mask
bugs. Follows the existing validation pattern used for `name`, `type`,
and `authentication.method`.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Widen `type` from literal `'ai_provider'` to `non-empty-string` in
  phpstan types, matching the actual validation which accepts any
  non-empty string.
- Update `@type` descriptions from "Currently, only 'ai_provider' is
  supported" to "e.g. 'ai_provider'".
- Document `setting_name` in the `register()` `@param` authentication
  block, noting it is optional and must be a non-empty string when
  provided.
- Update `register()` method description to mention explicit
  `setting_name` support.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Change the auto-generated `setting_name` pattern from
`connectors_ai_{$id}_api_key` to `connectors_{$type}_{$id}_api_key`
so the pattern works for any connector type, not just AI providers.

All AI providers registered through
`_wp_connectors_register_default_ai_providers()` infer their
`setting_name` as `connectors_ai_{$id}_api_key` before registration,
preserving backward compatibility.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Allow connectors to declare `constant_name` and `env_var_name` in
their authentication configuration. These optional fields specify
the PHP constant and environment variable to check for an API key.

Both fields are validated as non-empty strings when provided and
are omitted from the stored connector data when not set. AI
providers registered through the default registration infer both
values using the `{CONSTANT_CASE_ID}_API_KEY` convention.

Props jorgefilipecosta.
See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…cit names.

Change the function signature from `( $provider_id, $setting_name )`
to `( $setting_name, $env_var_name, $constant_name )` so it uses the
stored `env_var_name` and `constant_name` from the connector's
authentication data instead of deriving them from the provider ID.

Environment variable and constant checks are now skipped when their
respective name parameters are empty, making the function work
correctly for connectors that don't declare these fields.

Props jorgefilipecosta.
See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…ll types.

Remove `ai_provider` type guards from `_wp_connectors_rest_settings_dispatch()`,
`_wp_register_default_connector_settings()`, and
`_wp_connectors_get_connector_script_module_data()` so non-AI connector
types with `api_key` authentication work end-to-end.

- REST dispatch: masks keys for all connector types; AI key validation
  only runs for `ai_provider` type.
- Settings registration: registers settings for all `api_key` connectors;
  skips if the setting is already registered by an owning plugin; AI
  Client registry check only applies to `ai_provider` type.
- Script module data: for `ai_provider`, uses AI Client registry's
  `isProviderConfigured()`; for non-AI types, considers connected if
  key source is not `none`.

Props jorgefilipecosta.
See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
gziolo and others added 3 commits March 27, 2026 16:25
Use a non-AI example (`spam_filtering` + `akismet`) since built-in
AI providers provide an explicit `setting_name` rather than relying
on the auto-generated pattern.

Follow-up to [62116].
See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Concatenate type and ID before sanitizing instead of sanitizing
each part separately.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@gziolo gziolo force-pushed the add/connector-setting-name-validation branch from 7352695 to 570bb02 Compare March 27, 2026 15:29
gziolo and others added 3 commits March 27, 2026 16:32
Move the naming convention comment next to the relevant code and
compute the constant case key once instead of duplicating the
preg_replace call.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
… PHPDoc.

The function is now generic, so use a non-AI connector example
to better reflect its purpose.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Change the default test args from `ai_provider` to `test_type`
so registry tests exercise the generic path. The AI-specific test
(`test_register_skips_when_ai_not_supported`) explicitly sets
`ai_provider` where needed.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@github-actions
Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props gziolo.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions
Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

gziolo and others added 5 commits March 27, 2026 16:49
Ensure putenv and delete_option calls run before assertions so
cleanup happens even if the assertion fails.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…hod.

The `method` key is always present in the defaults array, so the
`?? ''` fallback is not needed.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Use string interpolation with str_replace wrapping the full
string, matching the Gutenberg implementation.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
- Mention constant_name and env_var_name in the authentication
  description for wp_get_connector() and wp_get_connectors().
- Use Anthropic in the wp_connectors_init action example.

See #64957.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant