// In includes/class-sae-content-generator.php public function generate_missing_content() { global $wpdb; $table_programs = $wpdb->prefix . 'sae_programs'; $table_content = $wpdb->prefix . 'sae_content'; $processed_count = 0; $limit = 5; // Process in small batches // Find programs that DO NOT have a post_id associated with them yet. $sql = " SELECT p.* FROM {$table_programs} p LEFT JOIN {$table_content} c ON p.program_id = c.program_id AND c.post_id > 0 WHERE p.is_active = 1 AND c.post_id IS NULL "; $new_programs = $wpdb->get_results($sql); if (empty($new_programs)) { SAE_Log_Service::log("Content Gen: No new programs found needing review posts."); return 0; } foreach ($new_programs as $program) { // --- 1. GENERATE CONTENT FOR THE DEFAULT LANGUAGE --- $default_lang_code = apply_filters('wpml_default_language', substr(get_locale(), 0, 2)); $language_name = $this->gpt_service->get_language_from_country_code($program->country_code); $ai_content = $this->gpt_service->generate_program_content($program->name, $program->country_code, $program->category, $language_name); if (is_wp_error($ai_content) || !isset($ai_content['title'])) continue; // --- 2. CREATE THE REVIEW POST IN THE DEFAULT LANGUAGE --- $post_data = [ 'post_title' => $ai_content['title'], 'post_content' => $ai_content['long_description'] . "\n\n" . '[sae_offer_block_for_post]', // A specific shortcode for this page 'post_status' => 'publish', 'post_type' => 'post' // Change to 'sae_review' if you add a CPT ]; $main_post_id = wp_insert_post($post_data); if ($main_post_id > 0) { // Save the generated content and link it to the new post $wpdb->insert($table_content, [ 'program_id' => $program->program_id, 'language_code' => $default_lang_code, 'post_id' => $main_post_id, // ... save title, descriptions, usps ... ]); // --- 3. (WPML) CREATE TRANSLATIONS --- $this->create_translations($main_post_id, $program); $processed_count++; } if ($processed_count >= $limit) break; } return $processed_count; } /** * --- NEW HELPER FOR WPML --- * Creates translated posts and links them to the original. */ private function create_translations($main_post_id, $program) { if (!function_exists('wpml_get_active_languages_filter')) return; $trid = apply_filters('wpml_element_trid', NULL, $main_post_id, 'post_post'); $default_lang = apply_filters('wpml_default_language', NULL); foreach (sae_get_active_languages() as $lang_code => $lang_info) { if ($lang_code == $default_lang) continue; // Skip the original language // Generate content in the target language $language_name = $lang_info['native_name']; $ai_content = $this->gpt_service->generate_program_content($program->name, $program->country_code, $program->category, $language_name); if (is_wp_error($ai_content) || !isset($ai_content['title'])) continue; // Create the translated post $translated_post_data = [ 'post_title' => $ai_content['title'], 'post_content' => $ai_content['long_description'] . "\n\n" . '[sae_offer_block_for_post]', 'post_status' => 'publish', 'post_type' => 'post' // 'sae_review' ]; $translated_post_id = wp_insert_post($translated_post_data); if ($translated_post_id > 0) { // Link the translation to the original in WPML $set_language_args = [ 'element_id' => $translated_post_id, 'element_type' => 'post_post', 'trid' => $trid, 'language_code' => $lang_code, 'source_language_code' => $default_lang ]; do_action('wpml_set_element_language_details', $set_language_args); // Save the translated content to our content table // ... (wpdb->insert call) ... } } }