Connectors: Sanitize provider IDs with hyphens in _wp_connectors_init().#11260
Connectors: Sanitize provider IDs with hyphens in _wp_connectors_init().#11260soderlind wants to merge 1 commit intoWordPress:trunkfrom
Conversation
Passes AI Client provider IDs through str_replace( '-', '_', $provider_id ) before registering them in WP_Connector_Registry, which only accepts [a-z0-9_]. The original provider ID is stored in a new 'provider_id' field so downstream functions can use it for AiClient lookups. Props PerS. Fixes #64861.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Patch for #64861:
_wp_connectors_init()should sanitize provider IDsProblem
_wp_connectors_init()passes AI Client provider IDs directly toWP_Connector_Registry::register(), which validates IDs against/^[a-z0-9_]+$/. Third-party providers using hyphens in their ID (e.g.azure-openai) are silently rejected with a_doing_it_wrongnotice, causing them to be missing from the connector registry even though they work correctly at the AiClient level.Approach
Option 1 from the ticket: sanitize hyphens → underscores before passing to the registry, while preserving the original provider ID for AiClient lookups.
Changes
class-wp-connector-registry.php
register()now accepts and stores an optionalprovider_idfield in connector data, allowing callers to preserve the original AI Client provider ID when it differs from the sanitized connector ID.@phpstan-type Connectorand$argsdocblock accordingly.connectors.php
_wp_connectors_init(): the loop variable fromgetRegisteredProviderIds()is now$provider_id. A sanitized$connector_id = str_replace( '-', '_', $provider_id )is used as the registry key.getProviderClassName()continues to receive the original$provider_idsince the AiClient registry stores providers under their original key. Both existing defaults and new providers store'provider_id' => $provider_idin the connector data.$provider_id = $connector_data['provider_id'] ?? $connector_idand use it for all AiClient calls:_wp_connectors_rest_settings_dispatch()— key validation via_wp_connectors_is_ai_api_key_valid()_wp_register_default_connector_settings()—hasProvider()check_wp_connectors_pass_default_keys_to_ai_client()—hasProvider(),_wp_connectors_get_api_key_source(),setProviderRequestAuthentication()_wp_connectors_get_connector_script_module_data()—hasProvider(),isProviderConfigured(),_wp_connectors_get_api_key_source()wp_get_connector()andwp_get_connectors()to document the optionalprovider_idfield.wpConnectorRegistry.php
Four new tests:
test_register_stores_provider_id— provider_id is preserved in returned connector datatest_register_omits_provider_id_when_not_provided— no provider_id key when absent from argstest_register_omits_provider_id_when_empty— no provider_id key when empty stringtest_get_registered_includes_provider_id— provider_id round-trips throughget_registered()Impact
anthropic,google,openai) have no hyphens and are unaffected.azure-openai→azure_openai) and appear correctly in Settings → Connectors.provider_idfield falls back to the connector ID via??, so existing code consuming connector data is unaffected.Trac ticket: https://core.trac.wordpress.org/ticket/64861
Use of AI Tools
GitHub Copilot + Opus 4.6 used to review the patch
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.