Skip to content
wiki.fftac.org

AI Engine - Source Excerpt 07

Back to AI Engine

Summary

This source excerpt preserves a bounded section of Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php so readers can inspect the evidence without opening the full source file.

**Source path:** Anti-Christ.org/wp-content/plugins/fftac-ai-engine/inc/ai-engine.php

function antichrist_engine_foundation_ai_handle_key_ajax()
{
    if (!check_ajax_referer('antichrist_engine_ai', 'nonce', false)) {
        return antichrist_engine_foundation_ai_send_json_error('invalid_nonce', 'The key request could not be verified. Refresh the page and try again.', 403);
    }

    if (!is_user_logged_in()) {
        return antichrist_engine_foundation_ai_handle_auth_required_ajax();
    }

    if (!antichrist_engine_security_check_rate_limit('ai_engine_key_management', 6, HOUR_IN_SECONDS)) {
        return antichrist_engine_foundation_ai_send_json_error('rate_limited', 'Too many key-management attempts were received. Please wait and try again.', 429);
    }

    $user_id = get_current_user_id();
    $mode = sanitize_key(antichrist_engine_security_get_post_value('mode'));

    if ('delete' === $mode) {
        antichrist_engine_foundation_ai_delete_user_api_key($user_id);

        return wp_send_json_success(array(
            'message' => 'Personal API key removed.',
            'key'     => antichrist_engine_foundation_ai_get_user_key_status($user_id),
        ));
    }

    $api_key = antichrist_engine_foundation_ai_normalize_user_api_key(antichrist_engine_security_get_post_value('api_key'));

    if (!antichrist_engine_foundation_ai_is_valid_user_api_key($api_key)) {
        return antichrist_engine_foundation_ai_send_json_error('invalid_key', 'Enter a valid OpenAI API key that starts with sk-. It is never displayed back after saving.', 400);
    }

    if (!antichrist_engine_foundation_ai_save_user_api_key($user_id, $api_key)) {
        return antichrist_engine_foundation_ai_send_json_error('key_not_saved', 'The key could not be encrypted and saved in this environment.', 500);
    }

    return wp_send_json_success(array(
        'message' => 'Personal API key saved securely.',
        'key'     => antichrist_engine_foundation_ai_get_user_key_status($user_id),
    ));
}
add_action('wp_ajax_antichrist_engine_ai_key', 'antichrist_engine_foundation_ai_handle_key_ajax');
add_action('wp_ajax_nopriv_antichrist_engine_ai_key', 'antichrist_engine_foundation_ai_handle_auth_required_ajax');

/**
 * Gets allowed AI output sharing modes.
 *
 * @return array<string, array<string, string>>
 */
function antichrist_engine_foundation_ai_get_share_modes()
{
    return array(
        'draft' => array(
            'status' => 'draft',
            'label'  => __('Private draft', 'antichrist-engine'),
        ),
        'review' => array(
            'status' => 'pending',
            'label'  => __('Journal review', 'antichrist-engine'),
        ),
        'public' => array(
            'status' => 'publish',
            'label'  => __('Public profile post', 'antichrist-engine'),
        ),
    );
}

/**
 * Gets a safe user display name for AI shared posts.
 *
 * @param int $user_id User ID.
 * @return string
 */
function antichrist_engine_foundation_ai_get_share_author_name($user_id)
{
    $user_id = absint($user_id);
    $display_name = $user_id ? (string) get_the_author_meta('display_name', $user_id) : '';

    return $display_name ? $display_name : sprintf(__('Member %d', 'antichrist-engine'), $user_id);
}

/**
 * Gets the author slug used in AI shared post slugs and profile URLs.
 *
 * @param int $user_id User ID.
 * @return string
 */
function antichrist_engine_foundation_ai_get_share_author_slug($user_id)
{
    $user_id = absint($user_id);
    $nicename = $user_id ? (string) get_the_author_meta('user_nicename', $user_id) : '';
    $display_name = antichrist_engine_foundation_ai_get_share_author_name($user_id);
    $slug = sanitize_title($nicename ? $nicename : $display_name);

    return $slug ? $slug : 'member-' . (string) $user_id;
}

/**
 * Formats shared AI text with safe paragraphs and fenced code blocks.
 *
 * @param string $text Generated or user-submitted text.
 * @return string
 */
