$guide_base . '-de_DE.html', 'de_AT' => $guide_base . '-de_DE.html', 'de_CH' => $guide_base . '-de_DE.html', 'de_LU' => $guide_base . '-de_DE.html', 'es_ES' => $guide_base . '-es_ES.html', 'es_MX' => $guide_base . '-es_ES.html', 'es_AR' => $guide_base . '-es_ES.html', 'es_CO' => $guide_base . '-es_ES.html', 'es_PE' => $guide_base . '-es_ES.html', 'es_CL' => $guide_base . '-es_ES.html', ); return isset( $supported_locales[ $locale ] ) ? $supported_locales[ $locale ] : $guide_base . '-en.html'; } /** * Logs debug information if debug mode is enabled. * * @param string $message * @param array $data * @return void */ public static function log_debug( $message, $data = array() ) { if ( ! get_option( 'wtofe_ai_debug_mode', false ) ) { return; } if ( ! class_exists( 'WC_Logger' ) ) { return; } $logger = wc_get_logger(); $context = array( 'source' => 'webtoffee-ai' ); if ( is_array( $data ) || is_object( $data ) ) { // @codingStandardsIgnoreStart $message .= ' Data: ' . print_r( $data, true ); // @codingStandardsIgnoreEnd } $logger->debug( $message, $context ); } /** * Generates an image prompt based on product details. * * @param int $post_id * @return string */ public static function generate_image_prompt( $post_id ) { $product = wc_get_product( absint( $post_id ) ); if ( ! $product ) { return ''; } $title = $product->get_name(); $short_description = $product->get_short_description(); $categories = wp_get_post_terms( absint( $post_id ), 'product_cat', array( 'fields' => 'names' ) ); $tags = wp_get_post_terms( absint( $post_id ), 'product_tag', array( 'fields' => 'names' ) ); $prompt_parts = array(); $prompt_parts[] = 'Product: ' . esc_html( $title ); if ( ! empty( $categories ) ) { $prompt_parts[] = 'Category: ' . esc_html( implode( ', ', $categories ) ); } if ( ! empty( $tags ) ) { $prompt_parts[] = 'Tags: ' . esc_html( implode( ', ', $tags ) ); } if ( ! empty( $short_description ) ) { $prompt_parts[] = 'Details: ' . esc_html( wp_strip_all_tags( $short_description ) ); } $prompt_parts[] = esc_html__( 'Style: Professional product photography, high quality, white background, clear lighting, centered composition', 'ai-content-generator-for-woocommerce' ); $prompt = implode( '. ', $prompt_parts ); self::log_debug( 'Generated image prompt', array( 'prompt' => $prompt ) ); return $prompt; } /** * Retrieves the stored ChatGPT API key. * * @return string */ public static function get_api_key() { $credentials = self::get_provider_credentials(); return $credentials['api_key']; } /** * Detects which provider and key should be used for a given feature. * * Priority: * 1. Plugin's own ChatGPT key (only when toggle is enabled). * 2. Connector key for the provider that owns the saved model for $feature. * 3. First configured connector (fallback, no model preference). * * @param string $feature e.g. 'description', 'tags', 'image_generation'. Empty for generic use. * @return array{ provider: string, api_key: string, model: string } */ private static function get_provider_credentials( $feature = '' ) { // 1. Plugin's own ChatGPT key — only when the admin has explicitly enabled it. // Skip this shortcut if the feature has a saved model that belongs to a non-OpenAI // provider (e.g. the admin chose a Gemini or Anthropic model for the chatbot). if ( (bool) get_option( 'wtofe_use_custom_api_key', false ) ) { $plugin_key = trim( (string) get_option( 'wtofe_chatgpt_api_key', '' ) ); if ( '' !== $plugin_key ) { $use_custom_key = true; if ( '' !== $feature ) { $saved_model_for_feature = trim( (string) get_option( 'wtofe_ai_model_' . $feature, '' ) ); if ( '' !== $saved_model_for_feature && 'openai' !== self::get_provider_for_model( $saved_model_for_feature ) ) { // The feature is configured to use a non-OpenAI model — let step 2 handle it. $use_custom_key = false; } } if ( $use_custom_key ) { $model = self::resolve_model_for_provider( 'openai', $feature ); return array( 'provider' => 'openai', 'api_key' => $plugin_key, 'model' => $model ); } } } // 2. When a feature is known, determine the provider from the saved model. if ( '' !== $feature ) { $saved_model = trim( (string) get_option( 'wtofe_ai_model_' . $feature, '' ) ); if ( '' !== $saved_model ) { $provider = self::get_provider_for_model( $saved_model ); if ( '' !== $provider ) { $connector_option = self::get_connector_option_for_provider( $provider ); $api_key = trim( (string) get_option( $connector_option, '' ) ); if ( '' !== $api_key ) { return array( 'provider' => $provider, 'api_key' => $api_key, 'model' => $saved_model ); } } } } // 3. Fallback — use the first configured connector with its provider default model. $connectors = array( 'openai' => 'connectors_ai_openai_api_key', 'google' => 'connectors_ai_google_api_key', 'anthropic' => 'connectors_ai_anthropic_api_key', ); foreach ( $connectors as $provider => $option ) { $api_key = trim( (string) get_option( $option, '' ) ); if ( '' !== $api_key ) { $model = self::get_default_model_for_provider( $provider, $feature ); return array( 'provider' => $provider, 'api_key' => $api_key, 'model' => $model ); } } return array( 'provider' => '', 'api_key' => '', 'model' => '' ); } /** * Returns whether the AI-generated badge is enabled. * * @return bool */ public static function is_badge_enabled() { return (bool) get_option( 'wtofe_ai_badge_enabled', false ); } /** * Returns the badge message, falling back to the default. * * @return string */ public static function get_badge_message() { $default = __( 'This content was generated with AI assistance.', 'ai-content-generator-for-woocommerce' ); return get_option( 'wtofe_ai_badge_message', $default ); } // ------------------------------------------------------------------------- // Shared API call logic — used by both AJAX handlers and Abilities callbacks // ------------------------------------------------------------------------- /** * Tone instructions map shared across description generators. * * @return array */ public static function tone_instructions() { return array( 'formal' => 'Write in a professional and business-like tone, using proper language and maintaining credibility.', 'casual' => 'Write in a friendly, conversational tone that connects with everyday shoppers.', 'persuasive' => 'Write in a compelling, action-oriented tone that emphasizes benefits and drives sales.', 'technical' => 'Write in a detailed, specification-focused tone that highlights technical features and capabilities.', 'luxury' => 'Write in an elegant, sophisticated tone that emphasizes exclusivity and premium quality.', 'creative' => 'Write in an imaginative, engaging tone that tells a story and captures attention.', ); } /** * Strips markdown code fences from a string, preserving the content inside. * Handles ```html, ```plaintext, ``` and similar wrappers. * * @param string $text * @return string */ private static function strip_code_fences( $text ) { // Remove opening fence with optional language identifier. $text = preg_replace( '/^```[a-z]*\s*/i', '', trim( $text ) ); // Remove closing fence. $text = preg_replace( '/\s*```\s*$/i', '', $text ); return trim( $text ); } /** * Cleans a plain-text AI response — strips HTML tags, markdown code fences, * backticks, asterisks used as bullets, and bracket wrappers like [word]. * * Use this for fields that must be plain text (short description, brands, tags). * * @param string $text * @return string */ private static function clean_plain_text( $text ) { $text = self::strip_code_fences( $text ); $text = wp_strip_all_tags( $text ); // Remove markdown bold/italic markers (**word** or *word*). $text = preg_replace( '/\*{1,2}([^*]+)\*{1,2}/', '$1', $text ); // Remove inline backticks. $text = preg_replace( '/`([^`]*)`/', '$1', $text ); // Strip bracket wrappers like [Brand Name] → Brand Name. $text = preg_replace( '/^\[([^\]]+)\]$/', '$1', trim( $text ) ); // Collapse multiple spaces/newlines to a single space. $text = preg_replace( '/\s+/', ' ', $text ); return trim( $text ); } }