hasProvider( $pid ) ) { return true; } } catch ( \Throwable $e ) { // Silent fallback. } } if ( function_exists( 'wp_get_connectors' ) ) { $connectors = call_user_func( 'wp_get_connectors' ); if ( is_array( $connectors ) && isset( $connectors[ $provider_id ] ) ) { return true; } } return false; } /** * Retrieve a list of active connectors configured in WordPress AI Client. * * @return array Associative array of [connector_id => connector_name] */ public static function get_active_connectors(): array { $active = array(); if ( function_exists( 'wp_get_connectors' ) ) { $connectors = call_user_func( 'wp_get_connectors' ); if ( is_array( $connectors ) ) { foreach ( $connectors as $id => $connector ) { $active[ $id ] = $connector['name'] ?? ucfirst( (string) $id ); } } } if ( class_exists( 'WordPress\AiClient\AiClient' ) ) { try { $registry = call_user_func( array( 'WordPress\AiClient\AiClient', 'defaultRegistry' ) ); if ( is_object( $registry ) && method_exists( $registry, 'hasProvider' ) ) { if ( $registry->hasProvider( 'google' ) && ! isset( $active['google'] ) ) { $active['google'] = 'Google Gemini'; } if ( $registry->hasProvider( 'openai' ) && ! isset( $active['openai'] ) ) { $active['openai'] = 'OpenAI'; } } } catch ( \Throwable $e ) { // Silent fallback. } } return $active; } /** * Retrieve API key for a provider via the official built-in WordPress AI Client API registry. * * Uses the built-in AI Client API (\WordPress\AiClient\AiClient) instead of reading * raw options from get_option(), adhering to WordPress guidelines. * * @param string $connector_id Connector / Provider ID (e.g. 'google', 'gemini', 'openai'). * @return string API key from AI Client registry or empty string. */ public static function get_connector_api_key( string $connector_id ): string { if ( ! class_exists( 'WordPress\AiClient\AiClient' ) ) { return ''; } try { $registry = call_user_func( array( 'WordPress\AiClient\AiClient', 'defaultRegistry' ) ); $provider_id = ( 'gemini' === $connector_id || 'google-gemini' === $connector_id ) ? 'google' : $connector_id; if ( 'open_ai' === $provider_id ) { $provider_id = 'openai'; } if ( is_object( $registry ) && method_exists( $registry, 'hasProvider' ) && $registry->hasProvider( $provider_id ) ) { $auth = method_exists( $registry, 'getProviderRequestAuthentication' ) ? $registry->getProviderRequestAuthentication( $provider_id ) : null; if ( is_object( $auth ) && method_exists( $auth, 'getApiKey' ) ) { $key = (string) $auth->getApiKey(); if ( ! empty( $key ) ) { return trim( $key ); } } } } catch ( \Throwable $e ) { // Silent fallback. } return ''; } /** * Check whether any AI connectors have been configured with credentials in WordPress. * * @return bool True if at least one AI connector has configured API credentials. */ public static function has_configured_connectors(): bool { // 1. Check known core providers via get_connector_api_key. $known_providers = array( 'google', 'gemini', 'openai', 'anthropic' ); foreach ( $known_providers as $provider ) { if ( ! empty( self::get_connector_api_key( $provider ) ) ) { return true; } } // 2. Check WordPress AI Client registry for any configured provider. if ( class_exists( 'WordPress\AiClient\AiClient' ) ) { try { $registry = call_user_func( array( 'WordPress\AiClient\AiClient', 'defaultRegistry' ) ); if ( is_object( $registry ) ) { if ( method_exists( $registry, 'hasConfiguredProvider' ) && $registry->hasConfiguredProvider() ) { return true; } if ( method_exists( $registry, 'isConfigured' ) && $registry->isConfigured() ) { return true; } if ( method_exists( $registry, 'getConfiguredProviders' ) ) { $configured = $registry->getConfiguredProviders(); if ( ! empty( $configured ) ) { return true; } } if ( method_exists( $registry, 'getProviders' ) ) { $providers = $registry->getProviders(); if ( is_array( $providers ) ) { foreach ( $providers as $pid => $pobj ) { $auth = method_exists( $registry, 'getProviderRequestAuthentication' ) ? $registry->getProviderRequestAuthentication( $pid ) : null; if ( is_object( $auth ) && method_exists( $auth, 'getApiKey' ) && ! empty( $auth->getApiKey() ) ) { return true; } } } } } } catch ( \Throwable $e ) { // Silent fallback. } } // 3. Check wp_get_connectors() for any configured connectors. if ( function_exists( 'wp_get_connectors' ) ) { try { $connectors = call_user_func( 'wp_get_connectors' ); if ( is_array( $connectors ) ) { foreach ( $connectors as $connector ) { if ( is_array( $connector ) ) { if ( ! empty( $connector['configured'] ) || ! empty( $connector['has_credentials'] ) || ! empty( $connector['connected'] ) || ! empty( $connector['api_key'] ) ) { return true; } } } } } catch ( \Throwable $e ) { // Silent fallback. } } return false; } /** * Retrieve the URL to configure connectors in the WordPress admin. * * @return string Admin URL for connectors configuration screen. */ public static function get_connectors_admin_url(): string { if ( file_exists( ABSPATH . 'wp-admin/options-connectors.php' ) ) { return admin_url( 'options-connectors.php' ); } global $submenu; if ( isset( $submenu['options-general.php'] ) && is_array( $submenu['options-general.php'] ) ) { foreach ( $submenu['options-general.php'] as $sub_item ) { if ( isset( $sub_item[2] ) && false !== strpos( (string) $sub_item[2], 'connector' ) ) { return admin_url( 'options-general.php?page=' . $sub_item[2] ); } } } return admin_url( 'options-general.php?page=connectors' ); } }