feat(ai): 打通 SEO 闭环与投稿写作辅助
对齐 Zibll 的 AI 生成 SEO 流程:统一 WP AI Client/主题 Provider 双后端、文章/分类 SEO 框与生成回填、投稿摘要/标签建议,并收敛扩展页为连接配置。
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
/**
|
||||
* AKSRT Admin — AI Controller
|
||||
*
|
||||
* Owns REST routes for the "AI 子系统" domain.
|
||||
* REST for AI connection config + SEO/writing generation.
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
@@ -14,50 +14,70 @@ class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
||||
private static $instance = null;
|
||||
|
||||
public static function get_instance(): self {
|
||||
if (null === self::$instance) self::$instance = new self();
|
||||
if (null === self::$instance) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
public function register_routes(): void {
|
||||
// AI status
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/status', [
|
||||
'methods' => 'GET',
|
||||
'callback' => [$this, 'get_status'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
'permission_callback' => [$this, 'check_logged_in'],
|
||||
]);
|
||||
|
||||
// AI settings update
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/settings', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'update_settings'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
]);
|
||||
|
||||
// AI SEO suggest
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/seo-generate', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'seo_generate'],
|
||||
'permission_callback' => [$this, 'check_logged_in'],
|
||||
]);
|
||||
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/seo-suggest', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'seo_suggest'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
'permission_callback' => [$this, 'check_write_assist'],
|
||||
]);
|
||||
|
||||
// AI summary suggest
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/summary-suggest', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'summary_suggest'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
'permission_callback' => [$this, 'check_write_assist'],
|
||||
]);
|
||||
|
||||
// AI tags suggest
|
||||
register_rest_route(self::API_NAMESPACE, '/ai/tags-suggest', [
|
||||
'methods' => 'POST',
|
||||
'callback' => [$this, 'tags_suggest'],
|
||||
'permission_callback' => [$this, 'check_permission'],
|
||||
'permission_callback' => [$this, 'check_write_assist'],
|
||||
]);
|
||||
}
|
||||
|
||||
public function check_logged_in(): bool {
|
||||
return is_user_logged_in();
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /ai/status — AI 状态
|
||||
* 投稿/编辑写作辅助:已登录且可编辑文章或允许前台投稿。
|
||||
*/
|
||||
public function check_write_assist(): bool {
|
||||
if (!is_user_logged_in()) {
|
||||
return false;
|
||||
}
|
||||
if (current_user_can('edit_posts')) {
|
||||
return true;
|
||||
}
|
||||
if (class_exists('AKSRT_Front_Submit')) {
|
||||
return (bool) AKSRT_Front_Submit::get_instance()->can_submit();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function get_status(): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI')) {
|
||||
return $this->success(['data' => [
|
||||
@@ -65,22 +85,19 @@ class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
||||
'provider' => 'none',
|
||||
'configured' => false,
|
||||
'available' => false,
|
||||
'backend' => 'none',
|
||||
]]);
|
||||
}
|
||||
|
||||
return $this->success(['data' => AKSRT_AI::get_instance()->get_status()]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /ai/settings — 更新 AI 设置
|
||||
*/
|
||||
public function update_settings(WP_REST_Request $request): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI')) {
|
||||
return $this->error('AI 模块未加载', 500);
|
||||
return $this->error(__('AI 模块未加载', 'aksrt'), 500);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$allowed = [
|
||||
'enabled' => 'bool',
|
||||
'provider' => 'string',
|
||||
@@ -93,7 +110,9 @@ class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
||||
|
||||
$config = [];
|
||||
foreach ($allowed as $key => $type) {
|
||||
if (!isset($params[$key])) continue;
|
||||
if (!isset($params[$key])) {
|
||||
continue;
|
||||
}
|
||||
switch ($type) {
|
||||
case 'bool':
|
||||
$config[$key] = !empty($params[$key]);
|
||||
@@ -110,7 +129,6 @@ class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
||||
}
|
||||
|
||||
$success = AKSRT_AI::get_instance()->update_config($config);
|
||||
|
||||
if ($success) {
|
||||
return $this->success(['message' => __('AI 设置已保存', 'aksrt')]);
|
||||
}
|
||||
@@ -118,84 +136,119 @@ class AKSRT_Admin_AI_Controller extends AKSRT_Admin_Controller_Base {
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /ai/seo-suggest — SEO 建议
|
||||
* POST /ai/seo-generate — 从 post/term 实体生成 SEO 三字段。
|
||||
*/
|
||||
public function seo_generate(WP_REST_Request $request): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI_SEO')) {
|
||||
return $this->error(__('AI SEO 模块未加载', 'aksrt'), 500);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$type = sanitize_key((string) ($params['type'] ?? 'post'));
|
||||
$id = absint($params['id'] ?? 0);
|
||||
|
||||
if (!in_array($type, ['post', 'term'], true) || $id <= 0) {
|
||||
return $this->error(__('参数错误', 'aksrt'), 422);
|
||||
}
|
||||
|
||||
if ($type === 'post') {
|
||||
if (!current_user_can('edit_post', $id)) {
|
||||
return $this->error(__('权限不足,无法编辑此文章', 'aksrt'), 403);
|
||||
}
|
||||
} else {
|
||||
if (!current_user_can('edit_term', $id)) {
|
||||
return $this->error(__('权限不足,无法编辑此分类/标签', 'aksrt'), 403);
|
||||
}
|
||||
}
|
||||
|
||||
$result = AKSRT_AI_SEO::get_instance()->generate_fields($type, $id);
|
||||
if (empty($result['success'])) {
|
||||
return $this->error((string) ($result['message'] ?? __('生成失败', 'aksrt')));
|
||||
}
|
||||
|
||||
return $this->success(['data' => $result['data']]);
|
||||
}
|
||||
|
||||
public function seo_suggest(WP_REST_Request $request): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI_SEO')) {
|
||||
return $this->error('AI SEO 模块未加载', 500);
|
||||
return $this->error(__('AI SEO 模块未加载', 'aksrt'), 500);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
||||
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
||||
$post_id = absint($params['post_id'] ?? 0);
|
||||
|
||||
if (empty($title)) {
|
||||
return $this->error('标题不能为空');
|
||||
if ($post_id > 0 && !current_user_can('edit_post', $post_id)) {
|
||||
return $this->error(__('权限不足', 'aksrt'), 403);
|
||||
}
|
||||
|
||||
if ($title === '') {
|
||||
return $this->error(__('标题不能为空', 'aksrt'));
|
||||
}
|
||||
|
||||
$result = AKSRT_AI_SEO::get_instance()->suggest($title, $content);
|
||||
|
||||
if ($result['success']) {
|
||||
if (!empty($result['success'])) {
|
||||
return $this->success(['data' => $result['data']]);
|
||||
}
|
||||
|
||||
return $this->error($result['message'] ?? '生成失败');
|
||||
return $this->error((string) ($result['message'] ?? __('生成失败', 'aksrt')));
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /ai/summary-suggest — 摘要建议
|
||||
*/
|
||||
public function summary_suggest(WP_REST_Request $request): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI') || !class_exists('AKSRT_AI_Prompt')) {
|
||||
return $this->error('AI 模块未加载', 500);
|
||||
return $this->error(__('AI 模块未加载', 'aksrt'), 500);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
||||
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
||||
$max_len = max(50, min(500, (int) ($params['max_length'] ?? 200)));
|
||||
|
||||
if (empty($content)) {
|
||||
return $this->error('内容不能为空');
|
||||
if (trim(wp_strip_all_tags($content)) === '') {
|
||||
return $this->error(__('内容不能为空', 'aksrt'));
|
||||
}
|
||||
|
||||
$prompt = AKSRT_AI_Prompt::get_instance()->build_summary_prompt($content, $max_len);
|
||||
$result = AKSRT_AI::get_instance()->generate($prompt);
|
||||
$result = AKSRT_AI::get_instance()->generate_text($prompt, [
|
||||
'max_tokens' => min(600, $max_len * 2),
|
||||
'temperature' => 0.5,
|
||||
]);
|
||||
|
||||
if ($result['success']) {
|
||||
if (!empty($result['success'])) {
|
||||
return $this->success(['data' => ['summary' => $result['content']]]);
|
||||
}
|
||||
|
||||
return $this->error($result['message'] ?? '生成失败');
|
||||
return $this->error((string) ($result['message'] ?? __('生成失败', 'aksrt')));
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /ai/tags-suggest — 标签建议
|
||||
*/
|
||||
public function tags_suggest(WP_REST_Request $request): WP_REST_Response {
|
||||
if (!class_exists('AKSRT_AI') || !class_exists('AKSRT_AI_Prompt')) {
|
||||
return $this->error('AI 模块未加载', 500);
|
||||
return $this->error(__('AI 模块未加载', 'aksrt'), 500);
|
||||
}
|
||||
|
||||
$params = $this->get_json_params($request);
|
||||
$title = sanitize_text_field((string) ($params['title'] ?? ''));
|
||||
$content = wp_kses_post((string) ($params['content'] ?? ''));
|
||||
|
||||
if (empty($title) && empty($content)) {
|
||||
return $this->error('标题或内容不能为空');
|
||||
if ($title === '' && trim(wp_strip_all_tags($content)) === '') {
|
||||
return $this->error(__('标题或内容不能为空', 'aksrt'));
|
||||
}
|
||||
|
||||
$prompt = AKSRT_AI_Prompt::get_instance()->build_tags_prompt($title, $content);
|
||||
$result = AKSRT_AI::get_instance()->generate($prompt);
|
||||
$result = AKSRT_AI::get_instance()->generate_text($prompt, [
|
||||
'max_tokens' => 300,
|
||||
'temperature' => 0.5,
|
||||
]);
|
||||
|
||||
if ($result['success']) {
|
||||
$tags = json_decode($result['content'], true);
|
||||
if (!empty($result['success'])) {
|
||||
$tags = json_decode((string) $result['content'], true);
|
||||
if (!is_array($tags)) {
|
||||
$tags = array_filter(array_map('trim', explode(',', $result['content'])));
|
||||
$tags = array_filter(array_map('trim', explode(',', (string) $result['content'])));
|
||||
}
|
||||
$tags = array_values(array_map('sanitize_text_field', $tags));
|
||||
return $this->success(['data' => ['tags' => $tags]]);
|
||||
}
|
||||
|
||||
return $this->error($result['message'] ?? '生成失败');
|
||||
return $this->error((string) ($result['message'] ?? __('生成失败', 'aksrt')));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user