function antichrist_engine_foundation_ai_format_shared_text_blocks($text)
{
    $text = str_replace(array("\r\n", "\r"), "\n", trim((string) $text));

    if ('' === $text) {
        return '';
    }

    $blocks = array();
    $text_lines = array();
    $code_lines = array();
    $code_language = '';
    $in_code_block = false;

    $flush_text_lines = static function () use (&$blocks, &$text_lines) {
        if (!$text_lines) {
            return;
        }

        $paragraph = array();

        foreach ($text_lines as $line) {
            if ('' === trim($line)) {
                if ($paragraph) {
                    $blocks[] = '<p>' . nl2br(esc_html(trim(implode("\n", $paragraph))), false) . '</p>';
                    $paragraph = array();
                }

                continue;
            }

            $paragraph[] = $line;
        }

        if ($paragraph) {
            $blocks[] = '<p>' . nl2br(esc_html(trim(implode("\n", $paragraph))), false) . '</p>';
        }

        $text_lines = array();
    };

    $flush_code_lines = static function () use (&$blocks, &$code_lines, &$code_language) {
        $code = rtrim(implode("\n", $code_lines));

        if ('' !== $code) {
            $language_class = $code_language ? ' class="language-' . esc_attr(sanitize_html_class($code_language)) . '"' : '';
            $blocks[] = '<pre class="ai-shared-output__code"><code' . $language_class . '>' . esc_html($code) . '</code></pre>';
        }

        $code_lines = array();
        $code_language = '';
    };

    foreach (explode("\n", $text) as $line) {
        if (preg_match('/^```([A-Za-z0-9_+#.-]*)\s*$/', $line, $matches)) {
            if ($in_code_block) {
                $flush_code_lines();
                $in_code_block = false;
                continue;
            }

            $flush_text_lines();
            $code_language = (string) ($matches[1] ?? '');
            $in_code_block = true;
            continue;
        }

        if ($in_code_block) {
            $code_lines[] = $line;
            continue;
        }

        $text_lines[] = $line;
    }

    if ($in_code_block) {
        $flush_code_lines();
    }

    $flush_text_lines();

    return implode("\n\n", $blocks);
}

/**
 * Formats AI output for a WordPress post body.
 *
 * @param string $prompt       User prompt.
 * @param string $answer       Assistant answer.
 * @param string $persona_key  Persona key.
 * @param string $protocol_key Inquiry protocol key.
 * @param string $author_name  Public author display name.
 * @return string
 */
function antichrist_engine_foundation_ai_format_shared_post_content($prompt, $answer, $persona_key, $protocol_key, $author_name)
{
    $persona = antichrist_engine_foundation_ai_get_persona($persona_key);
    $protocol = antichrist_engine_foundation_ai_get_protocol($protocol_key);
    $persona_label = (string) ($persona['label'] ?? __('AI flavor', 'antichrist-engine'));
    $protocol_label = (string) ($protocol['label'] ?? __('Inquiry protocol', 'antichrist-engine'));
    $prompt = trim((string) $prompt);
    $answer = trim((string) $answer);

    $blocks = array(
        '<p class="foundation-lead">' . esc_html(sprintf(__('AI-assisted inquiry shared by %s under the member profile layer. Treat this as a public thinking note, not Foundation doctrine, prophecy, professional advice, or final authority.', 'antichrist-engine'), $author_name)) . '</p>',
        '<p><strong>' . esc_html__('Response flavor:', 'antichrist-engine') . '</strong> ' . esc_html($persona_label) . '</p>',
        '<p><strong>' . esc_html__('Inquiry protocol:', 'antichrist-engine') . '</strong> ' . esc_html($protocol_label) . '</p>',
    );

    if ($prompt) {
        $blocks[] = '<h2>' . esc_html__('Prompt Under Review', 'antichrist-engine') . '</h2>';
        $blocks[] = '<blockquote>' . antichrist_engine_foundation_ai_format_shared_text_blocks($prompt) . '</blockquote>';
    }

    $blocks[] = '<h2>' . esc_html__('AI Response', 'antichrist-engine') . '</h2>';
    $blocks[] = '<div class="ai-shared-output">' . antichrist_engine_foundation_ai_format_shared_text_blocks($answer) . '</div>';
    $blocks[] = '<h2>' . esc_html__('Reader Boundary', 'antichrist-engine') . '</h2>';
    $blocks[] = '<p>' . esc_html__('This post preserves a generated output for discussion, revision, or review. The author remains responsible for judgment, citations, and any later public claim made from it.', 'antichrist-engine') . '</p>';

    return implode("\n\n", $blocks);
}

/**
 * Handles publishing or drafting an AI output as a user-authored post.
 *
 * @return mixed
 */
function antichrist_engine_foundation_ai_handle_share_ajax()
{
    if (!check_ajax_referer('antichrist_engine_ai', 'nonce', false)) {