'POST',
'callback' => 'aicont_generate_faq_api',
'permission_callback' => function() {
return current_user_can('edit_posts');
},
'args' => [
'keyword' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
'post_id' => ['required' => true, 'type' => 'integer', 'sanitize_callback' => 'absint'],
'post_title' => ['required' => true, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
'api_key' => ['required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
'site_language' => ['required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
'site_title' => ['required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_text_field'],
'prompt' => ['required' => false, 'type' => 'string', 'sanitize_callback' => 'sanitize_textarea_field'],
]
]);
});
function aicont_generate_faq_api($request) {
global $aicont_api_key, $aicont_site_language, $aicont_site_title;
$keyword = $request->get_param('keyword');
$post_id = $request->get_param('post_id');
$post_title = $request->get_param('post_title');
$api_key = $request->get_param('api_key') ?: $aicont_api_key;
$site_language = $request->get_param('site_language') ?: $aicont_site_language;
$site_title = $request->get_param('site_title') ?: $aicont_site_title;
$prompt = $request->get_param('prompt');
// Validation
if (empty($keyword)) return new WP_REST_Response(['success' => false, 'error_code' => 'err_keyword_empty'], 400);
if (empty($post_id)) return new WP_REST_Response(['success' => false, 'error_code' => 'err_post_id_invalid'], 400);
if (empty($post_title)) return new WP_REST_Response(['success' => false, 'error_code' => 'err_post_title_missing'], 400);
$post = get_post($post_id);
if (!$post) return new WP_REST_Response(['success' => false, 'error_code' => 'err_post_not_found'], 404);
$client_site_url = home_url('', 'https');
if (empty($client_site_url)) return new WP_REST_Response(['success' => false, 'error_code' => 'err_site_url'], 500);
$server_url = 'https://webtinus.com/wp-json/aicont/v1/generate_faq';
$body = wp_json_encode([
'keyword' => $keyword,
'post_id' => $post_id,
'post_title' => $post_title,
'prompt' => $prompt,
'api_key' => $api_key,
'client_site_url' => $client_site_url,
'site_title' => $site_title,
'site_language' => $site_language
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$response = wp_remote_post($server_url, [
'headers' => ['Content-Type' => 'application/json', 'X-Client-Site' => $client_site_url],
'body' => $body,
'timeout' => 120,
'sslverify' => true
]);
if (is_wp_error($response)) {
return new WP_REST_Response(['success' => false, 'error_code' => 'err_connection_failed'], 500);
}
$status_code = wp_remote_retrieve_response_code($response);
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if ($status_code !== 200 || !isset($data['success'])) {
$code = $data['error_code'] ?? 'err_server_error';
return new WP_REST_Response(['success' => false, 'error_code' => $code], $status_code);
}
if (!$data['success'] || empty($data['faq_content'])) {
return new WP_REST_Response(['success' => false, 'error_code' => $data['error_code'] ?? 'err_faq_empty'], 500);
}
$faq_content = trim($data['faq_content']);
$lines = array_filter(preg_split('/\r\n|\r|\n/', $faq_content), 'trim');
$template_type = get_option('aicont_plugin_template_type', 'ready');
$faq_html = '';
if ($template_type === 'custom') {
$article_class = sanitize_html_class(get_option('aicont_plugin_faq_article_id', 'faq-article'));
$question_class = sanitize_html_class(get_option('aicont_plugin_faq_question_id', 'faq-question'));
$answer_class = sanitize_html_class(get_option('aicont_plugin_faq_answer_id', 'faq-answer'));
$faq_html .= "";
$faq_html .= "" . esc_html__('Frequently Asked Questions', 'aicontify') . "
";
for ($i = 0; $i < count($lines); $i += 2) {
$question = trim($lines[$i] ?? '');
$answer = trim($lines[$i + 1] ?? '');
if ($question && $answer) {
$faq_html .= "" . esc_html($question) . "
";
$faq_html .= "" . wp_kses_post($answer) . "
";
}
}
$faq_html .= "";
} else {
$faq_html .= "
";
$faq_html .= "
" . esc_html__('Frequently Asked Questions', 'aicontify') . "
";
for ($i = 0; $i < count($lines); $i += 2) {
$question = trim($lines[$i] ?? '');
$answer = trim($lines[$i + 1] ?? '');
if ($question && $answer) {
$faq_html .= "
" . esc_html($question) . "
";
$faq_html .= "
" . wp_kses_post($answer) . "
";
}
}
$faq_html .= "
";
}
$saved = 0;
if ($template_type === 'custom') {
$acf_repeater_field = get_option('aicont_plugin_faq_article_id', 'faq_post_single');
$acf_question_field = get_option('aicont_plugin_faq_question_id', 'tit_faq_post_single');
$acf_answer_field = get_option('aicont_plugin_faq_answer_id', 'desc_faq_post_single');
if (!function_exists('delete_field') || !function_exists('add_row')) {
return new WP_REST_Response(['success' => false, 'error_code' => 'err_acf_missing'], 400);
}
delete_field($acf_repeater_field, $post_id);
for ($i = 0; $i < count($lines); $i += 2) {
$question = trim($lines[$i] ?? '');
$answer = trim($lines[$i + 1] ?? '');
if ($question && $answer) {
$added = add_row($acf_repeater_field, [
$acf_question_field => $question,
$acf_answer_field => $answer,
], $post_id);
if ($added) $saved++;
}
}
if ($saved === 0) {
return new WP_REST_Response(['success' => false, 'error_code' => 'err_save_failed'], 500);
}
} else {
$current_content = $post->post_content;
$post_data = [
'ID' => $post_id,
'post_content' => $current_content . "\n" . wp_kses_post($faq_html),
'post_modified' => current_time('mysql'),
'post_modified_gmt' => current_time('mysql', 1),
];
$post_updated = wp_update_post($post_data, true);
if (is_wp_error($post_updated)) {
return new WP_REST_Response(['success' => false, 'error_code' => 'err_save_failed'], 500);
}
}
return new WP_REST_Response([
'success' => true,
'message_code' => 'success_faq_generated',
'faq_content' => $faq_content,
'faq_html' => $faq_html
], 200);
}