openai_client = $openai_client; } public function generate_article($item, $tone = 'professional', $custom_style = '', $length = 'medium') { $length_guides = array( 'short' => '400-600 words', 'medium' => '800-1000 words', 'long' => '1200-1500 words' ); $length_guide = $length_guides[$length] ?? $length_guides['medium']; // Build style instruction $style_instruction = ''; if (!empty($custom_style)) { $style_instruction = "Follow this writing style guide:\n{$custom_style}"; } else { $style_instruction = "in a {$tone} tone"; } $system_prompt = "You are a professional content writer, ghostwriter, and SEO specialist. Create high-quality, engaging, SEO-optimized blog articles {$style_instruction}. Write with proper structure including introduction, body paragraphs, and conclusion. Use headings and subheadings appropriately."; $user_prompt = "Write a comprehensive blog article:\n\n"; $user_prompt .= "Title: {$item->title}\n\n"; if (!empty($item->outline)) { $user_prompt .= "Follow this outline:\n{$item->outline}\n\n"; } if (!empty($item->keywords)) { // Parse keywords - first one is primary, rest are secondary $keywords_array = array_map('trim', explode(',', $item->keywords)); $primary_keyword = $keywords_array[0] ?? ''; $secondary_keywords = array_slice($keywords_array, 1); $user_prompt .= "=== SEO KEYWORDS (MUST USE) ===\n"; $user_prompt .= "PRIMARY KEYWORD: {$primary_keyword}\n"; if (!empty($secondary_keywords)) { $user_prompt .= "SECONDARY KEYWORDS: " . implode(', ', $secondary_keywords) . "\n"; } $user_prompt .= "\nCRITICAL REQUIREMENTS:\n"; $user_prompt .= "- Use the PRIMARY keyword \"{$primary_keyword}\" in:\n"; $user_prompt .= " * The first paragraph (within first 100 words)\n"; $user_prompt .= " * At least 2 subheadings (

or

tags)\n"; $user_prompt .= " * The conclusion\n"; $user_prompt .= " * Naturally 3-5 times throughout the body\n"; if (!empty($secondary_keywords)) { $user_prompt .= "- Use EACH secondary keyword at least 1-2 times naturally in the content:\n"; foreach ($secondary_keywords as $sec_kw) { $user_prompt .= " * \"{$sec_kw}\"\n"; } } $user_prompt .= "- The article MUST be heavily optimized for these specific keywords\n"; $user_prompt .= "=== END SEO KEYWORDS ===\n\n"; } $user_prompt .= "Requirements:\n"; $user_prompt .= "- Length: {$length_guide}\n"; if (!empty($custom_style)) { $user_prompt .= "- Writing style: Follow the custom style guide provided in the system prompt\n"; } else { $user_prompt .= "- Tone: {$tone}\n"; } $user_prompt .= "- Use proper HTML formatting with

,

,

tags\n"; $user_prompt .= "- Include an engaging introduction\n"; $user_prompt .= "- Use subheadings to break up content\n"; $user_prompt .= "- Include a conclusion\n"; if (!empty($item->keywords)) { $user_prompt .= "- Optimize heavily for the specified SEO keywords\n"; } $user_prompt .= "\nWrite the complete article content (body only, no title needed):"; return $this->openai_client->generate_text($system_prompt, $user_prompt, 0.7, 3000); } public function generate_quick_article($topic, $length, $tone) { $length_guides = array( 'short' => '400-600 words', 'medium' => '800-1000 words', 'long' => '1200-1500 words' ); $system_prompt = "You are a professional content writer. Create high-quality, engaging content in a {$tone} tone."; $user_prompt = "Write a blog article about: {$topic}\n\n"; $user_prompt .= "Requirements:\n"; $user_prompt .= "- Length: {$length_guides[$length]}\n"; $user_prompt .= "- Tone: {$tone}\n"; $user_prompt .= "- Use proper HTML formatting with

,

,

