chain = $chain; $this->connectorsProvider = $connectorsProvider; $this->localProvider = $localProvider; } /** * Returns the singleton credential resolver. * * @since 2.6.0 * * @return self Singleton instance. */ public static function instance(): self { if ( null !== self::$instance ) { return self::$instance; } $connectors_provider = new ConnectorsCredentialProvider(); $local_provider = new LocalCredentialProvider(); $default_chain = array( $connectors_provider, $local_provider ); $filtered_chain = apply_filters( 'aipt_credential_provider_chain', $default_chain, null ); if ( ! self::isValidProviderList( $filtered_chain ) ) { $doing_it_wrong = '_' . 'doing_it_wrong'; $doing_it_wrong( __METHOD__, esc_html__( 'The aipt_credential_provider_chain filter must return an array of CredentialProviderInterface instances.', 'ai-product-tools' ), '2.6.0' ); $filtered_chain = $default_chain; } self::$instance = new self( new ChainedCredentialProvider( $filtered_chain ), $connectors_provider, $local_provider ); return self::$instance; } /** * Clears the singleton for unit tests. * * Runtime code must not call this method because it discards request-level * memoization and can change provider-chain identity mid-request. * * @since 2.6.0 * * @return void */ public static function reset(): void { self::$instance = null; } /** * Clears request-level credential memoization without replacing the chain. * * Runtime code must use this after credential writes so subsequent reads in the * same request observe fresh values while preserving provider-chain identity. * * @since 2.6.0 * * @return void */ public function flushMemoization(): void { foreach ( array( $this->chain, $this->connectorsProvider, $this->localProvider ) as $provider ) { if ( method_exists( $provider, 'clearCache' ) ) { $provider->clearCache(); } } } /** * Resolves an API key for the given AIPT provider. * * @since 2.6.0 * * @param string $aiptProvider One of 'openai', 'gemini', 'claude', 'openrouter'. * @return string|null The resolved API key, or null if not configured. */ public function getKey( string $aiptProvider ): ?string { return $this->chain->getKey( $aiptProvider ); } /** * Returns the credential source for the given provider. * * @since 2.6.0 * * @param string $aiptProvider One of 'openai', 'gemini', 'claude', 'openrouter'. * @return string One of CredentialSource constants. */ public function getSource( string $aiptProvider ): string { return $this->chain->getSource( $aiptProvider ); } /** * Checks if a credential is configured for the given provider. * * @since 2.6.0 * * @param string $aiptProvider One of 'openai', 'gemini', 'claude', 'openrouter'. * @return bool True if credential is configured, false otherwise. */ public function isConfigured( string $aiptProvider ): bool { return $this->chain->isConfigured( $aiptProvider ); } /** * Returns the human-readable label for the credential source. * * @since 2.6.0 * * @param string $aiptProvider One of 'openai', 'gemini', 'claude', 'openrouter'. * @return string Translated label suitable for display. */ public function getSourceLabel( string $aiptProvider ): string { return $this->chain->getSourceLabel( $aiptProvider ); } /** * Checks if any supported provider has a resolvable key. * * @since 2.6.0 * * @return bool True if at least one provider is configured. */ public function hasAnyConfiguredProvider(): bool { return $this->hasAnyUsableKey(); } /** * Checks if any supported provider has a resolvable non-empty key. * * @since 2.6.0 * * @return bool True if at least one provider has a usable key. */ public function hasAnyUsableKey(): bool { foreach ( ProviderNameMapper::aiptProviders() as $aipt_provider ) { if ( $this->isConfigured( $aipt_provider ) ) { return true; } } return false; } /** * True when any provider has a usable key sourced from LOCAL plugin storage * (CredentialSource::DATABASE_LOCAL). Connectors-managed keys (env / constant / * database_connectors) return false. Openrouter IS included — a local * openrouter key legitimately suppresses first-run onboarding. * * @since 2.6.x * @return bool */ public function hasAnyLocalUsableKey(): bool { foreach ( ProviderNameMapper::aiptProviders() as $aipt_provider ) { if ( CredentialSource::DATABASE_LOCAL === $this->getSource( $aipt_provider ) ) { return true; } } return false; } /** * AIPT provider ids whose key is Connectors-managed (env / constant / * database_connectors). Single source of truth for the connectorsManaged list * consumed by BOTH OnboardingService::getStatus() and Init::get_onboarding_status_fast(). * * @since 2.6.x * @return list */ public function connectorsManagedProviders(): array { $managed = array(); foreach ( $this->getStatus() as $provider_id => $state ) { if ( ! empty( $state['isConnectorsManaged'] ) ) { $managed[] = $provider_id; } } return array_values( $managed ); } /** * Returns aggregate credential status for REST and UI consumers. * * `hasConnector` (since 2.8.2) tells whether WordPress has a connector for * the provider, so the UI offers Settings > Connectors only where a key can * actually be added there (never for OpenRouter on core WordPress). * * @since 2.6.0 * * @return array Per-provider status map. */ public function getStatus(): array { $status = array(); foreach ( ProviderNameMapper::aiptProviders() as $aipt_provider ) { $source = $this->getSource( $aipt_provider ); $is_env = CredentialSource::ENV === $source; $is_constant = CredentialSource::CONSTANT === $source; $is_usable = $this->isConfigured( $aipt_provider ); $status[ $aipt_provider ] = array( 'id' => $aipt_provider, 'isConnectorsManaged' => self::isConnectorsManagedSource( $source ), 'hasConnector' => $this->connectorsProvider->hasConnector( $aipt_provider ), 'source' => $source, 'sourceLabel' => $this->getSourceLabel( $aipt_provider ), 'canEditLocally' => ! in_array( $source, array( CredentialSource::ENV, CredentialSource::CONSTANT, CredentialSource::DATABASE_CONNECTORS ), true ), 'hasLocalKey' => $this->localProvider->isConfigured( $aipt_provider ), 'hasUsableKey' => $is_usable, 'envVarName' => $is_env ? $this->connectorsProvider->getEnvVarName( $aipt_provider ) : null, 'constantName' => $is_constant ? $this->connectorsProvider->getConstantName( $aipt_provider ) : null, ); } return $status; } /** * Validates the provider list returned by the extension filter. * * @since 2.6.0 * * @param mixed $providers Candidate provider list. * @return bool True when every entry implements CredentialProviderInterface. */ private static function isValidProviderList( $providers ): bool { if ( ! is_array( $providers ) ) { return false; } foreach ( $providers as $provider ) { if ( ! $provider instanceof CredentialProviderInterface ) { return false; } } return true; } /** * Checks whether a source is managed by WordPress Connectors. * * @since 2.6.0 * * @param string $source Credential source. * @return bool True when the source belongs to Connectors. */ private static function isConnectorsManagedSource( string $source ): bool { return in_array( $source, array( CredentialSource::ENV, CredentialSource::CONSTANT, CredentialSource::DATABASE_CONNECTORS, ), true ); } }