ai_client_factory = $ai_client_factory; $this->style_descriptions = $style_descriptions ?: $this->getDefaultStyleDescriptions(); } /** * Get default style descriptions * * @return array */ private function getDefaultStyleDescriptions(): array { return [ 'Professional' => 'Use clear, concise, and formal language. Focus on features, benefits, and technical details.', 'Encouraging' => 'Use positive, motivational language that inspires confidence and action.', 'Exaggerated' => 'Use dramatic and hyperbolic language to create excitement and emphasis.', 'Friendly' => 'Use warm, conversational language that builds rapport and trust.', 'Storytelling' => 'Create a narrative that engages readers and connects emotionally.', 'Minimalist' => 'Use simple, straightforward language focusing on essential details.', 'Luxurious and Elegant' => 'Use sophisticated, refined language that conveys premium quality and exclusivity.', 'Adventurous' => 'Use dynamic, exciting language that creates a sense of exploration and discovery.', 'Educational' => 'Use informative, detailed language that explains and educates.', 'Humorous' => 'Use light-hearted, playful language with appropriate humor and wit.', ]; } /** * Generate multiple content types in a single AI request * * @param int $product_id Product ID * @param array $content_types Array of content types to generate * @param array $options Additional options (improveExisting, keepExisting, etc.) * @return array|\WP_Error Generated content array or error */ public function generate( int $product_id, array $content_types, array $options = [] ) { try { $content_types = $this->validateContentTypes( $content_types ); if ( empty( $content_types ) ) { return new \WP_Error( 'invalid_types', 'No valid content types specified', [ 'status' => 400 ] ); } $product = \wc_get_product( $product_id ); if ( ! $product ) { return new \WP_Error( 'invalid_product', 'Invalid product ID', [ 'status' => 404 ] ); } $refusal = ProductEligibility::refusalMessage( $product ); if ( null !== $refusal ) { return new \WP_Error( 'invalid_product', $refusal, [ 'status' => 400 ] ); } $api_provider = get_option( 'aipt_api_provider', 'openai' ); $model = $this->ai_client_factory->getDefaultModel( $api_provider ); $model = get_option( 'aipt_model', $model ); $model = $this->ai_client_factory->normalizeModel( $api_provider, $model ); $language = get_option( 'aipt_language', Activator::get_default_value( 'language' ) ); $writing_style = get_option( 'aipt_writing_style', Activator::get_default_value( 'writing_style' ) ); $style_description = $this->style_descriptions[ $writing_style ] ?? $this->style_descriptions['Professional']; $product_info = $this->getProductInfo( $product ); $prompts = $this->buildUnifiedPrompt( $content_types, $product_info, $language, $style_description, $options ); $client = $this->ai_client_factory->create(); $max_length = $this->calculateMaxLength( $content_types ); $response = $client->generate_description( $prompts['system'], $prompts['user'], $model, $max_length ); $parsed = $this->parseResponse( $response, $content_types ); $result = $this->postProcessContent( $parsed, $content_types, $product, $options ); if ( empty( $result ) ) { return new \WP_Error( 'generation_failed', __( 'The AI response did not contain any of the requested content. Please try again.', 'ai-product-tools' ), [ 'status' => 502 ] ); } return [ 'success' => true, 'content' => $result, 'types_generated' => array_keys( $result ), ]; } catch ( \Throwable $e ) { if ( $e instanceof ProviderCallException ) { return self::providerErrorResponse( $e ); } $message = $e->getMessage(); $status = ErrorHandler::extract_http_status_from_message( $message, 500 ); return new \WP_Error( 'generation_failed', $message, [ 'status' => $status ] ); } } /** * The WP_Error for a classified provider failure: the merchant sentence * as the message, the structured fields a caller needs to decide * whether to retry, and the technical text only in error_details — * never shown to a merchant. * * @param ProviderCallException $e Caught provider failure. * @return \WP_Error * * @since 2.8.0 */ private static function providerErrorResponse( ProviderCallException $e ): \WP_Error { $fallback_provider = (string) get_option( 'aipt_api_provider', 'openai' ); $provider_error = $e->toResponseArray( $fallback_provider ); return new \WP_Error( $provider_error['code'], $provider_error['message'], [ 'status' => ProviderCallException::httpStatusFor( $e->reason() ), 'reason' => $provider_error['reason'], 'retryable' => $provider_error['retryable'], 'runWide' => $provider_error['runWide'], 'retryAfter' => $provider_error['retryAfter'], 'provider' => $provider_error['provider'], 'helpUrl' => $provider_error['helpUrl'], 'error_details' => $e->getMessage(), ] ); } /** * Validate content types * * @param array $types Content types to validate * @return array Valid content types */ private function validateContentTypes( array $types ): array { return array_values( array_intersect( $types, self::$valid_types ) ); } /** * Build unified prompt for multiple content types * * @param array $content_types Content types to generate * @param array $product_info Product information * @param string $language Target language * @param string $style_description Writing style description * @param array $options Additional options * @return array System and user prompts */ private function buildUnifiedPrompt( array $content_types, array $product_info, string $language, string $style_description, array $options ): array { $max_length = get_option( 'aipt_max_length', Activator::get_default_value( 'max_length' ) ); $max_short_length = get_option( 'aipt_max_short_length', Activator::get_default_value( 'max_short_length' ) ); $max_tags_count = get_option( 'aipt_max_tags_count', 5 ); $system_prompt = "You are an expert e-commerce content writer. Your task is to generate high-quality, SEO-optimized product content.\n\n"; $system_prompt .= "CRITICAL RULES:\n"; $system_prompt .= "1. You are writing content ONLY for this specific product: \"{$product_info['name']}\"\n"; $system_prompt .= "2. DO NOT reference, mention, or include information about any other products\n"; $system_prompt .= "3. All content must be consistent and complementary - the short description should summarize the full description\n"; $system_prompt .= "4. Write in {$language} language\n"; $system_prompt .= "5. Use this writing style: {$style_description}\n"; $system_prompt .= "6. Format descriptions as HTML (use

,