Skip to content
wiki.fftac.org

AI Engine - Source Excerpt 08

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

return antichrist_engine_foundation_ai_send_json_error('invalid_nonce', 'The share 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_share_output', 10, HOUR_IN_SECONDS)) {
        return antichrist_engine_foundation_ai_send_json_error('rate_limited', 'Too many AI outputs were shared from this account or network. Please wait and try again.', 429);
    }

    $user_id = get_current_user_id();
    $modes = antichrist_engine_foundation_ai_get_share_modes();
    $visibility = sanitize_key(antichrist_engine_security_get_post_value('visibility'));
    $mode = isset($modes[$visibility]) ? $modes[$visibility] : $modes['review'];
    $title = antichrist_engine_security_sanitize_text_field(antichrist_engine_security_get_post_value('title'), 140);
    $prompt = antichrist_engine_foundation_ai_sanitize_plain_text(antichrist_engine_security_get_post_value('prompt'), 5000);
    $answer = antichrist_engine_foundation_ai_sanitize_plain_text(antichrist_engine_security_get_post_value('answer'), 24000);
    $persona_key = sanitize_key(antichrist_engine_security_get_post_value('persona'));
    $personas = antichrist_engine_foundation_ai_personas();
    $persona_key = isset($personas[$persona_key]) ? $persona_key : antichrist_engine_foundation_ai_get_default_persona_key();
    $persona = $personas[$persona_key] ?? antichrist_engine_foundation_ai_get_persona($persona_key);
    $protocol_key = antichrist_engine_foundation_ai_normalize_protocol_key(antichrist_engine_security_get_post_value('protocol'));
    $protocol = antichrist_engine_foundation_ai_get_protocol($protocol_key);

    if (!$answer) {
        return antichrist_engine_foundation_ai_send_json_error('empty_output', 'Choose a generated AI answer before sharing.', 400);
    }

    if (!$title) {
        $title_source = $prompt ? $prompt : $answer;
        $title = wp_trim_words($title_source, 12, '');
    }

    $title = $title ? $title : __('AI inquiry note', 'antichrist-engine');
    $author_name = antichrist_engine_foundation_ai_get_share_author_name($user_id);
    $author_slug = antichrist_engine_foundation_ai_get_share_author_slug($user_id);
    $base_slug = sanitize_title($author_slug . '-' . $title);
    $base_slug = $base_slug ? $base_slug : $author_slug . '-ai-inquiry';
    $unique_slug = substr($base_slug, 0, 92) . '-' . substr(str_replace('-', '', wp_generate_uuid4()), 0, 8);
    $content = antichrist_engine_foundation_ai_format_shared_post_content($prompt, $answer, $persona_key, $protocol_key, $author_name);
    $excerpt = wp_trim_words($answer, 34, '...');

    $post_id = wp_insert_post(array(
        'post_type'      => 'post',
        'post_status'    => (string) $mode['status'],
        'post_title'     => $title,
        'post_name'      => $unique_slug,
        'post_content'   => $content,
        'post_excerpt'   => $excerpt,
        'post_author'    => $user_id,
        'comment_status' => 'publish' === $mode['status'] ? 'open' : 'closed',
    ), true);

    if (is_wp_error($post_id) || !$post_id) {
        return antichrist_engine_foundation_ai_send_json_error('share_failed', 'The AI output could not be saved as a post just now.', 500);
    }

    update_post_meta($post_id, '_antichrist_ai_shared', '1');
    update_post_meta($post_id, '_antichrist_ai_persona', $persona_key);
    update_post_meta($post_id, '_antichrist_ai_persona_label', (string) ($persona['label'] ?? ''));
    update_post_meta($post_id, '_antichrist_ai_protocol', $protocol_key);
    update_post_meta($post_id, '_antichrist_ai_protocol_label', (string) ($protocol['label'] ?? ''));
    update_post_meta($post_id, '_antichrist_ai_prompt', $prompt);
    update_post_meta($post_id, '_antichrist_ai_visibility', $visibility);
    update_post_meta($post_id, '_antichrist_ai_author_slug', $author_slug);

    $permalink = get_permalink($post_id);
    $profile_url = get_author_posts_url($user_id, $author_slug);
    $status_label = (string) ($mode['label'] ?? $mode['status']);
    $message = 'publish' === $mode['status']
        ? __('AI output published to your public profile.', 'antichrist-engine')
        : ('pending' === $mode['status']
            ? __('AI output saved for journal review.', 'antichrist-engine')
            : __('AI output saved as a private draft.', 'antichrist-engine'));

    return wp_send_json_success(array(
        'message'    => $message,
        'postId'     => (int) $post_id,
        'status'     => (string) $mode['status'],
        'statusLabel' => $status_label,
        'slug'       => $unique_slug,
        'permalink'  => $permalink,
        'profileUrl' => $profile_url,
        'protocol'   => $protocol_key,
    ));
}
add_action('wp_ajax_antichrist_engine_ai_share', 'antichrist_engine_foundation_ai_handle_share_ajax');
add_action('wp_ajax_nopriv_antichrist_engine_ai_share', 'antichrist_engine_foundation_ai_handle_auth_required_ajax');