tags\n"; $user_prompt .= "- Include an engaging introduction and conclusion\n"; $user_prompt .= "- Use subheadings to break up content\n\n"; $user_prompt .= "Write the complete article content:"; return $this->openai_client->generate_text($system_prompt, $user_prompt, 0.7, 3000); } public function suggest_title($topic) { $system_prompt = 'You are an expert at creating engaging, SEO-friendly article titles.'; $user_prompt = "Suggest 5 engaging, SEO-friendly article titles about: {$topic}\n\n"; $user_prompt .= "Return as JSON array: [\"title1\", \"title2\", ...]"; return $this->openai_client->generate_json($system_prompt, $user_prompt, 0.9); } /** * Refine an existing article based on user feedback */ public function refine_article($original_content, $refinement_type, $custom_feedback = '', $research_context = '') { $system_prompt = 'You are a professional content editor and writer. Improve and refine existing content based on specific feedback while maintaining the original article structure and key points.'; $user_prompt = "Here is the original article:\n\n{$original_content}\n\n"; // Add research context if available if (!empty($research_context)) { $user_prompt .= "\n{$research_context}\n\n"; $user_prompt .= "IMPORTANT: Use the specific data, statistics, and examples from the research above to improve the article.\n\n"; } // Build refinement instructions switch ($refinement_type) { case 'add_detail': $user_prompt .= "Refinement Task: Add more detail and depth to this article. Expand on key points with additional information, explanations, and context. "; if (!empty($research_context)) { $user_prompt .= "USE THE RESEARCH FINDINGS to add specific data points and examples. "; } $user_prompt .= "Keep the same structure but make it more comprehensive.\n\n"; break; case 'add_examples': $user_prompt .= "Refinement Task: Add concrete examples, case studies, or real-world scenarios. "; if (!empty($research_context)) { $user_prompt .= "USE THE SPECIFIC EXAMPLES from the research findings. Include company names and specific outcomes. "; } $user_prompt .= "Make the content more practical and relatable.\n\n"; break; case 'improve_intro': $user_prompt .= "Refinement Task: Rewrite and improve the introduction to be more engaging and compelling. "; if (!empty($research_context)) { $user_prompt .= "Start with a powerful statistic or insight from the research. "; } $user_prompt .= "Keep the rest of the article the same.\n\n"; break; case 'improve_conclusion': $user_prompt .= "Refinement Task: Rewrite and improve the conclusion to be more impactful. "; if (!empty($research_context)) { $user_prompt .= "Reference key statistics from the research. "; } $user_prompt .= "Include a strong call-to-action.\n\n"; break; case 'improve_seo': $user_prompt .= "Refinement Task: Improve the SEO optimization. Add relevant keywords naturally, improve headings. "; if (!empty($research_context)) { $user_prompt .= "Use trending topics from the research. "; } $user_prompt .= "Maintain readability.\n\n"; break; case 'make_longer': $user_prompt .= "Refinement Task: Expand this article significantly (50% more content). "; if (!empty($research_context)) { $user_prompt .= "ADD NEW SECTIONS based on the research findings. "; } $user_prompt .= "Add detailed explanations and deeper analysis.\n\n"; break; case 'make_shorter': $user_prompt .= "Refinement Task: Condense this article (reduce by 30%). Remove redundancy, keep only the most important points.\n\n"; break; case 'add_statistics': $user_prompt .= "Refinement Task: Add relevant statistics, data points, and metrics throughout the article. "; if (!empty($research_context)) { $user_prompt .= "USE EVERY RELEVANT STATISTIC from the research findings. Add at least 5-10 specific data points. "; } $user_prompt .= "Make claims more authoritative with data.\n\n"; break; case 'custom': if (empty($custom_feedback)) { return new WP_Error('no_feedback', 'Custom feedback is required'); } $user_prompt .= "Refinement Task: {$custom_feedback}\n"; if (!empty($research_context)) { $user_prompt .= "\nUSE THE RESEARCH FINDINGS to support your improvements.\n"; } $user_prompt .= "\n"; break; default: return new WP_Error('invalid_type', 'Invalid refinement type'); } $user_prompt .= "Requirements:\n"; $user_prompt .= "- Maintain proper HTML formatting with

,

,

tags\n"; $user_prompt .= "- Keep the core message and main points\n"; if (!empty($research_context)) { $user_prompt .= "- Make SUBSTANTIAL changes using the research data\n"; $user_prompt .= "- Integrate research findings throughout the entire article\n"; $user_prompt .= "- Use specific numbers, names, and examples\n"; } $user_prompt .= "- Return ONLY the refined article content\n\n"; $user_prompt .= "Write the complete refined article:"; $max_tokens = !empty($research_context) ? 5000 : 4000; return $this->openai_client->generate_text($system_prompt, $user_prompt, 0.7, $max_tokens); } /** * Generate article with research from URLs */ public function generate_article_with_research($topic, $length, $tone, $research_brief) { $length_guides = array( 'short' => '400-600 words', 'medium' => '800-1000 words', 'long' => '1200-1500 words' ); $system_prompt = "You are a professional content writer. Create high-quality, well-researched content based on verified sources. Use a {$tone} tone."; $user_prompt = "Write a blog article about: {$topic}\n\n"; $user_prompt .= "{$research_brief}\n\n"; $user_prompt .= "CRITICAL REQUIREMENTS:\n"; $user_prompt .= "- Use SPECIFIC data from the research (exact numbers, percentages)\n"; $user_prompt .= "- Include real names (companies, people, products)\n"; $user_prompt .= "- Reference specific dates from the research\n"; $user_prompt .= "- Cite information naturally\n"; $user_prompt .= "- Make this article DATA-DRIVEN and SPECIFIC\n\n"; $user_prompt .= "Requirements:\n"; $user_prompt .= "- Length: {$length_guides[$length]}\n"; $user_prompt .= "- Tone: {$tone}\n"; $user_prompt .= "- HTML formatting with

,

,

tags\n"; $user_prompt .= "- Engaging introduction with strong hook\n"; $user_prompt .= "- Descriptive subheadings\n"; $user_prompt .= "- Strong conclusion\n\n"; $user_prompt .= "Write the complete article (HTML formatted):"; return $this->openai_client->generate_text($system_prompt, $user_prompt, 0.7, 4000); } }