'err_post_id_invalid']); } if (empty($keyword)) { wp_send_json_error(['code' => 'err_keyword_empty']); } $post_title = get_the_title($post_id); if (empty($post_title)) { wp_send_json_error(['code' => 'err_post_title_missing']); } if (empty($custom_prompt)) { $custom_prompt = get_post_meta($post_id, 'aicont_singlepost_faq_prompt_custom', true); } if (empty($custom_prompt)) { $custom_prompt = get_option('aicont_plugin_faq_prompt_custom', ''); } $active_license = get_option('aicont_active_license', ''); $result = aicont_call_webtinus_faq([ 'keyword' => $keyword, 'post_id' => $post_id, 'post_title' => $post_title, 'prompt' => $custom_prompt, 'api_key' => $active_license ]); if (is_wp_error($result) || empty($result['faq_content'])) { wp_send_json_error(['code' => $result['error_code'] ?? 'err_faq_empty']); } $saved = aicont_save_faq_to_post($post_id, $result['faq_content']); if (!$saved) { wp_send_json_error(['code' => 'err_save_failed']); } wp_send_json_success(['message' => 'success_faq_generated']); } function aicont_call_webtinus_faq($args) { $client_site_url = home_url('', 'https'); $server_url = 'https://webtinus.com/wp-json/aicont/v1/generate_faq1'; $body = wp_json_encode([ 'keyword' => $args['keyword'], 'post_id' => $args['post_id'], 'post_title' => $args['post_title'], 'prompt' => $args['prompt'], 'api_key' => $args['api_key'] ?? '', 'client_site_url' => $client_site_url, 'site_title' => get_bloginfo('name'), 'site_language' => get_locale() ], 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' => 180 ]); if (is_wp_error($response)) return ['error_code' => 'err_connection_failed']; $data = json_decode(wp_remote_retrieve_body($response), true); if (empty($data['success']) || empty($data['faq_content'])) { return ['error_code' => $data['error_code'] ?? 'err_faq_empty']; } return ['faq_content' => trim($data['faq_content'])]; } function aicont_save_faq_to_post($post_id, $faq_content) { $lines = array_filter(preg_split('/\r\n|\r|\n/', $faq_content), 'trim'); $template_type = get_option('aicont_plugin_template_type', 'ready'); if ($template_type === 'custom') { if (!function_exists('update_field')) { return false; } $repeater = get_option('aicont_plugin_faq_article_id', 'faq_post_single'); $q_field = get_option('aicont_plugin_faq_question_id', 'tit_faq_post_single'); $a_field = get_option('aicont_plugin_faq_answer_id', 'desc_faq_post_single'); delete_field($repeater, $post_id); foreach (array_chunk($lines, 2) as $pair) { if (count($pair) === 2) { add_row($repeater, [$q_field => $pair[0], $a_field => $pair[1]], $post_id); } } } else { $faq_html = "

" . __('Frequently Asked Questions', 'aicontify') . "

"; foreach (array_chunk($lines, 2) as $pair) { if (count($pair) === 2) { $faq_html .= "

" . esc_html($pair[0]) . "

"; $faq_html .= "

" . wp_kses_post($pair[1]) . "

"; } } $faq_html .= "
"; $post = get_post($post_id); $new_content = $post->post_content . "\n" . $faq_html; wp_update_post([ 'ID' => $post_id, 'post_content' => $new_content ], true); } return true; }