/**
 * Legacy non-AJAX AI postback guard.
 *
 * @return void
 */
function antichrist_engine_foundation_ai_handle_submission()
{
    $page_url = antichrist_engine_foundation_get_page_url('ai_engine');

    if (!is_user_logged_in()) {
        $login_url = function_exists('antichrist_engine_membership_get_portal_url')
            ? antichrist_engine_membership_get_portal_url('login', array('redirect_to' => $page_url))
            : wp_login_url($page_url);

        wp_safe_redirect($login_url);
        exit;
    }

    check_admin_referer('antichrist_engine_ai_engine');
    wp_safe_redirect(add_query_arg('ai_status', 'ajax_required', $page_url));
    exit;
}
add_action('admin_post_antichrist_engine_ai_engine', 'antichrist_engine_foundation_ai_handle_submission');

/**
 * Gets a stored AI result.
 *
 * @return array<string, string>
 */
function antichrist_engine_foundation_ai_get_result()
{
    $token = isset($_GET['ai_result']) ? antichrist_engine_security_sanitize_text_field(wp_unslash($_GET['ai_result']), 64) : '';

    if (!$token || !preg_match('/^[a-f0-9-]{36}$/i', $token)) {
        return array();
    }

    $result = get_transient('antichrist_engine_ai_result_' . $token);

    return is_array($result) ? $result : array();
}

/**
 * Gets the AI engine notice for the current request.
 *
 * @return array<string, string>
 */
function antichrist_engine_foundation_ai_get_notice()
{
    $status = isset($_GET['ai_status']) ? sanitize_key(wp_unslash($_GET['ai_status'])) : '';

    if (!$status) {
        return array();
    }

    $messages = array(
        'empty'         => array(
            'type'    => 'error',
            'message' => 'Enter a question, belief, or premise before asking the Adversary to respond.',
        ),
        'too_long'      => array(
            'type'    => 'error',
            'message' => 'That prompt is too large for the current key policy. Shorten it or save a personal OpenAI API key.',
        ),
        'invalid_submission' => array(
            'type'    => 'error',
            'message' => 'The AI request could not be verified. Refresh the page and try again.',
        ),
        'rate_limited'  => array(
            'type'    => 'error',
            'message' => 'Too many AI requests were received from this account, browser, or network. Please wait a few minutes and try again.',
        ),
        'auth_required' => array(
            'type'    => 'error',
            'message' => 'Sign in before using the live AI Engine.',
        ),
        'unconfigured'  => array(
            'type'    => 'error',
            'message' => 'Live AI responses require a saved personal OpenAI API key or a configured limited Foundation key.',
        ),
        'user_key_required' => array(
            'type'    => 'error',
            'message' => 'This request is too large for the limited Foundation key. Save your own OpenAI API key for larger token use.',
        ),
        'ajax_required' => array(
            'type'    => 'error',
            'message' => 'The AI Engine now runs through the secure AJAX console. Reload the page and try again.',
        ),
        'request_failed' => array(
            'type'    => 'error',
            'message' => 'The AI engine could not complete the request just now. Please try again shortly.',
        ),
        'empty_response' => array(
            'type'    => 'error',
            'message' => 'The AI engine returned an empty response. Try rephrasing the prompt.',
        ),
        'success'       => array(
            'type'    => 'success',
            'message' => 'A new adversarial reframing is ready below.',
        ),
    );

    return $messages[$status] ?? array();
}