ProviderRegistry::createDefaultProviderRequestAuthentication( WordPressAiClientProvidersclass-string $className ): WordPressAiClientProviders?RequestAuthenticationInterface

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

Creates a default request authentication instance for a provider.

Parameters

$className<span class="WordPressAiClientProvidersclass-string”>WordPressAiClientProvidersclass-stringrequired
The provider class name.

Return

WordPressAiClientProviders?RequestAuthenticationInterface The default request authentication instance, or null if not required or if no credential data can be found.

Source

private function createDefaultProviderRequestAuthentication(string $className): ?RequestAuthenticationInterface
{
    $providerMetadata = $className::metadata();
    $providerId = $providerMetadata->getId();
    $authenticationMethod = $providerMetadata->getAuthenticationMethod();
    if ($authenticationMethod === null) {
        return null;
    }
    $authenticationClass = $authenticationMethod->getImplementationClass();
    if ($authenticationClass === null) {
        return null;
    }
    $authenticationSchema = $authenticationClass::getJsonSchema();
    // Iterate over all JSON schema object properties to try to determine the necessary authentication data.
    $authenticationData = [];
    if (isset($authenticationSchema['properties']) && is_array($authenticationSchema['properties'])) {
        /** @var array<string, mixed> $details */
        foreach ($authenticationSchema['properties'] as $property => $details) {
            $envVarName = $this->getEnvVarName($providerId, $property);
            // Try to get the value from environment variable or constant.
            $envValue = getenv($envVarName);
            if ($envValue === \false) {
                if (!defined($envVarName)) {
                    continue;
                    // Skip if neither environment variable nor constant is defined.
                }
                $envValue = constant($envVarName);
                if (!is_scalar($envValue)) {
                    continue;
                }
            }
            if (isset($details['type'])) {
                switch ($details['type']) {
                    case 'boolean':
                        $authenticationData[$property] = filter_var($envValue, \FILTER_VALIDATE_BOOLEAN);
                        break;
                    case 'number':
                        $authenticationData[$property] = (int) $envValue;
                        break;
                    case 'string':
                    default:
                        $authenticationData[$property] = (string) $envValue;
                }
            } else {
                // Default to string if no type is specified.
                $authenticationData[$property] = (string) $envValue;
            }
        }
        // If any required fields are missing, return null to avoid immediate errors.
        if (isset($authenticationSchema['required']) && is_array($authenticationSchema['required'])) {
            /** @var list<string> $requiredProperties */
            $requiredProperties = $authenticationSchema['required'];
            if (array_diff_key(array_flip($requiredProperties), $authenticationData)) {
                return null;
            }
        }
    }
    /** @var RequestAuthenticationInterface */
    /** @var array<string, mixed> $authenticationData */
    return $authenticationClass::fromArray($authenticationData);
}

Changelog

VersionDescription
0.1.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.