/**
* 处理单篇文章的单个提示词模板
*
* @param int $post_id 要处理的文章ID
* @param array $settings 处理设置
* @return void
*/
private static function process_single_post_single_template($post_id, $settings) {
$post = get_post($post_id);
if (!$post) {
return;
}
try {
// 标记文章为处理中状态
update_post_meta($post_id, 'ai_processing_status', 'processing');
update_post_meta($post_id, 'ai_processing_start_time', current_time('mysql'));
// 优化后端处理:设置资源限制,避免服务器过载
@set_time_limit(300); // 5分钟
@ini_set('memory_limit', '256M');
// 强制垃圾回收,释放内存
if (function_exists('gc_collect_cycles')) {
gc_collect_cycles();
}
error_log("后端处理开始: 文章ID {$post_id}");
$post_processor = new Post_Processor();
$qa_creator = new QA_Creator();
// 解析目标设置
$target_post_type = isset($settings['target_post_type']) ? $settings['target_post_type'] : 'post';
$target_taxonomy = isset($settings['target_taxonomy']) ? $settings['target_taxonomy'] : '';
$content = $post_processor->extract_post_content($post);
// 获取提示词模板列表
$selected_templates = isset($settings['prompt_templates']) ? $settings['prompt_templates'] : array('default');
// 确保是数组格式
if (!is_array($selected_templates)) {
$selected_templates = array($selected_templates);
}
// 如果没有选择任何模板,使用默认模板
if (empty($selected_templates)) {
$selected_templates = array('default');
}
// 获取文章已处理的模板计数
$processed_template_count = get_post_meta($post_id, 'ai_processed_template_count', true);
if (!$processed_template_count) {
$processed_template_count = 0;
}
// 确定当前要处理的模板
if ($processed_template_count >= count($selected_templates)) {
// 所有模板都已处理,标记文章为完成
update_post_meta($post_id, 'ai_processed', current_time('mysql'));
// 清除处理状态,标记为完成
delete_post_meta($post_id, 'ai_processing_status');
delete_post_meta($post_id, 'ai_processing_start_time');
delete_post_meta($post_id, 'ai_processing_error');
delete_post_meta($post_id, 'ai_failed_time');
delete_post_meta($post_id, 'ai_fail_count');
delete_post_meta($post_id, 'ai_processed_template_count');
error_log("文章 {$post_id} 所有模板处理完成");
return;
}
$current_template = $selected_templates[$processed_template_count];
error_log("开始处理文章 {$post_id} 的模板 {$current_template} (" . ($processed_template_count + 1) . "/" . count($selected_templates) . ")");
// 为当前模板创建单独的设置
$template_settings = $settings;
$template_settings['prompt_templates'] = array($current_template);
// 使用AI处理器处理当前模板
$ai_processor = new AI_Processor($template_settings);
$qa_pairs = $ai_processor->process_content($content);
// 第一阶段:创建草稿
$target_settings_array = array(
'post_type' => $target_post_type,
'taxonomy' => $target_taxonomy
);
$created_posts = $qa_creator->create_qa_posts($qa_pairs, $post, $target_settings_array);
if (empty($created_posts)) {
throw new Exception('创建问答文章失败');
}
// 第二阶段:发布草稿
$publish_result = $qa_creator->publish_draft_qa_posts($created_posts);
if ($publish_result['published_count'] === 0) {
$qa_creator->cleanup_failed_drafts($created_posts);
throw new Exception('发布问答文章失败');
}
// 清理部分失败的草稿
if (!empty($publish_result['failed_posts'])) {
$qa_creator->cleanup_failed_drafts($publish_result['failed_posts']);
$created_posts = array_diff($created_posts, $publish_result['failed_posts']);
}
// 记录生成的问答文章
$existing_qa_posts = get_post_meta($post_id, 'generated_qa_posts', true);
if (!$existing_qa_posts) {
$existing_qa_posts = array();
}
// 合并新生成的文章
$all_qa_posts = array_merge($existing_qa_posts, $created_posts);
update_post_meta($post_id, 'generated_qa_posts', $all_qa_posts);
// 更新已处理的模板计数
update_post_meta($post_id, 'ai_processed_template_count', $processed_template_count + 1);
// 清除处理状态(但不标记为完全完成,因为还有其他模板需要处理)
delete_post_meta($post_id, 'ai_processing_status');
delete_post_meta($post_id, 'ai_processing_start_time');
delete_post_meta($post_id, 'ai_processing_error');
delete_post_meta($post_id, 'ai_failed_time');
delete_post_meta($post_id, 'ai_fail_count');
// 记录到数据库
AI_QA_Database_Manager::log_processing($post_id, $settings['ai_model'], 0); // 后台处理用户ID为0
AI_QA_Database_Manager::update_processing_result($post_id, $created_posts, count($created_posts));
// 添加到历史记录
AI_QA_History_Manager::add_history_record($post_id, $created_posts, $settings);
// 清理大变量,释放内存
unset($content, $qa_pairs, $target_settings_array);
// 强制垃圾回收
if (function_exists('gc_collect_cycles')) {
gc_collect_cycles();
}
error_log("后端处理完成: 文章ID {$post_id} 的模板 {$current_template},生成 " . count($created_posts) . " 个问答");
} catch (Exception $e) {
// 记录失败状态
if (isset($settings)) {
try {
AI_QA_Database_Manager::update_processing_result($post_id, array(), 0, 'failed', $e->getMessage());
} catch (Exception $db_error) {
error_log('后台处理记录失败状态出错: ' . $db_error->getMessage());
}
}
error_log('后台处理文章失败: ' . $post_id . ' - ' . $e->getMessage());
throw $e; // 重新抛出异常,让上层处理
}
}
}
toolkit-AIGC软件库