openai_client = $openai_client; $this->database = new AIGH_Database(); } public function create_plan($topic, $num_articles, $persona = '') { $system_prompt = 'You are an expert content strategist and SEO specialist. Create detailed content plans with engaging titles, comprehensive outlines, relevant categories, and targeted keywords.'; if (!empty($persona)) { $system_prompt .= "\n\nThe content will be written from the perspective of: {$persona}. Tailor the article titles and outlines to match this expert voice."; } $user_prompt = "Create a content plan for {$num_articles} blog articles about: {$topic}\n\n"; $user_prompt .= "Return a JSON array where each article has:\n"; $user_prompt .= "- title: Engaging, SEO-friendly title\n"; $user_prompt .= "- outline: 3-5 bullet points outlining the article structure\n"; $user_prompt .= "- category: Suggested category/topic\n"; $user_prompt .= "- keywords: 3-5 relevant keywords (comma-separated)\n\n"; $user_prompt .= "Make articles varied and comprehensive to cover different aspects of the topic.\n"; $user_prompt .= "Format: [{\"title\": \"...\", \"outline\": \"...\", \"category\": \"...\", \"keywords\": \"...\"}]"; $articles = $this->openai_client->generate_json($system_prompt, $user_prompt, 0.8); if (is_wp_error($articles)) { return $articles; } if (!is_array($articles)) { return new WP_Error('invalid_plan', 'Invalid plan format returned'); } $plan_id = $this->database->create_plan($topic, "Generated plan with {$num_articles} articles"); if (!$plan_id) { return new WP_Error('db_error', 'Failed to create plan in database'); } $base_date = new DateTime('now', new DateTimeZone(get_option('timezone_string') ?: 'UTC')); $base_date->modify('+1 day'); foreach ($articles as $index => $article) { if (!isset($article['title']) || !isset($article['outline'])) { continue; } $scheduled_date = clone $base_date; $scheduled_date->modify('+' . ($index * 3) . ' days'); $item_data = array( 'title' => $article['title'], 'outline' => $article['outline'], 'category' => $article['category'] ?? '', 'keywords' => $article['keywords'] ?? '', 'scheduled_date' => $scheduled_date->format('Y-m-d H:i:s') ); $this->database->add_plan_item($plan_id, $item_data); } return array( 'plan_id' => $plan_id, 'articles_created' => count($articles) ); } public function regenerate_item_outline($item) { $system_prompt = 'You are an expert content strategist. Create detailed article outlines.'; $user_prompt = "Create a detailed outline for an article titled: {$item->title}\n\n"; $user_prompt .= "Category: {$item->category}\n"; $user_prompt .= "Keywords: {$item->keywords}\n\n"; $user_prompt .= "Provide 4-6 main points that should be covered in the article. Make it comprehensive and engaging."; return $this->openai_client->generate_text($system_prompt, $user_prompt, 0.7, 500); } }