Make WordPress Core


Ignore:
Timestamp:
03/19/2026 06:50:08 AM (8 months ago)
Author:
gziolo
Message:

Connectors: Allow hyphens in connector IDs

Expands the connector ID validation regex from /^[a-z0-9_]+$/ to /^[a-z0-9_-]+$/, aligning with the PHP AI Client library naming conventions. Hyphens are normalized to underscores when generating setting_name (e.g., azure-openai → connectors_ai_azure_openai_api_key).

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11285.

Props pers, gziolo, jorgefilipecosta, westonruter, flixos90, mukesh27.
Fixes #64861.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-connector-registry.php

    r62032 r62056  
    6868         * Validates the provided arguments and stores the connector in the registry.
    6969         * For connectors with `api_key` authentication, a `setting_name` is automatically
    70          * generated using the pattern `connectors_ai_{$id}_api_key` (e.g., connector ID
    71          * `openai` produces `connectors_ai_openai_api_key`). This setting name is used
    72          * for the Settings API registration and REST API exposure.
     70         * generated using the pattern `connectors_ai_{$id}_api_key`, with hyphens in the ID
     71         * normalized to underscores (e.g., connector ID `openai` produces
     72         * `connectors_ai_openai_api_key`, and `azure-openai` produces
     73         * `connectors_ai_azure_openai_api_key`). This setting name is used for the Settings
     74         * API registration and REST API exposure.
    7375         *
    7476         * Registering a connector with an ID that is already registered will trigger a
    … …  
    8183         *
    8284         * @param string $id   The unique connector identifier. Must match the pattern
    83          *                     `/^[a-z0-9_]+$/` (lowercase alphanumeric and underscores only).
     85         *                     `/^[a-z0-9_-]+$/` (lowercase alphanumeric, hyphens, and underscores only).
    8486         * @param array  $args {
    8587         *     An associative array of arguments for the connector.
    … …  
    107109         */
    108110        public function register( string $id, array $args ): ?array {
    109                 if ( ! preg_match( '/^[a-z0-9_]+$/', $id ) ) {
     111                if ( ! preg_match( '/^[a-z0-9_-]+$/', $id ) ) {
    110112                        _doing_it_wrong(
    111113                                __METHOD__,
    112114                                __(
    113                                         'Connector ID must contain only lowercase alphanumeric characters and underscores.'
     115                                        'Connector ID must contain only lowercase alphanumeric characters, hyphens, and underscores.'
    114116                                ),
    115117                                '7.0.0'
    … …  
    186188                                $connector['authentication']['credentials_url'] = $args['authentication']['credentials_url'];
    187189                        }
    188                         $connector['authentication']['setting_name'] = "connectors_ai_{$id}_api_key";
     190                        $connector['authentication']['setting_name'] = 'connectors_ai_' . str_replace( '-', '_', $id ) . '_api_key';
    189191                }
    190192
Note: See TracChangeset for help on using the changeset viewer.