$use_custom, 'wtofe_ai_debug_mode' => (bool) get_option( 'wtofe_ai_debug_mode', false ), ); foreach ( self::TEXT_FEATURES as $feature ) { $values[ 'wtofe_ai_model_' . $feature ] = WTOFE_Settings_App::selectable_model( get_option( 'wtofe_ai_model_' . $feature, '' ) ); } $values['wtofe_ai_model_image_generation'] = WTOFE_Settings_App::selectable_model( get_option( 'wtofe_ai_model_image_generation', '' ), 'image_generation' ); $data = array( 'hasActiveConnector' => AI_Content_Generator_For_WooCommerce_Helper::has_active_connector(), 'usingCustomKey' => $use_custom && '' !== trim( $api_key ), 'textFeatures' => self::TEXT_FEATURES, // Pick the help text under "Use Custom ChatGPT API Key". 'connectorsAvailable' => $connectors_available, 'hasConnectorKeys' => $connectors_available && AI_Content_Generator_For_WooCommerce_Helper::has_connector_keys(), 'apiKey' => array( 'hasKey' => '' !== $api_key, 'masked' => AI_Content_Generator_For_WooCommerce_Admin::mask_provider_api_key( $api_key ), ), 'nonces' => array( 'validateKey' => wp_create_nonce( 'wtofe_validate_api_key' ), 'saveKey' => wp_create_nonce( 'wtofe_save_api_key' ), 'removeKey' => wp_create_nonce( 'wtofe_remove_api_key' ), 'testConnection' => wp_create_nonce( 'wtofe_test_connection' ), ), ); // Connected providers overview — only on WordPress 7.0+, where the // Settings > Connectors screen it links to exists. if ( $connectors_available ) { $data = array_merge( $data, self::get_providers_overview() ); } return array( 'order' => 10, 'group' => 'general', 'values' => $values, 'data' => $data, ); } /** * One status card per supported provider (OpenAI, Anthropic, Google): * connected via the WordPress AI Connector plugin ('plugin'), this * plugin's custom API key ('custom'), both ('both'), or not set up ('') * — plus the masked key in effect. * * @return array{providers: array[], showDualKeyNotice: bool} */ private static function get_providers_overview() { $use_custom_key = (bool) get_option( 'wtofe_use_custom_api_key', false ); $custom_key = trim( (string) get_option( 'wtofe_chatgpt_api_key', '' ) ); $providers = array( 'openai' => array( 'plugin_key' => trim( (string) get_option( 'connectors_ai_openai_api_key', '' ) ), 'custom_key' => $custom_key, ), 'anthropic' => array( 'plugin_key' => trim( (string) get_option( 'connectors_ai_anthropic_api_key', '' ) ), 'custom_key' => '', ), 'google' => array( 'plugin_key' => trim( (string) get_option( 'connectors_ai_google_api_key', '' ) ), 'custom_key' => '', ), ); $cards = array(); foreach ( $providers as $id => $provider ) { $has_plugin = '' !== $provider['plugin_key']; $has_custom = '' !== $provider['custom_key']; if ( $has_plugin && $has_custom && $use_custom_key ) { $method = 'both'; } elseif ( $has_plugin ) { $method = 'plugin'; } elseif ( $has_custom ) { $method = 'custom'; } else { $method = ''; } // The key in effect: for OpenAI the custom key wins only while // "Use Custom ChatGPT API Key" is on; otherwise the connector key. if ( 'openai' === $id && $use_custom_key && $has_custom ) { $in_effect = $provider['custom_key']; } elseif ( $has_plugin ) { $in_effect = $provider['plugin_key']; } else { $in_effect = $provider['custom_key']; } $cards[] = array( 'id' => $id, 'connected' => $has_plugin || $has_custom, 'method' => $method, 'maskedKey' => AI_Content_Generator_For_WooCommerce_Admin::mask_provider_api_key( $in_effect ), ); } return array( 'providers' => $cards, // Only flagged when the custom key is actually in effect over the connector. 'showDualKeyNotice' => $use_custom_key && '' !== $providers['openai']['plugin_key'] && '' !== $custom_key, ); } // ------------------------------------------------------------------------- // Advanced Generation Settings // ------------------------------------------------------------------------- /** @return array */ private static function advanced_section() { $token_defaults = array( 'title' => 2000, 'description' => 4000, 'short_description' => 2000, 'brands' => 500, 'tags' => 500, ); $default_temp = 1.0; $timeout = absint( get_option( 'wtofe_ai_request_timeout', 60 ) ); $values = array( 'wtofe_ai_request_timeout' => $timeout ? $timeout : 60, 'wtofe_ai_badge_enabled' => AI_Content_Generator_For_WooCommerce_Helper::is_badge_enabled(), 'wtofe_ai_badge_message' => (string) get_option( 'wtofe_ai_badge_message', '' ), ); $token_features = array(); foreach ( self::TEXT_FEATURES as $key ) { $values[ 'wtofe_ai_max_tokens_' . $key ] = get_option( 'wtofe_ai_max_tokens_' . $key, $token_defaults[ $key ] ); $values[ 'wtofe_ai_temperature_' . $key ] = get_option( 'wtofe_ai_temperature_' . $key, $default_temp ); $token_features[] = array( 'key' => $key, 'defaultTokens' => $token_defaults[ $key ], 'defaultTemp' => $default_temp, ); } return array( 'order' => 20, 'group' => 'general', 'values' => $values, 'data' => array( 'hasActiveConnector' => AI_Content_Generator_For_WooCommerce_Helper::has_active_connector(), 'tokenFeatures' => $token_features, // What product pages show when the message is left blank. 'badgeDefault' => __( 'This content was generated with AI assistance.', 'ai-content-generator-for-woocommerce' ), ), ); } }