<?php
defined('ABSPATH') or die();
use \Eventviva\ImageResize;
use AiomaticOpenAI\OpenAi\OpenAi;
use Aws\S3\S3Client;

/*
 * "Test API key" feature.
 *
 * Provides a one-click connection test for each supported text/LLM provider key
 * field on the main settings page. It validates the key currently typed in the
 * field (or, if empty, the key already saved in the settings) by making a single,
 * cheap, read-only call to the provider (a model listing / key-info endpoint where
 * available). Nothing is generated and no content is produced, so there is no cost
 * beyond a trivial metadata request. The result is reported to the UI as a simple
 * "Connected" / "Invalid key" message.
 */
function aimogen_provider_key_field_map()
{
    return array(
        'openai'      => array('field' => 'app_id',             'label' => 'OpenAI'),
        'xai'         => array('field' => 'app_id_xai',         'label' => 'xAI'),
        'claude'      => array('field' => 'app_id_claude',      'label' => 'Anthropic Claude'),
        'google'      => array('field' => 'app_id_google',      'label' => 'Google Gemini'),
        'perplexity'  => array('field' => 'app_id_perplexity',  'label' => 'Perplexity'),
        'groq'        => array('field' => 'app_id_groq',        'label' => 'Groq'),
        'nvidia'      => array('field' => 'app_id_nvidia',      'label' => 'NVIDIA'),
        'openrouter'  => array('field' => 'app_id_openrouter',  'label' => 'OpenRouter'),
        'huggingface' => array('field' => 'app_id_huggingface', 'label' => 'HuggingFace'),
    );
}

add_action('wp_ajax_aimogen_test_api_key', 'aimogen_ajax_test_api_key');
function aimogen_ajax_test_api_key()
{
    check_ajax_referer('aimogen_test_api_key', 'nonce');

    if (!current_user_can('manage_options')) {
        wp_send_json_error(array('message' => esc_html__('You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer')));
    }

    $provider  = isset($_POST['provider']) ? sanitize_key($_POST['provider']) : '';
    $field_map = aimogen_provider_key_field_map();

    if (!isset($field_map[$provider])) {
        wp_send_json_error(array('message' => esc_html__('Unknown provider.', 'aiomatic-automatic-ai-content-writer')));
    }

    // The key may legitimately contain multiple newline-separated keys (key rotation),
    // so do not run sanitize_text_field on it - just unslash and trim whitespace.
    $raw_key = isset($_POST['key']) ? trim((string) wp_unslash($_POST['key'])) : '';

    // If nothing was typed in the field, fall back to the key already saved in settings.
    if ($raw_key === '') {
        $settings = get_option('aiomatic_Main_Settings', array());
        $field    = $field_map[$provider]['field'];
        if (is_array($settings) && isset($settings[$field])) {
            $raw_key = trim((string) $settings[$field]);
        }
    }

    if ($raw_key === '') {
        wp_send_json_error(array('message' => esc_html__('No API key entered yet - paste a key first, then test.', 'aiomatic-automatic-ai-content-writer')));
    }

    // When several keys are present (rotation), validate the first non-empty one.
    $keys        = array_values(array_filter(array_map('trim', preg_split('/\r\n|\r|\n/', $raw_key))));
    $key_to_test = isset($keys[0]) ? $keys[0] : $raw_key;

    $result = aimogen_test_provider_key($provider, $key_to_test);

    if (!empty($result['ok'])) {
        $msg = ($result['message'] !== '') ? $result['message'] : esc_html__('Connected', 'aiomatic-automatic-ai-content-writer');
        if (count($keys) > 1) {
            $msg .= ' ' . sprintf(esc_html__('(tested the first of %d keys)', 'aiomatic-automatic-ai-content-writer'), count($keys));
        }
        wp_send_json_success(array('message' => $msg));
    }

    wp_send_json_error(array('message' => ($result['message'] !== '' ? $result['message'] : esc_html__('Could not verify the key.', 'aiomatic-automatic-ai-content-writer'))));
}

/**
 * Validate a single provider API key.
 *
 * @return array{ok:bool,message:string}
 */
function aimogen_test_provider_key($provider, $key)
{
    $key = trim((string) $key);
    if ($key === '') {
        return array('ok' => false, 'message' => esc_html__('No API key provided.', 'aiomatic-automatic-ai-content-writer'));
    }

    $connected = esc_html__('Connected', 'aiomatic-automatic-ai-content-writer');

    switch ($provider) {

        case 'openai':
            // Reuse the plugin's own OpenAI/AiomaticAPI model listing (handles AiomaticAPI keys too).
            $error  = '';
            $models = aiomatic_get_models($key, 0, $error);
            if ($models !== false && trim((string) $error) === '') {
                return array('ok' => true, 'message' => $connected);
            }
            return array('ok' => false, 'message' => aimogen_humanize_key_error($error));

        case 'claude':
            // Reuse the plugin's Anthropic model listing.
            $error = '';
            $res   = aiomatic_claude_list_models($key, '', '', 0, $error);
            if ($res !== false && trim((string) $error) === '') {
                return array('ok' => true, 'message' => $connected);
            }
            return array('ok' => false, 'message' => aimogen_humanize_key_error($error));

        case 'xai':
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://api.x.ai/v1/models', array('Authorization' => 'Bearer ' . $key))
            );

        case 'groq':
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://api.groq.com/openai/v1/models', array('Authorization' => 'Bearer ' . $key))
            );

        case 'nvidia':
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://integrate.api.nvidia.com/v1/models', array('Authorization' => 'Bearer ' . $key))
            );

        case 'openrouter':
            // Authenticated key-info endpoint (public /models would 200 even for a bad key).
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://openrouter.ai/api/v1/auth/key', array('Authorization' => 'Bearer ' . $key))
            );

        case 'huggingface':
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://huggingface.co/api/whoami-v2', array('Authorization' => 'Bearer ' . $key))
            );

        case 'google':
            // Gemini returns 400 API_KEY_INVALID for a bad key, so only a 2xx counts as valid;
            // the extracted error message is descriptive for the rest.
            return aimogen_interpret_probe(
                aimogen_http_probe('GET', 'https://generativelanguage.googleapis.com/v1beta/models?pageSize=1&key=' . urlencode($key), array())
            );

        case 'perplexity':
            // No free listing endpoint. A minimal (max_tokens=1) request tells us whether the
            // key authenticates: 401/403 => invalid; 200/400/422 => the key was accepted
            // (a 400/422 just means the tiny request body was rejected, not the credentials).
            $body = wp_json_encode(array(
                'model'      => 'sonar',
                'messages'   => array(array('role' => 'user', 'content' => 'ping')),
                'max_tokens' => 1,
            ));
            return aimogen_interpret_probe(
                aimogen_http_probe('POST', 'https://api.perplexity.ai/chat/completions', array(
                    'Authorization' => 'Bearer ' . $key,
                    'Content-Type'  => 'application/json',
                ), $body),
                array(200, 400, 422)
            );
    }

    return array('ok' => false, 'message' => esc_html__('Unknown provider.', 'aiomatic-automatic-ai-content-writer'));
}

/**
 * Make a single lightweight HTTP request for key validation.
 *
 * @return array{status:int,body:string,error:string}
 */
function aimogen_http_probe($method, $url, $headers = array(), $body = null)
{
    // Honour any proxy the user configured for their normal API traffic.
    add_action('http_api_curl', 'aiomatic_add_proxy');

    $args = array(
        'timeout'   => 20,
        'headers'   => $headers,
        'sslverify' => false,
    );

    if (strtoupper($method) === 'POST') {
        $args['body'] = $body;
        $response = wp_remote_post($url, $args);
    } else {
        $response = wp_remote_get($url, $args);
    }

    remove_action('http_api_curl', 'aiomatic_add_proxy');

    if (is_wp_error($response)) {
        return array('status' => 0, 'body' => '', 'error' => $response->get_error_message());
    }

    return array(
        'status' => (int) wp_remote_retrieve_response_code($response),
        'body'   => (string) wp_remote_retrieve_body($response),
        'error'  => '',
    );
}

/**
 * Turn a probe result into an {ok,message} outcome based on HTTP status.
 *
 * @return array{ok:bool,message:string}
 */
function aimogen_interpret_probe($probe, $valid_statuses = array(200))
{
    if (!empty($probe['error'])) {
        return array('ok' => false, 'message' => sprintf(
            esc_html__('Connection failed: %s', 'aiomatic-automatic-ai-content-writer'),
            aimogen_trim_key_message($probe['error'], esc_html__('network error', 'aiomatic-automatic-ai-content-writer'))
        ));
    }

    $status = (int) $probe['status'];

    if (in_array($status, $valid_statuses, true) || ($status >= 200 && $status < 300)) {
        return array('ok' => true, 'message' => esc_html__('Connected', 'aiomatic-automatic-ai-content-writer'));
    }

    if ($status === 429) {
        // The credentials were accepted; the account is just rate-limited/over quota right now.
        return array('ok' => true, 'message' => esc_html__('Connected (rate limited right now, but the key is valid).', 'aiomatic-automatic-ai-content-writer'));
    }

    if ($status === 401 || $status === 403) {
        return array('ok' => false, 'message' => aimogen_extract_provider_error(
            $probe['body'],
            esc_html__('Invalid or unauthorized API key.', 'aiomatic-automatic-ai-content-writer')
        ));
    }

    return array('ok' => false, 'message' => aimogen_extract_provider_error(
        $probe['body'],
        sprintf(esc_html__('Unexpected response (HTTP %d).', 'aiomatic-automatic-ai-content-writer'), $status)
    ));
}

/**
 * Pull a human-readable error message out of a provider's JSON error body.
 */
function aimogen_extract_provider_error($body, $fallback = '')
{
    $body = (string) $body;
    if ($body === '') {
        return $fallback;
    }

    $decoded = json_decode($body, true);
    if (is_array($decoded)) {
        if (isset($decoded['error']) && is_array($decoded['error']) && isset($decoded['error']['message'])) {
            return aimogen_trim_key_message($decoded['error']['message'], $fallback);
        }
        if (isset($decoded['error']) && is_string($decoded['error'])) {
            return aimogen_trim_key_message($decoded['error'], $fallback);
        }
        if (isset($decoded['message']) && is_string($decoded['message'])) {
            return aimogen_trim_key_message($decoded['message'], $fallback);
        }
        if (isset($decoded['detail']) && is_string($decoded['detail'])) {
            return aimogen_trim_key_message($decoded['detail'], $fallback);
        }
    }

    return $fallback;
}

/**
 * Normalize/shorten a message for display in the settings UI.
 */
function aimogen_trim_key_message($message, $fallback = '')
{
    $message = trim(wp_strip_all_tags((string) $message));
    if ($message === '') {
        return $fallback;
    }
    if (function_exists('mb_strlen') && function_exists('mb_substr')) {
        if (mb_strlen($message) > 200) {
            $message = mb_substr($message, 0, 200) . '…';
        }
    } elseif (strlen($message) > 200) {
        $message = substr($message, 0, 200) . '…';
    }
    return $message;
}

/**
 * Map the descriptive error strings returned by the reused model-listing functions
 * (OpenAI/Claude) to a concise, friendly message.
 */
function aimogen_humanize_key_error($error, $fallback = '')
{
    $error = trim((string) $error);
    if ($error === '') {
        return ($fallback !== '') ? $fallback : esc_html__('Could not verify the key.', 'aiomatic-automatic-ai-content-writer');
    }

    $low = strtolower($error);

    if (strpos($low, 'insufficient_quota') !== false || strpos($low, 'quota') !== false || strpos($low, 'billing') !== false || strpos($low, 'insufficient funds') !== false || strpos($low, 'no credit') !== false) {
        return esc_html__('The key looks valid, but the account has no available quota/credits. Add billing on the provider side.', 'aiomatic-automatic-ai-content-writer');
    }
    if (strpos($low, 'rate limit') !== false || strpos($low, 'rate_limit') !== false || strpos($low, '429') !== false || strpos($low, 'too many requests') !== false) {
        return esc_html__('The key is valid but is currently rate-limited. Try again in a moment.', 'aiomatic-automatic-ai-content-writer');
    }
    if (strpos($low, '401') !== false || strpos($low, '403') !== false || strpos($low, 'unauthorized') !== false || strpos($low, 'invalid api key') !== false || strpos($low, 'incorrect api key') !== false || strpos($low, 'invalid_api_key') !== false || strpos($low, 'authentication') !== false || strpos($low, 'invalid x-api-key') !== false) {
        return esc_html__('Invalid or unauthorized API key.', 'aiomatic-automatic-ai-content-writer');
    }

    return aimogen_trim_key_message($error, esc_html__('Could not verify the key.', 'aiomatic-automatic-ai-content-writer'));
}

add_action('wp_ajax_ai_meta_generate', function () {
    check_ajax_referer('ai_meta_nonce', 'nonce');

    $post_id  = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
    $meta_key = isset($_POST['meta_key']) ? sanitize_key($_POST['meta_key']) : '';
    $prompt = sanitize_textarea_field($_POST['prompt'] ?? '');
    $model  = sanitize_text_field($_POST['model'] ?? '');

    if (!$post_id || !$meta_key || !$prompt) {
        wp_send_json_error([
            'message' => 'Missing required parameters.'
        ]);
    }
    if (empty($model)) {
        $model = AIMOGEN_DEFAULT_MODEL;
    }
    if (!current_user_can('edit_post', $post_id)) {
        wp_send_json_error([
            'message' => 'Permission denied.'
        ]);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            exit;
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}

    $thread_id = '';
    $aierror = '';
    $finish_reason = '';

    $generated_text = aiomatic_generate_text(
        $token,
        $model,
        $prompt,
        $available_tokens,
        1, 1, 0, 0,
        false,
        'taxonomyDescriptionWriter',
        0,
        $finish_reason,
        $aierror,
        false, false, false,
        '', '', 'user',
        $tax_assistant_id ?? '',
        $thread_id,
        '',
        'disabled',
        '',
        false, false,
        array(),
        '', '', ''
    );

    if ($generated_text === false) {
        wp_send_json_error([
            'message' => 'Failed to generate AI content: ' . $aierror
        ]);
    }

    $result = aiomatic_sanitize_ai_result($generated_text);

    update_post_meta($post_id, $meta_key, $result);
    
    wp_send_json_success([
        'result' => $result
    ]);
});

add_action('wp_ajax_aiomatic_bulk_rules', 'aiomatic_bulk_rules');
function aiomatic_bulk_rules(){

    check_ajax_referer('aiomatic_bulk_nonce');

    if(!current_user_can('manage_options')){
        wp_die('Permission denied');
    }

    $rules  = array_map('sanitize_text_field', $_POST['rules'] ?? []);
    $action = sanitize_text_field($_POST['bulk_action'] ?? '');

    if(empty($rules))
        wp_send_json_error();

    switch($action){

        case 'run_all':
            foreach($rules as $rule){
                aiomatic_run_rule_now($rule, 4);
            }
        break;

        case 'delete_posts':
            foreach($rules as $rule){
                aiomatic_delete_rule_posts($rule, true);
            }
        break;

        case 'trash_posts':
            foreach($rules as $rule){
                aiomatic_delete_rule_posts($rule, false);
            }
        break;

        case 'duplicate':
            foreach($rules as $rule){
                aiomatic_duplicate_rule($rule);
            }
        break;
    }

    wp_send_json_success();
}

add_action('wp_ajax_aiomatic_get_rule_defaults', function(){
    if (!current_user_can('manage_options')) wp_send_json_error(array('message' => 'Insufficient permissions.'), 403);
    check_ajax_referer('aiomatic_rule_defaults_nonce', 'nonce');

    $creator_key = isset($_POST['creator_key']) ? wp_unslash($_POST['creator_key']) : '';
    $payload = aiomatic_get_rule_defaults_payload($creator_key);

    wp_send_json_success(array('payload' => $payload));
});

add_action('wp_ajax_aiomatic_save_rule_defaults', function(){
    if (!current_user_can('manage_options')) wp_send_json_error(array('message' => 'Insufficient permissions.'), 403);
    check_ajax_referer('aiomatic_rule_defaults_nonce', 'nonce');

    $creator_key = isset($_POST['creator_key']) ? wp_unslash($_POST['creator_key']) : '';
    $raw = isset($_POST['payload']) ? wp_unslash($_POST['payload']) : '';
    $payload = json_decode($raw, true);

    if (!is_array($payload)) wp_send_json_error(array('message' => 'Invalid payload.'), 400);

    $clean = array();
    foreach ($payload as $k => $v) {
        if (!is_string($k)) continue;
        if (strpos($k, 'aiomatic_') !== 0) continue;

        if (is_string($v) || is_numeric($v)) {
            $clean[$k] = sanitize_text_field((string)$v);
        } elseif (is_array($v)) {
            $tmp = array();
            foreach ($v as $vv) {
                if (is_string($vv) || is_numeric($vv)) $tmp[] = sanitize_text_field((string)$vv);
            }
            $clean[$k] = $tmp;
        }
    }

    $scope = (isset($_POST['scope']) && $_POST['scope'] === 'site') ? 'site' : 'user';
    aiomatic_save_rule_defaults_payload($creator_key, $clean, $scope);

    wp_send_json_success(array('message' => 'Saved.'));
});

add_action('wp_ajax_aiomatic_reset_rule_defaults', function(){
    if (!current_user_can('manage_options')) wp_send_json_error(array('message' => 'Insufficient permissions.'), 403);
    check_ajax_referer('aiomatic_rule_defaults_nonce', 'nonce');

    $creator_key = isset($_POST['creator_key']) ? sanitize_key(wp_unslash($_POST['creator_key'])) : '';
    $user_id = get_current_user_id();

    $all = get_user_meta($user_id, 'aiomatic_rule_defaults_map', true);
    if (!is_array($all)) $all = array();

    unset($all[$creator_key]);

    if (empty($all)) delete_user_meta($user_id, 'aiomatic_rule_defaults_map');
    else update_user_meta($user_id, 'aiomatic_rule_defaults_map', $all);

    wp_send_json_success(array('message' => 'Reset.'));
});

add_action('wp_ajax_aimogen_save_allowed_domains', 'aimogen_save_allowed_domains');
function aimogen_save_allowed_domains()
{
    check_ajax_referer('aimogen_user_remote_nonce', 'nonce');

    if (!is_user_logged_in()) {
        wp_send_json_error();
    }

    $post_id  = isset($_POST['post_id']) ? intval($_POST['post_id']) : 0;
    $domains = isset($_POST['domains'])
        ? sanitize_textarea_field($_POST['domains'])
        : '';

    if (get_post_type($post_id) !== 'aiomatic_remote_chat') {
        wp_send_json_error();
    }

    if ((int)get_post_field('post_author', $post_id) !== get_current_user_id()) {
        wp_send_json_error();
    }

    $domains = trim($domains);

    if (!empty($domains)) {

        $parts = array_map('trim', explode(',', $domains));

        $clean = array();

        foreach ($parts as $domain) {

            if (empty($domain)) {
                continue;
            }

            $domain = rtrim($domain, '/');

            if (!preg_match('#^https?://#i', $domain)) {
                $domain = 'https://' . $domain;
            }

            $clean[] = esc_url_raw($domain);
        }

        $domains = implode(', ', array_unique($clean));
    }

    update_post_meta($post_id, '_aiomatic_allowed_domains', $domains);

    wp_send_json_success();
}

add_action('wp_ajax_aimogen_user_create_remote', 'aimogen_user_create_remote');
function aimogen_user_create_remote()
{
    check_ajax_referer('aimogen_user_remote_nonce', 'nonce');

    if (!is_user_logged_in()) {
        wp_send_json_error();
    }

    $user_id = get_current_user_id();
    $max     = intval($_POST['max']);

    $count = count_user_posts($user_id, 'aiomatic_remote_chat');

    if ($count >= $max) {
        wp_send_json_error('Instance limit reached.');
    }

    $post_id = wp_insert_post(array(
        'post_title'   => 'Chatbot ' . uniqid(),
        'post_status'  => 'publish',
        'post_type'    => 'aiomatic_remote_chat',
        'post_author'  => $user_id,
        'post_content' => '[aimogen-chat-form]',
    ));

    if (!$post_id || is_wp_error($post_id)) {
        wp_send_json_error('Creation failed.');
    }

    update_post_meta($post_id, '_aimogen_user_generated', '1');
    update_post_meta($post_id, '_aiomatic_remote_secret', wp_generate_password(32, false, false));

    wp_send_json_success();
}

add_action('wp_ajax_aimogen_user_delete_remote', 'aimogen_user_delete_remote');
function aimogen_user_delete_remote()
{
    check_ajax_referer('aimogen_user_remote_nonce', 'nonce');

    $post_id = absint($_POST['post_id']);
    $post = get_post($post_id);

    if (!$post || $post->post_type !== 'aiomatic_remote_chat') {
        wp_send_json_error();
    }

    if ($post->post_author != get_current_user_id()) {
        wp_send_json_error('Unauthorized.');
    }

    wp_delete_post($post_id, true);

    wp_send_json_success();
}

add_action('wp_ajax_aimogen_user_regen_remote', 'aimogen_user_regen_remote');
function aimogen_user_regen_remote()
{
    check_ajax_referer('aimogen_user_remote_nonce', 'nonce');

    $post_id = absint($_POST['post_id']);
    $post = get_post($post_id);

    if (!$post || $post->post_type !== 'aiomatic_remote_chat') {
        wp_send_json_error();
    }

    if ($post->post_author != get_current_user_id()) {
        wp_send_json_error('Unauthorized.');
    }

    update_post_meta($post_id, '_aiomatic_remote_secret', wp_generate_password(32, false, false));

    wp_send_json_success();
}

add_action('wp_ajax_aiomatic_install_language', function () {
    check_ajax_referer('openai-ajax-nonce', 'security');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $pack = sanitize_text_field($_POST['pack']);
    $language_pack_url = 'https://wpinitiate.com/aiomatic-language-packs/';
    $local_language_dir = plugin_dir_path(__FILE__) . 'languages';
    aiomatic_copy_language_pack($pack, $language_pack_url, $local_language_dir);
    aiomatic_update_installed_language_packs_meta($pack, 'add');
    wp_send_json_success(esc_html__('Language pack installed successfully.', 'aiomatic-automatic-ai-content-writer'));
});

add_action('wp_ajax_aiomatic_uninstall_language', function () {
    check_ajax_referer('openai-ajax-nonce', 'security');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    global $wp_filesystem;
    if (!is_a($wp_filesystem, 'WP_Filesystem_Base')) {
        include_once(ABSPATH . 'wp-admin/includes/file.php');
        $creds = request_filesystem_credentials(site_url());
        wp_filesystem($creds);
    }
    $pack = sanitize_text_field($_POST['pack']);
    $local_language_dir = plugin_dir_path(__FILE__) . 'languages';
    $mo_path = $local_language_dir . '/' . $pack . '.mo';
    $po_path = $local_language_dir . '/' . $pack . '.po';
    if ($wp_filesystem->exists($mo_path)) {
        unlink($mo_path);
    }
    if ($wp_filesystem->exists($po_path)) {
        unlink($po_path);
    }
    aiomatic_update_installed_language_packs_meta($pack, 'remove');
    wp_send_json_success(esc_html__('Language pack uninstalled successfully.', 'aiomatic-automatic-ai-content-writer'));
});
add_action('wp_ajax_aiomatic_log_details', function () {
    if (!current_user_can('manage_options')) {
        wp_send_json_error();
    }
    if (
        empty($_POST['nonce']) ||
        !wp_verify_nonce($_POST['nonce'], 'openai-ajax-nonce')
    ) {
        wp_send_json_error(['message' => 'Invalid nonce']);
    }
    $log_id = intval($_POST['log_id'] ?? 0);
    if (!$log_id) {
        wp_send_json_error();
    }

    global $aiomatic_stats;
    $meta = $aiomatic_stats->get_log_meta($log_id);

    if (empty($meta)) {
        wp_send_json_success([
            'empty' => true,
        ]);
    }

    wp_send_json_success($meta);
});
add_action( 'wp_ajax_aimogen_dismiss_dashboard_notice', function () {
    check_ajax_referer( 'aimogen_dashboard_notice' );
    update_user_meta( get_current_user_id(), '_aimogen_dashboard_notice_dismissed', 1 );
    wp_die();
});

/**
 * Setup wizard "1-click first result": generate a short article inline so new
 * users see Aimogen working within seconds of adding their API key.
 */
add_action( 'wp_ajax_aimogen_wizard_first_result', 'aimogen_wizard_first_result' );
function aimogen_wizard_first_result() {
    if ( ! current_user_can( 'access_aiomatic_menu' ) && ! current_user_can( 'manage_options' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }
    check_ajax_referer( 'aimogen_wizard_first_result', 'nonce' );

    if ( ! function_exists( 'aimogen' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'The Aimogen AI API is not available on this site.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }

    $topic = isset( $_POST['topic'] ) ? sanitize_text_field( wp_unslash( $_POST['topic'] ) ) : '';
    if ( $topic === '' ) {
        $topic = wp_strip_all_tags( get_bloginfo( 'name' ) );
    }
    if ( $topic === '' ) {
        $topic = esc_html__( 'my website', 'aiomatic-automatic-ai-content-writer' );
    }

    $prompt = sprintf(
        "You are an expert SEO content writer. Write a comprehensive, in-depth, fully SEO-optimized article about \"%s\".\n\n"
        . "Requirements:\n"
        . "- Make it as detailed and thorough as possible: aim for at least 1200 words.\n"
        . "- Begin directly with a compelling introduction paragraph that hooks the reader and naturally includes the main keyword. Do NOT repeat the title as a heading (the title is handled separately).\n"
        . "- Organize the body into several logical sections, each introduced by a descriptive, keyword-aware <h2> heading. Use <h3> subheadings where it helps structure.\n"
        . "- Cover the topic from multiple angles, such as an overview, key benefits, practical tips, step-by-step guidance, common mistakes to avoid, and comparisons where relevant.\n"
        . "- Use <ul> or <ol> lists where appropriate to improve readability and scannability.\n"
        . "- Include a \"Frequently Asked Questions\" section (as an <h2>) with 4-5 questions as <h3> headings, each with a concise, helpful answer.\n"
        . "- End with an <h2>Conclusion</h2> section that summarizes the key takeaways.\n"
        . "- Write in a natural, engaging, human tone. Use the main keyword and semantically related terms naturally for SEO, without keyword stuffing.\n"
        . "- Write like a real human, not like an AI. Don't use excessive amount of lists, don't use AI specific wording and characters, write like a real human.\n"
        . "- Output clean, valid HTML only (headings, paragraphs, lists, and inline emphasis). Do NOT include <html>, <head>, <body> or <h1> tags, inline styles, or markdown code fences.",
        $topic
    );

    $response = aimogen()->ask( $prompt, array(
        'env'         => 'setupWizardFirstResult',
        'temperature' => 0.75,
        'max_tokens'  => 4000,
    ) );

    if ( ! is_object( $response ) || ! method_exists( $response, 'failed' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'Unexpected response from the AI engine.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }

    if ( $response->failed() ) {
        wp_send_json_error( array( 'message' => $response->error() ) );
    }

    $html = trim( (string) $response->text() );
    // Strip stray markdown fences some models add despite the instruction.
    $html = preg_replace( '/^```[a-zA-Z]*\s*/', '', $html );
    $html = preg_replace( '/\s*```$/', '', $html );

    $safe_html = wp_kses_post( $html );

    if ( trim( wp_strip_all_tags( $safe_html ) ) === '' ) {
        wp_send_json_error( array( 'message' => esc_html__( 'The AI returned an empty result. Please try again.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }

    wp_send_json_success( array(
        'topic' => $topic,
        'html'  => $safe_html,
        'model' => $response->meta( 'model' ),
    ) );
}

/**
 * Setup wizard: save a generated first result as a draft post.
 */
add_action( 'wp_ajax_aimogen_wizard_save_draft', 'aimogen_wizard_save_draft' );
function aimogen_wizard_save_draft() {
    if ( ! current_user_can( 'edit_posts' ) ) {
        wp_send_json_error( array( 'message' => esc_html__( 'You are not allowed to create posts.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }
    check_ajax_referer( 'aimogen_wizard_first_result', 'nonce' );

    $title   = isset( $_POST['topic'] ) ? sanitize_text_field( wp_unslash( $_POST['topic'] ) ) : '';
    $content = isset( $_POST['html'] ) ? wp_kses_post( wp_unslash( $_POST['html'] ) ) : '';

    if ( trim( wp_strip_all_tags( $content ) ) === '' ) {
        wp_send_json_error( array( 'message' => esc_html__( 'Nothing to save yet. Generate a result first.', 'aiomatic-automatic-ai-content-writer' ) ) );
    }
    if ( $title === '' ) {
        $title = esc_html__( 'My first Aimogen post', 'aiomatic-automatic-ai-content-writer' );
    }

    $post_id = wp_insert_post( array(
        'post_title'   => $title,
        'post_content' => $content,
        'post_status'  => 'draft',
        'post_type'    => 'post',
    ), true );

    if ( is_wp_error( $post_id ) || ! $post_id ) {
        $message = is_wp_error( $post_id )
            ? $post_id->get_error_message()
            : esc_html__( 'Could not create the draft.', 'aiomatic-automatic-ai-content-writer' );
        wp_send_json_error( array( 'message' => $message ) );
    }

    wp_send_json_success( array(
        'post_id'  => $post_id,
        'edit_url' => get_edit_post_link( $post_id, 'raw' ),
    ) );
}
add_action('wp_ajax_aiomatic_live_log_details', function () {
    if (
        !isset($_POST['nonce']) ||
        !wp_verify_nonce($_POST['nonce'], 'aiomatic_live_feed')
    ) {
        wp_send_json_error();
    }

    if (!current_user_can('manage_options')) {
        wp_send_json_error();
    }

    $log_id = isset($_POST['log_id']) ? intval($_POST['log_id']) : 0;
    if ($log_id <= 0) {
        wp_send_json_error();
    }

    global $wpdb;
    $table = $wpdb->prefix . 'aiomatic_logmeta';

    $rows = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT meta_key, meta_value FROM $table WHERE log_id = %d",
            $log_id
        ),
        ARRAY_A
    );

    if (empty($rows)) {
        wp_send_json_success([
            'html' => '<em>No detailed data stored.</em>'
        ]);
    }

    $out = '<div class="aiomatic-log-detail-wrap">';

    foreach ($rows as $row) {
        $val = maybe_unserialize($row['meta_value']);

        $out .= '<div class="aiomatic-log-detail">';
        $out .= '<strong>' . esc_html($row['meta_key']) . '</strong><br/>';

        if (is_array($val) || is_object($val)) {
            $out .= '<pre>' . esc_html(wp_json_encode($val, JSON_PRETTY_PRINT)) . '</pre>';
        } else {
            $out .= '<pre>' . esc_html((string)$val) . '</pre>';
        }

        $out .= '</div>';
    }

    $out .= '</div>';

    wp_send_json_success([
        'html' => $out
    ]);
});
add_action('wp_ajax_aiomatic_delete_log', function () {

    if (!current_user_can('manage_options')) {
        wp_send_json_error(['message' => 'Permission denied']);
    }

    if (
        empty($_POST['nonce']) ||
        !wp_verify_nonce($_POST['nonce'], 'openai-ajax-nonce')
    ) {
        wp_send_json_error(['message' => 'Invalid nonce']);
    }

    $log_id = intval($_POST['log_id'] ?? 0);
    if (!$log_id) {
        wp_send_json_error(['message' => 'Invalid log id']);
    }

    global $aiomatic_stats;

    if ($aiomatic_stats->delete_log($log_id)) {
        wp_send_json_success();
    }

    wp_send_json_error(['message' => 'Delete failed']);
});
add_action('wp_ajax_aiomatic_get_terms', 'aiomatic_get_terms_cb');
function aiomatic_get_terms_cb() {
    check_ajax_referer('openai-run-nonce', 'nonce');

    $post_type = isset($_POST['post_type']) ? sanitize_key($_POST['post_type']) : 'post';
    $taxonomy  = ($post_type === 'product') ? 'product_cat' : 'category';

    $terms = get_terms(array(
        'taxonomy'   => $taxonomy,
        'orderby'    => 'name',
        'hide_empty' => 0,
        'order'      => 'ASC',
    ));

    if (is_wp_error($terms)) {
        wp_send_json_error(array('message' => $terms->get_error_message()));
    }

    $out = array();
    foreach ($terms as $t) {
        $out[] = array(
            'id'   => (int) $t->term_id,
            'name' => sanitize_text_field($t->name),
        );
    }

    wp_send_json_success($out);
}

add_action('wp_ajax_nopriv_aiomatic_realtime_session', 'aiomatic_realtime_session');
add_action('wp_ajax_aiomatic_realtime_session', 'aiomatic_realtime_session');
function aiomatic_realtime_session() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        wp_send_json_error(['status' => 'error', 'message' => 'Azure/Claude API is not currently supported for realtime chat API.'], 500);
    }
    if (!isset($aiomatic_Main_Settings['app_id'])) 
    {
        $aiomatic_Main_Settings['app_id'] = '';
    }
    if (isset($_POST['model']) && trim($_POST['model']) != '') 
    {
        $model = trim($_POST['model']);
        if(!in_array($model, AIMOGEN_REALTIME_MODELS))
        {
            wp_send_json_error(['status' => 'error', 'message' => 'Incorrect Realtime model sent: ' . $model], 500);
        }
    }
    else
    {
        $model = AIMOGEN_DEFAULT_REALTIME_MODEL;
    }
    if (isset($_POST['voice']) && trim($_POST['voice']) != '') 
    {
        $voice = trim($_POST['voice']);
    }
    else
    {
        $voice = 'verse';
    }
    if (isset($_POST['instructions']) && trim($_POST['instructions']) != '') 
    {
        $instructions = trim($_POST['instructions']);
    }
    else
    {
        $instructions = '';
    }
    if (isset($_POST['temperature']) && trim($_POST['temperature']) != '') 
    {
        $temperature = trim($_POST['temperature']);
    }
    else
    {
        $temperature = '';
    }
    if (isset($_POST['max_tokens']) && trim($_POST['max_tokens']) != '') 
    {
        $max_tokens = trim($_POST['max_tokens']);
    }
    else
    {
        $max_tokens = '';
    }
    if(!in_array($voice, AIMOGEN_REALTIME_VOICES))
    {
        $voice = 'verse';
    }
    if (isset($_POST['user_token_cap_per_day']) && trim($_POST['user_token_cap_per_day']) != '') 
    {
        $user_token_cap_per_day = trim($_POST['user_token_cap_per_day']);
    }
    else
    {
        $user_token_cap_per_day = '';
    }
    if (isset($_POST['user_id']) && trim($_POST['user_id']) != '') 
    {
        $user_id = trim($_POST['user_id']);
    }
    else
    {
        $user_id = '';
    }
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $dummy_query = new stdClass();
    $ok = apply_filters( 'aiomatic_ai_allowed', true, $aiomatic_Limit_Settings, $dummy_query );
    if ( $ok !== true ) {
        wp_send_json_error(['status' => 'usage', 'message' => esc_html__('Chatbot is rate limited:', 'aiomatic-automatic-ai-content-writer') . ' ' . $ok], 500);
    }
    $used_token_count = 0;
    if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            wp_send_json_error(['status' => 'usage', 'message' => sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) )]);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_realtime_chat_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                wp_send_json_error(['status' => 'usage', 'message' => esc_html__('Daily token count of your user account was exceeded! Please try again tomorrow.', 'aiomatic-automatic-ai-content-writer')], 500);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    $functions = [];
    require_once(dirname(__FILE__) . "/aiomatic-god-mode.php");
    $god_mode = false;
    $ai = false;
    $lead = false;
    $dalle = false;
    $toubiz = false;
    $google_image = false;
    $stable = false;
    $midjourney = false;
    $replicate = false;
    $ideogram = false;
    $cloudflare = false;
    $clickbank = false;
    $amazon = false;
    $amazon_details = false;
    $booking = false;
    $scraper = false;
    $rss = false;
    $google = false;
    $captions = false;
    $royalty = false;
    $youtube = false;
    $email = false;
    $facebook = false;
    $facebook_image = false;
    $twitter = false;
    $instagram = false;
    $pinterest = false;
    $business = false;
    $youtube_community = false;
    $reddit = false;
    $linkedin = false;
    $webhook = false;
    $stable_video = false;
    $database = false;
    $chart = false;
    $threads = false;
    $end = false;
    $omniblock = false;
    if(isset($_POST['enable_god_mode']))
    {
        if($_POST['enable_god_mode'] === 'disabled' || $_POST['enable_god_mode'] === 'off')
        {
            $_POST['enable_god_mode'] = '';
        }
        if(is_string($_POST['enable_god_mode']) && !empty($_POST['enable_god_mode']))
        {
            $forced_functions = explode(',', $_POST['enable_god_mode']);
            if(in_array('god_mode_ai', $forced_functions))
            {
                $ai = true;
            }
            if(in_array('god_mode_enable_chart', $forced_functions))
            {
                $chart = true;
            }
            if(in_array('god_mode_enable_end', $forced_functions))
            {
                $end = true;
            }
            if(in_array('god_mode_enable_dalle', $forced_functions))
            {
                $dalle = true;
            }
            if(in_array('god_mode_enable_midjourney', $forced_functions))
            {
                $midjourney = true;
            }
            if(in_array('god_mode_enable_replicate', $forced_functions))
            {
                $replicate = true;
            }
            if(in_array('god_mode_enable_ideogram', $forced_functions))
            {
                $ideogram = true;
            }
            if(in_array('god_mode_enable_cloudflare', $forced_functions))
            {
                $cloudflare = true;
            }
            if(in_array('god_mode_enable_stable', $forced_functions))
            {
                $stable = true;
            }
            if(in_array('god_mode_enable_stable_video', $forced_functions))
            {
                $stable_video = true;
            }
            if(in_array('god_mode_enable_clickbank', $forced_functions))
            {
                $clickbank = true;
            }
            if(in_array('god_mode_enable_amazon', $forced_functions))
            {
                $amazon = true;
            }
            if(in_array('god_mode_enable_amazon_details', $forced_functions))
            {
                $amazon_details = true;
            }
            if(in_array('god_mode_enable_booking', $forced_functions))
            {
                $booking = true;
            }
            if(in_array('god_mode_enable_scraper', $forced_functions))
            {
                $scraper = true;
            }
            if(in_array('god_mode_enable_rss', $forced_functions))
            {
                $rss = true;
            }
            if(in_array('god_mode_enable_google', $forced_functions))
            {
                $google = true;
            }
            if(in_array('god_mode_enable_youtube_captions', $forced_functions))
            {
                $captions = true;
            }
            if(in_array('god_mode_enable_database', $forced_functions))
            {
                $database = true;
            }
            if(in_array('god_mode_enable_omniblock', $forced_functions))
            {
                $omniblock = true;
            }
            if(in_array('god_mode_enable_royalty', $forced_functions))
            {
                $royalty = true;
            }
            if(in_array('god_mode_lead_capture', $forced_functions))
            {
                $lead = true;
            }
            if(in_array('god_mode_enable_youtube', $forced_functions))
            {
                $youtube = true;
            }
            if(in_array('god_mode_enable_email', $forced_functions))
            {
                $email = true;
            }
            if(in_array('god_mode_enable_webhook', $forced_functions))
            {
                $webhook = true;
            }
            if(in_array('god_mode_enable_wp', $forced_functions))
            {
                $god_mode = true;
            }
            if(in_array('god_mode_enable_facebook_post', $forced_functions))
            {
                $facebook = true;
            }
            if(in_array('god_mode_enable_twitter_post', $forced_functions))
            {
                $twitter = true;
            }
            if(in_array('god_mode_enable_threads_post', $forced_functions))
            {
                $threads = true;
            }
            if(in_array('god_mode_enable_instagram_post', $forced_functions))
            {
                $instagram = true;
            }
            if(in_array('god_mode_enable_pinterest_post', $forced_functions))
            {
                $pinterest = true;
            }
            if(in_array('god_mode_enable_google_post', $forced_functions))
            {
                $business = true;
            }
            if(in_array('god_mode_enable_youtube_post', $forced_functions))
            {
                $youtube_community = true;
            }
            if(in_array('god_mode_enable_reddit_post', $forced_functions))
            {
                $reddit = true;
            }
            if(in_array('god_mode_enable_linkedin_post', $forced_functions))
            {
                $linkedin = true;
            }
            if(in_array('god_mode_enable_toubiz', $forced_functions))
            {
                $toubiz = true;
            }
            if(in_array('god_mode_enable_google_image', $forced_functions))
            {
                $google_image = true;
            }
        }
    }
    $functions = aiomatic_return_god_function_realtime($god_mode, $ai, $lead, $dalle, $stable, $midjourney, $replicate, $ideogram, $amazon, $amazon_details, $scraper, $rss, $google, $captions, $royalty, $youtube, $email, $facebook, $facebook_image, $twitter, $threads, $instagram, $pinterest, $business, $youtube_community, $reddit, $linkedin, $webhook, $stable_video, $database, $chart, $toubiz, $google_image, $end, $omniblock, $booking, $clickbank, $cloudflare);
    
    $input_params = [
        'model' => $model,
        'voice' => $voice,
    ];
    if($temperature !== '' && is_numeric($temperature))
    {
        $input_params['temperature'] = floatval($temperature);
    }
    if(!empty($instructions))
    {
        $input_params['instructions'] = $instructions;
    }
    if(!empty($max_tokens))
    {
        $input_params['max_response_output_tokens'] = intval($max_tokens);
    }
    if(!empty($functions))
    {
        $input_params['tools'] = $functions;
        $input_params['tool_choice'] = 'auto';
    }
    $do_transcript = false;
    if($do_transcript === true)
    {
        $input_params['input_audio_transcription'] = array('model' => 'whisper-1');
    }
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $response = wp_remote_post('https://api.openai.com/v1/realtime/sessions', [
        'headers' => [
            'Authorization' => 'Bearer ' . $token,
            'Content-Type'  => 'application/json',
        ],
        'body' => json_encode($input_params),
    ]);
    if (is_wp_error($response)) {
        wp_send_json_error(['status' => 'error', 'message' => $response->get_error_message()], 500);
    }
    $body = wp_remote_retrieve_body($response);
    $data = json_decode($body, true);
    if (isset($data['error'])) {
        wp_send_json_error(['status' => 'error', 'message' => $data['error']['message']], 500);
    }
    wp_send_json_success(['client_secret' => $data['client_secret']]);
}

add_action( 'wp_ajax_aiomatic_write_tax_description', 'aiomatic_write_tax_description' );
function aiomatic_write_tax_description() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['tag_ID']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (tag_ID)' ) );
        exit;
	}
    $tag_ID = $_POST['tag_ID'];
    if(!isset($_POST['taxonomy']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (taxonomy)' ) );
        exit;
	}
    $taxonomy = $_POST['taxonomy'];
    $my_term = get_term_by('id', $tag_ID, $taxonomy);
    if($my_term == false)
	{
		wp_send_json_error( array( 'message' => 'Taxonomy ID not found: ' . print_r($tag_ID, true) ) );
        exit;
	}
    $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
    
    if (isset($aiomatic_Spinner_Settings['tax_description_prompt']) && trim($aiomatic_Spinner_Settings['tax_description_prompt']) != '') 
    {
        $prompt = trim($aiomatic_Spinner_Settings['tax_description_prompt']);
    }
    else
    {
        $prompt = 'Write a description for a WordPress %%term_taxonomy_name%% with the following title: "%%term_name%%"';
    }
    if (isset($aiomatic_Spinner_Settings['tax_description_model']) && trim($aiomatic_Spinner_Settings['tax_description_model']) != '') 
    {
        $model = trim($aiomatic_Spinner_Settings['tax_description_model']);
    }
    else
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    if (isset($aiomatic_Spinner_Settings['tax_assistant_id']) && trim($aiomatic_Spinner_Settings['tax_assistant_id']) != '') 
    {
        $tax_assistant_id = trim($aiomatic_Spinner_Settings['tax_assistant_id']);
    }
    else
    {
        $tax_assistant_id = '';
    }
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $prompt = str_replace('%%term_id%%', $my_term->term_id, $prompt);
    $prompt = str_replace('%%term_name%%', $my_term->name, $prompt);
    $prompt = str_replace('%%term_slug%%', $my_term->slug, $prompt);
    $prompt = str_replace('%%term_description%%', $my_term->description, $prompt);
    $prompt = str_replace('%%term_taxonomy_name%%', $my_term->taxonomy, $prompt);
    $prompt = str_replace('%%term_taxonomy_id%%', $my_term->term_taxonomy_id, $prompt);
	$query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            exit;
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, 1, 1, 0, 0, false, 'taxonomyDescriptionWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $tax_assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
        exit;
	}
	else
	{
		$new_post_content = aiomatic_sanitize_ai_result($generated_text);
	}
    do_action('aiomatic_tax_description_reply', $new_post_content);
    wp_send_json_success( array('content' => $new_post_content) );
    die();
}

add_action('wp_ajax_aiomatic_dismiss_notice', 'aiomatic_dismiss_notice');
function aiomatic_dismiss_notice()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if ( isset($_POST['notice_id']) ) {
        $user_id = get_current_user_id();
        $notice_id = sanitize_text_field( $_POST['notice_id'] );
        update_user_meta( $user_id, $notice_id . '_dismissed', true );
        wp_send_json_success();
    }
    else
    {
        wp_send_json_error();
    }
    die();
}
add_action('wp_ajax_aiomatic_activation', 'aiomatic_activation');
function aiomatic_activation()
{
    if(!wp_verify_nonce( $_POST['nonce'], 'activation-secret-nonce'))
    {
        echo 'You are not allowed to do this action!';
        die();
    }
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $code                 = $_POST['code'];
    $plugin = plugin_basename(__FILE__);
    $plugin_slug = explode('/', $plugin);
    $plugin_slug = $plugin_slug[0];
    if((aiomatic_strlen(trim($code)) != 36 && aiomatic_strlen(trim($code)) != 41) || strstr($code, '-') == false)
    {
        aiomatic_log_to_file('Invalid registration code submitted');
        echo 'Invalid registration code submitted!';
        die();
    }
    else
    {
        $ch = curl_init('https://wpinitiate.com/verify-purchase/purchase.php');
        if($ch !== false)
        {
            $data           = array();
            $data['code']   = trim($code);
            $data['siteURL']   = get_bloginfo('url');
            $data['siteName']   = get_bloginfo('name');
            $data['siteEmail']   = get_bloginfo('admin_email');
            $fdata = "";
            foreach ($data as $key => $val) {
                $fdata .= "$key=" . urlencode(trim($val)) . "&";
            }
            curl_setopt($ch, CURLOPT_POST, 1);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fdata);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
            curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36');
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_TIMEOUT, 60);
            $result = curl_exec($ch);
            if($result === false)
            {
                curl_close($ch);
                aiomatic_log_to_file('Failed to get verification response: ' . curl_error($ch));
                echo 'Failed to get verification response: ' . esc_html(curl_error($ch));
                die();
            }
            else
            {
                $rj = json_decode($result, true);
                if(isset($rj['error']))
                {
                    echo esc_html($rj['error']);
                    die();
                }
                elseif(isset($rj['item_name']))
                {
                    $rj['code'] = $code;
                    if($rj['item_id'] == '7323' || $rj['item_id'] == '38877369' || $rj['item_id'] == '13371337' || $rj['item_id'] == '19200046')
                    {
                        if (is_multisite()) 
                        {
                            $main_site_id = get_network()->site_id;
                            switch_to_blog($main_site_id);
                            aiomatic_update_option($plugin_slug . '_registration', $rj);
                            restore_current_blog();
                        } 
                        else 
                        {
                            aiomatic_update_option($plugin_slug . '_registration', $rj);
                        }
                    }
                    else
                    {
                        aiomatic_log_to_file('Invalid response from purchase code verification (are you sure you submitted the right purchase code?): ' . print_r($rj, true));
                        echo 'Invalid response from purchase code verification (are you sure you submitted the right purchase code?): ' . esc_html(print_r($rj, true));
                        die();
                    }
                }
                else
                {
                    aiomatic_log_to_file('Invalid json from purchase code verification: ' . print_r($result, true));
                    echo 'Invalid json from purchase code verification: ' . esc_html(print_r($result, true));
                    die();
                }
            }
            curl_close($ch);
        }
        else
        {
            aiomatic_log_to_file('Failed to init curl when trying to make purchase verification.');
            echo 'Failed to init curl!';
            die();
        }
    }
    echo 'ok';
    die();
}
add_action('wp_ajax_aiomatic_dismiss_expiration_notice', function () {
    check_ajax_referer('aiomatic-dismiss', 'nonce');

    if (!current_user_can('manage_options')) {
        wp_send_json_error();
    }

    $notice = sanitize_text_field($_POST['notice'] ?? '');
    if (!$notice) {
        wp_send_json_error();
    }

    update_user_meta(get_current_user_id(), $notice, 1);

    wp_send_json_success();
});

add_action('wp_ajax_aiomatic_clear_data', 'aiomatic_clear_plugin_data');
function aiomatic_clear_plugin_data() {
    check_ajax_referer('aiomatic_clear_data_nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if (!defined('DOING_AJAX'))
    {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(isset($_POST['wipe_data']) && $_POST['wipe_data'] === '1')
	{
        delete_option('aiomatic_Main_Settings');
        delete_option('aiomatic_Spinner_Settings');
        delete_option('aiomatic_Chatbot_Settings');
        delete_option('aiomatic_Limit_Settings');
        delete_option('aiomatic_rules_list');
        delete_option('aiomatic_forum_list');
        delete_option('aiomatic_reply_list');
        delete_option('aiomatic_youtube_list');
        delete_option('aiomatic_amazon_list');
        delete_option('aiomatic_review_list');
        delete_option('aiomatic_csv_list');
        delete_option('aiomatic_omni_list');
        delete_option('aiomatic_listicle_list');
        delete_option('aiomatic_deployments_list');
        delete_option('aiomatic_posts_per_page');
        delete_option('aiomatic_elevenlabs');
        delete_option('aiomatic_google_voices');
        delete_option('aiomatic_setup_wizard_ran');
        delete_option('aiomatic_openrouter_model_list');
        delete_option('aiomatic_replicate_model_list');
        delete_option('aiomatic_dafault_omni_template');
        delete_option('aiomatic_templates');
        delete_option('aiomatic_realtime_chat_page_id');
        delete_option('aiomatic_do_post_uniqid');
        delete_option('aiomatic_processed_keywords');
        delete_option('aiomatic_ollama_embedding_models');
        delete_option('aiomatic_ollama_models');
        delete_option('aiomatic_running_list');
        delete_option('aiomatic_last_time');
        delete_option('headless_calls');
        delete_option('crspinrewriter_spin_time');
        delete_option('aiomatic_chat_page_id');
        delete_option('aiomatic_custom_models');
        delete_option('aiomatic_Menu_Rules');
        delete_option('aiomatic_image_cards_order');
        delete_option('aiomatic_serp_cards_order');
        delete_option('aiomatic_keyword_list');
        delete_option('aiomatic_assistant_list');
        delete_option('aiomatic_huggingface_models');
        delete_option('aiomatic_Editor_Rules');
        delete_option('coderevo_translate_alt');
    }
    if(isset($_POST['revoke']) && $_POST['revoke'] === '1')
    {
        $plugin = plugin_basename(__FILE__);
        $plugin_slug = explode('/', $plugin);
        $plugin_slug = $plugin_slug[0];
        $ch = curl_init('https://wpinitiate.com/verify-purchase/revoke.php');
        if($ch !== false)
        {
            $data           = array();
            $data['siteURL']   = get_bloginfo('url');
            $purchase_code = '';
            $uoptions = array();
            $is_activated = aiomatic_is_activated($plugin_slug, $uoptions);
            if($is_activated === true || $is_activated === 2 || $is_activated === -2 || $is_activated === -3)
            {
                if(isset($uoptions['code']))
                {
                    $purchase_code = $uoptions['code'];
                }
                if(!empty($purchase_code))
                {
                    $data['purchaseCode']   = $purchase_code;
                }
                else
                {
                    if (is_multisite()) 
                    {
                        $main_site_id = get_network()->site_id;
                        switch_to_blog($main_site_id);
                        aiomatic_update_option($plugin_slug . '_registration', false);
                        restore_current_blog();
                    } 
                    else 
                    {
                        aiomatic_update_option($plugin_slug . '_registration', false);
                    }
                    echo 'ok';
                    die();
                }

                $fdata = "";
                foreach ($data as $key => $val) {
                    $fdata .= "$key=" . urlencode(trim($val)) . "&";
                }
                curl_setopt($ch, CURLOPT_POST, 1);
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fdata);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
                curl_setopt($ch, CURLOPT_TIMEOUT, 60);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
                curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
                $result = curl_exec($ch);
                if($result === false)
                {
                    aiomatic_log_to_file('Failed to revoke verification response: ' . curl_error($ch));
                }
                curl_close($ch);
                if (is_multisite()) 
                {
                    $main_site_id = get_network()->site_id;
                    switch_to_blog($main_site_id);
                    aiomatic_update_option($plugin_slug . '_registration', false);
                    restore_current_blog();
                } 
                else 
                {
                    aiomatic_update_option($plugin_slug . '_registration', false);
                }
            }
            else
            {
                echo 'ok';
                wp_die();
            }
        }
        else
        {
            aiomatic_log_to_file('Failed to init curl to revoke verification response.');
            echo 'Failed to init curl!';
            die();
        }
    }
    echo 'ok';
    wp_die();
}
add_action('wp_ajax_aiomatic_revoke', 'aiomatic_revoke');
function aiomatic_revoke()
{
    if(!wp_verify_nonce($_POST['nonce'], 'activation-secret-nonce'))
    {
        echo 'You are not allowed to do this action!';
        die();
    }
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $plugin = plugin_basename(__FILE__);
    $plugin_slug = explode('/', $plugin);
    $plugin_slug = $plugin_slug[0];
    $ch = curl_init('https://wpinitiate.com/verify-purchase/revoke.php');
    if($ch !== false)
    {
        $data           = array();
        $data['siteURL']   = get_bloginfo('url');

        $purchase_code = '';
        $uoptions = array();
        aiomatic_is_activated($plugin_slug, $uoptions);
        if(isset($uoptions['code']))
        {
            $purchase_code = $uoptions['code'];
        }
        if(!empty($purchase_code))
        {
            $data['purchaseCode']   = $purchase_code;
        }
        else
        {
            if (is_multisite()) 
            {
                $main_site_id = get_network()->site_id;
                switch_to_blog($main_site_id);
                aiomatic_update_option($plugin_slug . '_registration', false);
                restore_current_blog();
            } 
            else 
            {
                aiomatic_update_option($plugin_slug . '_registration', false);
            }
            echo 'ok';
            die();
        }

        $fdata = "";
        foreach ($data as $key => $val) {
            $fdata .= "$key=" . urlencode(trim($val)) . "&";
        }
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $fdata);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        $result = curl_exec($ch);
        if($result === false)
        {
            aiomatic_log_to_file('Failed to revoke verification response: ' . curl_error($ch));
        }
        curl_close($ch);
        if (is_multisite()) 
        {
            $main_site_id = get_network()->site_id;
            switch_to_blog($main_site_id);
            aiomatic_update_option($plugin_slug . '_registration', false);
            restore_current_blog();
        } 
        else 
        {
            aiomatic_update_option($plugin_slug . '_registration', false);
        }
    }
    else
    {
        aiomatic_log_to_file('Failed to init curl to revoke verification response.');
        echo 'Failed to init curl!';
        die();
    }
    echo 'ok';
    die();
}
add_action( 'wp_ajax_aiomatic_write_tax_description_manual', 'aiomatic_write_tax_description_manual' );
function aiomatic_write_tax_description_manual() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', false);
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Spinner_Settings['tax_description_manual']) && trim($aiomatic_Spinner_Settings['tax_description_manual']) != '') 
    {
        $taxonomy = trim($aiomatic_Spinner_Settings['tax_description_manual']);
    }
    else
    {
        $taxonomy = 'category';
    }
    if (isset($aiomatic_Spinner_Settings['max_tax_nr']) && trim($aiomatic_Spinner_Settings['max_tax_nr']) != '') 
    {
        $max_tax_nr = intval(trim($aiomatic_Spinner_Settings['max_tax_nr']));
    }
    else
    {
        $max_tax_nr = 5;
    }
    if (isset($aiomatic_Spinner_Settings['overwite_tax']) && trim($aiomatic_Spinner_Settings['overwite_tax']) == 'on')
    {
        $overwite_tax = true;
    }
    else
    {
        $overwite_tax = false;
    }
    $process_once = isset($aiomatic_Spinner_Settings['tax_process_once']) && $aiomatic_Spinner_Settings['tax_process_once'] === 'on';
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
    
    if (isset($aiomatic_Spinner_Settings['tax_description_prompt']) && trim($aiomatic_Spinner_Settings['tax_description_prompt']) != '') 
    {
        $prompt = trim($aiomatic_Spinner_Settings['tax_description_prompt']);
    }
    else
    {
        $prompt = 'Write a description for a WordPress %%term_taxonomy_name%% with the following title: "%%term_name%%"';
    }
    if (isset($aiomatic_Spinner_Settings['tax_description_model']) && trim($aiomatic_Spinner_Settings['tax_description_model']) != '') 
    {
        $model = trim($aiomatic_Spinner_Settings['tax_description_model']);
    }
    else
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    if (isset($aiomatic_Spinner_Settings['tax_assistant_id']) && trim($aiomatic_Spinner_Settings['tax_assistant_id']) != '') 
    {
        $tax_assistant_id = trim($aiomatic_Spinner_Settings['tax_assistant_id']);
    }
    else
    {
        $tax_assistant_id = '';
    }
    $hide_empty = isset($aiomatic_Spinner_Settings['tax_hide_empty']) && $aiomatic_Spinner_Settings['tax_hide_empty'] === 'on';
    $orderby = !empty($aiomatic_Spinner_Settings['tax_orderby']) ? $aiomatic_Spinner_Settings['tax_orderby'] : 'count';
    $order   = !empty($aiomatic_Spinner_Settings['tax_order']) ? $aiomatic_Spinner_Settings['tax_order'] : 'DESC';
    
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $args = array(
        'taxonomy'   => $taxonomy,
        'hide_empty' => $hide_empty,
        'number'     => $max_tax_nr,
    );
    $args['orderby'] = $orderby;
    $args['order']   = $order;
    if (isset($aiomatic_Spinner_Settings['tax_parent_only']) && $aiomatic_Spinner_Settings['tax_parent_only'] === 'on') {
        $args['parent'] = 0;
    }
    if (isset($aiomatic_Spinner_Settings['tax_childless']) && $aiomatic_Spinner_Settings['tax_childless'] === 'on') {
        $args['childless'] = true;
    }
    if (!empty($aiomatic_Spinner_Settings['tax_include_ids'])) {
        $args['include'] = array_map('intval', explode(',', $aiomatic_Spinner_Settings['tax_include_ids']));
    }

    if (!empty($aiomatic_Spinner_Settings['tax_exclude_ids'])) {
        $args['exclude'] = array_map('intval', explode(',', $aiomatic_Spinner_Settings['tax_exclude_ids']));
    }
    if (!empty($aiomatic_Spinner_Settings['tax_search'])) {
        $args['search'] = sanitize_text_field($aiomatic_Spinner_Settings['tax_search']);
    }
    $args['update_term_meta_cache'] = false;
    if ($process_once) {
        $args['meta_query'] = array(
            array(
                'key'     => '_aiomatic_desc_generated',
                'compare' => 'NOT EXISTS',
            ),
        );
    }
    $filtered_terms = [];
    $terms = get_terms($args);
    if (!empty($terms) && !is_wp_error($terms)) 
    {
        foreach ($terms as $term) 
        {
            if($overwite_tax === true)
            {
                $filtered_terms[] = $term; 
                if (count($filtered_terms) >= $max_tax_nr) break;
            }
            else
            {
                if (empty($term->description)) 
                {
                    $filtered_terms[] = $term; 
                    if (count($filtered_terms) >= $max_tax_nr) break;
                }
            }
        }
    } 
    else 
    {
        if(is_wp_error($terms))
        {
            wp_send_json_error( array( 'message' => 'An error occurred: ' . $terms->get_error_message()) );
            exit;
        }
        wp_send_json_error( array( 'message' => 'No ' . $taxonomy . ' terms found without a description.' ) );
        exit;
    }
    if(count($filtered_terms) == 0)
    {
        wp_send_json_error( array( 'message' => 'No ' . $taxonomy . ' tax terms found without a description.' ) );
        exit;
    }
    $processed_list = array();
    foreach ($filtered_terms as $my_term) 
    {
        if(in_array($my_term->term_id, $processed_list))
        {
            continue;
        }
        $processed_list[] = $my_term->term_id;
        $thisprompt = str_replace('%%term_id%%', $my_term->term_id, $prompt);
        $thisprompt = str_replace('%%term_name%%', $my_term->name, $thisprompt);
        $thisprompt = str_replace('%%term_slug%%', $my_term->slug, $thisprompt);
        $thisprompt = str_replace('%%term_description%%', $my_term->description, $thisprompt);
        $thisprompt = str_replace('%%term_taxonomy_name%%', $my_term->taxonomy, $thisprompt);
        $thisprompt = str_replace('%%term_taxonomy_id%%', $my_term->term_taxonomy_id, $thisprompt);
        $query_token_count = count(aiomatic_encode($thisprompt));
        $max_tokens = aimogenpro_get_max_tokens($model);
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $thisprompt, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $string_len = aiomatic_strlen($thisprompt);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $aicontent = aiomatic_substr($thisprompt, 0, $string_len);
            $aicontent = trim($aicontent);
            if(empty($aicontent))
            {
                wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
                exit;
            }
            $query_token_count = count(aiomatic_encode($aicontent));
            $available_tokens = $max_tokens - $query_token_count;
        }
        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
            aiomatic_log_to_file('Writing Taxonomy description for: ' . print_r($taxonomy, true) . ' - ' . $my_term->name);
        }
        $thread_id = '';
        $aierror = '';
        $finish_reason = '';
        $generated_text = aiomatic_generate_text($token, $model, $thisprompt, $available_tokens, 1, 1, 0, 0, false, 'taxonomyDescriptionWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $tax_assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
        if($generated_text === false)
        {
            wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
            exit;
        }
        else
        {
            $new_post_content = aiomatic_sanitize_ai_result($generated_text);
        }
        do_action('aiomatic_tax_description_reply', $new_post_content);
        $args = array(
            'description' => $new_post_content,
        );
        $updated_term = wp_update_term($my_term->term_id, $taxonomy, $args);
        if (is_wp_error($updated_term)) {
            wp_send_json_error( array( 'message' => 'An error occurred when updating taxonomy description: ' . $updated_term->get_error_message() ) );
            exit;
        }
        update_term_meta($my_term->term_id, '_aiomatic_desc_generated', 1);
        if (isset($aiomatic_Spinner_Settings['tax_seo_auto']))
        {
            if($aiomatic_Spinner_Settings['tax_seo_auto'] == 'copy')
            {
                aiomatic_save_term_seo_description($my_term->term_id, $new_post_content, $taxonomy);
            }
            elseif($aiomatic_Spinner_Settings['tax_seo_auto'] == 'write')
            {
                $xdescription = aiomatic_auto_write_tax_SEO_description($my_term->term_id, $taxonomy);
                if(!empty($xdescription))
                {
                    aiomatic_save_term_seo_description($my_term->term_id, $xdescription, $taxonomy);
                }
            }
        }
    }
    wp_send_json_success( array('content' => 'ok') );
    die();
}

add_action( 'wp_ajax_aiomatic_refresh_ollama_models', 'aiomatic_refresh_ollama_models' );
function aiomatic_refresh_ollama_models() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['ollama_url']) || trim($aiomatic_Main_Settings['ollama_url']) == '') 
    {
        wp_send_json_error(array( 'message' => 'You need to enter an Ollama API URl for this to work!'));
        exit;
    }
    aiomatic_get_ollama_embedding_models(true);
    $llama_models = aiomatic_get_ollama_models(true);
    if($llama_models !== false)
    {
        wp_send_json_success( array('data' => $llama_models) );
        die();
    }
    else
    {
        wp_send_json_error(array( 'message' => 'Failed to get Ollama models list.'));
        die();
    }
}
add_action( 'wp_ajax_aiomatic_refresh_openrouter_models', 'aiomatic_refresh_openrouter_models' );
function aiomatic_refresh_openrouter_models() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['app_id_openrouter']) || trim($aiomatic_Main_Settings['app_id_openrouter']) == '') 
    {
        wp_send_json_error(array( 'message' => 'You need to enter an OpenRouter API URl for this to work!'));
        exit;
    }
    delete_option('aiomatic_openrouter_model_list');
    wp_send_json_success( array('data' => 'ok') );
    die();
}
add_action( 'wp_ajax_aiomatic_refresh_replicate_models', 'aiomatic_refresh_replicate_models' );
function aiomatic_refresh_replicate_models() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['replicate_app_id']) || trim($aiomatic_Main_Settings['replicate_app_id']) == '') 
    {
        wp_send_json_error(array( 'message' => 'You need to enter an Replicate API URl for this to work!'));
        exit;
    }
    $api_key = trim($aiomatic_Main_Settings['replicate_app_id']);
    $url = 'https://api.replicate.com/v1/collections/text-to-image';
    $response = wp_remote_get($url, array(
        'headers' => array(
            'Authorization' => 'Bearer ' . $api_key
        )
    ));
    if (is_wp_error($response)) {
        aiomatic_log_to_file('Failed to fetch models');
    }
    else
    {
        $body = wp_remote_retrieve_body($response);
        $data = json_decode($body);

        if (isset($data->models)) {
            usort($data->models, function($a, $b) {
                return $b->run_count - $a->run_count;
            });
            $return_data = array();
            foreach ($data->models as $model) 
            {
                $model_name = $model->name ?? '';
                $model_version = $model->latest_version->id ?? '';
                $return_data[$model_version] = $model_name;
            }
            if(count($return_data) == 0)
            {
                aiomatic_log_to_file('No returned models found');
            }
            else
            {
                aiomatic_update_option('aiomatic_replicate_model_list', $return_data);
                wp_send_json_success( array('data' => 'ok') );
            }
        } else {
            aiomatic_log_to_file('No models found');
        }
    }
    wp_send_json_error(array( 'message' => 'Failed to get Replicate models list.'));
    die();
}

add_action( 'wp_ajax_aiomatic_write_aicontent_info', 'aiomatic_write_aicontent_info' );
function aiomatic_write_aicontent_info() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['step']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (step)' ) );
        exit;
	}
    $step = $_POST['step'];
    if(!isset($_POST['title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (title)' ) );
        exit;
	}
    $title = $_POST['title'];
    if(!isset($_POST['model']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (model)' ) );
        exit;
	}
    $model = $_POST['model'];
    if(!isset($_POST['focus']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (focus)' ) );
        exit;
	}
    $aiomatic_ai_focus = $_POST['focus'];
    if(!isset($_POST['assistant_id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (assistant_id)' ) );
        exit;
	}
    $assistant_id = $_POST['assistant_id'];
    if(!isset($_POST['titlep']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (titlep)' ) );
        exit;
	}
    $titlep = $_POST['titlep'];
    if(!isset($_POST['post_id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (post_id)' ) );
        exit;
	}
    $post_id = $_POST['post_id'];
    $vision_file = '';
    if(isset($_POST['vision']))
	{
		$vision = intval($_POST['vision']);
        if($vision == 1)
        {
            if(aiomatic_is_vision_model($model, $assistant_id))
            {
                $vision_file = wp_get_attachment_url(get_post_thumbnail_id($post_id));
                if($vision_file === false)
                {
                    $vision_file = '';
                }
            }
        }
	}
    if(!isset($_POST['seop']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (seop)' ) );
        exit;
	}
    $seop = $_POST['seop'];
    if(!isset($_POST['seo_title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (seo_title)' ) );
        exit;
	}
    $seo_title = $_POST['seo_title'];
    if(!isset($_POST['contentp']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (contentp)' ) );
        exit;
	}
    $contentp = $_POST['contentp'];
    if(!isset($_POST['shortp']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (shortp)' ) );
        exit;
	}
    $shortp = $_POST['shortp'];
    if(!isset($_POST['tagp']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (tagp)' ) );
        exit;
	}
    $tagp = $_POST['tagp'];
    if(!isset($_POST['prod_title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prod_title)' ) );
        exit;
	}
    $prod_title = $_POST['prod_title'];
    if(!isset($_POST['prod_content']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prod_content)' ) );
        exit;
	}
    $prod_content = $_POST['prod_content'];
    if(!isset($_POST['prod_excerpt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prod_excerpt)' ) );
        exit;
	}
    $prod_excerpt = $_POST['prod_excerpt'];
    if(!isset($_POST['post_type']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (post_type)' ) );
        exit;
	}
    $post_type = $_POST['post_type'];
    if(!empty($step))
    {
        if($step == 'title')
        {
            $prompt = $titlep;
        }
        elseif($step == 'meta')
        {
            $prompt = $seop;
        }
        elseif($step == 'metatitle')
        {
            $prompt = $seo_title;
        }
        elseif($step == 'description')
        {
            $prompt = $contentp;
        }
        elseif($step == 'short')
        {
            $prompt = $shortp;
        }
        elseif($step == 'tags')
        {
            $prompt = $tagp;
        }
        elseif($step == 'focus')
        {
            $prompt = $aiomatic_ai_focus;
        }
        else
        {
            wp_send_json_error( array( 'message' => 'Incorrect step sent' . print_r($step, true) ) );
            exit;
        }
    }
    else
    {
        wp_send_json_error( array( 'message' => 'Empty content sent' ) );
        exit;
    }
    if(empty($prompt))
    {
        wp_send_json_error( array( 'message' => 'Empty prompt sent' ) );
        exit;
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    $new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $prompt = str_replace('%%post_title_idea%%', $title, $prompt);
    $prompt = str_replace('%%post_title%%', $prod_title, $prompt);
    $prompt = str_replace('%%post_excerpt%%', $prod_excerpt, $prompt);
    $prompt = str_replace('%%post_content%%', $prod_content, $prompt);
    $prompt = str_replace('%%post_type%%', $post_type, $prompt);
    $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
    $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
    $prompt = aiomatic_replaceSynergyShortcodes($prompt);
    if (isset($aiomatic_Main_Settings['custom_html'])) {
        $prompt = str_replace('%%custom_html%%', $aiomatic_Main_Settings['custom_html'], $prompt);
    }
    if (isset($aiomatic_Main_Settings['custom_html2'])) {
        $prompt = str_replace('%%custom_html2%%', $aiomatic_Main_Settings['custom_html2'], $prompt);
    }
    if($post_id != '')
    {
        preg_match_all('#%%!([^!]*?)!%%#', $prompt, $matched_content);
        if(isset($matched_content[1][0]))
        {
            foreach($matched_content[1] as $mc)
            {
                $post_custom_data = get_post_meta($post_id, $mc, true);
                if($post_custom_data != '')
                {
                    $prompt = str_replace('%%!' . $mc . '!%%', $post_custom_data, $prompt);
                }
                else
                {
                    $prompt = str_replace('%%!' . $mc . '!%%', '', $prompt);
                }
            }
        }
        preg_match_all('#%%!!([^!]*?)!!%%#', $prompt, $matched_content);
        if(isset($matched_content[1][0]))
        {
            foreach($matched_content[1] as $mc)
            {
                $ctaxs = '';
                $terms = get_the_terms( $post_id, $mc );
                if ( ! empty( $terms ) && ! is_wp_error( $terms ) )
                {
                    $ctaxs_arr = array();
                    foreach ( $terms as $term ) {
                        $ctaxs_arr[] = $term->slug;
                    }
                    $ctaxs = implode(',', $ctaxs_arr);
                }
                if($post_custom_data != '')
                {
                    $prompt = str_replace('%%!!' . $mc . '!!%%', $ctaxs, $prompt);
                }
                else
                {
                    $prompt = str_replace('%%!!' . $mc . '!!%%', '', $prompt);
                }
            }
        }
    }
    else
    {
        preg_match_all('#%%!([^!]*?)!%%#', $prompt, $matched_content);
        if(isset($matched_content[1][0]))
        {
            foreach($matched_content[1] as $mc)
            {
                $prompt = str_replace('%%!' . $mc . '!%%', '', $prompt);
            }
        }
        preg_match_all('#%%!!([^!]*?)!!%%#', $prompt, $matched_content);
        if(isset($matched_content[1][0]))
        {
            foreach($matched_content[1] as $mc)
            {
                $prompt = str_replace('%%!!' . $mc . '!!%%', '', $prompt);
            }
        }
    }
    if ( is_user_logged_in() ) 
    {
        $user_id = get_current_user_id();
        if($user_id !== 0)
        {
            preg_match_all('#%%~([^!]*?)~%%#', $prompt, $matched_content);
            if(isset($matched_content[1][0]))
            {
                foreach($matched_content[1] as $mc)
                {
                    $post_custom_data = get_user_meta($user_id, $mc, true);
                    if($post_custom_data != '')
                    {
                        $prompt = str_replace('%%~' . $mc . '~%%', $post_custom_data, $prompt);
                    }
                    else
                    {
                        $prompt = str_replace('%%~' . $mc . '~%%', '', $prompt);
                    }
                }
            }
        }
        else
        {
            preg_match_all('#%%~([^!]*?)~%%#', $prompt, $matched_content);
            if(isset($matched_content[1][0]))
            {
                foreach($matched_content[1] as $mc)
                {
                    $prompt = str_replace('%%~' . $mc . '~%%', '', $prompt);
                }
            }
        }
    } 
    else 
    {
        preg_match_all('#%%~([^!]*?)~%%#', $prompt, $matched_content);
        if(isset($matched_content[1][0]))
        {
            foreach($matched_content[1] as $mc)
            {
                $prompt = str_replace('%%~' . $mc . '~%%', '', $prompt);
            }
        }
    }
    $prompt = preg_replace_callback('#%%random_image_url\[([^\]]*?)\]%%#', function ($matches) {
        $arv = array();
        $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, '', $arv);
        return $my_img;
    }, $prompt);
    $prompt = preg_replace_callback('#%%random_image\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
        if(isset($matches[2]))
        {
            $chance = trim($matches[2], '[]');
        }
        else
        {
            $chance = '';
        }
        $arv = array();
        $my_img = aiomatic_get_random_image_google($matches[1], 0, 0, $chance, $arv);
        return '<img src="' . $my_img . '">';
    }, $prompt);
    $prompt = preg_replace_callback('#%%random_video\[([^\]]*?)\](\[\d+\])?%%#', function ($matches) {
        if(isset($matches[2]))
        {
            $chance = trim($matches[2], '[]');
        }
        else
        {
            $chance = '';
        }
        $my_vid = aiomatic_get_video($matches[1], $chance, array());
        return $my_vid;
    }, $prompt);
    $prompt = apply_filters('aiomatic_replace_aicontent_shortcode', $prompt);
    preg_match_all('#%%related_questions_([^%]*?)%%#i', $prompt, $mxatches);
    if(isset($mxatches[1][0]))
    {
        foreach($mxatches[1] as $googlematch)
        {
            $mtchres = aiomatic_scrape_related_questions($googlematch, 5, '', 1, 1, 0, 0, 2000, '', '');
            if(is_array($mtchres) && !empty($mtchres))
            {
                $quests = array();
                foreach($mtchres as $mra)
                {
                    if(isset($mra['q']))
                    {
                        $quests[] = $mra['q'];
                    }
                }
                $mtchres = implode(',', $quests);
            }
            $prompt = str_ireplace('%%related_questions_' . $googlematch . '%%', $mtchres, $prompt);
        }
    }
	$query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            exit;
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, 1, 1, 0, 0, false, 'aiContentInfoWriter', 0, $finish_reason, $aierror, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
        exit;
	}
	else
	{
		$new_post_content = aiomatic_sanitize_ai_result($generated_text);
        if($step == 'meta')
        {
            if(!empty($post_id))
            {
                aiomatic_save_seo_description($post_id, $new_post_content, '');
            }
        }
        elseif($step == 'metatitle')
        {
            if(!empty($post_id))
            {
                aiomatic_save_seo_title($post_id, $new_post_content);
            }
        }
        elseif($step == 'focus')
        {
            if(!empty($post_id))
            {
                aiomatic_save_seo_description($post_id, '', $new_post_content);
            }
        }
	}
    do_action('aiomatic_aicontent_reply', $new_post_content);
    wp_send_json_success( array('content' => $new_post_content) );
    die();
}

add_action( 'wp_ajax_aiomatic_save_post_ai', 'aiomatic_save_post_ai' );
function aiomatic_save_post_ai() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if(!isset($_POST['post_id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (post_id)' ) );
        exit;
	}
    $post_id = $_POST['post_id'];
    if(!isset($_POST['aiomatic_title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_title)' ) );
        exit;
	}
    $this_post = get_post($post_id);
    if($this_post === null)
    {
        wp_send_json_error( array( 'message' => 'Incorrect post_id sent' ) );
        exit;
    }
    if ( !current_user_can('manage_options') ) 
    {
        if ($this_post->post_author != get_current_user_id()) 
        {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
    }
    $aiomatic_title = $_POST['aiomatic_title'];
    if(!isset($_POST['aiomatic_ai_seo']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_seo)' ) );
        exit;
	}
    $aiomatic_ai_seo = $_POST['aiomatic_ai_seo'];
    if(!isset($_POST['aiomatic_ai_seo_title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_seo_title)' ) );
        exit;
	}
    $aiomatic_ai_seo_title = $_POST['aiomatic_ai_seo_title'];
    if(!isset($_POST['aiomatic_ai_content']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_content)' ) );
        exit;
	}
    $aiomatic_ai_content = $_POST['aiomatic_ai_content'];
    if(!isset($_POST['aiomatic_ai_excerpt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_excerpt)' ) );
        exit;
	}
    $aiomatic_ai_excerpt = $_POST['aiomatic_ai_excerpt'];
    if(!isset($_POST['aiomatic_ai_tags']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_tags)' ) );
        exit;
	}
    $aiomatic_ai_tags = $_POST['aiomatic_ai_tags'];
    if(!isset($_POST['aiomatic_ai_focus']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (aiomatic_ai_focus)' ) );
        exit;
	}
    $aiomatic_ai_focus = $_POST['aiomatic_ai_focus'];
    if(empty($aiomatic_title) && empty($aiomatic_ai_seo) && empty($aiomatic_ai_seo_title) && empty($aiomatic_ai_content) && empty($aiomatic_ai_excerpt) && empty($aiomatic_ai_tags) && empty($aiomatic_ai_focus))
    {
        wp_send_json_error( array( 'message' => 'Incorrect query sent (nothing to save)' ) );
        exit;
    }
    if(!empty($post_id))
    {
        $need_change = false;
        $my_post = array();
        if($this_post->post_status == 'auto-draft')
        {
            $my_post['post_status'] = 'draft';
        }
        $my_post['ID'] = $post_id;
        if(!empty($aiomatic_title))
        {
            $my_post['post_title'] = $aiomatic_title;
            $need_change = true;
        }
        if(!empty($aiomatic_ai_content))
        {
            $my_post['post_content'] = $aiomatic_ai_content;
            $need_change = true;
        }
        if(!empty($aiomatic_ai_excerpt))
        {
            $my_post['post_excerpt'] = $aiomatic_ai_excerpt;
            $need_change = true;
        }
        if(!empty($aiomatic_ai_tags))
        {
            $my_post['tags_input'] = $aiomatic_ai_tags;
            $need_change = true;
        }
        if(!empty($aiomatic_ai_seo) || !empty($aiomatic_ai_focus))
        {
            aiomatic_save_seo_description($post_id, $aiomatic_ai_seo, $aiomatic_ai_focus);
        }
        if(!empty($aiomatic_ai_seo_title))
        {
            aiomatic_save_seo_title($post_id, $aiomatic_ai_seo_title);
        }
        if($need_change)
        {
            remove_filter('content_save_pre', 'wp_filter_post_kses');
            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
            try
            {
                $post_id = wp_update_post($my_post, true);
            }
            catch(Exception $e)
            {
                aiomatic_log_to_file('Exception in saving post: ' . $e->getMessage());
            }
            add_filter('content_save_pre', 'wp_filter_post_kses');
            add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        }
    }
    else
    {
        wp_send_json_error( array( 'message' => 'Empty post_id sent' ) );
        exit;
    }
    $post_link = get_edit_post_link($post_id);
    $post_link = str_replace('&amp;', '&', $post_link);
    wp_send_json_success( array('content' => $post_link) );
    die();
}

add_action('wp_ajax_aiomatic_get_elevenlabs_voice_chat', 'aiomatic_get_elevenlabs_voice_chat');
add_action('wp_ajax_nopriv_aiomatic_get_elevenlabs_voice_chat', 'aiomatic_get_elevenlabs_voice_chat');
function aiomatic_get_elevenlabs_voice_chat()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with elevenlabs');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(!isset($_REQUEST['x_input_text']) || empty($_REQUEST['x_input_text']))
    {
        $aiomatic_result['msg'] = 'No text to send to text-to-speech!';
        wp_send_json($aiomatic_result);
    }
    if ((!isset($aiomatic_Main_Settings['elevenlabs_app_id']) || trim($aiomatic_Main_Settings['elevenlabs_app_id']) == ''))
    {
        $aiomatic_result['msg'] = 'You need to enter an ElevenLabs API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if(isset($aiomatic_Chatbot_Settings['eleven_voice_custom']) && $aiomatic_Chatbot_Settings['eleven_voice_custom'] != '')
    {
        $voice = $aiomatic_Chatbot_Settings['eleven_voice_custom'];
    }
    else
    {
        if(isset($aiomatic_Chatbot_Settings['eleven_voice']) && $aiomatic_Chatbot_Settings['eleven_voice'] != '')
        {
            $voice = $aiomatic_Chatbot_Settings['eleven_voice'];
        }
        else
        {
            $voice = '21m00Tcm4TlvDq8ikWAM';
        }
    }
    if(isset($_REQUEST['overwrite_voice']) && !empty($_REQUEST['overwrite_voice']))
    {
        $voice = trim(stripslashes($_REQUEST['overwrite_voice']));
    }
    $message = wp_strip_all_tags(sanitize_text_field($_REQUEST['x_input_text']));
    $session = aiomatic_get_session_id();
    $query = new Aiomatic_Query($message, 0, 'elevenlabs', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['elevenlabs_app_id']), $session, 1, '', '');
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $ok = apply_filters( 'aiomatic_tts_allowed', true, $aiomatic_Limit_Settings, $query );
    if ( $ok !== true ) 
    {
        $aiomatic_result['msg'] = 'Rate limited: ' . $ok;
        wp_send_json($aiomatic_result);
    }
    $result = aiomatic_elevenlabs_stream($voice, $message, 'aiomatic_Chatbot_Settings');
    if(is_array($result)){
        wp_send_json($result);
    }
    else
    {
        apply_filters( 'aiomatic_ai_reply_text', $query, $message );
        echo $result;
        die();
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_get_god_mode_function', 'aiomatic_get_god_mode_function');
function aiomatic_get_god_mode_function()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    require_once (dirname(__FILE__) . "/aiomatic-god-mode.php"); 
    $god_mode = false;
    $dalle = false;
    $stable = false;
    $midjourney = false;
    $replicate = false;
    $ideogram = false;
    $cloudflare = false;
    $scraper = false;
    $clickbank = false;
    $amazon = false;
    $amazon_details = false;
    $booking = false;
    $rss = false;
    $google = false;
    $captions = false;
    $royalty = false;
    $youtube = false;
    $email = false;
    $facebook = false;
    $facebook_image = false;
    $twitter = false;
    $threads = false;
    $instagram = false;
    $pinterest = false;
    $business = false;
    $youtube_community = false;
    $reddit = false;
    $linkedin = false;
    $webhook = false;
    $stable_video = false;
    $lead = false;
    $ai = false;
    $database = false;
    $chart = false;
    $toubiz = false;
    $google_image = false;
    $end = false;
    $omniblock = false;
    if(isset($_REQUEST['god_mode']) && $_REQUEST['god_mode'] == '1')
    {
        $god_mode = true;
    }
    if(isset($_REQUEST['chart']) && $_REQUEST['chart'] == '1')
    {
        $chart = true;
    }
    if(isset($_REQUEST['dalle']) && $_REQUEST['dalle'] == '1')
    {
        $dalle = true;
    }
    if(isset($_REQUEST['stable']) && $_REQUEST['stable'] == '1')
    {
        $stable = true;
    }
    if(isset($_REQUEST['midjourney']) && $_REQUEST['midjourney'] == '1')
    {
        $midjourney = true;
    }
    if(isset($_REQUEST['replicate']) && $_REQUEST['replicate'] == '1')
    {
        $replicate = true;
    }
    if(isset($_REQUEST['ideogram']) && $_REQUEST['ideogram'] == '1')
    {
        $ideogram = true;
    }
    if(isset($_REQUEST['cloudflare']) && $_REQUEST['cloudflare'] == '1')
    {
        $cloudflare = true;
    }
    if(isset($_REQUEST['scraper']) && $_REQUEST['scraper'] == '1')
    {
        $scraper = true;
    }
    if(isset($_REQUEST['amazon']) && $_REQUEST['amazon'] == '1')
    {
        $amazon = true;
    }
    if(isset($_REQUEST['clickbank']) && $_REQUEST['clickbank'] == '1')
    {
        $clickbank = true;
    }
    if(isset($_REQUEST['amazon_details']) && $_REQUEST['amazon_details'] == '1')
    {
        $amazon_details = true;
    }
    if(isset($_REQUEST['booking']) && $_REQUEST['booking'] == '1')
    {
        $booking = true;
    }
    if(isset($_REQUEST['rss']) && $_REQUEST['rss'] == '1')
    {
        $rss = true;
    }
    if(isset($_REQUEST['google']) && $_REQUEST['google'] == '1')
    {
        $google = true;
    }
    if(isset($_REQUEST['captions']) && $_REQUEST['captions'] == '1')
    {
        $captions = true;
    }
    if(isset($_REQUEST['royalty']) && $_REQUEST['royalty'] == '1')
    {
        $royalty = true;
    }
    if(isset($_REQUEST['youtube']) && $_REQUEST['youtube'] == '1')
    {
        $youtube = true;
    }
    if(isset($_REQUEST['email']) && $_REQUEST['email'] == '1')
    {
        $email = true;
    }
    if(isset($_REQUEST['facebook']) && $_REQUEST['facebook'] == '1')
    {
        $facebook = true;
    }
    if(isset($_REQUEST['facebook_image']) && $_REQUEST['facebook_image'] == '1')
    {
        $facebook_image = true;
    }
    if(isset($_REQUEST['twitter']) && $_REQUEST['twitter'] == '1')
    {
        $twitter = true;
    }
    if(isset($_REQUEST['threads']) && $_REQUEST['threads'] == '1')
    {
        $threads = true;
    }
    if(isset($_REQUEST['instagram']) && $_REQUEST['instagram'] == '1')
    {
        $instagram = true;
    }
    if(isset($_REQUEST['pinterest']) && $_REQUEST['pinterest'] == '1')
    {
        $pinterest = true;
    }
    if(isset($_REQUEST['business']) && $_REQUEST['business'] == '1')
    {
        $business = true;
    }
    if(isset($_REQUEST['youtube_community']) && $_REQUEST['youtube_community'] == '1')
    {
        $youtube_community = true;
    }
    if(isset($_REQUEST['reddit']) && $_REQUEST['reddit'] == '1')
    {
        $reddit = true;
    }
    if(isset($_REQUEST['linkedin']) && $_REQUEST['linkedin'] == '1')
    {
        $linkedin = true;
    }
    if(isset($_REQUEST['webhook']) && $_REQUEST['webhook'] == '1')
    {
        $webhook = true;
    }
    if(isset($_REQUEST['database']) && $_REQUEST['database'] == '1')
    {
        $database = true;
    }
    if(isset($_REQUEST['omniblock']) && $_REQUEST['omniblock'] == '1')
    {
        $omniblock = true;
    }
    if(isset($_REQUEST['stable_video']) && $_REQUEST['stable_video'] == '1')
    {
        $stable_video = true;
    }
    if(isset($_REQUEST['lead_capture']) && $_REQUEST['lead_capture'] == '1')
    {
        $lead = true;
    }
    if(isset($_REQUEST['ai']) && $_REQUEST['ai'] == '1')
    {
        $ai = true;
    }
    if(isset($_REQUEST['toubiz']) && $_REQUEST['toubiz'] == '1')
    {
        $toubiz = true;
    }
    if(isset($_REQUEST['end']) && $_REQUEST['end'] == '1')
    {
        $end = true;
    }
    if(isset($_REQUEST['google_image']) && $_REQUEST['google_image'] == '1')
    {
        $google_image = true;
    }
    if(isset($_REQUEST['cloudflare']) && $_REQUEST['cloudflare'] == '1')
    {
        $cloudflare = true;
    }
    $wp_god_mode = aiomatic_return_god_function_assistants($god_mode, $ai, $lead, $dalle, $stable, $midjourney, $replicate, $ideogram, $amazon, $amazon_details, $scraper, $rss, $google, $captions, $royalty, $youtube, $email, $facebook, $facebook_image, $twitter, $threads, $instagram, $pinterest, $business, $youtube_community, $reddit, $linkedin, $webhook, $stable_video, $database, $chart, $toubiz, $google_image, $end, $omniblock, $booking, $clickbank, $cloudflare);
    $aiomatic_result = array('status' => 'success', 'json' => json_encode($wp_god_mode));
    wp_send_json($aiomatic_result);
    die();
}

add_action( 'wp_ajax_aiomatic_delete_lead', 'aiomatic_delete_lead_ajax' );
function aiomatic_delete_lead_ajax() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $lead_id = isset( $_POST['lead_id'] ) ? intval( $_POST['lead_id'] ) : 0;
    $deleted = wp_delete_post( $lead_id, true );
    if ( $deleted ) {
        wp_send_json_success();
    } else {
        wp_send_json_error( __( 'Failed to delete the lead.', 'aiomatic-automatic-ai-content-writer' ) );
    }
    wp_die();
}

add_action( 'wp_ajax_aiomatic_bulk_delete_leads', 'aiomatic_bulk_delete_leads_ajax' );
function aiomatic_bulk_delete_leads_ajax() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $lead_ids = isset( $_POST['lead_ids'] ) ? array_map( 'intval', $_POST['lead_ids'] ) : array();

    if ( empty( $lead_ids ) ) {
        wp_send_json_error( esc_html__( 'No leads selected.', 'aiomatic-automatic-ai-content-writer' ) );
        wp_die();
    }
    foreach ( $lead_ids as $lead_id ) {
        wp_delete_post( $lead_id, true );
    }
    wp_send_json_success();
    wp_die();
}

add_action( 'wp_ajax_aiomatic_export_leads_csv', 'aiomatic_export_leads_to_csv_ajax' );
function aiomatic_export_leads_to_csv_ajax() 
{
    check_ajax_referer( 'openai-ajax-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $paged = 1; 
    $posts_per_page = 100; 
    $leads = array(); 
    do {
        $args = array(
            'post_type'      => 'aiomatic_lead', 
            'post_status'    => 'publish',  
            'posts_per_page' => $posts_per_page,
            'paged'          => $paged,   
        );
        $query = new WP_Query($args);
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $leads[] = $query->post; 
            }
        }
        $paged++;
        wp_reset_postdata(); 
    } while ($query->have_posts());
    if ( empty( $leads ) ) 
    {
        wp_send_json_error( esc_html__( 'No leads available for export.', 'aiomatic-automatic-ai-content-writer' ) );
        wp_die();
    }
    $csv_output = '';
    $columns = array( 'Email', 'Name', 'Phone Number', 'Job Title', 'Company', 'Location', 'Birth Date', 'How You Found Us', 'Website URL', 'Preferred Contact Method', 'Date Collected' );
    $csv_output .= '"' . implode( '","', $columns ) . '"' . "\n";
    foreach ( $leads as $lead ) {
        $row = array(
            $lead->post_title,
            get_post_meta( $lead->ID, 'name', true ),
            get_post_meta( $lead->ID, 'phone_number', true ),
            get_post_meta( $lead->ID, 'job_title', true ),
            get_post_meta( $lead->ID, 'company_name', true ),
            get_post_meta( $lead->ID, 'location', true ),
            get_post_meta( $lead->ID, 'birth_date', true ),
            get_post_meta( $lead->ID, 'how_you_found_us', true ),
            get_post_meta( $lead->ID, 'website_url', true ),
            get_post_meta( $lead->ID, 'preferred_contact_method', true ),
            get_the_date( '', $lead->ID ),
        );
        $escaped_row = array_map( function( $field ) {
            return str_replace( '"', '""', $field );
        }, $row );

        $csv_output .= '"' . implode( '","', $escaped_row ) . '"' . "\n";
    }
    wp_send_json_success( array( 'csv' => $csv_output ) );
    wp_die();
}

add_action('wp_ajax_aiomatic_check_process_status', 'aiomatic_check_process_status');
function aiomatic_check_process_status()
{
    check_ajax_referer('openai-bulk-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with status polling');
    $poll = get_transient('aiomatic_log_history');
    if($poll !== false)
    {
        $aiomatic_result['msg'] = esc_html($poll);
        $aiomatic_result['status'] = 'success';
    }
    else
    {
        $aiomatic_result['msg'] = 'Running status not found';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_get_openai_voice_chat', 'aiomatic_get_openai_voice_chat');
add_action('wp_ajax_nopriv_aiomatic_get_openai_voice_chat', 'aiomatic_get_openai_voice_chat');
function aiomatic_get_openai_voice_chat()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OpenAI TTS');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(!isset($_REQUEST['x_input_text']) || empty($_REQUEST['x_input_text']))
    {
        $aiomatic_result['msg'] = 'No text to send to text-to-speech!';
        wp_send_json($aiomatic_result);
    }
    if (!isset($aiomatic_Main_Settings['app_id'])) 
    {
        $aiomatic_Main_Settings['app_id'] = '';
    }
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if (empty($token))
    {
        $aiomatic_result['msg'] = 'You need to enter an OpenAI API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    if(aiomatic_is_aiomaticapi_key($token) || (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)))
    {
        $aiomatic_result['msg'] = 'Only OpenAI API keys are supported at the moment.';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if(isset($aiomatic_Chatbot_Settings['open_model_id']) && $aiomatic_Chatbot_Settings['open_model_id'] != '')
    {
        $open_model_id = $aiomatic_Chatbot_Settings['open_model_id'];
    }
    else
    {
        $open_model_id = 'tts-1';
    }
    if(isset($aiomatic_Chatbot_Settings['open_voice']) && $aiomatic_Chatbot_Settings['open_voice'] != '')
    {
        $open_voice = $aiomatic_Chatbot_Settings['open_voice'];
    }
    else
    {
        $open_voice = 'alloy';
    }
    if(isset($_REQUEST['overwrite_voice']) && !empty($_REQUEST['overwrite_voice']))
    {
        $open_voice = trim(stripslashes($_REQUEST['overwrite_voice']));
    }
    if(isset($aiomatic_Chatbot_Settings['open_format']) && $aiomatic_Chatbot_Settings['open_format'] != '')
    {
        $open_format = $aiomatic_Chatbot_Settings['open_format'];
    }
    else
    {
        $open_format = 'mp3';
    }
    if(isset($aiomatic_Chatbot_Settings['open_speed']) && $aiomatic_Chatbot_Settings['open_speed'] != '')
    {
        $open_speed = $aiomatic_Chatbot_Settings['open_speed'];
    }
    else
    {
        $open_speed = '1';
    }
    $message = wp_strip_all_tags(sanitize_text_field($_REQUEST['x_input_text']));
    $session = aiomatic_get_session_id();
    $query = new Aiomatic_Query($message, 0, 'openai-' . $open_model_id, '0', '', 'text-to-speech', 'text-to-speech', $token, $session, 1, '', '');
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $ok = apply_filters( 'aiomatic_tts_allowed', true, $aiomatic_Limit_Settings, $query );
    if ( $ok !== true ) 
    {
        $aiomatic_result['msg'] = 'Rate limited: ' . $ok;
        wp_send_json($aiomatic_result);
    }
    $result = aiomatic_openai_voice_stream($token, $open_model_id, $open_voice, $open_format, $open_speed, $message);
    if(is_array($result))
    {
        wp_send_json($result);
    }
    else
    {
        apply_filters( 'aiomatic_ai_reply_text', $query, $message );
        switch ($open_format) 
        {
            case 'opus':
                header('Content-Type: audio/opus');
                break;
            case 'aac':
                header('Content-Type: audio/aac');
                break;
            case 'flac':
                header('Content-Type: audio/flac');
                break;
            default:
                header('Content-Type: audio/mpeg');
        }
        echo $result;
        die();
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_get_google_voice_chat', 'aiomatic_get_google_voice_chat');
add_action('wp_ajax_nopriv_aiomatic_get_google_voice_chat', 'aiomatic_get_google_voice_chat');
function aiomatic_get_google_voice_chat()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Google Voice');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(!isset($_REQUEST['x_input_text']) || empty($_REQUEST['x_input_text']))
    {
        $aiomatic_result['msg'] = 'No text to send to text-to-speech!';
        wp_send_json($aiomatic_result);
    }
    if ((!isset($aiomatic_Main_Settings['google_app_id']) || trim($aiomatic_Main_Settings['google_app_id']) == ''))
    {
        $aiomatic_result['msg'] = 'You need to enter an Google Text-to-Speech API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if(isset($aiomatic_Chatbot_Settings['google_voice']) && $aiomatic_Chatbot_Settings['google_voice'] != '')
    {
        $voice = $aiomatic_Chatbot_Settings['google_voice'];
    }
    else
    {
        $aiomatic_result['msg'] = 'You need to select a Google Text-to-Speech Voice Name for this feature to work.';
        wp_send_json($aiomatic_result);
    }
    if(isset($_REQUEST['overwrite_voice']) && !empty($_REQUEST['overwrite_voice']))
    {
        $voice = trim(stripslashes($_REQUEST['overwrite_voice']));
    }
    if(isset($aiomatic_Chatbot_Settings['audio_profile']) && $aiomatic_Chatbot_Settings['audio_profile'] != '')
    {
        $audio_profile = $aiomatic_Chatbot_Settings['audio_profile'];
    }
    else
    {
        $audio_profile = '';
    }
    if(isset($aiomatic_Chatbot_Settings['voice_language']) && $aiomatic_Chatbot_Settings['voice_language'] != '')
    {
        $voice_language = $aiomatic_Chatbot_Settings['voice_language'];
    }
    else
    {
        $aiomatic_result['msg'] = 'You need to select a Google Text-to-Speech Voice Language for this feature to work.';
        wp_send_json($aiomatic_result);
    }
    if(isset($aiomatic_Chatbot_Settings['voice_speed']) && $aiomatic_Chatbot_Settings['voice_speed'] != '')
    {
        $voice_speed = $aiomatic_Chatbot_Settings['voice_speed'];
    }
    else
    {
        $voice_speed = '';
    }
    if(isset($aiomatic_Chatbot_Settings['voice_pitch']) && $aiomatic_Chatbot_Settings['voice_pitch'] != '')
    {
        $voice_pitch = $aiomatic_Chatbot_Settings['voice_pitch'];
    }
    else
    {
        $voice_pitch = '';
    }
    if(isset($aiomatic_Chatbot_Settings['google_voice_model']) && $aiomatic_Chatbot_Settings['google_voice_model'] != '')
    {
        $google_voice_model = $aiomatic_Chatbot_Settings['google_voice_model'];
    }
    else
    {
        $google_voice_model = '';
    }
    $message = wp_strip_all_tags(sanitize_text_field($_REQUEST['x_input_text']));
    $session = aiomatic_get_session_id();
    $query = new Aiomatic_Query($message, 0, 'google', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['google_app_id']), $session, 1, '', '');
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $ok = apply_filters( 'aiomatic_tts_allowed', true, $aiomatic_Limit_Settings, $query );
    if ( $ok !== true ) 
    {
        $aiomatic_result['msg'] = 'Rate limited: ' . $ok;
        wp_send_json($aiomatic_result);
    }
    $result = aiomatic_google_stream($voice, $voice_language, $audio_profile, $voice_speed, $voice_pitch, $google_voice_model, $message);
    if(is_array($result)){
        if(isset($result['status']) && $result['status'] == 'success')
        {
            apply_filters( 'aiomatic_ai_reply_text', $query, $message );
        }
        wp_send_json($result);
    }
    else
    {
        echo $result;
        die();
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_get_d_id_video_chat', 'aiomatic_get_d_id_video_chat');
add_action('wp_ajax_nopriv_aiomatic_get_d_id_video_chat', 'aiomatic_get_d_id_video_chat');
function aiomatic_get_d_id_video_chat()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with D-ID');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(!isset($_REQUEST['x_input_text']) || empty($_REQUEST['x_input_text']))
    {
        $aiomatic_result['msg'] = 'No text to send to text-to-video!';
        wp_send_json($aiomatic_result);
    }
    if ((!isset($aiomatic_Main_Settings['did_app_id']) || trim($aiomatic_Main_Settings['did_app_id']) == ''))
    {
        $aiomatic_result['msg'] = 'You need to enter an Google Text-to-video API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if(isset($aiomatic_Chatbot_Settings['did_image']) && $aiomatic_Chatbot_Settings['did_image'] != '')
    {
        $did_image = $aiomatic_Chatbot_Settings['did_image'];
    }
    else
    {
        $did_image = 'https://create-images-results.d-id.com/api_docs/assets/noelle.jpeg';
    }
    if(isset($aiomatic_Chatbot_Settings['did_voice']) && $aiomatic_Chatbot_Settings['did_voice'] != '')
    {
        $did_voice = $aiomatic_Chatbot_Settings['did_voice'];
    }
    else
    {
        $did_voice = 'microsoft:en-US-JennyNeural:Cheerful';
    }
    if(isset($_REQUEST['overwrite_voice']) && !empty($_REQUEST['overwrite_voice']))
    {
        $did_voice = trim(stripslashes($_REQUEST['overwrite_voice']));
    }
    $message = wp_strip_all_tags(sanitize_text_field($_REQUEST['x_input_text']));
    $session = aiomatic_get_session_id();
    $query = new Aiomatic_Query($message, 0, 'd-id', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['did_app_id']), $session, 1, '', '');
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $ok = apply_filters( 'aiomatic_tts_allowed', true, $aiomatic_Limit_Settings, $query );
    if ( $ok !== true ) 
    {
        $aiomatic_result['msg'] = 'Rate limited: ' . $ok;
        wp_send_json($aiomatic_result);
    }
    $result = aiomatic_d_id_video($did_image, $message, $did_voice);
    if(is_array($result)){
        if(isset($result['status']) && $result['status'] == 'success')
        {
            apply_filters( 'aiomatic_ai_reply_text', $query, $message );
        }
        else
        {
            if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
                aiomatic_log_to_file('D-ID Video Failed: ' . print_r($result, true));
            }
        }
        wp_send_json($result);
    }
    else
    {
        echo esc_html($result);
        die();
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_load_chat_conversation_data', 'aiomatic_load_chat_conversation_data');
add_action('wp_ajax_nopriv_aiomatic_load_chat_conversation_data', 'aiomatic_load_chat_conversation_data');
function aiomatic_load_chat_conversation_data()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong');
    if(isset($_POST['dataid']) && trim($_POST['dataid']) !== '')
    {
        $dataid = $_POST['dataid'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    if(isset($_POST['persistent_guests']) && trim($_POST['persistent_guests']) !== '')
    {
        $persistent_guests = $_POST['persistent_guests'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    if(isset($_POST['persistent']) && trim($_POST['persistent']) !== '')
    {
        $persistent = $_POST['persistent'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    $user_id = get_current_user_id();
    if($user_id == 0 && ($persistent_guests == 'on' || $persistent_guests == '1'))
    {
        $user_id = aiomatic_get_guest_id();
    }
    if($user_id == 0)
    {
        $aiomatic_result['msg'] = 'You are not allowed to do this action';
        wp_send_json($aiomatic_result);
    }
    if($dataid === 'new-chat')
    {
        if(isset($_POST['init_message']) && trim($_POST['init_message']) !== '')
        {
            $init_message = trim($_POST['init_message']);
        }
        else
        {
            $init_message = '';
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = array('data' => stripslashes($init_message));
        wp_send_json($aiomatic_result);
    }
    if(is_numeric($user_id))
    {
        $conversation_data = get_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, true);
        if(!is_array($conversation_data))
        {
            $conversation_data = array();
        }
    }
    else
    {
        $conversation_data = get_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id);
        if(!is_array($conversation_data))
        {
            $conversation_data = array();
        }
    }
    if(!isset($conversation_data[$dataid]))
    {
        $aiomatic_result['msg'] = 'Conversation not found in database';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $conversation_data[$dataid];
        wp_send_json($aiomatic_result);
    }
}

add_action('wp_ajax_aiomatic_remove_chat_logs', 'aiomatic_remove_chat_logs');
add_action('wp_ajax_nopriv_aiomatic_remove_chat_logs', 'aiomatic_remove_chat_logs');
function aiomatic_remove_chat_logs()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong');
    if(isset($_POST['dataid']) && trim($_POST['dataid']) !== '')
    {
        $dataid = $_POST['dataid'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    if(isset($_POST['persistent_guests']) && trim($_POST['persistent_guests']) !== '')
    {
        $persistent_guests = $_POST['persistent_guests'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    if(isset($_POST['persistent']) && trim($_POST['persistent']) !== '')
    {
        $persistent = $_POST['persistent'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }
    $user_id = get_current_user_id();
    if($user_id == 0 && ($persistent_guests == 'on' || $persistent_guests == '1'))
    {
        $user_id = aiomatic_get_guest_id();
    }
    if($user_id == 0)
    {
        $aiomatic_result['msg'] = 'You are not allowed to do this action';
        wp_send_json($aiomatic_result);
    }
    if($dataid === 'new-chat')
    {
        $aiomatic_result['msg'] = 'This is not allowed';
        wp_send_json($aiomatic_result);
    }
    if(is_numeric($user_id))
    {
        $conversation_data = get_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, true);
        if(!is_array($conversation_data))
        {
            $conversation_data = array();
        }
    }
    else
    {
        $conversation_data = get_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id);
        if(!is_array($conversation_data))
        {
            $conversation_data = array();
        }
    }
    if(isset($conversation_data[$dataid]))
    {
        unset($conversation_data[$dataid]);
        if(is_numeric($user_id))
        {
            update_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, $conversation_data);
        }
        else
        {
            $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
            if(isset($aiomatic_Chatbot_Settings['remember_chat_transient']) && $aiomatic_Chatbot_Settings['remember_chat_transient'] !== '' && is_numeric($aiomatic_Chatbot_Settings['remember_chat_transient']))
            {
                $remember_time = intval($aiomatic_Chatbot_Settings['remember_chat_transient']);
            }
            else
            {
                $remember_time = 0;
            }
            set_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id, $conversation_data, $remember_time);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'ok';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $aiomatic_result['data'] = 'Conversation not found';
        wp_send_json($aiomatic_result);
    }
}
add_action('wp_ajax_aiomatic_get_d_id_default_video_chat', 'aiomatic_get_d_id_default_video_chat');
add_action('wp_ajax_nopriv_aiomatic_get_d_id_default_video_chat', 'aiomatic_get_d_id_default_video_chat');
function aiomatic_get_d_id_default_video_chat()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $message = '<break time="5000ms"/>';
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with D-ID');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if ((!isset($aiomatic_Main_Settings['did_app_id']) || trim($aiomatic_Main_Settings['did_app_id']) == ''))
    {
        $aiomatic_result['msg'] = 'You need to enter an Google Text-to-video API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    if(isset($_POST['did_image']) && trim($_POST['did_image']) != '')
    {
        $did_image = $_POST['did_image'];
    }
    else
    {
        $aiomatic_result['msg'] = 'Insuficient parameters for call!';
        wp_send_json($aiomatic_result);
    }

    $transient_key = 'aiomatic_did_local_avatar_' . md5($did_image . $message);
    $cached_response = get_transient($transient_key);
    if ($cached_response !== false) 
    {
        wp_send_json($cached_response);
        die();
    }

    $filename = basename($did_image);
    $filename = explode("?", $filename);
    $filename = $filename[0];
    $filename = str_replace('%', '-', $filename);
    $filename = str_replace('#', '-', $filename);
    $filename = str_replace('&', '-', $filename);
    $filename = str_replace('{', '-', $filename);
    $filename = str_replace('}', '-', $filename);
    $filename = str_replace('\\', '-', $filename);
    $filename = str_replace('<', '-', $filename);
    $filename = str_replace('>', '-', $filename);
    $filename = str_replace('*', '-', $filename);
    $filename = str_replace('/', '-', $filename);
    $filename = str_replace('$', '-', $filename);
    $filename = str_replace('\'', '-', $filename);
    $filename = str_replace('"', '-', $filename);
    $filename = str_replace(':', '-', $filename);
    $filename = str_replace('@', '-', $filename);
    $filename = str_replace('+', '-', $filename);
    $filename = str_replace('|', '-', $filename);
    $filename = str_replace('=', '-', $filename);
    $filename = str_replace('`', '-', $filename);
    $local_exist = aiomatic_check_video_locally($filename);
    if($local_exist !== false)
    {
        $result['video'] = $local_exist;
        $result['status'] = 'success';
        wp_send_json($result);
    }
    $session = aiomatic_get_session_id();
    $query = new Aiomatic_Query($message, 0, 'd-id', '0', '', 'text-to-speech', 'text-to-speech', trim($aiomatic_Main_Settings['did_app_id']), $session, 1, '', '');
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $ok = apply_filters( 'aiomatic_tts_allowed', true, $aiomatic_Limit_Settings, $query );
    if ( $ok !== true ) 
    {
        $aiomatic_result['msg'] = 'Rate limited: ' . $ok;
        wp_send_json($aiomatic_result);
    }
    $result = aiomatic_d_id_idle_video($did_image, $message);
    if(is_array($result))
    {
        if(isset($result['status']) && $result['status'] == 'success' && isset($result['video']))
        {
            $local_url = aiomatic_copy_video_locally($result['video'], $filename, 'local');
            if(isset($local_url[0]) && $local_url !== false)
            {
                $result['video'] = $local_url[0];
            }
            else
            {
                aiomatic_log_to_file('Failed to copy default video locally to your server! Please check on available storage space.');
            }
            set_transient($transient_key, $result, 99900000);
            apply_filters( 'aiomatic_ai_reply_text', $query, $message );
        }
        else
        {
            if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
                aiomatic_log_to_file('D-ID Video Failed: ' . print_r($result, true));
            }
        }
        wp_send_json($result);
        die();
    }
    else
    {
        echo esc_html($result);
        die();
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_comment_replier', 'aiomatic_comment_replier');
function aiomatic_comment_replier()
{
    check_ajax_referer('openai-comment-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['zid']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (id)' ) );
        exit;
	}
    $comment_id = sanitize_text_field($_POST['zid']);
    $comment = get_comment($comment_id);
    if(!$comment || is_wp_error($comment))
    {
        wp_send_json_error( array( 'message' => 'Failed to find comment with ID: ' . $comment_id) );
        exit;
    }
    $post = get_post($comment->comment_post_ID);
    if(!$post) 
    {
        wp_send_json_error( array( 'message' => 'Failed to find post for comment, with ID: ' . $comment->comment_post_ID) );
        exit;
    }
	$aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
	if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
    if (isset($aiomatic_Main_Settings['comment_prompt']) && trim($aiomatic_Main_Settings['comment_prompt']) != '') 
    {
        $prompt = trim($aiomatic_Main_Settings['comment_prompt']);
    }
    else
    {
        $prompt = 'Write a reply for %%username%%\'s comment on the post titled "%%post_title%%". The user\'s comment is: %%comment%%';
    }
    if (isset($aiomatic_Main_Settings['comment_model']) && trim($aiomatic_Main_Settings['comment_model']) != '') 
    {
        $model = trim($aiomatic_Main_Settings['comment_model']);
    }
    else
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    if (isset($aiomatic_Main_Settings['comment_assistant_id']) && trim($aiomatic_Main_Settings['comment_assistant_id']) != '') 
    {
        $comment_assistant_id = trim($aiomatic_Main_Settings['comment_assistant_id']);
    }
    else
    {
        $comment_assistant_id = '';
    }
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $prompt = str_replace('%%post_title%%', $post->post_title, $prompt);
    $prompt = str_replace('%%post_excerpt%%', $post->post_excerpt, $prompt);
    $prompt = str_replace('%%username%%', $comment->comment_author, $prompt);
    $prompt = str_replace('%%comment%%', $comment->comment_content, $prompt);
	$query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            exit;
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, 1, 1, 0, 0, false, 'singleCommentWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $comment_assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
        exit;
	}
	else
	{
		$new_post_content = aiomatic_sanitize_ai_result($generated_text);
	}
    do_action('aiomatic_comment_reply', $new_post_content);
	wp_send_json_success( array( 'content' => $new_post_content ) );
    exit;
}

add_action('wp_ajax_aiomatic_save_mcp_server', function(){
    check_ajax_referer('aiomatic_mcp_nonce');
    $servers = get_option('aiomatic_mcp_servers', []);
    $id = (isset($_POST['id']) && !empty($_POST['id'])) ? sanitize_text_field($_POST['id']) : uniqid();
    $servers[$id] = [
        'name' => sanitize_text_field($_POST['name']),
        'description' => sanitize_textarea_field($_POST['description']),
        'url' => esc_url_raw($_POST['url']),
        'global' => sanitize_text_field($_POST['global']),
        'token' => sanitize_text_field($_POST['token']),
    ];
    update_option('aiomatic_mcp_servers', $servers);
    wp_send_json_success();
});

add_action('wp_ajax_aiomatic_delete_mcp_server', function(){
    check_ajax_referer('aiomatic_mcp_nonce');
    $servers = get_option('aiomatic_mcp_servers', []);
    $id = sanitize_text_field($_POST['id']);
    if (isset($servers[$id])) {
        unset($servers[$id]);
        update_option('aiomatic_mcp_servers', $servers);
    }
    wp_send_json_success();
});

add_action('wp_ajax_aiomatic_generate_media_text', 'aiomatic_generate_media_text');
function aiomatic_generate_media_text()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['prompt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prompt)' ) );
        exit;
	}
    $prompt = sanitize_text_field($_POST['prompt']);
    if(!isset($_POST['title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (title)' ) );
        exit;
	}
    $title = sanitize_text_field($_POST['title']);
    if(!isset($_POST['caption']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (caption)' ) );
        exit;
	}
    $caption = sanitize_text_field($_POST['caption']);
    if(!isset($_POST['alt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (alt)' ) );
        exit;
	}
    $alt = sanitize_text_field($_POST['alt']);
    if(!isset($_POST['content']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (content)' ) );
        exit;
	}
    $content = sanitize_text_field($_POST['content']);
    if(!isset($_POST['model']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (model)' ) );
        exit;
	}
    $model = sanitize_text_field($_POST['model']);
    if(!isset($_POST['assistant_id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (assistant_id)' ) );
        exit;
	}
    $assistant_id = sanitize_text_field($_POST['assistant_id']);
    if(!isset($_POST['id']) || !is_numeric($_POST['id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (id)' ) );
        exit;
	}
    $attachment_id = sanitize_text_field($_POST['id']);
    $vision_file = '';
    if(isset($_POST['ai_vision']) && $_POST['ai_vision'] == 'on')
    {
        if(aiomatic_is_vision_model($model, $assistant_id))
        {
            $vision_file = wp_get_attachment_url(intval($attachment_id));
        }
    }
	$aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
	if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $prompt = str_replace('%%image_title%%', $title, $prompt);
    $prompt = str_replace('%%image_caption%%', $caption, $prompt);
    $prompt = str_replace('%%image_alt%%', $alt, $prompt);
    $prompt = str_replace('%%image_description%%', $content, $prompt);
    $blog_title = html_entity_decode(get_bloginfo('title'));
    $prompt = str_replace('%%blog_title%%', $blog_title, $prompt);
    $prompt = str_replace('%%random_sentence%%', aiomatic_random_sentence_generator(), $prompt);
    $prompt = str_replace('%%random_sentence2%%', aiomatic_random_sentence_generator(false), $prompt);
    $parent_post_id = wp_get_post_parent_id($attachment_id);
    if ($parent_post_id) 
    {
        $parent_post_title = get_the_title($parent_post_id);
        $prompt = str_replace('%%parent_title%%', $parent_post_title, $prompt);
        $parent_post_excerpt = get_the_excerpt($parent_post_id);
        $prompt = str_replace('%%parent_excerpt%%', $parent_post_excerpt, $prompt);
        $parent_post_content = get_the_content($parent_post_id);
        $prompt = str_replace('%%parent_content%%', $parent_post_content, $prompt);
    } 
    else 
    {
        $prompt = str_replace('%%parent_title%%', '', $prompt);
        $prompt = str_replace('%%parent_excerpt%%', '', $prompt);
        $prompt = str_replace('%%parent_content%%', '', $prompt);
    }
    $prompt = aiomatic_replaceSynergyShortcodes($prompt);
	$query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            exit;
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, 1, 1, 0, 0, false, 'singleMediaWriter', 0, $finish_reason, $aierror, false, false, false, $vision_file, '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
        exit;
	}
	else
	{
		$new_post_content = aiomatic_sanitize_ai_result($generated_text);
	}
	wp_send_json_success( array( 'content' => $new_post_content ) );
    exit;
}
add_action('wp_ajax_aiomatic_assistant_prompt', 'aiomatic_assistant_prompt');
function aiomatic_assistant_prompt()
{
    if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'openai-ajax-nonce')) {
        wp_send_json_error(['message' => 'Invalid nonce'], 403);
    }

    if (!current_user_can('edit_posts')) {
        wp_send_json_error(['message' => 'Unauthorized'], 403);
    }

    if (!isset($_POST['prompt'])) {
        wp_send_json_error(['message' => 'Missing prompt']);
    }

    $prompt = trim(wp_kses_post(wp_unslash($_POST['prompt'])));

    if ($prompt === '') {
        wp_send_json_error(['message' => 'Empty prompt']);
    }

    $settings = get_option('aiomatic_Main_Settings', []);
    if (!is_array($settings)) {
        $settings = [];
    }

    if (!aiomatic_check_api_keys($settings)) {
        wp_send_json_error(['message' => 'Missing API key']);
    }

    $model = aiomatic_get_default_model_name($settings);
    $all_models = aiomatic_get_all_models(true);

    if (empty($model) || !is_array($all_models) || !in_array($model, $all_models, true)) {
        $model = !empty($all_models) ? reset($all_models) : '';
    }

    if (empty($model)) {
        wp_send_json_error(['message' => 'No valid AI model available']);
    }

    $token = '';
    if (!empty($settings['app_id'])) {
        $appids = preg_split('/\r\n|\r|\n/', trim($settings['app_id']));
        $appids = array_filter($appids);

        if (!empty($appids)) {
            $token = $appids[array_rand($appids)];
        }
    }

    $token = apply_filters('aimogen_openai_api_key', $token);
    $token = apply_filters('aiomatic_openai_api_key', $token);

    if (empty($token)) {
        wp_send_json_error(['message' => 'Invalid API token']);
    }
    $blog_title = get_bloginfo('name');
    $blog_desc  = get_bloginfo('description');
    $site_url   = home_url();
    $current_date = current_time('Y-m-d');
    $prompt = str_replace('%%blog_name%%', $blog_title, $prompt);
    $prompt = str_replace('%%blog_description%%', $blog_desc, $prompt);
    $prompt = str_replace('%%site_url%%', $site_url, $prompt);
    $prompt = str_replace('%%current_date%%', $current_date, $prompt);
    $current_user = wp_get_current_user();
    $prompt = str_replace('%%author_name%%', $current_user->display_name, $prompt);
    $prompt = str_replace('%%language%%', get_locale(), $prompt);
    $prompt = aiomatic_replaceSynergyShortcodes($prompt);
    $query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);

    if (!is_numeric($max_tokens) || $max_tokens <= 0) {
        wp_send_json_error(['message' => 'Invalid model token configuration']);
    }

    $available_tokens = aiomatic_compute_available_tokens(
        $model,
        $max_tokens,
        $prompt,
        $query_token_count
    );

    if ($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS) {

        $len = aiomatic_strlen($prompt);
        $half = (int) floor($len / 2);

        if ($half <= 0) {
            wp_send_json_error(['message' => 'Prompt too short or invalid']);
        }

        $prompt = trim(aiomatic_substr($prompt, 0, -$half));

        if ($prompt === '') {
            wp_send_json_error(['message' => 'Prompt became empty after trimming']);
        }

        $query_token_count = count(aiomatic_encode($prompt));
        $available_tokens = max(1, $max_tokens - $query_token_count);
    }

    $available_tokens = (int) $available_tokens;
    if ($available_tokens <= 0) {
        wp_send_json_error(['message' => 'No tokens available for generation']);
    }

    $thread_id = '';
    $finish_reason = '';
    $aierror = '';

    $generated = aiomatic_generate_text(
        $token,
        $model,
        $prompt,
        $available_tokens,
        1, 1, 0, 0,
        false,
        'dashboardAssistant',
        0,
        $finish_reason,
        $aierror,
        false, false, false,
        '',
        '',
        'user',
        '',
        $thread_id,
        '',
        'disabled',
        '',
        false,
        false,
        [],
        '',
        '',
        ''
    );

    if ($generated === false) {
        wp_send_json_error([
            'message' => !empty($aierror) ? $aierror : 'AI generation failed'
        ]);
    }

    $content = aiomatic_sanitize_ai_result($generated);

    if (empty($content)) {
        wp_send_json_error(['message' => 'Empty response from AI']);
    }

    wp_send_json_success([
        'content' => $content
    ]);
}
add_action('wp_ajax_aiomatic_save_media_text', 'aiomatic_save_media_text');
function aiomatic_save_media_text()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (title)' ) );
        exit;
	}
    $title = sanitize_text_field(wp_unslash($_POST['title']));
    if(!isset($_POST['caption']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (caption)' ) );
        exit;
	}
    $caption = sanitize_text_field(wp_unslash($_POST['caption']));
    if(!isset($_POST['alt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (alt)' ) );
        exit;
	}
    $alt = sanitize_text_field(wp_unslash($_POST['alt']));
    if(!isset($_POST['content']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (content)' ) );
        exit;
	}
    $content = sanitize_text_field(wp_unslash($_POST['content']));
    if(!isset($_POST['id']) || !is_numeric($_POST['id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (id)' ) );
        exit;
	}
    $id = sanitize_text_field($_POST['id']);
    if(empty($alt) && empty($title) && empty($caption) && empty($content))
    {
        wp_send_json_error( array( 'message' => 'You need to enter at least a value' ) );
        exit;
    }
    elseif(!empty($alt) && empty($title) && empty($caption) && empty($content))
    {
        update_post_meta($id, '_wp_attachment_image_alt', $alt);
        $new_post_content = 'ok';
        wp_send_json_success( array( 'content' => $new_post_content ) );
    }
    $attachment_data = array(
        'ID'           => $id
    );
    if(!empty($title))
    {
        $attachment_data['post_title'] = $title;
    }
    if(!empty($caption))
    {
        $attachment_data['post_excerpt'] = $caption;
    }
    if(!empty($content))
    {
        $attachment_data['post_content'] = $content;
    }
    $result = wp_update_post($attachment_data);
	if (is_wp_error($result))
	{
		wp_send_json_error( array( 'message' => 'Failed to save media: ' . $result->get_error_message()) );
        exit;
	}
	else
	{
        if(!empty($alt))
        {
            update_post_meta($id, '_wp_attachment_image_alt', $alt);
        }
		$new_post_content = 'ok';
	}
    if(isset($_POST['rename']) && $_POST['rename'] == 'on' && !empty($title))
	{
		$rez = aiomatic_rename_media_file( $id, $title );
        if($rez === false)
        {
            wp_send_json_error( array( 'message' => 'Failed to rename file to match SEO title') );
            exit;
        }
	}
	wp_send_json_success( array( 'content' => $new_post_content ) );
    exit;
}

add_action('wp_ajax_aiomatic_get_elevenlabs_voices', 'aiomatic_update_elevenlabs_voices_func');
add_action('wp_ajax_nopriv_aiomatic_get_elevenlabs_voices', 'aiomatic_update_elevenlabs_voices_func');
function aiomatic_update_elevenlabs_voices_func()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong EleventLabs');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['elevenlabs_app_id']) || trim($aiomatic_Main_Settings['elevenlabs_app_id']) == '')
    {
        $aiomatic_result['msg'] = 'You need to enter an ElevenLabs API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $my_voices = aiomatic_update_elevenlabs_voices();
    if(is_array($my_voices))
    {
        aiomatic_update_option('aiomatic_elevenlabs', $my_voices);
        $aiomatic_result['status'] = 'success';
    }
    else
    {
        $aiomatic_result['msg'] = 'Failed to list ElevenLabs Voices!';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_get_google_voices', 'aiomatic_update_google_voices_func');
add_action('wp_ajax_nopriv_aiomatic_get_google_voices', 'aiomatic_update_google_voices_func');
function aiomatic_update_google_voices_func()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong Google Voice Function');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if (!isset($aiomatic_Main_Settings['google_app_id']) || trim($aiomatic_Main_Settings['google_app_id']) == '')
    {
        $aiomatic_result['msg'] = 'You need to enter an Google Text-to-Speech API key for this to work!';
        wp_send_json($aiomatic_result);
    }
    if (isset($aiomatic_Chatbot_Settings['voice_language']) && trim($aiomatic_Chatbot_Settings['voice_language']) != '')
    {
        $voice_language = trim($aiomatic_Chatbot_Settings['voice_language']);
    }
    else
    {
        $voice_language = 'en-US';
    }
    $my_voices = aiomatic_update_google_voices($voice_language);
    if(is_array($my_voices))
    {
        aiomatic_update_option('aiomatic_google_voices' . sanitize_title($voice_language), $my_voices);
        $aiomatic_result['status'] = 'success';
    }
    else
    {
        $aiomatic_result['msg'] = 'Failed to list Google Text-to-Speech Voices!';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_form_upload', 'aiomatic_form_upload');
function aiomatic_form_upload()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form upload');
    if(isset($_FILES['file']) && empty($_FILES['file']['error']))
    {
        $file_name = sanitize_file_name(basename($_FILES['file']['name']));
        $filetype = wp_check_filetype($file_name);
        if($filetype['ext'] !== 'json' && !aiomatic_endsWith($file_name, '.json')){
            $aiomatic_result['msg'] = 'Only files with the json extension are supported, you sent: ' . $file_name;
            wp_send_json($aiomatic_result);
        }
        global $wp_filesystem;
        if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
            include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
            wp_filesystem($creds);
        }
        $fc = $wp_filesystem->get_contents($_FILES['file']['tmp_name']);
        if(empty($fc))
        {
            $aiomatic_result['msg'] = 'Failed to read file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        $fc_dec = json_decode($fc, true);
        if($fc_dec === null)
        {
            $aiomatic_result['msg'] = 'Failed to decode json file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        if(isset($_POST['overwrite']))
        {
            $overwrite = $_POST['overwrite'];
        }
        else
        {
            $overwrite = '0';
        }
        foreach($fc_dec as $jsonf)
        {
            $address_post_id = 0;
            $query = new WP_Query(
                array(
                    'post_type'              => 'aiomatic_forms',
                    'title'                  => $jsonf['title'],
                    'post_status'            => 'any',
                    'posts_per_page'         => 1,
                    'no_found_rows'          => true,
                    'ignore_sticky_posts'    => true,
                    'update_post_term_cache' => false,
                    'update_post_meta_cache' => false,
                    'orderby'                => 'post_date ID',
                    'order'                  => 'ASC',
                )
            );
            
            if ( ! empty( $query->post ) ) {
                if($overwrite != '1')
                {
                    //form already exists, skipping it
                    continue;
                }
                else
                {
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        $address_post_id = get_the_ID();
                        break;
                    }
                }
            }
            $forms_data = array(
                'post_type' => 'aiomatic_forms',
                'post_title' => $jsonf['title'],
                'post_content' => $jsonf['description'],
                'post_status' => 'publish'
            );
            $forms_data['ID'] = $address_post_id;
            remove_filter('content_save_pre', 'wp_filter_post_kses');
            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
            if($overwrite != '1')
            {
                $forms_id = wp_insert_post($forms_data);
            }
            else
            {
                if(isset($forms_data['ID']) && $forms_data['ID'] != '0')
                {
                    $forms_id = wp_update_post($forms_data);
                }
                else
                {
                    $forms_id = wp_insert_post($forms_data);
                }
            }
            add_filter('content_save_pre', 'wp_filter_post_kses');
            add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
            if(is_wp_error($forms_id))
            {
                aiomatic_log_to_file('Failed to import form: ' . $forms_id->get_error_message());
            }
            elseif($forms_id === 0)
            {
                aiomatic_log_to_file('Failed to insert form to database: ' . print_r($forms_data, true));
            }
            else 
            {
                update_post_meta($forms_id, 'prompt', $jsonf['prompt']);
                if(isset($jsonf['assistant_id']))
                {
                    update_post_meta($forms_id, 'assistant_id', $jsonf['assistant_id']);
                }
                update_post_meta($forms_id, 'model', $jsonf['model']);
                update_post_meta($forms_id, 'header', $jsonf['header']);
                if(!isset($jsonf['editor']))
                {
                    $jsonf['editor'] = 'textarea';
                }
                update_post_meta($forms_id, 'editor', $jsonf['editor']);
                if(!isset($jsonf['advanced']))
                {
                    $jsonf['advanced'] = 'hide';
                }
                if(!isset($jsonf['html']))
                {
                    $jsonf['html'] = '0';
                }
                update_post_meta($forms_id, 'advanced', $jsonf['advanced']);
                update_post_meta($forms_id, 'submit', $jsonf['submit']);
                update_post_meta($forms_id, 'max', $jsonf['max']);
                update_post_meta($forms_id, 'temperature', $jsonf['temperature']);
                update_post_meta($forms_id, 'topp', $jsonf['topp']);
                update_post_meta($forms_id, 'presence', $jsonf['presence']);
                update_post_meta($forms_id, 'frequency', $jsonf['frequency']);
                update_post_meta($forms_id, 'response', $jsonf['response']);
                update_post_meta($forms_id, 'html', $jsonf['html']);
                if(isset($jsonf['streaming_enabled']))
                {
                    update_post_meta($forms_id, 'streaming_enabled', $jsonf['streaming_enabled']);
                }
                else
                {
                    update_post_meta($forms_id, 'streaming_enabled', '0');
                }
                update_post_meta($forms_id, 'type', $jsonf['type']);
                update_post_meta($forms_id, '_aiomaticfields', $jsonf['aiomaticfields']);
            }
        }
        $aiomatic_result['status'] = 'success';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_persona_upload', 'aiomatic_persona_upload');
function aiomatic_persona_upload()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with persona upload');
    if(isset($_FILES['file']) && empty($_FILES['file']['error']))
    {
        $file_name = sanitize_file_name(basename($_FILES['file']['name']));
        $filetype = wp_check_filetype($file_name);
        if($filetype['ext'] !== 'json' && !aiomatic_endsWith($file_name, '.json')){
            $aiomatic_result['msg'] = 'Only files with the json extension are supported, you sent: ' . $file_name;
            wp_send_json($aiomatic_result);
        }
        global $wp_filesystem;
        if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
            include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
            wp_filesystem($creds);
        }
        $fc = $wp_filesystem->get_contents($_FILES['file']['tmp_name']);
        if(empty($fc))
        {
            $aiomatic_result['msg'] = 'Failed to read file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        $fc_dec = json_decode($fc, true);
        if($fc_dec === null)
        {
            $aiomatic_result['msg'] = 'Failed to decode json file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        if(isset($_POST['overwrite']))
        {
            $overwrite = $_POST['overwrite'];
        }
        else
        {
            $overwrite = '0';
        }
        foreach($fc_dec as $jsonf)
        {
            $address_post_id = 0;
            $query = new WP_Query(
                array(
                    'post_type'              => 'aiomatic_personas',
                    'title'                  => $jsonf['name'],
                    'post_status'            => 'any',
                    'posts_per_page'         => 1,
                    'no_found_rows'          => true,
                    'ignore_sticky_posts'    => true,
                    'update_post_term_cache' => false,
                    'update_post_meta_cache' => false,
                    'orderby'                => 'post_date ID',
                    'order'                  => 'ASC',
                )
            );
            
            if ( ! empty( $query->post ) ) {
                if($overwrite != '1')
                {
                    //persona already exists, skipping it
                    continue;
                }
                else
                {
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        $address_post_id = get_the_ID();
                        break;
                    }
                }
            }
            $name    = isset( $jsonf['name'] ) ? sanitize_text_field( $jsonf['name'] ) : '';
            $role    = isset( $jsonf['role'] ) ? sanitize_text_field( $jsonf['role'] ) : '';
            $prompt  = isset( $jsonf['prompt'] ) ? wp_kses_post( $jsonf['prompt'] ) : '';
            $message = isset( $jsonf['message'] ) ? wp_kses_post( $jsonf['message'] ) : '';
            $model   = isset( $jsonf['model'] ) ? sanitize_text_field( $jsonf['model'] ) : '';

            if ( $name === '' ) {
                continue;
            }
            $personas_data = array(
                'post_type' => 'aiomatic_personas',
                'post_title' => $name,
                'post_excerpt' => $role,
                'post_content' => $prompt,
                'post_status' => 'publish'
            );
            $personas_data['ID'] = $address_post_id;
            if($overwrite != '1')
            {
                $personas_id = wp_insert_post($personas_data);
            }
            else
            {
                if(isset($personas_data['ID']) && $personas_data['ID'] != '0')
                {
                    $personas_id = wp_update_post($personas_data);
                }
                else
                {
                    $personas_id = wp_insert_post($personas_data);
                }
            }
            if(is_wp_error($personas_id))
            {
                aiomatic_log_to_file('Failed to import persona: ' . $personas_id->get_error_message());
            }
            elseif($personas_id === 0)
            {
                aiomatic_log_to_file('Failed to insert persona to database: ' . print_r($personas_data, true));
            }
            else 
            {
                if(is_numeric($jsonf['avatar']))
                {
                    if($jsonf['avatar'] > 0)
                    {
                        require_once(ABSPATH . 'wp-admin/includes/image.php');
                        require_once(ABSPATH . 'wp-admin/includes/media.php');
                        set_post_thumbnail( $personas_id, $jsonf['avatar'] );
                    }
                }
                elseif(filter_var($jsonf['avatar'], FILTER_VALIDATE_URL))
                {
                    if (!aiomatic_generate_featured_image($jsonf['avatar'], $personas_id)) 
                    {
                        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) 
                        {
                            aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $jsonf['avatar']);
                        }
                    }
                }
                if(isset($jsonf['message']) && !empty($jsonf['message']))
                {
                    update_post_meta($personas_id, '_persona_first_message', $message);
                }
                if(isset($jsonf['model']) && !empty($jsonf['model']))
                {
                    update_post_meta($personas_id, '_persona_model', $model);
                }
            }
        }
        $aiomatic_result['status'] = 'success';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_assistant_upload', 'aiomatic_assistant_upload');
function aiomatic_assistant_upload()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with assistant upload');
    if(isset($_FILES['file']) && empty($_FILES['file']['error']))
    {
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
            if(aiomatic_is_aiomaticapi_key($token))
            {
                $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for text moderation.';
                wp_send_json($aiomatic_result);
            }
        }
        $file_name = sanitize_file_name(basename($_FILES['file']['name']));
        $filetype = wp_check_filetype($file_name);
        if($filetype['ext'] !== 'json' && !aiomatic_endsWith($file_name, '.json')){
            $aiomatic_result['msg'] = 'Only files with the json extension are supported, you sent: ' . $file_name;
            wp_send_json($aiomatic_result);
        }
        global $wp_filesystem;
        if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
            include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
            wp_filesystem($creds);
        }
        $fc = $wp_filesystem->get_contents($_FILES['file']['tmp_name']);
        if(empty($fc))
        {
            $aiomatic_result['msg'] = 'Failed to read file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        $fc_dec = json_decode($fc, true);
        if($fc_dec === null)
        {
            $aiomatic_result['msg'] = 'Failed to decode json file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        if(isset($_POST['overwrite']))
        {
            $overwrite = $_POST['overwrite'];
        }
        else
        {
            $overwrite = '0';
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        foreach($fc_dec as $jsonf)
        {
            $vector_store_id = '';
            if(empty($jsonf['role']))
            {
                $jsonf['role'] = '';
            }
            if(empty($jsonf['prompt']))
            {
                $jsonf['prompt'] = '';
            }
            if(empty($jsonf['message']))
            {
                $jsonf['message'] = '';
            }
            $existing_openai = false;
            $address_post_id = 0;
            $assistant_id = '';
            $temperature = '';
            $topp = '';
            if(isset($jsonf['temperature']))
            {
                $temperature = $jsonf['temperature'];
            }
            if(isset($jsonf['topp']))
            {
                $topp = $jsonf['topp'];
            }
            if(isset($jsonf['id']) && !empty($jsonf['id']))
            {
                $query = new WP_Query(
                    array(
                        'post_type'              => 'aiomatic_assistants',
                        'meta_query' => array(
                            array(
                                'key'     => '_assistant_id',
                                'value'   => $jsonf['id'],
                                'compare' => 'EXISTS'
                            ),
                        ),
                        'post_status'            => 'any',
                        'posts_per_page'         => 1,
                        'no_found_rows'          => true,
                        'ignore_sticky_posts'    => true,
                        'update_post_term_cache' => false,
                        'update_post_meta_cache' => false,
                        'orderby'                => 'post_date ID',
                        'order'                  => 'ASC',
                    )
                );
                if ( ! empty( $query->post ) ) 
                {
                    if($overwrite != '1')
                    {
                        //assistant already exists, skipping it
                        continue;
                    }
                    else
                    {
                        while ( $query->have_posts() ) {
                            $query->the_post();
                            $address_post_id = get_the_ID();
                            $assistant_id = get_post_meta($address_post_id, '_assistant_id', true);
                            break;
                        }
                    }
                }
                if(!empty($assistant_id))
                {
                    try
                    {
                        $ex_assistant = aiomatic_openai_retrieve_assistant($token, $assistant_id);
                        if(isset($ex_assistant['id']) && $ex_assistant['id'] == $assistant_id)
                        {
                            $existing_openai = true;
                        }
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                }
            }
            if($existing_openai == true)
            {
                $tools = [];
                if($jsonf['code_interpreter'] == 'on')
                {
                    $tools[] = ['type' => 'code_interpreter'];
                }
                if($jsonf['file_search'] == 'on')
                {
                    $tools[] = ['type' => 'file_search'];
                }
                $functions_json = $jsonf['functions'];
                if($functions_json === false)
                {
                    $functions = array();
                }
                else
                {
                    if(is_array($functions_json) && !isset($functions_json['name']))
                    {
                        $functions = $functions_json;
                    }
                    elseif(isset($functions_json['name']))
                    {
                        $functions = array($functions_json);
                    }
                    else
                    {
                        $functions = array();
                    }
                }
                foreach($functions as $func)
                {
                    $tools[] = ['type' => 'function', 'function' => $func];
                }
                try
                {
                    if($address_post_id != '' && $address_post_id != 0)
                    {
                        $vector_store_id = get_post_meta($address_post_id, '_assistant_vector_store_id', true);
                    }
                    $metadata = '';
                    $assistantData = aiomatic_openai_modify_assistant($token, $assistant_id, $jsonf['model'], $jsonf['name'], $jsonf['role'], $jsonf['prompt'], $temperature, $topp, $tools, $jsonf['files'], $metadata, $vector_store_id, $address_post_id);
                    if($assistantData === false)
                    {
                        $aiomatic_result['msg'] = 'Failed to update assistant using the API';
                        wp_send_json($aiomatic_result);
                    }
                }
                catch(Exception $e)
                {
                    $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $tools = [];
                if($jsonf['code_interpreter'] == 'on')
                {
                    $tools[] = ['type' => 'code_interpreter'];
                }
                if($jsonf['file_search'] == 'on')
                {
                    $tools[] = ['type' => 'file_search'];
                }
                $functions_json = $jsonf['functions'];
                if($functions_json === false)
                {
                    $functions = array();
                }
                else
                {
                    if(is_array($functions_json) && !isset($functions_json['name']))
                    {
                        $functions = $functions_json;
                    }
                    elseif(isset($functions_json['name']))
                    {
                        $functions = array($functions_json);
                    }
                    else
                    {
                        $functions = array();
                    }
                }
                foreach($functions as $func)
                {
                    $tools[] = ['type' => 'function', 'function' => $func];
                }
                try
                {
                    $metadata = '';
                    $assistantData = aiomatic_openai_save_assistant(
                        $token,
                        $jsonf['model'],
                        $jsonf['name'],
                        $jsonf['role'],
                        $temperature,
                        $topp,
                        $jsonf['prompt'],
                        $tools,
                        $jsonf['files'],
                        $metadata,
                        $vector_store_id
                    );
                    if($assistantData === false)
                    {
                        $aiomatic_result['msg'] = 'Failed to save assistant using the API';
                        wp_send_json($aiomatic_result);
                    }
                    else
                    {
                        $assistant_id = $assistantData['id'];
                    }
                }
                catch(Exception $e)
                {
                    $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                    wp_send_json($aiomatic_result);
                }
            }
            $assistants_data = array(
                'post_type' => 'aiomatic_assistants',
                'post_title' => $jsonf['name'],
                'post_excerpt' => $jsonf['role'],
                'post_content' => $jsonf['prompt'],
                'post_status' => 'publish'
            );
            $assistants_data['ID'] = $address_post_id;
            remove_filter('content_save_pre', 'wp_filter_post_kses');
            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
            if($overwrite != '1')
            {
                $assistants_id_local = wp_insert_post($assistants_data);
            }
            else
            {
                if(isset($assistants_data['ID']) && $assistants_data['ID'] != '0')
                {
                    $assistants_id_local = wp_update_post($assistants_data);
                }
                else
                {
                    $assistants_id_local = wp_insert_post($assistants_data);
                }
            }
            add_filter('content_save_pre', 'wp_filter_post_kses');
            add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
            if(is_wp_error($assistants_id_local))
            {
                aiomatic_log_to_file('Failed to import assistant: ' . $assistants_id_local->get_error_message());
            }
            elseif($assistants_id_local === 0)
            {
                aiomatic_log_to_file('Failed to insert assistant to database: ' . print_r($assistants_data, true));
            }
            else 
            {
                if(is_numeric($jsonf['avatar']))
                {
                    if($jsonf['avatar'] > 0)
                    {
                        require_once(ABSPATH . 'wp-admin/includes/image.php');
                        require_once(ABSPATH . 'wp-admin/includes/media.php');
                        set_post_thumbnail( $assistants_id_local, $jsonf['avatar'] );
                    }
                }
                elseif(filter_var($jsonf['avatar'], FILTER_VALIDATE_URL))
                {
                    if (!aiomatic_generate_featured_image($jsonf['avatar'], $assistants_id_local)) 
                    {
                        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) 
                        {
                            aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $jsonf['avatar']);
                        }
                    }
                }
                if(isset($jsonf['message']) && !empty($jsonf['message']))
                {
                    update_post_meta($assistants_id_local, '_assistant_first_message', $jsonf['message']);
                }
                else
                {
                    update_post_meta($assistants_id_local, '_assistant_first_message', '');
                }
                update_post_meta($assistants_id_local, '_assistant_id', $assistant_id);
                if(!empty($jsonf['model']))
                {
                    update_post_meta($assistants_id_local, '_assistant_model', $jsonf['model']);
                }
                $tools = [];
                if($jsonf['code_interpreter'] == 'on')
                {
                    $tools[] = ['type' => 'code_interpreter'];
                }
                if($jsonf['file_search'] == 'on')
                {
                    $tools[] = ['type' => 'file_search'];
                }
                $functions_json = $jsonf['functions'];
                if($functions_json === false)
                {
                    $functions = array();
                }
                else
                {
                    if(is_array($functions_json) && !isset($functions_json['name']))
                    {
                        $functions = $functions_json;
                    }
                    elseif(isset($functions_json['name']))
                    {
                        $functions = array($functions_json);
                    }
                    else
                    {
                        $functions = array();
                    }
                }
                foreach($functions as $func)
                {
                    $tools[] = ['type' => 'function', 'function' => $func];
                }
                if(!empty($tools))
                {
                    update_post_meta($assistants_id_local, '_assistant_tools', $tools);
                }
                else
                {
                    update_post_meta($assistants_id_local, '_assistant_tools', array());
                }
                if(!empty($temperature))
                {
                    update_post_meta($assistants_id_local, '_assistant_temperature', $temperature);
                }
                if(!empty($topp))
                {
                    update_post_meta($assistants_id_local, '_assistant_topp', $topp);
                }
                if(!empty($vector_store_id))
                {
                    update_post_meta($assistants_id_local, '_assistant_vector_store_id', $vector_store_id);
                }
                if(!empty($jsonf['files']))
                {
                    update_post_meta($assistants_id_local, '_assistant_files', $jsonf['files']);
                }
                else
                {
                    update_post_meta($assistants_id_local, '_assistant_files', array());
                }
            }
        }
        $aiomatic_result['status'] = 'success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_omni_upload', 'aiomatic_omni_upload');
function aiomatic_omni_upload()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock Template upload');
    if(isset($_FILES['file']) && empty($_FILES['file']['error']))
    {
        $file_name = sanitize_file_name(basename($_FILES['file']['name']));
        $filetype = wp_check_filetype($file_name);
        if($filetype['ext'] !== 'json' && !aiomatic_endsWith($file_name, '.json')){
            $aiomatic_result['msg'] = 'Only files with the json extension are supported, you sent: ' . $file_name;
            wp_send_json($aiomatic_result);
        }
        global $wp_filesystem;
        if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
            include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
            wp_filesystem($creds);
        }
        $fc = $wp_filesystem->get_contents($_FILES['file']['tmp_name']);
        if(empty($fc))
        {
            $aiomatic_result['msg'] = 'Failed to read file: ' . $_FILES['file']['tmp_name'];
            wp_send_json($aiomatic_result);
        }
        $fc = aiomatic_removeBOM($fc);
        $fc_dec = json_decode($fc, true);
        if($fc_dec === null)
        {
            $aiomatic_result['msg'] = 'Failed to decode template json file: ' . $_FILES['file']['tmp_name'] . ' - ' . json_last_error_msg();
            wp_send_json($aiomatic_result);
        }
        if(isset($_POST['overwrite']))
        {
            $overwrite = $_POST['overwrite'];
        }
        else
        {
            $overwrite = '0';
        }
        foreach($fc_dec as $jsonf)
        {
            $address_post_id = 0;
            if(isset($jsonf['id']) && !empty($jsonf['id']))
            {
                $query = new WP_Query(
                    array(
                        'post_type'              => 'aiomatic_omni_temp',
                        'title'                  => $jsonf['name'],
                        'post_status'            => 'any',
                        'posts_per_page'         => 1,
                        'no_found_rows'          => true,
                        'ignore_sticky_posts'    => true,
                        'update_post_term_cache' => false,
                        'update_post_meta_cache' => false,
                        'orderby'                => 'post_date ID',
                        'order'                  => 'ASC',
                    )
                );
                if ( ! empty( $query->post ) ) 
                {
                    if($overwrite != '1')
                    {
                        aiomatic_log_to_file('Template existing, skipping: ' . $jsonf['name']);
                        //template already exists, skipping it
                        continue;
                    }
                    else
                    {
                        while ( $query->have_posts() ) {
                            $query->the_post();
                            $address_post_id = get_the_ID();
                            break;
                        }
                    }
                }
            }
            if(is_array($jsonf['json']))
            {
                $jsonf['json'] = json_encode($jsonf['json']);
            }
            $json_me = addslashes($jsonf['json']);
            if($json_me === false)
            {
                $json_me = $jsonf['json'];
            }
            $omni_data = array(
                'post_type' => 'aiomatic_omni_temp',
                'post_title' => $jsonf['name'],
                'post_content' => $json_me,
                'post_status' => 'publish'
            );
            $omni_data['ID'] = $address_post_id;
            remove_filter('content_save_pre', 'wp_filter_post_kses');
            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
            if($overwrite != '1')
            {
                $omni_id_local = wp_insert_post($omni_data);
            }
            else
            {
                if(isset($omni_data['ID']) && $omni_data['ID'] != '0')
                {
                    $omni_id_local = wp_update_post($omni_data);
                }
                else
                {
                    $omni_id_local = wp_insert_post($omni_data);
                }
            }
            add_filter('content_save_pre', 'wp_filter_post_kses');
            add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
            if(is_wp_error($omni_id_local))
            {
                aiomatic_log_to_file('Failed to import OmniBlock Template: ' . $omni_id_local->get_error_message());
            }
            elseif($omni_id_local === 0)
            {
                aiomatic_log_to_file('Failed to insert OmniBlock Template to database: ' . print_r($omni_data, true));
            }
            else 
            {
                update_post_meta($omni_id_local, 'aiomatic_json', $json_me);
                if(isset($jsonf['category']) && !empty($jsonf['category']))
                {
                    if(!is_array($jsonf['category']))
                    {
                        $terms_array = explode(';', $jsonf['category']);
                    }
                    else
                    {
                        $terms_array = $jsonf['category'];
                    }
                    wp_set_object_terms($omni_id_local, $terms_array, 'ai_template_categories');
                }
            }
        }
        $aiomatic_result['status'] = 'success';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_omni_ai', 'aiomatic_omni_ai');
function aiomatic_omni_ai()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock Template generator');
    if(!isset($_POST['ai_prompt']) || empty($_POST['ai_prompt']))
    {
        $aiomatic_result['msg'] = 'You need to provide a prompt for the AI to generate the OmniBlock Template.';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['omni_name']))
    {
        $aiomatic_result['msg'] = 'You need to provide a name for the OmniBlock Template.';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['ai_model']) || empty($_POST['ai_model']))
    {
        $aiomatic_result['msg'] = 'You need to provide a model for the AI to generate the OmniBlock Template.';
        wp_send_json($aiomatic_result);
    }
    $model = sanitize_text_field(wp_unslash($_POST['ai_model']));
    $name = sanitize_text_field(wp_unslash($_POST['omni_name']));
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
        $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
        if(aiomatic_is_aiomaticapi_key($token))
        {
            $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for text moderation.';
            wp_send_json($aiomatic_result);
        }
    }
    $prompt = sanitize_text_field(wp_unslash($_POST['ai_prompt']));
    $prompt = str_replace('%%example_template%%', '{"name":"Amazon Single Product Review","id":3157,"json":[{"identifier":"ac","name":"Amazon Product Listing","type":"amazon_listing","parameters":{"asin":"%%keyword%%","aff_id":"","target_country":"com","sort_results":"none","max_product_count":"1","listing_template":"%%product_counter%%. %%product_title%% - Link: %%product_url%% - Price: %%product_price%%","critical":"1"}},{"identifier":"2","name":"Process the product details","type":"ai_text","parameters":{"prompt":"Summarize the following product information, make it concise and further usable by an AI writer, List it in a precise format which is easy to be understood, add also the product links to the summary: %%product_listing_ac%%","model":"gpt-4o-mini-1106","assistant_id":"","critical":"0"}},{"identifier":"5","name":"AI Text","type":"ai_text","parameters":{"prompt":"Below are details of a best selling product which matches this search \"%%keyword%%\", that sells on amazon.\nYour first instruction is:\nWrite an introduction for a product review blog post of the product detailed below. Write as if you had first-hand experience with the product you are describing. \n\nWrite the article so that it will pass AI content detection tools and reads as if written by a human, using a mix of long and complex sentences alongside shorter ones.\n\nFormat the article using Markdown format. Include relevant headings and style relevant words and terms in bold, italic, and underline formatting.\n\n<details>\n%%ai_text_2%%\n</details>","model":"gpt-4o-mini-1106","assistant_id":"","critical":"0"}},{"identifier":"6","name":"AI Text","type":"ai_text","parameters":{"prompt":"Below are details of a best selling product which matches this search \"%%keyword%%\", that sells on amazon.\nYou will be creating an in-depth product review article for this product, to help people who are in need of this product to discover it. Your instruction is:\nWrite 5 consecutive headings for a product review article of the below product, that starts with an overview, highlights specific features and aspects of the product, provides detailed insights and specific recommendations. Add each heading on a new line. Don\'t add numbers to the headings, hyphens or any types of quotes. Write as if you had first-hand experience with the product you are describing. Return only the headings list, nothing else.\n\n<details>\n%%ai_text_2%%\n</details>","model":"gpt-4o-mini-1106","assistant_id":"","critical":"0"}},{"identifier":"ad","name":"AI Text For Each Line Of Input","type":"ai_text_foreach","parameters":{"multiline_input":"%%ai_text_6%%","max_runs":"","prepend":"","append":"","prompt":"Below are details of a best selling product which matches this search \"%%keyword%%\", that sells on amazon. You will be creating an in-depth product review article for this product, to help people who are in need of this product to discover it. Your instruction is:\nWrite the content of a product review post, for the following section heading: \"%%current_input_line%%\". This is the %%current_input_line_counter%% section of the article. Don\'t repeat the product title in the created content, also don\'t be repetitive in general. Don\'t add an intro or outro. Use HTML for formatting, include unnumbered lists and bold. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple.\n\n<details>\n%%ai_text_2%%\n</details>","model":"gpt-4o-mini","assistant_id":"","critical":"0"}},{"identifier":"8","name":"AI Text","type":"ai_text","parameters":{"prompt":"Below are details of a best selling product which matches this search \"%%keyword%%\", that sells on amazon.\nYour first instruction is:\nWrite an outro for a product review blog post of the product detailed below. Write as if you had first-hand experience with the product you are describing. \n\nWrite the article so that it will pass AI content detection tools and reads as if written by a human, using a mix of long and complex sentences alongside shorter ones.\n\nFormat the article using Markdown format. Include relevant headings and style relevant words and terms in bold, italic, and underline formatting.\n\n<details>\n%%ai_text_2%%\n</details>","model":"gpt-4o-mini-1106","assistant_id":"","critical":"0"}},{"identifier":"3","name":"Generate featured image","type":"dalle_ai_image","parameters":{"prompt":"Generate a high-resolution, visually compelling image that creatively interprets the theme encapsulated by this keyword: \"%%keyword%%\". The image should be versatile enough to fit various niches, from technology and lifestyle to nature and science. It should feature a central, eye-catching element that abstractly represents the topic, surrounded by relevant, subtler motifs that provide context and depth. The composition should be balanced and aesthetically pleasing, with a harmonious color palette that complements the mood of the title. The artwork should be suitable for use as a captivating header image for a blog post.","model":"gpt-image-2","image_size":"1024x1024","critical":"0"}},{"identifier":"ab","name":"Create title","type":"ai_text","parameters":{"prompt":"Write a single engaging WordPress post title for the below text. Don\'t return anything else, just the title. The text is:\n%%ai_text_2%%","model":"gpt-4o-mini-1106","assistant_id":"","critical":"0"}},{"identifier":"4","name":"Publish post","type":"save_post","parameters":{"post_title":"%%ai_text_ab%%","post_content":"%%ai_text_5%%<br />%%ai_text_foreach_ad%%<br />%%ai_text_8%%","post_excerpt":"","post_author":"","post_status":"publish","post_type":"post","post_format":"post-format-standard","post_parent":"","post_comments":"0","post_pingbacks":"0","post_date":"","post_custom_fields":"","post_slug":"","post_custom_taxonomies":"","post_lang":"","post_categories":"","post_tags":"","content_regex":"","replace_regex":"","overwrite_existing":"0","featured_image":"%%dalle_image_3%%","critical":"0"}}],"category":[]}', $prompt);
    $all_omni_types = json_encode(aiomatic_omniblocks_default_block_types());
    if(empty($all_omni_types))
    {
        $all_omni_types = '';
    }
    $prompt = str_replace('%%all_omniblock_types%%', $all_omni_types, $prompt);
    $prompt = str_replace('%%title%%', $name, $prompt);

    if(isset($_POST['overwrite']))
    {
        $overwrite = $_POST['overwrite'];
    }
    else
    {
        $overwrite = '0';
    }
    $address_post_id = 0;
    $query = new WP_Query(
        array(
            'post_type'              => 'aiomatic_omni_temp',
            'title'                  => $name,
            'post_status'            => 'any',
            'posts_per_page'         => 1,
            'no_found_rows'          => true,
            'ignore_sticky_posts'    => true,
            'update_post_term_cache' => false,
            'update_post_meta_cache' => false,
            'orderby'                => 'post_date ID',
            'order'                  => 'ASC',
        )
    );
    if ( ! empty( $query->post ) ) 
    {
        if($overwrite != '1')
        {
            $aiomatic_result['msg'] = 'Template existing, skipping: ' . $name;
            wp_send_json($aiomatic_result);
        }
        else
        {
            while ( $query->have_posts() ) {
                $query->the_post();
                $address_post_id = get_the_ID();
                break;
            }
        }
    }
    
    $all_models = aiomatic_get_all_models(true);
    if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    $query_token_count = count(aiomatic_encode($prompt));
    $max_tokens = aimogenpro_get_max_tokens($model);
    $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
    if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
    {
        $string_len = aiomatic_strlen($prompt);
        $string_len = $string_len / 2;
        $string_len = intval(0 - $string_len);
        $prompt = aiomatic_substr($prompt, 0, $string_len);
        $prompt = trim($prompt);
        $query_token_count = count(aiomatic_encode($prompt));
        $available_tokens = $max_tokens - $query_token_count;
    }

    $fc = '';
    if(!empty($prompt))
    {
        $thread_id = '';
        $aierror = '';
        $finish_reason = '';
        $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, 1, 1, 0, 0, false, 'omniBlockTemplateGenerator', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', '', $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
        if($generated_text === false)
        {
            $aiomatic_result['msg'] = 'Failed to generate an OmniBlock template using AI.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $fc = aiomatic_sanitize_ai_result($generated_text);
            $fc = str_replace('```json', '', $fc);
            $fc = str_replace('```', '', $fc);
            $fc = trim($fc);
        }
    }
    else
    {
        $aiomatic_result['msg'] = 'Incorrect prompt provided for OmniBlock Template generation.';
        wp_send_json($aiomatic_result);
    }

    $jsonf = json_decode($fc, true);
    if($jsonf === null)
    {
        $aiomatic_result['msg'] = 'Failed to decode AI generated template: ' . json_last_error_msg() . ' - ' . $fc;
        wp_send_json($aiomatic_result);
    }
    if(!isset($jsonf['json']) || empty($jsonf['json']) || !is_array($jsonf['json']))
    {
        $aiomatic_result['msg'] = 'AI generated OmniBlock Template does not contain a json.';
        wp_send_json($aiomatic_result);
    }
    $save_json = json_encode($jsonf['json']);
    $save_json = addslashes($save_json);
    if(empty($save_json))
    {
        $aiomatic_result['msg'] = 'Failed to encode OmniBlock Template JSON!';
        wp_send_json($aiomatic_result);
    }
    $omni_data = array(
        'post_type' => 'aiomatic_omni_temp',
        'post_title' => $name,
        'post_content' => $save_json,
        'post_status' => 'publish'
    );
    $omni_data['ID'] = $address_post_id;
    remove_filter('content_save_pre', 'wp_filter_post_kses');
    remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
    if($overwrite != '1')
    {
        $omni_id_local = wp_insert_post($omni_data);
    }
    else
    {
        if(isset($omni_data['ID']) && $omni_data['ID'] != '0')
        {
            $omni_id_local = wp_update_post($omni_data);
        }
        else
        {
            $omni_id_local = wp_insert_post($omni_data);
        }
    }
    add_filter('content_save_pre', 'wp_filter_post_kses');
    add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
    if(is_wp_error($omni_id_local))
    {
        aiomatic_log_to_file('Failed to import OmniBlock Template: ' . $omni_id_local->get_error_message());
    }
    elseif($omni_id_local === 0)
    {
        aiomatic_log_to_file('Failed to insert OmniBlock Template to database: ' . print_r($omni_data, true));
    }
    else 
    {
        update_post_meta($omni_id_local, 'aiomatic_json', $save_json);
        if(isset($jsonf['category']) && !empty($jsonf['category']))
        {
            if(!is_array($jsonf['category']))
            {
                $terms_array = explode(';', $jsonf['category']);
            }
            else
            {
                $terms_array = $jsonf['category'];
            }
            wp_set_object_terms($omni_id_local, $terms_array, 'ai_template_categories');
        }
    }
    
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_default_form', 'aiomatic_default_form');
function aiomatic_default_form()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with default forms');
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $default_file = dirname(__FILE__) . "/defaults/form-defaults.json";
    if(!$wp_filesystem->exists($default_file))
    {
        $aiomatic_result['msg'] = 'Default form json file not found: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc = $wp_filesystem->get_contents($default_file);
    if(empty($fc))
    {
        $aiomatic_result['msg'] = 'Failed to read file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc_dec = json_decode($fc, true);
    if($fc_dec === null)
    {
        $aiomatic_result['msg'] = 'Failed to decode json file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $overwrite = '0';
    foreach($fc_dec as $jsonf)
    {
        $address_post_id = 0;
        $query = new WP_Query(
            array(
                'post_type'              => 'aiomatic_forms',
                'title'                  => $jsonf['title'],
                'post_status'            => 'any',
                'posts_per_page'         => 1,
                'no_found_rows'          => true,
                'ignore_sticky_posts'    => true,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false,
                'orderby'                => 'post_date ID',
                'order'                  => 'ASC',
            )
        );
        
        if ( ! empty( $query->post ) ) {
            if($overwrite != '1')
            {
                //form already exists, skipping it
                continue;
            }
            else
            {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    $address_post_id = get_the_ID();
                    break;
                }
            }
        }
        $forms_data = array(
            'post_type' => 'aiomatic_forms',
            'post_title' => $jsonf['title'],
            'post_content' => $jsonf['description'],
            'post_status' => 'publish'
        );
        $forms_data['ID'] = $address_post_id;
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        if($overwrite != '1')
        {
            $forms_id = wp_insert_post($forms_data);
        }
        else
        {
            if(isset($forms_data['ID']) && $forms_data['ID'] != '0')
            {
                $forms_id = wp_update_post($forms_data);
            }
            else
            {
                $forms_id = wp_insert_post($forms_data);
            }
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($forms_id))
        {
            aiomatic_log_to_file('Failed to import form: ' . $forms_id->get_error_message());
        }
        elseif($forms_id === 0)
        {
            aiomatic_log_to_file('Failed to insert form to database: ' . print_r($forms_data, true));
        }
        else 
        {
            update_post_meta($forms_id, 'prompt', $jsonf['prompt']);
            update_post_meta($forms_id, 'model', $jsonf['model']);
            if(isset($jsonf['assistant_id']))
            {
                update_post_meta($forms_id, 'assistant_id', $jsonf['assistant_id']);
            }
            update_post_meta($forms_id, 'header', $jsonf['header']);
            if(!isset($jsonf['editor']))
            {
                $jsonf['editor'] = 'textarea';
            }
            update_post_meta($forms_id, 'editor', $jsonf['editor']);
            if(!isset($jsonf['advanced']))
            {
                $jsonf['advanced'] = 'hide';
            }
            if(!isset($jsonf['html']))
            {
                $jsonf['html'] = '0';
            }
            update_post_meta($forms_id, 'advanced', $jsonf['advanced']);
            update_post_meta($forms_id, 'submit', $jsonf['submit']);
            update_post_meta($forms_id, 'max', $jsonf['max']);
            update_post_meta($forms_id, 'temperature', $jsonf['temperature']);
            update_post_meta($forms_id, 'topp', $jsonf['topp']);
            update_post_meta($forms_id, 'presence', $jsonf['presence']);
            update_post_meta($forms_id, 'frequency', $jsonf['frequency']);
            update_post_meta($forms_id, 'response', $jsonf['response']);
            update_post_meta($forms_id, 'html', $jsonf['html']);
            if(isset($jsonf['streaming_enabled']))
            {
                update_post_meta($forms_id, 'streaming_enabled', $jsonf['streaming_enabled']);
            }
            else
            {
                update_post_meta($forms_id, 'streaming_enabled', '0');
            }
            update_post_meta($forms_id, 'type', $jsonf['type']);
            update_post_meta($forms_id, '_aiomaticfields', $jsonf['aiomaticfields']);
        }
    }
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_create_openai_vector_store_ajax', 'aiomatic_create_openai_vector_store_ajax');

function aiomatic_create_openai_vector_store_ajax()
{
    check_ajax_referer('aiomatic_openai_vector_store_nonce', 'nonce');

    if(!current_user_can('manage_options'))
    {
        wp_send_json_error(array(
            'message' => 'Permission denied.'
        ));
    }
    require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php");
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);

    if(!isset($aiomatic_Main_Settings['app_id']) || trim($aiomatic_Main_Settings['app_id']) == '')
    {
        wp_send_json_error(array(
            'message' => 'Missing OpenAI API key.'
        ));
    }

    if(!function_exists('aiomatic_openai_create_vector_store'))
    {
        wp_send_json_error(array(
            'message' => 'OpenAI Vector Store helper function not found.'
        ));
    }

    $name = isset($_POST['name']) ? sanitize_text_field(wp_unslash($_POST['name'])) : '';

    if($name == '')
    {
        $name = 'Aiomatic Vector Store';
    }

    try
    {
        $created_store = aiomatic_openai_create_vector_store(trim($aiomatic_Main_Settings['app_id']), $name);

        if(empty($created_store['id']))
        {
            wp_send_json_error(array(
                'message' => 'Vector Store created, but no ID was returned.'
            ));
        }

        wp_send_json_success(array(
            'id' => $created_store['id'],
            'name' => !empty($created_store['name']) ? $created_store['name'] : $name
        ));
    }
    catch(Exception $e)
    {
        wp_send_json_error(array(
            'message' => $e->getMessage()
        ));
    }
}
add_action('wp_ajax_aiomatic_default_assistant', 'aiomatic_default_assistant');
function aiomatic_default_assistant()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with default assistants');
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
        $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
        if(aiomatic_is_aiomaticapi_key($token))
        {
            $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for text moderation.';
            wp_send_json($aiomatic_result);
        }
    }
    $default_file = dirname(__FILE__) . "/defaults/assistant-defaults.json";
    if(!$wp_filesystem->exists($default_file))
    {
        $aiomatic_result['msg'] = 'Default assistant json file not found: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc = $wp_filesystem->get_contents($default_file);
    if(empty($fc))
    {
        $aiomatic_result['msg'] = 'Failed to read file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc_dec = json_decode($fc, true);
    if($fc_dec === false || $fc_dec === null)
    {
        $aiomatic_result['msg'] = 'Failed to decode json file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $overwrite = '0';
    require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
    foreach($fc_dec as $jsonf)
    {
        $vector_store_id = '';
        if(empty($jsonf['role']))
        {
            $jsonf['role'] = '';
        }
        if(empty($jsonf['prompt']))
        {
            $jsonf['prompt'] = '';
        }
        if(empty($jsonf['message']))
        {
            $jsonf['message'] = '';
        }
        $existing_openai = false;
        $address_post_id = 0;
        $assistant_id = '';
        $temperature = '';
        $topp = '';
        if(isset($jsonf['temperature']))
        {
            $temperature = $jsonf['temperature'];
        }
        if(isset($jsonf['topp']))
        {
            $topp = $jsonf['topp'];
        }
        if(isset($jsonf['id']) && !empty($jsonf['id']))
        {
            $query = new WP_Query(
                array(
                    'post_type'              => 'aiomatic_assistants',
                    'meta_query' => array(
                        array(
                            'key'     => '_assistant_id',
                            'value'   => $jsonf['id'],
                            'compare' => 'EXISTS'
                        ),
                    ),
                    'post_status'            => 'any',
                    'posts_per_page'         => 1,
                    'no_found_rows'          => true,
                    'ignore_sticky_posts'    => true,
                    'update_post_term_cache' => false,
                    'update_post_meta_cache' => false,
                    'orderby'                => 'post_date ID',
                    'order'                  => 'ASC',
                )
            );
            if ( ! empty( $query->post ) ) 
            {
                if($overwrite != '1')
                {
                    //assistant already exists, skipping it
                    continue;
                }
                else
                {
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        $address_post_id = get_the_ID();
                        $assistant_id = get_post_meta($address_post_id, '_assistant_id', true);
                        break;
                    }
                }
            }
            if(!empty($assistant_id))
            {
                try
                {
                    $ex_assistant = aiomatic_openai_retrieve_assistant($token, $assistant_id);
                    if(isset($ex_assistant['id']) && $ex_assistant['id'] == $assistant_id)
                    {
                        $existing_openai = true;
                    }
                }
                catch(Exception $e)
                {
                    $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                    wp_send_json($aiomatic_result);
                }
            }
        }
        if($existing_openai == true)
        {
            $tools = [];
            if($jsonf['code_interpreter'] == 'on')
            {
                $tools[] = ['type' => 'code_interpreter'];
            }
            if($jsonf['file_search'] == 'on')
            {
                $tools[] = ['type' => 'file_search'];
            }
            $functions_json = $jsonf['functions'];
            if($functions_json === false)
            {
                $functions = array();
            }
            else
            {
                if(is_array($functions_json) && !isset($functions_json['name']))
                {
                    $functions = $functions_json;
                }
                elseif(isset($functions_json['name']))
                {
                    $functions = array($functions_json);
                }
                else
                {
                    $functions = array();
                }
            }
            foreach($functions as $func)
            {
                $tools[] = ['type' => 'function', 'function' => $func];
            }
            try
            {
                if($address_post_id != '' && $address_post_id != 0)
                {
                    $vector_store_id = get_post_meta($address_post_id, '_assistant_vector_store_id', true);
                }
                $metadata = '';
                $assistantData = aiomatic_openai_modify_assistant($token, $assistant_id, $jsonf['model'], $jsonf['name'], $jsonf['role'], $jsonf['prompt'], $temperature, $topp, $tools, $jsonf['files'], $metadata, $vector_store_id, $address_post_id);
                if($assistantData === false)
                {
                    $aiomatic_result['msg'] = 'Failed to update assistant using the API';
                    wp_send_json($aiomatic_result);
                }
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $tools = [];
            if($jsonf['code_interpreter'] == 'on')
            {
                $tools[] = ['type' => 'code_interpreter'];
            }
            if($jsonf['file_search'] == 'on')
            {
                $tools[] = ['type' => 'file_search'];
            }
            $functions_json = $jsonf['functions'];
            if($functions_json === false)
            {
                $functions = array();
            }
            else
            {
                if(is_array($functions_json) && !isset($functions_json['name']))
                {
                    $functions = $functions_json;
                }
                elseif(isset($functions_json['name']))
                {
                    $functions = array($functions_json);
                }
                else
                {
                    $functions = array();
                }
            }
            foreach($functions as $func)
            {
                $tools[] = ['type' => 'function', 'function' => $func];
            }
            try
            {
                $metadata = '';
                $assistantData = aiomatic_openai_save_assistant(
                    $token,
                    $jsonf['model'],
                    $jsonf['name'],
                    $jsonf['role'],
                    $temperature,
                    $topp,
                    $jsonf['prompt'],
                    $tools,
                    $jsonf['files'],
                    $metadata,
                    $vector_store_id
                );
                if($assistantData === false)
                {
                    $aiomatic_result['msg'] = 'Failed to save assistant using the API';
                    wp_send_json($aiomatic_result);
                }
                else
                {
                    $assistant_id = $assistantData['id'];
                }
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to retrieve assistant using the API '  . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
        }
        $assistants_data = array(
            'post_type' => 'aiomatic_assistants',
            'post_title' => $jsonf['name'],
            'post_excerpt' => $jsonf['role'],
            'post_content' => $jsonf['prompt'],
            'post_status' => 'publish'
        );
        $assistants_data['ID'] = $address_post_id;
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        if($overwrite != '1')
        {
            $assistants_id_local = wp_insert_post($assistants_data);
        }
        else
        {
            if(isset($assistants_data['ID']) && $assistants_data['ID'] != '0')
            {
                $assistants_id_local = wp_update_post($assistants_data);
            }
            else
            {
                $assistants_id_local = wp_insert_post($assistants_data);
            }
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($assistants_id_local))
        {
            aiomatic_log_to_file('Failed to import assistant: ' . $assistants_id_local->get_error_message());
        }
        elseif($assistants_id_local === 0)
        {
            aiomatic_log_to_file('Failed to insert assistant to database: ' . print_r($assistants_data, true));
        }
        else 
        {
            if(is_numeric($jsonf['avatar']))
            {
                if($jsonf['avatar'] > 0)
                {
                    require_once(ABSPATH . 'wp-admin/includes/image.php');
                    require_once(ABSPATH . 'wp-admin/includes/media.php');
                    set_post_thumbnail( $assistants_id_local, $jsonf['avatar'] );
                }
            }
            elseif(filter_var($jsonf['avatar'], FILTER_VALIDATE_URL))
            {
                if (!aiomatic_generate_featured_image($jsonf['avatar'], $assistants_id_local)) 
                {
                    if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) 
                    {
                        aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $jsonf['avatar']);
                    }
                }
            }
            if(isset($jsonf['message']) && !empty($jsonf['message']))
            {
                update_post_meta($assistants_id_local, '_assistant_first_message', $jsonf['message']);
            }
            else
            {
                update_post_meta($assistants_id_local, '_assistant_first_message', '');
            }
            update_post_meta($assistants_id_local, '_assistant_id', $assistant_id);
            if(!empty($jsonf['model']))
            {
                update_post_meta($assistants_id_local, '_assistant_model', $jsonf['model']);
            }
            $tools = [];
            if($jsonf['code_interpreter'] == 'on')
            {
                $tools[] = ['type' => 'code_interpreter'];
            }
            if($jsonf['file_search'] == 'on')
            {
                $tools[] = ['type' => 'file_search'];
            }
            $functions_json = $jsonf['functions'];
            if($functions_json === false)
            {
                $functions = array();
            }
            else
            {
                if(is_array($functions_json) && !isset($functions_json['name']))
                {
                    $functions = $functions_json;
                }
                elseif(isset($functions_json['name']))
                {
                    $functions = array($functions_json);
                }
                else
                {
                    $functions = array();
                }
            }
            foreach($functions as $func)
            {
                $tools[] = ['type' => 'function', 'function' => $func];
            }
            if(!empty($tools))
            {
                update_post_meta($assistants_id_local, '_assistant_tools', $tools);
            }
            else
            {
                update_post_meta($assistants_id_local, '_assistant_tools', array());
            }
            if(!empty($vector_store_id))
            {
                update_post_meta($assistants_id_local, '_assistant_vector_store_id', $vector_store_id);
            }
            if(!empty($temperature))
            {
                update_post_meta($assistants_id_local, '_assistant_temperature', $temperature);
            }
            if(!empty($topp))
            {
                update_post_meta($assistants_id_local, '_assistant_topp', $topp);
            }
            if(!empty($jsonf['files']))
            {
                update_post_meta($assistants_id_local, '_assistant_files', $jsonf['files']);
            }
            else
            {
                update_post_meta($assistants_id_local, '_assistant_files', array());
            }
        }
    }
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_default_omni', 'aiomatic_default_omni');
function aiomatic_default_omni()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with default OmniBlock templates');
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $default_file = dirname(__FILE__) . "/defaults/omni-templates-defaults.json";
    if(!$wp_filesystem->exists($default_file))
    {
        $aiomatic_result['msg'] = 'Default OmniBlock templates json file not found: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc = $wp_filesystem->get_contents($default_file);
    if(empty($fc))
    {
        $aiomatic_result['msg'] = 'Failed to read file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc_dec = json_decode($fc, true);
    if($fc_dec === false || $fc_dec === null)
    {
        $aiomatic_result['msg'] = 'Failed to decode json file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $overwrite = '0';
    foreach($fc_dec as $jsonf)
    {
        $address_post_id = 0;
        if(isset($jsonf['id']) && !empty($jsonf['id']))
        {
            $query = new WP_Query(
                array(
                    'post_type'              => 'aiomatic_omni_temp',
                    'post_status'            => 'any',
                    'title'                  => $jsonf['name'],
                    'posts_per_page'         => 1,
                    'no_found_rows'          => true,
                    'ignore_sticky_posts'    => true,
                    'update_post_term_cache' => false,
                    'update_post_meta_cache' => false,
                    'orderby'                => 'post_date ID',
                    'order'                  => 'ASC',
                )
            );
            if ( ! empty( $query->post ) ) 
            {
                if($overwrite != '1')
                {
                    //template already exists, skipping it
                    continue;
                }
                else
                {
                    while ( $query->have_posts() ) {
                        $query->the_post();
                        $address_post_id = get_the_ID();
                        break;
                    }
                }
            }
        }
        if(is_array($jsonf['json']))
        {
            $jsonf['json'] = json_encode($jsonf['json']);
        }
        $json_me = addslashes($jsonf['json']);
        if($json_me === false)
        {
            $json_me = $jsonf['json'];
        }
        $omni_data = array(
            'post_type' => 'aiomatic_omni_temp',
            'post_title' => $jsonf['name'],
            'post_content' => $json_me,
            'post_status' => 'publish'
        );
        $omni_data['ID'] = $address_post_id;
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        if($overwrite != '1')
        {
            $omni_id_local = wp_insert_post($omni_data);
        }
        else
        {
            if(isset($omni_data['ID']) && $omni_data['ID'] != '0')
            {
                $omni_id_local = wp_update_post($omni_data);
            }
            else
            {
                $omni_id_local = wp_insert_post($omni_data);
            }
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($omni_id_local))
        {
            aiomatic_log_to_file('Failed to import OmniBlock Template: ' . $omni_id_local->get_error_message());
        }
        elseif($omni_id_local === 0)
        {
            aiomatic_log_to_file('Failed to insert OmniBlock Template to database: ' . print_r($omni_data, true));
        }
        else 
        {
            update_post_meta($omni_id_local, 'aiomatic_json', $json_me);
            if(isset($jsonf['category']) && !empty($jsonf['category']))
            {
                if(!is_array($jsonf['category']))
                {
                    $terms_array = explode(';', $jsonf['category']);
                }
                else
                {
                    $terms_array = $jsonf['category'];
                }
                wp_set_object_terms($omni_id_local, $terms_array, 'ai_template_categories');
            }
        }
    }
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_default_persona', 'aiomatic_default_persona');
function aiomatic_default_persona()
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with default personas');
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $default_file = dirname(__FILE__) . "/defaults/persona-defaults.json";
    if(!$wp_filesystem->exists($default_file))
    {
        $aiomatic_result['msg'] = 'Default persona json file not found: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc = $wp_filesystem->get_contents($default_file);
    if(empty($fc))
    {
        $aiomatic_result['msg'] = 'Failed to read file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $fc_dec = json_decode($fc, true);
    if($fc_dec === false || $fc_dec === null)
    {
        $aiomatic_result['msg'] = 'Failed to decode json file: ' . $default_file;
        wp_send_json($aiomatic_result);
    }
    $overwrite = '0';
    foreach($fc_dec as $jsonf)
    {
        $address_post_id = 0;
        $query = new WP_Query(
            array(
                'post_type'              => 'aiomatic_personas',
                'title'                  => $jsonf['name'],
                'post_status'            => 'any',
                'posts_per_page'         => 1,
                'no_found_rows'          => true,
                'ignore_sticky_posts'    => true,
                'update_post_term_cache' => false,
                'update_post_meta_cache' => false,
                'orderby'                => 'post_date ID',
                'order'                  => 'ASC',
            )
        );
        
        if ( ! empty( $query->post ) ) {
            if($overwrite != '1')
            {
                //persona already exists, skipping it
                continue;
            }
            else
            {
                while ( $query->have_posts() ) {
                    $query->the_post();
                    $address_post_id = get_the_ID();
                    break;
                }
            }
        }
        $personas_data = array(
            'post_type' => 'aiomatic_personas',
            'post_title' => $jsonf['name'],
            'post_excerpt' => $jsonf['role'],
            'post_content' => $jsonf['prompt'],
            'post_status' => 'publish'
        );
        $personas_data['ID'] = $address_post_id;
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        if($overwrite != '1')
        {
            $personas_id = wp_insert_post($personas_data);
        }
        else
        {
            if(isset($personas_data['ID']) && $personas_data['ID'] != '0')
            {
                $personas_id = wp_update_post($personas_data);
            }
            else
            {
                $personas_id = wp_insert_post($personas_data);
            }
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($personas_id))
        {
            aiomatic_log_to_file('Failed to import persona: ' . $personas_id->get_error_message());
        }
        elseif($personas_id === 0)
        {
            aiomatic_log_to_file('Failed to insert persona to database: ' . print_r($personas_data, true));
        }
        else 
        {
            if(isset($jsonf['avatar']))
            {
                if(is_numeric($jsonf['avatar']))
                {
                    if($jsonf['avatar'] > 0)
                    {
                        require_once(ABSPATH . 'wp-admin/includes/image.php');
                        require_once(ABSPATH . 'wp-admin/includes/media.php');
                        set_post_thumbnail( $personas_id, $jsonf['avatar'] );
                    }
                }
                elseif(filter_var($jsonf['avatar'], FILTER_VALIDATE_URL))
                {
                    if (!aiomatic_generate_featured_image($jsonf['avatar'], $personas_id)) 
                    {
                        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) 
                        {
                            aiomatic_log_to_file('aiomatic_generate_featured_image failed for ' . $jsonf['avatar']);
                        }
                    }
                }
            }
            if(isset($jsonf['message']) && !empty($jsonf['message']))
            {
                update_post_meta($personas_id, '_persona_first_message', $jsonf['message']);
            }
            if(isset($jsonf['model']) && !empty($jsonf['model']))
            {
                update_post_meta($personas_id, '_persona_model', $jsonf['model']);
            }
        }
    }
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_save_image', 'aiomatic_save_image');
function aiomatic_save_image() {
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	$imagesrc = $_POST['imagesrc'];
    if(empty($imagesrc))
    {
        wp_send_json_error(array('error' => 'No image argument data found'));
    }
	$post_id = $_POST['post_id'];
    if(isset($_POST['orig_prompt']))
    {
	    $orig_prompt = $_POST['orig_prompt'];
    }
    else
    {
        $orig_prompt = 'image';
    }
    if(empty($post_id))
    {
        $post_id = null;
    }
    $size = 'full';
    $localpath = false;
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(aiomatic_substr( $imagesrc, 0, 21 ) === "data:image/png;base64")
    {
        $attachment_id = aiomatic_upload_base64_image($imagesrc, $orig_prompt, $post_id);
        if ( is_wp_error( $attachment_id ) ) {
            wp_send_json_error( $attachment_id );
        }
        if (  $attachment_id === false ) {
            wp_send_json_error(array('error' => 'Failed to upload image'));
        }
        $alt = wp_strip_all_tags( $orig_prompt, true );
        update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $alt ) );
        list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $size );
        wp_send_json_success( compact( 'attachment_id', 'url', 'width', 'height', 'size' ) );
    }
    else
    {
        if ((isset($aiomatic_Main_Settings['ai_resize_height']) && $aiomatic_Main_Settings['ai_resize_height'] !== '') || (isset($aiomatic_Main_Settings['ai_resize_width']) && $aiomatic_Main_Settings['ai_resize_width'] !== ''))
        {
            try
            {
                if (isset($aiomatic_Main_Settings['copy_locally']) && ($aiomatic_Main_Settings['copy_locally'] == 'on' || $aiomatic_Main_Settings['copy_locally'] == 'amazon' || $aiomatic_Main_Settings['copy_locally'] == 'wasabi' || $aiomatic_Main_Settings['copy_locally'] == 'generics3' || $aiomatic_Main_Settings['copy_locally'] == 'digital'))
                {
                    $attached_id = '';
                    $localpath = aiomatic_copy_image_locally($imagesrc, $attached_id, '', false, $orig_prompt);
                    if(isset($localpath[1]) && $localpath !== false)
                    {
                        if(!class_exists('\Eventviva\ImageResize')){require_once (dirname(__FILE__) . "/res/ImageResize/ImageResize.php");}
                        $imageRes = new ImageResize($localpath[1]);
                        if (isset($aiomatic_Main_Settings['ai_resize_quality']) && $aiomatic_Main_Settings['ai_resize_quality'] !== '')
                        {
                            $imageRes->quality_jpg = intval($aiomatic_Main_Settings['ai_resize_quality']);
                        }
                        else
                        {
                            $imageRes->quality_jpg = 100;
                        }
                        if ((isset($aiomatic_Main_Settings['ai_resize_height']) && $aiomatic_Main_Settings['ai_resize_height'] !== '') && (isset($aiomatic_Main_Settings['ai_resize_width']) && $aiomatic_Main_Settings['ai_resize_width'] !== ''))
                        {
                            $imageRes->resizeToBestFit($aiomatic_Main_Settings['ai_resize_width'], $aiomatic_Main_Settings['ai_resize_height'], true);
                        }
                        elseif (isset($aiomatic_Main_Settings['ai_resize_width']) && $aiomatic_Main_Settings['ai_resize_width'] !== '')
                        {
                            $imageRes->resizeToWidth($aiomatic_Main_Settings['ai_resize_width'], true);
                        }
                        elseif (isset($aiomatic_Main_Settings['ai_resize_height']) && $aiomatic_Main_Settings['ai_resize_height'] !== '')
                        {
                            $imageRes->resizeToHeight($aiomatic_Main_Settings['ai_resize_height'], true);
                        }
                        $imageRes->save($localpath[1]);
                    }
                }
            }
            catch(Exception $e)
            {
                aiomatic_log_to_file('Failed to resize AI generated image: ' . $localpath[0] . ' to sizes ' . $aiomatic_Main_Settings['ai_resize_width'] . ' - ' . $aiomatic_Main_Settings['ai_resize_height'] . '. Exception thrown ' . esc_html($e->getMessage()) . '!');
            }
        }
        if(isset($localpath[0]))
        {
            $imagesrc = $localpath[0];
        }
        $file_name_is = aiomatic_extract_keywords_from_prompt($orig_prompt);
        $attachment_id = aiomatic_media_sideload_image( $imagesrc, $post_id, $orig_prompt, 'id', $file_name_is );
        if ( is_wp_error( $attachment_id ) ) {
            wp_send_json_error( $attachment_id );
        }
        $alt = wp_strip_all_tags( $orig_prompt, true );
        update_post_meta( $attachment_id, '_wp_attachment_image_alt', wp_slash( $alt ) );
        list( $url, $width, $height ) = wp_get_attachment_image_src( $attachment_id, $size );
        wp_send_json_success( compact( 'attachment_id', 'url', 'width', 'height', 'size' ) );
    }
    die();
}

add_action('wp_ajax_aiomatic_generate_image_ajax', 'aiomatic_generate_image_ajax');
function aiomatic_generate_image_ajax() {
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with image generator');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for AI generated images';
        wp_send_json($aiomatic_result);
    }
    $ai_model = $_POST['ai_model'];
    if($ai_model == 'stable')
    {
        if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for stable images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if(!empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $instruction = stripslashes($_POST['instruction']);
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['stability_app_id']) || trim($aiomatic_Main_Settings['stability_app_id']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid Stability.AI API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_stable_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if($image_size == '512x512')
        {
            $width = '512';
            $height = '512';
        }
        elseif($image_size == '1024x1024')
        {
            $width = '1024';
            $height = '1024';
        }
        else
        {
            $wds = explode('x', $image_size);
            if(isset($wds[1]))
            {
                $width = trim($wds[0]);
                $height = trim($wds[1]);
            }
            else
            {
                $width = '512';
                $height = '512';
            }
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_stability_image($instruction, $height, $width, 'mediaLibraryStableImage', 0, true, $ierror, false, false, '', 0);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_stable_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    elseif($ai_model == 'google')
    {
        if(!isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for stable images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if(!empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $instruction = stripslashes($_POST['instruction']);
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['app_id_google']) || trim($aiomatic_Main_Settings['app_id_google']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid Google AI Studio API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_stable_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '') 
        {
            $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
        }
        else
        {
            $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_image_google($instruction, $model, 'mediaLibraryGoogleImage', true, $ierror, false);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_stable_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    elseif($ai_model == 'midjourney')
    {
        if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for midjourney images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if($user_token_cap_per_day != '' && !empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $image_size = str_replace('??', 'x', $image_size);
        $instruction = stripslashes($_POST['instruction']);
        $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
        if(!in_array($image_size, $sizes))
        {
            $aiomatic_result['msg'] = 'Incorrect Midjourney image size provided: ' . $image_size;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['midjourney_app_id']) || trim($aiomatic_Main_Settings['midjourney_app_id']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid GoAPI (midjourney) API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_midjourney_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if($image_size == '512x512')
        {
            $width = '512';
            $height = '512';
        }
        elseif($image_size == '1024x1024')
        {
            $width = '1024';
            $height = '1024';
        }
        else
        {
            $width = '512';
            $height = '512';
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_ai_image_midjourney($instruction, $width, $height, 'mediaLibraryMidjourneyImage', true, $ierror);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_midjourney_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    elseif($ai_model == 'replicate')
    {
        if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for replicate images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if($user_token_cap_per_day != '' && !empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $image_size = str_replace('??', 'x', $image_size);
        $instruction = stripslashes($_POST['instruction']);
        $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
        if(!in_array($image_size, $sizes))
        {
            $aiomatic_result['msg'] = 'Incorrect Replicate image size provided: ' . $image_size;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['replicate_app_id']) || trim($aiomatic_Main_Settings['replicate_app_id']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid GoAPI (Replicate) API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_replicate_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if($image_size == '512x512')
        {
            $width = '512';
            $height = '512';
        }
        elseif($image_size == '1024x1024')
        {
            $width = '1024';
            $height = '1024';
        }
        else
        {
            $width = '512';
            $height = '512';
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_replicate_image($instruction, $width, $height, 'mediaLibraryReplicateImage', true, $ierror);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_replicate_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    elseif($ai_model == 'ideogram')
    {
        if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for ideogram images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if($user_token_cap_per_day != '' && !empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $image_size = str_replace('??', 'x', $image_size);
        $instruction = stripslashes($_POST['instruction']);
        $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
        if(!in_array($image_size, $sizes))
        {
            $aiomatic_result['msg'] = 'Incorrect Ideogram image size provided: ' . $image_size;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['ideogram_app_id']) || trim($aiomatic_Main_Settings['ideogram_app_id']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid GoAPI (Ideogram) API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_ideogram_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if($image_size == '512x512')
        {
            $width = '512';
            $height = '512';
        }
        elseif($image_size == '1024x1024')
        {
            $width = '1024';
            $height = '1024';
        }
        else
        {
            $width = '512';
            $height = '512';
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_ideogram_image($instruction, $width, $height, 'mediaLibraryIdeogramImage', true, $ierror);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_ideogram_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    elseif($ai_model == 'cloudflare')
    {
        if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
        {
            $aiomatic_result['msg'] = 'Incomplete POST request for cloudflare images';
            wp_send_json($aiomatic_result);
        }
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if($user_token_cap_per_day != '' && !empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $image_size = str_replace('??', 'x', $image_size);
        $instruction = stripslashes($_POST['instruction']);
        $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
        if(!in_array($image_size, $sizes))
        {
            $aiomatic_result['msg'] = 'Incorrect Cloudflare image size provided: ' . $image_size;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!isset($aiomatic_Main_Settings['cloudflare_worker_url']) || trim($aiomatic_Main_Settings['cloudflare_worker_url']) == '' || !isset($aiomatic_Main_Settings['cloudflare_worker_secret']) || trim($aiomatic_Main_Settings['cloudflare_worker_secret']) == '') 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid Cloudflare Worker URL and Secret for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_cloudflare_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'Daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        if($image_size == '512x512')
        {
            $width = '512';
            $height = '512';
        }
        elseif($image_size == '1024x1024')
        {
            $width = '1024';
            $height = '1024';
        }
        else
        {
            $width = '512';
            $height = '512';
        }
        $ierror = '';
        $temp_get_imgs = aiomatic_generate_cloudflare_image($instruction, $width, $height, 'mediaLibraryCloudflareImage', true, $ierror);
        if($temp_get_imgs !== false)
        {
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_cloudflare_image_tokens', $used_token_count);
            }
            $aiomatic_result['data'] = $temp_get_imgs;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
        wp_send_json($aiomatic_result);
    }
    else
    {
        $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
        if(!empty($user_token_cap_per_day))
        {
            $user_token_cap_per_day = intval($user_token_cap_per_day);
        }
        $user_id = sanitize_text_field($_POST['user_id']);
        $image_size = $_POST['image_size'];
        $image_size = str_replace('??', 'x', $image_size);
        $instruction = stripslashes($_POST['instruction']);
        $sizes = array('1024x1024', '1792x1024', '1024x1792', '1536x1024', '1024x1536');
        if(!in_array($image_size, $sizes))
        {
            $aiomatic_result['msg'] = 'Invalid image size provided: ' . $image_size;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
        {
            $aiomatic_result['msg'] = 'You need to insert a valid AI service API Key for this to work!';
            wp_send_json($aiomatic_result);
        }
        $used_token_count = 0;
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
            {
                /* translators: %s: URL */
                $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
                wp_send_json($aiomatic_result);
            }
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_image_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
                if($used_token_count > $user_token_cap_per_day)
                {
                    $aiomatic_result['msg'] = 'The daily token count usage for your user account was exceeded! Please try again tomorrow.';
                    wp_send_json($aiomatic_result);
                }
            }
            else
            {
                $used_token_count = 0;
            }
        }
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        $aierror = '';
        $response_text = aiomatic_generate_ai_image($token, 1, $instruction, $image_size, 'mediaLibraryDallEImage', true, 0, $aierror, $ai_model, $instruction);
        if($response_text !== false && is_array($response_text))
        {
            foreach($response_text as $tmpimg)
            {
                $aiomatic_result['data'] = $tmpimg;
                $aiomatic_result['status'] = 'success';
                do_action('aiomatic_ai_image_reply', $tmpimg);
                wp_send_json($aiomatic_result);
            }
            if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_tokens', $used_token_count);
            }
        }
        $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $aierror . ' -- ' . print_r($response_text, true);
        wp_send_json($aiomatic_result);
    }
    die();
}

add_action('wp_ajax_aiomatic_generate_royalty_free_image_ajax', 'aiomatic_generate_royalty_free_image_ajax');
function aiomatic_generate_royalty_free_image_ajax() {
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with image generator');
    if(!isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for royalty free images';
        wp_send_json($aiomatic_result);
    }
    $instruction = stripslashes(trim($_POST['instruction']));
    $image_source = $_POST['image_source'];
    $img_attr = '';
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $raw_img_list = array();
    $full_result_list = array();
    $attached_id = '';
    $z_img = aiomatic_get_free_image($aiomatic_Main_Settings, $instruction, $img_attr, $attached_id, 10, true, $raw_img_list, array($image_source), $full_result_list);
    if(!empty($z_img))
    {
        $aiomatic_result['data'] = $full_result_list;
        $aiomatic_result['status'] = 'success';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'No images returned for: ' . esc_html($instruction) . ', from: ' . esc_html($image_source);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_image_ajax_submit', 'aiomatic_image_submit');
add_action('wp_ajax_nopriv_aiomatic_image_ajax_submit', 'aiomatic_image_submit');
function aiomatic_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with image submission');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for OpenAI images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    if(isset($_POST['image_model']))
    {
	    $image_model = stripslashes($_POST['image_model']);
        if(!in_array($image_model, AIMOGEN_DALLE_IMAGE_MODELS))
        {
            $image_model = 'gpt-image-2';
        }
    }
    else
    {
        $image_model = 'gpt-image-2';
    }
    $sizes = array('1024x1024', '1792x1024', '1024x1792', '1536x1024', '1024x1536');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid AI service API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
	$aierror = '';
    $response_text = aiomatic_generate_ai_image($token, 1, $instruction, $image_size, 'shortcodeImageForm', false, 0, $aierror, $image_model, $instruction);
    if($response_text !== false && is_array($response_text))
    {
        foreach($response_text as $tmpimg)
        {
            $aiomatic_result['data'] = $tmpimg;
            $aiomatic_result['status'] = 'success';
            do_action('aiomatic_ai_form_image_reply', $tmpimg);
            wp_send_json($aiomatic_result);
        }
        if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_image_tokens', $used_token_count);
        }
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $aierror . ' -- ' . print_r($response_text, true);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_run_my_bulk_embeddings_action', 'aiomatic_run_my_bulk_embeddings_action');
function aiomatic_run_my_bulk_embeddings_action()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['rule_timeout']) && $aiomatic_Main_Settings['rule_timeout'] != '') {
        $timeout = intval($aiomatic_Main_Settings['rule_timeout']);
    } else {
        $timeout = 36000;
    }
    ini_set('memory_limit', '-1');
    ini_set('default_socket_timeout', $timeout);
    ini_set('safe_mode', 'Off');
    ini_set('max_execution_time', $timeout);
    ini_set('ignore_user_abort', 1);
    ini_set('user_agent', aiomatic_get_random_user_agent());
    if(function_exists('ignore_user_abort'))
    {
        ignore_user_abort(true);
    }
    if(function_exists('set_time_limit'))
    {
        set_time_limit($timeout);
    }
    if (isset($aiomatic_Main_Settings['enable_detailed_logging']) && $aiomatic_Main_Settings['enable_detailed_logging'] == 'on') 
    {
        aiomatic_log_exec_time('Bulk Embeddings');
    }
    if (isset($aiomatic_Main_Settings['aiomatic_enabled']) && $aiomatic_Main_Settings['aiomatic_enabled'] == 'on') 
    {
        $namespace = '';
        $query     = array(
        );
        if (isset($_POST['author_id']) && $_POST['author_id'] != '') {
            $query['author'] = $_POST['author_id'];
        }
        if (isset($_POST['author_name']) && $_POST['author_name'] != '') {
            $query['author_name'] = $_POST['author_name'];
        }
        if (isset($_POST['category_name']) && $_POST['category_name'] != '') {
            $query['category_name'] = $_POST['category_name'];
        }
        if (isset($_POST['tag_name']) && $_POST['tag_name'] != '') {
            $query['tag'] = $_POST['tag_name'];
        }
        if (isset($_POST['post_id']) && $_POST['post_id'] != '') {
            $postids = $_POST['post_id'];
            $postids = explode(',', $postids);
            $postids = array_map('trim', $postids);
            $query['post__in'] = $postids;
        }
        if (isset($_POST['namespace']) && $_POST['namespace'] != '') {
            $namespace = $_POST['namespace'];
        }
        if (isset($_POST['post_name']) && $_POST['post_name'] != '') {
            $query['name'] = $_POST['post_name'];
        }
        if (isset($_POST['pagename']) && $_POST['pagename'] != '') {
            $query['pagename'] = $_POST['pagename'];
        }
        if (isset($_POST['year']) && $_POST['year'] != '') {
            $query['year'] = $_POST['year'];
        }
        if (isset($_POST['month']) && $_POST['month'] != '') {
            $query['monthnum'] = $_POST['month'];
        }
        if (isset($_POST['day']) && $_POST['day'] != '') {
            $query['day'] = $_POST['day'];
        }
        if (isset($_POST['post_parent']) && $_POST['post_parent'] != '') {
            $query['post_parent'] = $_POST['post_parent'];
        }
        if (isset($_POST['page_id']) && $_POST['page_id'] != '') {
            $query['page_id'] = $_POST['page_id'];
        }
        if (isset($_POST['max_nr']) && $_POST['max_nr'] != '') {
            $max_nr = intval($_POST['max_nr']);
        }
        else
        {
            $max_nr = 0;
        }
        if (isset($_POST['embedding_template']) && $_POST['embedding_template'] != '') {
            $embedding_template = $_POST['embedding_template'];
        }
        else
        {
            $embedding_template = trim($aiomatic_Main_Settings['embedding_template']);
        }
        if (isset($_POST['max_posts']) && $_POST['max_posts'] != '') 
        {
            if(intval($_POST['max_posts']) != -1 && $max_nr > intval($_POST['max_posts']))
            {
                $query['posts_per_page'] = $max_nr;
            }
            else
            {
                $query['posts_per_page'] = $_POST['max_posts'];
            }
        }
        else
        {
            if($max_nr > 5)
            {
                $query['posts_per_page'] = $max_nr;
            }
        }
        if (isset($_POST['search_offset']) && $_POST['search_offset'] != '') {
            $query['offset'] = $_POST['search_offset'];
        }
        if (isset($_POST['search_query']) && $_POST['search_query'] != '') {
            $query['s'] = $_POST['search_query'];
        }
        if (!empty($_POST['meta_name'])) {
            $meta_name  = sanitize_text_field($_POST['meta_name']);
            $meta_value = isset($_POST['meta_value']) ? sanitize_text_field($_POST['meta_value']) : '';

            if ($meta_value !== '') {
                $meta_query[] = [
                    'key'     => $meta_name,
                    'value'   => $meta_value,
                    'compare' => '=',
                ];
            } else {
                $meta_query[] = [
                    'key'     => $meta_name,
                    'compare' => 'EXISTS',
                ];
            }
        }
        if (isset($_POST['order']) && $_POST['order'] != 'default') {
            $query['order'] = $_POST['order'];
        }
        if (isset($_POST['orderby']) && $_POST['orderby'] != 'default') {
            $query['orderby'] = $_POST['orderby'];
        }
        if (isset($_POST['featured_image']) && $_POST['featured_image'] !== 'any') {
            if ($_POST['featured_image'] === 'with') {
                $meta_query[] = [
                    'key'     => '_thumbnail_id',
                    'compare' => 'EXISTS',
                ];
            } elseif ($_POST['featured_image'] === 'without') {
                $meta_query[] = [
                    'key'     => '_thumbnail_id',
                    'compare' => 'NOT EXISTS',
                ];
            }
        }
        $custom_name = 'aiomatic_indexed';
        if (!isset($_POST['no_twice']) || $_POST['no_twice'] !== 'on') {
            $meta_query[] = [
                'key'     => $custom_name,
                'compare' => 'NOT EXISTS',
            ];
        }
        if (!empty($meta_query)) {
            $query['meta_query'] = array_merge(['relation' => 'AND'], $meta_query);
        }
        if (isset($_POST['post_status']) && $_POST['post_status'] != '') {
            $query['post_status'] = array_map('trim', explode(',', $_POST['post_status']));
        }
        else
        {
            $query['post_status'] = 'any';
        }
        if (isset($_POST['type_post']) && $_POST['type_post'] != '') {
            $query['post_type'] = array_map('trim', explode(',', $_POST['type_post']));
        }
        else
        {
            $query['post_type'] = 'post';
        }
        $processed = 0;
        $post_list = get_posts($query);
        if(count($post_list) > 0)
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if ($embedding_template != '')
            {
                if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
                {
                    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
                    $appids = array_filter($appids);
                    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
                    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
                    require_once(dirname(__FILE__) . "/res/Embeddings.php");
                    $embdedding = new Aiomatic_Embeddings($token);
                    foreach ($post_list as $tpost) 
                    {
                        if($max_nr > 0 && $processed == $max_nr)
                        {
                            break;
                        }
                        $processed++;
                        $post_url = get_permalink($tpost->ID);
                        $post_title = $tpost->post_title;
                        $post_excerpt = $tpost->post_excerpt;
                        $post_id = $tpost->ID;
                        $post_content = $tpost->post_content;
                        if (strstr($embedding_template, '%%post_content%%') !== false && isset($aiomatic_Main_Settings['rewrite_embedding']) && trim($aiomatic_Main_Settings['rewrite_embedding']) == 'on' && isset($aiomatic_Main_Settings['embedding_rw_prompt']) && trim($aiomatic_Main_Settings['embedding_rw_prompt']) != '')
                        {
                            $embedding_rw_prompt = trim($aiomatic_Main_Settings['embedding_rw_prompt']);
                            $embedding_rw_prompt = str_replace('%%post_url%%', $post_url, $embedding_rw_prompt);
                            $embedding_rw_prompt = str_replace('%%post_title%%', $post_title, $embedding_rw_prompt);
                            $embedding_rw_prompt = str_replace('%%post_excerpt%%', $post_excerpt, $embedding_rw_prompt);
                            $embedding_rw_prompt = str_replace('%%post_content%%', strip_shortcodes($post_content), $embedding_rw_prompt);
                            $embedding_rw_prompt = str_replace('%%post_id%%', $post_id, $embedding_rw_prompt);
                            if($embedding_rw_prompt != '')
                            {
                                if(isset($aiomatic_Main_Settings['embedding_rw_model']) && trim($aiomatic_Main_Settings['embedding_rw_model']) != '')
                                {
                                    $rw_model = trim($aiomatic_Main_Settings['embedding_rw_model']);
                                }
                                else
                                {
                                    $rw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
                                }
                                $all_models = aiomatic_get_all_models(true);
                                if(!in_array($rw_model, $all_models))
                                {
                                    $rw_model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
                                }
                                $query_token_count = count(aiomatic_encode($embedding_rw_prompt));
                                $max_tokens = aimogenpro_get_max_tokens($rw_model);
                                $available_tokens = aiomatic_compute_available_tokens($rw_model, $max_tokens, $embedding_rw_prompt, $query_token_count);
                                if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
                                {
                                    $string_len = aiomatic_strlen($embedding_rw_prompt);
                                    $string_len = $string_len / 2;
                                    $string_len = intval(0 - $string_len);
                                    $embedding_rw_prompt = aiomatic_substr($embedding_rw_prompt, 0, $string_len);
                                    $embedding_rw_prompt = trim($embedding_rw_prompt);
                                    $query_token_count = count(aiomatic_encode($embedding_rw_prompt));
                                    $available_tokens = $max_tokens - $query_token_count;
                                }
                                if(!empty($embedding_rw_prompt))
                                {
                                    $thread_id = '';
                                    $aierror = '';
                                    $finish_reason = '';
                                    $generated_text = aiomatic_generate_text($token, $rw_model, $embedding_rw_prompt, $available_tokens, 1, 1, 0, 0, false, 'embeddingsOptimizer', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', '', $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
                                    if($generated_text === false)
                                    {
                                        aiomatic_log_to_file('Failed to optimize post content for embeddings: ' . print_r($embedding_rw_prompt, true));
                                    }
                                    else
                                    {
                                        $post_content = aiomatic_sanitize_ai_result($generated_text);
                                    }
                                }
                            }
                        }
                        $emb_template = trim($embedding_template);
                        $emb_template = str_replace('%%post_url%%', $post_url, $emb_template);
                        $emb_template = str_replace('%%post_title%%', $post_title, $emb_template);
                        $emb_template = str_replace('%%post_excerpt%%', $post_excerpt, $emb_template);
                        $emb_template = str_replace('%%post_content%%', strip_shortcodes($post_content), $emb_template);
                        $emb_template = str_replace('%%post_id%%', $post_id, $emb_template);
                        $emb_template = aiomatic_replaceEmbeddingsAIPostShortcodes($emb_template, $post_id);
                        $current_user = wp_get_current_user();
                        if ( !($current_user instanceof WP_User) || !is_user_logged_in()) 
                        {
                            $emb_template = str_replace('%%user_name%%', '', $emb_template);
                            $emb_template = str_replace('%%user_email%%', '' , $emb_template);
                            $emb_template = str_replace('%%user_display_name%%', '', $emb_template);
                            $emb_template = str_replace('%%user_role%%', '', $emb_template);
                            $emb_template = str_replace('%%user_id%%', '' , $emb_template);
                            $emb_template = str_replace('%%user_firstname%%', '' , $emb_template);
                            $emb_template = str_replace('%%user_lastname%%', '' , $emb_template);
                            $emb_template = str_replace('%%user_description%%', '' , $emb_template);
                            $emb_template = str_replace('%%user_url%%', '' , $emb_template);
                        }
                        else
                        {
                            $emb_template = str_replace('%%user_name%%', $current_user->user_login, $emb_template);
                            $emb_template = str_replace('%%user_email%%', $current_user->user_email , $emb_template);
                            $emb_template = str_replace('%%user_display_name%%', $current_user->display_name, $emb_template);
                            $emb_template = str_replace('%%user_role%%', implode(',', $current_user->roles), $emb_template);
                            $emb_template = str_replace('%%user_id%%', $current_user->ID , $emb_template);
                            $emb_template = str_replace('%%user_firstname%%', $current_user->user_firstname , $emb_template);
                            $emb_template = str_replace('%%user_lastname%%', $current_user->user_lastname , $emb_template);
                            $user_desc = get_the_author_meta( 'description', $current_user->ID );
                            $emb_template = str_replace('%%user_description%%', $user_desc , $emb_template);
                            $user_url = get_the_author_meta( 'user_url', $current_user->ID );
                            $emb_template = str_replace('%%user_url%%', $user_url , $emb_template);
                        }
                        if($emb_template != '')
                        {
                            $embid = get_post_meta($post_id, $custom_name, true);
                            if(!empty($embid))
                            {
                                $my_emb = get_post($embid);
                            }
                            else
                            {
                                $my_emb = null;
                            }
                            if(!empty($embid) && $my_emb != null)
                            {
                                $my_emb->post_content = $emb_template;
                                wp_update_post($my_emb);
                            }
                            else
                            {
                                $rez = $embdedding->aiomatic_create_single_embedding_nojson($emb_template, $namespace);
                                if(isset($rez['status']) && $rez['status'] == 'error')
                                {
                                    aiomatic_log_to_file('Failed to save embedding for post id: ' . $post_id . ' error: ' . print_r($rez, true));
                                }
                                else
                                {
                                    if(!isset($rez['id']))
                                    {
                                        aiomatic_log_to_file('Unrecognized result of embedding: ' . $post_id . ' error: ' . print_r($rez, true));
                                    }
                                    else
                                    {
                                        update_post_meta($tpost->ID, $custom_name, $rez['id']);
                                    }
                                }
                            }
                        }
                    }
                }
                else
                {
                    aiomatic_log_to_file('You need to set up an OpenAI API key in the Aimogen plugin\' settings, for this to work!');
                    echo 'fail';
                }
            }
            else
            {
                aiomatic_log_to_file('No embedding template set in plugin settings!');
                echo 'fail';
            }
        }
    }
    if($processed == 0)
    {
        echo 'nochange';
    }
    else
    {
        echo 'ok';
    }
    die();
}
add_action('wp_ajax_aiomatic_generate_video', 'aiomatic_ajax_generate_video');
add_action('wp_ajax_nopriv_aiomatic_generate_video', 'aiomatic_ajax_generate_video');
function aiomatic_ajax_generate_video() {
    check_ajax_referer('aiomatic_video_nonce', 'nonce');
    $prompt = sanitize_textarea_field($_POST['prompt']);
    $duration = sanitize_text_field($_POST['duration']);
    $resolution = sanitize_text_field($_POST['resolution']);
    $model = sanitize_text_field($_POST['model']);

    $video_id = aiomatic_create_openai_video($prompt, $model, $resolution, $duration, 'aimogen', '');
    if (is_array($video_id) && isset($video_id['error'])) {
        wp_send_json_error($video_id);
    }
    wp_send_json_success(['id' => $video_id]);
}

add_action('wp_ajax_aiomatic_check_video_status', 'aiomatic_ajax_check_video_status');
add_action('wp_ajax_nopriv_aiomatic_check_video_status', 'aiomatic_ajax_check_video_status');
function aiomatic_ajax_check_video_status() {
    check_ajax_referer('aiomatic_video_nonce', 'nonce');
    $video_id = sanitize_text_field($_POST['video_id']);
    $result = aiomatic_check_openai_video_status($video_id);
    if (is_array($result) && isset($result['error'])) {
        wp_send_json_error($result);
    }
    wp_send_json_success($result);
}
add_action('wp_ajax_aiomatic_delete_video', 'aiomatic_ajax_delete_video');
function aiomatic_ajax_delete_video() {
    check_ajax_referer('aiomatic_video_nonce', 'nonce');
    $attachment_id = intval($_POST['attachment_id']);
    $result = aiomatic_delete_video_from_library($attachment_id);
    if (is_array($result) && isset($result['error'])) {
        wp_send_json_error($result);
    }
    wp_send_json_success();
}
add_action('wp_ajax_aiomatic_run_my_bulk_action', 'aiomatic_run_my_bulk_action');
function aiomatic_run_my_bulk_action()
{
    check_ajax_referer('openai-bulk-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    echo esc_html(aiomatic_do_bulk_post());
    die();
}
add_action('wp_ajax_aiomatic_run_my_bulk_action_test', 'aiomatic_run_my_bulk_action_test');
function aiomatic_run_my_bulk_action_test()
{
    check_ajax_referer('openai-bulk-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    echo esc_html(aiomatic_do_bulk_post_test());
    die();
}
add_action('wp_ajax_aiomatic_preview_form', 'aiomatic_preview_form');
function aiomatic_preview_form()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['id']) || !isset($_POST['id']))
    {
        die();
    }
    echo do_shortcode('[aimogen-form id="' . esc_html(trim($_POST['id'])) . '"]');
    die();
}
add_action('wp_ajax_aiomatic_history_form', 'aiomatic_history_form');
function aiomatic_history_form()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if (!isset($_POST['id']) || empty($_POST['id'])) {
        echo '<div class="notice notice-error"><p>' . esc_html__('Invalid form ID.', 'aiomatic-automatic-ai-content-writer') . '</p></div>';
        die();
    }

    $form_id = sanitize_text_field($_POST['id']);
    $paged = 1;
    $per_page = 10;
    $all_users = [];

    do {
        $args = [
            'number' => $per_page,
            'offset' => ($paged - 1) * $per_page,
            'meta_query' => [
                [
                    'key' => 'aiomatic_form_history_' . $form_id,
                    'compare' => 'EXISTS',
                ],
            ],
            'fields' => ['ID', 'user_login'],
        ];

        $user_query = new WP_User_Query($args);

        if (!empty($user_query->get_results())) {
            $all_users = array_merge($all_users, $user_query->get_results());
        }

        $paged++;
    } while (!empty($user_query->get_results()));

    if (empty($all_users)) {
        echo '<p>' . esc_html__('No users found with the specified meta key.', 'aiomatic-automatic-ai-content-writer') . '</p>';
    }

    ob_start();
    ?>
    <div class="aiomatic-all-users-history">
        <table class="widefat striped">
            <thead>
                <tr>
                    <th><?php esc_html_e('User', 'aiomatic-automatic-ai-content-writer'); ?></th>
                    <th><?php esc_html_e('History', 'aiomatic-automatic-ai-content-writer'); ?></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($all_users as $user) : ?>
                    <?php
                    $user_id = $user->ID;
                    $user_login = $user->user_login;
                    $history = get_user_meta($user_id, 'aiomatic_form_history_' . $form_id, true);
                    if (empty($history) || !is_array($history)) {
                        $history = [];
                    }
                    ?>
                    <tr>
                        <td><?php echo esc_html($user_login); ?></td>
                        <td>
                            <ul class="aiomatic-user-history-list">
                                <?php foreach ($history as $index => $entry) : ?>
                                    <li class="aiomatic-history-item">
                                        <span class="aiomatic-history-snippet" data-index="<?php echo esc_attr($index); ?>" data-full-text="<?php echo esc_attr($entry); ?>" data-form-id="<?php echo esc_attr($form_id); ?>">
                                            <?php echo esc_html(wp_trim_words($entry, 10, '...')); ?>
                                        </span>
                                        <button class="aiomatic-delete-entry button-link-delete" title="<?php esc_attr_e('Delete entry', 'aiomatic-automatic-ai-content-writer'); ?>" data-index="<?php echo esc_attr($index); ?>" data-form-id="<?php echo esc_attr($form_id); ?>" data-user-id="<?php echo esc_attr($user_id); ?>">
                                            &times;
                                        </button>
                                    </li>
                                <?php endforeach; ?>
                            </ul>
                        </td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
        <div id="aiomatic-full-text-display-<?php echo esc_attr($form_id); ?>" class="aiomatic-full-text-display">
            <p class="description"><?php esc_html_e('Select an entry to view the full text.', 'aiomatic-automatic-ai-content-writer'); ?></p>
        </div>
    </div>
    <?php
    $html = ob_get_clean();
    echo $html;
    die();
}

add_action('wp_ajax_aiomatic_image_editor_ajax_submit', 'aiomatic_image_editor_ajax_submit');
add_action('wp_ajax_nopriv_aiomatic_image_editor_ajax_submit', 'aiomatic_image_editor_ajax_submit');
function aiomatic_image_editor_ajax_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Stable Difussion');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for stable images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
	$aiomatic_image_strength = floatval($_POST['aiomatic_image_strength']);
    $sizes = array('1024x1024', '512x512');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Stable Diffusion image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['stability_app_id']) || trim($aiomatic_Main_Settings['stability_app_id']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Stability.AI API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_stable_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    if (!isset($_FILES['image'])) {
        wp_send_json_error(['msg' => 'Please upload an image.']);
    }
    $uploaded_file = $_FILES['image'];
    if ($uploaded_file['error'] !== UPLOAD_ERR_OK) {
        wp_send_json_error(['msg' => 'Upload error: ' . $uploaded_file['error']]);
    }
    $max_size = 100 * 1024 * 1024;
    if ($uploaded_file['size'] > $max_size) {
        wp_send_json_error(['msg' => 'File is too large.']);
    }
    $uploaded_path = wp_upload_dir()['path'] . '/' . basename(sanitize_file_name($uploaded_file['name']));
    $validate = wp_check_filetype_and_ext($uploaded_file['tmp_name'], sanitize_file_name($uploaded_file['name']));
    if (empty($validate['type']) || !in_array(strtolower($validate['ext']), ['jpg', 'jpeg', 'png', 'gif', 'webp', 'bmp', 'tif', 'tiff', 'avif', 'heic', 'heif', 'ico'])) {
        wp_send_json_error(['msg' => 'File type is not allowed.']);
    }
    if (!move_uploaded_file($uploaded_file['tmp_name'], $uploaded_path)) {
        wp_send_json_error(['msg' => 'Failed to save uploaded image.']);
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_stability_image($instruction, $height, $width, 'shortcodeChatStableImage', 0, true, $ierror, false, false, $uploaded_path, $aiomatic_image_strength);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_stable_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_stable_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_stable_image_ajax_submit', 'aiomatic_stable_image_submit');
add_action('wp_ajax_nopriv_aiomatic_stable_image_ajax_submit', 'aiomatic_stable_image_submit');
function aiomatic_stable_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Stable Difussion');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for stable images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    $sizes = array('1024x1024', '512x512');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Stable Diffusion image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['stability_app_id']) || trim($aiomatic_Main_Settings['stability_app_id']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Stability.AI API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_stable_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_stability_image($instruction, $height, $width, 'shortcodeChatStableImage', 0, true, $ierror, false, false, '', 0);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_stable_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_stable_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_google_image_ajax_submit', 'aiomatic_google_image_submit');
add_action('wp_ajax_nopriv_aiomatic_google_image_ajax_submit', 'aiomatic_google_image_submit');
function aiomatic_google_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Google Image AI');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for google images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$instruction = stripslashes($_POST['instruction']);
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['app_id_google']) || trim($aiomatic_Main_Settings['app_id_google']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Google AI Studio API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_google_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    $ierror = '';
    if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '') 
    {
        $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
    }
    else
    {
        $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
    }
    $temp_get_imgs = aiomatic_generate_image_google($instruction, $model, 'shortcodeChatGoogleImage', true, $ierror, false);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_google_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_google_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_midjourney_image_ajax_submit', 'aiomatic_midjourney_image_submit');
add_action('wp_ajax_nopriv_aiomatic_midjourney_image_ajax_submit', 'aiomatic_midjourney_image_submit');
function aiomatic_midjourney_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Midjourney');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for midjourney images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Midjourney image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['midjourney_app_id']) || trim($aiomatic_Main_Settings['midjourney_app_id']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid GoAPI (Midjourney) API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_midjourney_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_ai_image_midjourney($instruction, $width, $height, 'shortcodeChatMidjourneyImage', true, $ierror);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_midjourney_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_midjourney_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_replicate_image_ajax_submit', 'aiomatic_replicate_image_submit');
add_action('wp_ajax_nopriv_aiomatic_replicate_image_ajax_submit', 'aiomatic_replicate_image_submit');
function aiomatic_replicate_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Replicate');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for replicate images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Replicate image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['replicate_app_id']) || trim($aiomatic_Main_Settings['replicate_app_id']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Replicate API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_replicate_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_replicate_image($instruction, $width, $height, 'shortcodeChatReplicateImage', true, $ierror);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_replicate_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_replicate_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_ideogram_image_ajax_submit', 'aiomatic_ideogram_image_submit');
add_action('wp_ajax_nopriv_aiomatic_ideogram_image_ajax_submit', 'aiomatic_ideogram_image_submit');
function aiomatic_ideogram_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Ideogram');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for ideogram images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Ideogram image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['ideogram_app_id']) || trim($aiomatic_Main_Settings['ideogram_app_id']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Ideogram API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_ideogram_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_ideogram_image($instruction, $width, $height, 'shortcodeChatIdeogramImage', true, $ierror);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_ideogram_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_ideogram_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_cloudflare_image_ajax_submit', 'aiomatic_cloudflare_image_submit');
add_action('wp_ajax_nopriv_aiomatic_cloudflare_image_ajax_submit', 'aiomatic_cloudflare_image_submit');
function aiomatic_cloudflare_image_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with Cloudflare images');
    if(!isset($_POST['image_size']) || !isset($_POST['instruction']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for Cloudflare images';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$image_size = $_POST['image_size'];
    $image_size = str_replace('??', 'x', $image_size);
	$instruction = stripslashes($_POST['instruction']);
    $sizes = array('1024x1024', '512x512', '1024x1792', '1792x1024');
    if(!in_array($image_size, $sizes))
    {
        $aiomatic_result['msg'] = 'Invalid Cloudflare image size provided: ' . $image_size;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['cloudflare_worker_url']) || trim($aiomatic_Main_Settings['cloudflare_worker_url']) == '' || !isset($aiomatic_Main_Settings['cloudflare_worker_secret']) || trim($aiomatic_Main_Settings['cloudflare_worker_secret']) == '') 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid Cloudflare Worker URL and secret for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_cloudflare_image_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'The daily token count for your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($image_size == '512x512')
    {
        $width = '512';
        $height = '512';
    }
    elseif($image_size == '1024x1024')
    {
        $width = '1024';
        $height = '1024';
    }
    else
    {
        $width = '512';
        $height = '512';
    }
    $ierror = '';
    $temp_get_imgs = aiomatic_generate_cloudflare_image($instruction, $width, $height, 'shortcodeChatCloudflareImage', true, $ierror);
    if($temp_get_imgs !== false)
    {
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_cloudflare_image_tokens', $used_token_count);
        }
        $aiomatic_result['data'] = $temp_get_imgs;
        $aiomatic_result['status'] = 'success';
        do_action('aiomatic_cloudflare_image_reply', $temp_get_imgs);
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['msg'] = 'Error occurred when calling image API: ' . $ierror;
    wp_send_json($aiomatic_result);
    die();
}

add_action( 'wp_ajax_aiomatic_get_image', 'aiomatic_get_image' );
add_action( 'wp_ajax_nopriv_aiomatic_get_image', 'aiomatic_get_image' );
function aiomatic_get_image() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if(isset($_GET['id']) ){
        if(empty($_GET['id']))
        {
            $data = array(
                'image'    => '<img id="aiomatic-preview-image">',
            );
            wp_send_json_success( $data );
        }
        $image = wp_get_attachment_image( filter_input( INPUT_GET, 'id', FILTER_VALIDATE_INT ), 'thumbnail', false, array( 'id' => 'aiomatic-preview-image' ) );
        $data = array(
            'image'    => $image,
        );
        wp_send_json_success( $data );
    } else {
        wp_send_json_error();
    }
    die();
}
add_action( 'wp_ajax_create_post', 'aiomatic_create_post' );
function aiomatic_create_post() {
	check_ajax_referer( 'create_post', 'nonce' );
    if ( ! is_user_logged_in() ) {
        wp_send_json_error(['message'=>'Not logged in'], 401);
    }
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if ( $_SERVER['REQUEST_METHOD'] !== 'POST' ) {
        wp_send_json_error(['message'=>'Invalid request'],405);
    }
    $user_id = get_current_user_id();
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot continue!' ) );
    }
    $last = get_user_meta($user_id, 'aiomatic_last_post', true);

    if ($last && (time() - $last) < 10) {
        wp_send_json_error(['message'=> 'Slow down, try again in a couple of seconds']);
    }

    update_user_meta($user_id, 'aiomatic_last_post', time());
    if(isset($_POST['metaFieldsArray']))
    {
        $metaFieldsArray = $_POST['metaFieldsArray'];
    }
    else
    {
        $metaFieldsArray = array();
    }
	$post_title = stripslashes($_POST['title']);
	$post_content = stripslashes($_POST['content']);
	$post_excerpt = stripslashes($_POST['excerpt']);
	$submit_status = sanitize_text_field( stripslashes($_POST['submit_status']) );
	$submit_type = isset($_POST['submit_type']) ? sanitize_text_field( stripslashes($_POST['submit_type']) ) : 'post';
	$post_sticky = sanitize_text_field( $_POST['post_sticky'] );
    if(!isset($_POST['post_author']))
    {
        $post_author = $user_id;
        if($post_author == 0)
        {
            wp_send_json_error( array( 'message' => 'You are not logged in' ) );
        }
    }
    else
    {
	    $post_author = stripslashes($_POST['post_author']);
    }
	$aiomatic_image_id = stripslashes( $_POST['aiomatic_image_id'] );
	$post_date = stripslashes($_POST['post_date']);
	$post_tags = stripslashes( $_POST['post_tags'] );
	$post_category = stripslashes(sanitize_text_field( stripslashes($_POST['post_category']) ));
	$post_category = json_decode($post_category, true);
	if ( empty( $post_title ) || empty( $post_content ) ) {
	  wp_send_json_error( array( 'message' => 'Title and Content are required fields' ) );
	}
    if(empty($submit_type))
    {
        $submit_type = 'post';
    }
    if(!in_array($submit_type, get_post_types( '', 'names' )))
    {
        $submit_type = 'post';
    }
	$statuses = get_post_statuses();
	$statuses['trash'] = 'Trash';
	if(!array_key_exists($submit_status, $statuses))
	{
		wp_send_json_error( array( 'message' => 'Invalid post status submitted: ' . $submit_status . ' - ' .print_r($statuses, true) ) );
	}
	$author_obj = get_user_by('id', $post_author);
	if($author_obj === false)
	{
		wp_send_json_error( array( 'message' => 'Invalid post author submitted' ) );
	}
	$post_args = array(
		'post_title' => $post_title,
		'post_content' => $post_content,
		'post_excerpt' => $post_excerpt,
		'post_status' => $submit_status,
		'post_type' => $submit_type,
		'post_author' => $post_author,
		'post_date' => $post_date
	);
    if(!empty($post_tags))
	{
		$post_args['tags_input'] = $post_tags;
	}
    if(!empty($metaFieldsArray))
	{
		$post_args['meta_input'] = $metaFieldsArray;
	}
    remove_filter('content_save_pre', 'wp_filter_post_kses');
    remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
	$post_id = wp_insert_post( $post_args );
    add_filter('content_save_pre', 'wp_filter_post_kses');
    add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
	if ( is_wp_error( $post_id ) ) {
	  wp_send_json_error( array( 'message' => $post_id->get_error_message() ) );
	}
    elseif ( $post_id === 0 ) {
        wp_send_json_error( array( 'message' => 'Failed to insert post: ' . $post_title ) );
    }
    $words = str_word_count(wp_strip_all_tags($post_args['post_content']));
    aiomatic_increment_publishing_stats($words);
    aiomatic_set_last_activity(
        'article',
        ['post_id' => $post_id]
    );
	if ($post_sticky == 'on') 
	{
		stick_post($post_id);
	}
	if(is_array($post_category))
	{
		$default_category = get_option('default_category');
        $target_cat = 'category';
        if($submit_type == 'product')
        {
            $target_cat = 'product_cat';
        }
        wp_set_object_terms($post_id, array_map( 'intval', $post_category), $target_cat, true);
		if(is_numeric($default_category))
		{
			if(!in_array($default_category, $post_category))
			{
				$deftrerm = get_term_by('id', $default_category, $target_cat);
				if($deftrerm !== false)
				{
					wp_remove_object_terms( $post_id, $deftrerm->slug, $target_cat );
				}
			}
		}
	}
	if($aiomatic_image_id != '' && is_numeric($aiomatic_image_id))
	{
		$aiomatic_image_id = intval($aiomatic_image_id);
		require_once(ABSPATH . 'wp-admin/includes/image.php');
		require_once(ABSPATH . 'wp-admin/includes/media.php');
		set_post_thumbnail($post_id, $aiomatic_image_id);
	}
	wp_send_json_success( array( 'post_id' => $post_id ) );
    die();
}
add_action( 'wp_ajax_aiomatic_write_text', 'aiomatic_write_text' );
add_action( 'wp_ajax_nopriv_aiomatic_write_text', 'aiomatic_write_text' );
function aiomatic_write_text() 
{
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    require_once(dirname(__FILE__) . "/res/aiomatic-chars.php");
	if(!isset($_POST['prompt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prompt)' ) );
	}
	$prompt = stripslashes( $_POST['prompt'] );
	if(!isset($_POST['model']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (model)' ) );
	}
	$model = stripslashes( $_POST['model'] );
	if(!isset($_POST['assistant_id']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (assistant_id)' ) );
	}
	$assistant_id = stripslashes( $_POST['assistant_id'] );
	if(!isset($_POST['max_tokens']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (max_tokens)' ) );
	}
	$max_tokens = stripslashes( $_POST['max_tokens'] );
	if(!isset($_POST['temperature']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (temperature)' ) );
	}
	$temperature = stripslashes( $_POST['temperature'] );
	if(!isset($_POST['title']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (title)' ) );
	}
	$title = stripslashes( $_POST['title'] );
	if(!isset($_POST['language']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (language)' ) );
	}
	$language = stripslashes( $_POST['language'] );
	if(!isset($_POST['writing_style']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (writing_style)' ) );
	}
	$writing_style = stripslashes( $_POST['writing_style'] );
	if(!isset($_POST['writing_tone']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (writing_tone)' ) );
	}
	$writing_tone = stripslashes( $_POST['writing_tone'] );
	if(!isset($_POST['topics']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (topics)' ) );
	}
	$topics = stripslashes( $_POST['topics'] );
	if(!isset($_POST['sections']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (sections)' ) );
	}
	$sections = stripslashes( $_POST['sections'] );
	if(!isset($_POST['sections_count']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (sections_count)' ) );
	}
	$sections_count = stripslashes( $_POST['sections_count'] );
	if(!isset($_POST['paragraph_count']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (paragraph_count)' ) );
	}
	$paragraph_count = stripslashes( $_POST['paragraph_count'] );
	if(!isset($_POST['content_gen_type']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (content_gen_type)' ) );
	}
    if(isset($_POST['metakeyinput']))
	{
	    $metakeyinput = stripslashes( $_POST['metakeyinput'] );
	}
    else
    {
        $metakeyinput = '';
    }
	$content_gen_type = stripslashes( $_POST['content_gen_type'] );
	$temperature = floatval($temperature);
	$max_tokens = intval($max_tokens);
	if($max_tokens > 2048)
	{
		$big_model = false;
		if(!aiomatic_is_trained_model($model))
		{
			$big_model = true;
		}
		elseif(strstr($model, 'turbo') !== false && !aiomatic_is_trained_model($model))
		{
			$big_model = true;
		}
		elseif(strstr($model, 'gpt-4') !== false && !aiomatic_is_trained_model($model))
		{
			$big_model = true;
		}
		if($big_model == false)
		{
			$max_tokens = 2048;
		}
	}
	$aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if($content_gen_type == 'yes')
    {
        $main_prompt = $prompt;
        if($sections != '')
        {
            $post_sections_arr = preg_split('/\r\n|\r|\n/', $sections);
        }
        else
        {
            $post_sections_arr = array();
        }
        foreach($post_sections_arr as $current_section)
        {
            $prompt = str_replace('%%title%%', $title, $main_prompt);
            $prompt = str_replace('%%current_section%%', $current_section, $main_prompt);
            $prompt = str_replace('%%language%%', $language, $prompt);
            $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
            $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
            $prompt = str_replace('%%topic%%', $topics, $prompt);
            $prompt = str_replace('%%sections%%', $sections, $prompt);
            $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
            $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
            $prompt = str_replace('%%meta_title%%', $metakeyinput, $prompt);
            
            $query_token_count = count(aiomatic_encode($prompt));
            $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
            if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
            {
                $string_len = aiomatic_strlen($prompt);
                $string_len = $string_len / 2;
                $string_len = intval(0 - $string_len);
                $aicontent = aiomatic_substr($prompt, 0, $string_len);
                $aicontent = trim($aicontent);
                if(empty($aicontent))
                {
                    wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
                }
                $query_token_count = count(aiomatic_encode($aicontent));
                $available_tokens = $max_tokens - $query_token_count;
            }
            $thread_id = '';
            $aierror = '';
            $finish_reason = '';
            $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, 1, 0, 0, false, 'singlePostWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
            if($generated_text === false)
            {
                wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
            }
            else
            {
                $new_post_content .= '<h2>' . $current_section . '</h2>';
                $new_post_content .= aiomatic_sanitize_ai_result($generated_text) . ' ';
            }
        }
    }
    else
    {
        $prompt = str_replace('%%title%%', $title, $prompt);
        $prompt = str_replace('%%language%%', $language, $prompt);
        $prompt = str_replace('%%writing_style%%', $writing_style, $prompt);
        $prompt = str_replace('%%writing_tone%%', $writing_tone, $prompt);
        $prompt = str_replace('%%topic%%', $topics, $prompt);
        $prompt = str_replace('%%sections%%', $sections, $prompt);
        $prompt = str_replace('%%sections_count%%', $sections_count, $prompt);
        $prompt = str_replace('%%paragraphs_per_section%%', $paragraph_count, $prompt);
        $prompt = str_replace('%%meta_title%%', $metakeyinput, $prompt);
        
        $query_token_count = count(aiomatic_encode($prompt));
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $string_len = aiomatic_strlen($prompt);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $aicontent = aiomatic_substr($prompt, 0, $string_len);
            $aicontent = trim($aicontent);
            if(empty($aicontent))
            {
                wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
            }
            $query_token_count = count(aiomatic_encode($aicontent));
            $available_tokens = $max_tokens - $query_token_count;
        }
        $thread_id = '';
        $aierror = '';
        $finish_reason = '';
        $generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, 1, 0, 0, false, 'singlePostWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
        if($generated_text === false)
        {
            wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
        }
        else
        {
            $new_post_content = aiomatic_sanitize_ai_result($generated_text);
        }
    }
    if (!isset($aiomatic_Main_Settings['no_undetectibility']) || $aiomatic_Main_Settings['no_undetectibility'] != 'on') 
    {
        if (!isset($aiomatic_Main_Settings['no_undetectibility_single']) || $aiomatic_Main_Settings['no_undetectibility_single'] != 'on') 
        {
            if(!isset($xchars))
            {
                $xchars = array();
            }
            $rand_percentage = rand(10, 20);
            $new_post_content = aiomatic_make_unique($new_post_content, $xchars, $rand_percentage);
        }
    }
    do_action('aiomatic_text_writer_reply', $new_post_content);
	wp_send_json_success( array( 'content' => $new_post_content ) );
    die();
}

add_action( 'wp_ajax_aiomatic_delete_template', 'aiomatic_delete_template' );
function aiomatic_delete_template() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        if(!isset($aiomatic_templates[$template_name]))
        {
            wp_send_json_error( array( 'message' => 'Template name not found in database, please refresh this page to update template listing' ) );
        }
        else
        {
            unset($aiomatic_templates[$template_name]);
            update_user_meta( $user_id, $key, $aiomatic_templates );
        }
    }
    wp_send_json_success( array( 'content' => 'saved' ) );
    die();
}
add_action( 'wp_ajax_aiomatic_delete_template_advanced', 'aiomatic_delete_template_advanced' );
function aiomatic_delete_template_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates_advanced'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        if(!isset($aiomatic_templates[$template_name]))
        {
            wp_send_json_error( array( 'message' => 'Template name not found in database, please refresh this page to update template listing' ) );
        }
        else
        {
            unset($aiomatic_templates[$template_name]);
            update_user_meta( $user_id, $key, $aiomatic_templates );
        }
    }
    wp_send_json_success( array( 'content' => 'saved' ) );
    die();
}

add_action( 'wp_ajax_aiomatic_save_template', 'aiomatic_save_template' );
function aiomatic_save_template() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
	if(!isset($_POST['template_options']))
	{
		wp_send_json_error( array( 'message' => 'Template settings are required!' ) );
	}
	$template_options = $_POST['template_options'];
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        $aiomatic_templates[$template_name] = $template_options;
        update_user_meta( $user_id, $key, $aiomatic_templates );
    }
    wp_send_json_success( array( 'content' => 'saved' ) );
    die();
}
add_action( 'wp_ajax_aiomatic_save_template_advanced', 'aiomatic_save_template_advanced' );
function aiomatic_save_template_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
	if(!isset($_POST['template_options']))
	{
		wp_send_json_error( array( 'message' => 'Template settings are required!' ) );
	}
	$template_options = $_POST['template_options'];
	
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates_advanced'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        $aiomatic_templates[$template_name] = $template_options;
        update_user_meta( $user_id, $key, $aiomatic_templates );
    }
    wp_send_json_success( array( 'content' => 'saved' ) );
    die();
}

add_action( 'wp_ajax_aiomatic_load_template', 'aiomatic_load_template' );
function aiomatic_load_template() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
    $aiomatic_templates = array();
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        if($template_name == 'Default Template')
        {
            $author_obj = get_user_by('id', $user_id);
            if($author_obj !== false)
            {
                $user_login = $author_obj->ID;
            }
            else
            {
                aiomatic_log_to_file('Failed to detect current user name: ' . $user_id);
                $user_login = 1;
            }
            $dt = new DateTime();
            $datef = $dt->format('Y-m-d H:i:s');
            $default_category = get_option('default_category');
            $aiomatic_templates = array
            (
                'title' => '',
                'topics' => '',
                'submit_status' => 'draft',
                'submit_type' => 'post',
                'post_sticky' => 'no',
                'post_author' => $user_login,
                'post_date' => $datef,
                'post_category' => array($default_category),
                'post_tags' => '',
                'language' => 'English',
                'writing_style' => 'Creative',
                'writing_tone' => 'Neutral',
                'sections_count' => 2,
                'paragraph_count' => 3,
                'model' => AIMOGEN_DEFAULT_MODEL,
                'max_tokens' => 4000,
                'temperature' => 1,
                'prompt_title' => 'Write a title for an article about "%%topic%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.',
                'prompt_sections' => 'Write %%sections_count%% consecutive headings for an article about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.',
                'prompt_content' => 'Write an article about "%%title%%" in %%language%%. The article is organized by the following headings:

%%sections%%

Write %%paragraphs_per_section%% paragraphs per heading.

Use HTML for formatting, include h2 tags, h3 tags, lists and bold.

Add an introduction and a conclusion.

Style: %%writing_style%%. Tone: %%writing_tone%%.',
                'prompt_excerpt' => 'Write an excerpt for an article about "%%title%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters.'
                    );
        }
        else
        {
            $key = 'aiomatic_templates'; 
            $single = true; 
            $aiomatic_templates = get_user_meta( $user_id, $key, $single );
            if(!is_array($aiomatic_templates))
            {
                $aiomatic_templates = array();
            }
            if(!isset($aiomatic_templates[$template_name]))
            {
                wp_send_json_error( array( 'message' => 'Template name not found in the database' ) );
            }
            $aiomatic_templates = $aiomatic_templates[$template_name];
        }
    }
    wp_send_json_success( array( 'content' => $aiomatic_templates ) );
    die();
}

add_action( 'wp_ajax_aiomatic_import_templates_advanced', 'aiomatic_import_templates_advanced' );
function aiomatic_import_templates_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['templates']))
	{
		wp_send_json_error( array( 'message' => 'Template json is required!' ) );
	}
    $templates = $_POST['templates'];
    if(!is_array($_POST['templates'])) {
        wp_send_json_error(['message' => 'Invalid JSON data']);
    }
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates_advanced';
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        $templates = array_merge($templates, $aiomatic_templates);
        update_user_meta( $user_id, $key, $templates );
    }
    wp_send_json_success( array( 'status' => 'ok' ) );
    die();
}
add_action( 'wp_ajax_aiomatic_import_templates', 'aiomatic_import_templates' );
function aiomatic_import_templates() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['templates']))
	{
		wp_send_json_error( array( 'message' => 'Template json is required!' ) );
	}
    $templates = $_POST['templates'];
    if(!is_array($_POST['templates'])) {
        wp_send_json_error(['message' => 'Invalid JSON data']);
    }
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates';
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
        $templates = array_merge($templates, $aiomatic_templates);
        update_user_meta( $user_id, $key, $templates );
    }
    wp_send_json_success( array( 'status' => 'ok' ) );
    die();
}
add_action( 'wp_ajax_aiomatic_export_templates', 'aiomatic_export_templates' );
function aiomatic_export_templates() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_templates = array();
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
    }
    wp_send_json_success( array( 'content' => $aiomatic_templates ) );
    die();
}
add_action( 'wp_ajax_aiomatic_export_templates_advanced', 'aiomatic_export_templates_advanced' );
function aiomatic_export_templates_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_templates = array();
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        $key = 'aiomatic_templates_advanced'; 
        $single = true; 
        $aiomatic_templates = get_user_meta( $user_id, $key, $single );
        if(!is_array($aiomatic_templates))
        {
            $aiomatic_templates = array();
        }
    }
    wp_send_json_success( array( 'content' => $aiomatic_templates ) );
    die();
}

add_action( 'wp_ajax_aiomatic_load_template_advanced', 'aiomatic_load_template_advanced' );
function aiomatic_load_template_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['template_name']))
	{
		wp_send_json_error( array( 'message' => 'Template name is required!' ) );
	}
	$template_name = sanitize_text_field( stripslashes($_POST['template_name']) );
    if(empty($template_name))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid template name!' ) );
    }
    $aiomatic_templates = array();
	$user_id = get_current_user_id(); 
    if($user_id == 0)
    {
        wp_send_json_error( array( 'message' => 'No user logged in, cannot find templates!' ) );
    }
    else
    {
        if($template_name == 'Default Template')
        {
            $author_obj = get_user_by('id', $user_id);
            if($author_obj !== false)
            {
                $user_login = $author_obj->ID;
            }
            else
            {
                aiomatic_log_to_file('Failed to detect current user name: ' . $user_id);
                $user_login = 1;
            }
            $dt = new DateTime();
            $datef = $dt->format('Y-m-d H:i:s');
            $default_category = get_option('default_category');
            $aiomatic_templates = array(
'title_advanced' => '',
'posting_mode_changer' => '1a',
'aiomatic_topics_list' => '',
'aiomatic_listicle_list' => '',
'aiomatic_titles' => '',
'aiomatic_youtube' => '',
'aiomatic_roundup' => '',
'aiomatic_review' => '',
'csv_title' => '',
'submit_status_advanced' => 'draft',
'submit_type_advanced' => 'post',
'post_sticky_advanced' => 'no',
'post_author_advanced' => $user_login,
'post_date_advanced' => $datef,
'post_category_advanced' => array($default_category),
'post_tags_advanced' => '',
'title_generator_method1a' => 'ai',
'assistant_id1a' => '',
'title_generator_method6' => 'ai',
'assistant_id6' => '',
'assistant_id1b' => '',
'assistant_id2' => '',
'assistant_id3' => '',
'assistant_id4' => '',
'post_sections_list1a' => '',
'section_count1a' => '3-4',
'sections_role1a' => 'h2',
'paragraph_count1a' => 2,
'topic_images1a' => '',
'img_all_headings1a' => 1,
'heading_img_location1a' => 'top',
'topic_videos1a' => '',
'title_outro1a' => '{In Conclusion|To Conclude|In Summary|To Wrap It Up|Key Takeaways|Future Outlook|Closing Remarks|The Conclusion|Final Thoughts|In Retrospect|The Way Forward|Wrapping Up|Concluding Remarks|Insights and Conclusions}',
'enable_toc1a' => 0,
'title_toc1a' => 'Table of Contents',
'enable_qa1a' => 0,
'title_qa1a' => 'Q&A',
'content_language1a' => 'English',
'writing_style1a' => 'Creative',
'writing_tone1a' => 'Neutral',
'title_prompt1a' => 'Write a title for an article about "%%topic%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.',
'topic_title_model1a' => AIMOGEN_DEFAULT_MODEL,
'intro_prompt1a' => 'Craft an introduction for an article about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_intro_model1a' => AIMOGEN_DEFAULT_MODEL,
'sections_prompt1a' => 'Write %%sections_count%% consecutive headings for an article about "%%title%%" that highlight specific aspects, provide detailed insights and specific recommendations. The headings must be written in %%language%%, following a %%writing_style%% style and a %%writing_tone%% tone. Don\'t add numbers to the headings, hyphens or any types of quotes. Return only the headings list, nothing else.',
'topic_sections_model1a' => AIMOGEN_DEFAULT_MODEL,
'content_prompt1a' => 'Write the content of a post section for the heading "%%current_section%%" in %%language%%. The title of the post is: "%%title%%". Don\'t add the title at the beginning of the created content. Be creative and unique. Don\'t repeat the heading in the created content. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_content_model1a' => AIMOGEN_DEFAULT_MODEL,
'single_content_call-11a' => 0,
'qa_prompt1a' => 'Write a Q&A for an article about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_qa_model1a' => AIMOGEN_DEFAULT_MODEL,
'outro_prompt1a' => 'Write an outro for an article about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_outro_model1a' => AIMOGEN_DEFAULT_MODEL,
'excerpt_prompt1a' => 'Write a short excerpt for an article about "%%title%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters.',
'topic_excerpt_model1a' => AIMOGEN_DEFAULT_MODEL,
'strip_by_regex_prompts1a' => '',
'replace_regex_prompts1a' => '',
'run_regex_on1a' => 'content',
'global_prepend1a' => '',
'global_append1a' => '',
'link_type1a' => 'disabled',
'max_links1a' => '',
'link_list1a' => '',
'link_nofollow1a' => 0,
'link_post_types1a' => '',
'max_tokens1a' => '',
'max_seed_tokens1a' => '',
'temperature1a' => '',
'top_p1a' => '',
'presence_penalty1a' => '',
'frequency_penalty1a' => '',
'search_query_repetition1a' => 0,
'enable_ai_images1a' => 0,
'ai_command_image1a' => 'Generate a high-resolution, visually compelling image that creatively interprets the theme encapsulated by this post title: "%%post_title%%". The image should be versatile enough to fit various niches, from technology and lifestyle to nature and science. It should feature a central, eye-catching element that abstractly represents the topic, surrounded by relevant, subtler motifs that provide context and depth. The composition should be balanced and aesthetically pleasing, with a harmonious color palette that complements the mood of the title. The artwork should be suitable for use as a captivating header image for a blog post.',
'model1a' => '1024x1024',
'post_prepend1a' => '',
'post_append1a' => '',
'custom_shortcodes1a' => '',
'strip_title1a' => 0,
'skip_spin1a' => 0,
'skip_translate1a' => 0,
'strip_by_regex1a' => '',
'replace_regex1a' => '',
'post_sections_list6' => '',
'section_count6' => '3-4',
'sections_role6' => 'h2',
'paragraph_count6' => 2,
'topic_images6' => '',
'img_all_headings6' => 1,
'heading_img_location6' => 'top',
'topic_videos6' => '',
'title_outro6' => '{In Conclusion|To Conclude|In Summary|To Wrap It Up|Key Takeaways|Future Outlook|Closing Remarks|The Conclusion|Final Thoughts|In Retrospect|The Way Forward|Wrapping Up|Concluding Remarks|Insights and Conclusions}',
'enable_toc6' => 0,
'title_toc6' => 'Table of Contents',
'enable_qa6' => 0,
'title_qa6' => 'Q&A',
'content_language6' => 'English',
'writing_style6' => 'Creative',
'writing_tone6' => 'Neutral',
'title_prompt6' => 'Write a title for a listicle about "%%topic%%" in %%language%%. The listicle will include %%sections_count%% items. Style: %%writing_style%%. Tone: %%writing_tone%%. Include a specific number in the title to indicate a list. Must be between 40 and 60 characters.',
'topic_title_model6' => AIMOGEN_DEFAULT_MODEL,
'intro_prompt6' => 'Craft an introduction for a listicle about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Highlight the number of items in the list and what the reader can expect to learn or gain from the listicle.',
'topic_intro_model6' => AIMOGEN_DEFAULT_MODEL,
'sections_prompt6' => 'Write %%sections_count%% consecutive entries for a listicle about "%%title%%". The entries must be written in %%language%%, following a %%writing_style%% style and a %%writing_tone%% tone. Don\'t use HTML in your response, write only plain text entries, one on each line, as I will use these entries to further create content for each of them. Return only the entries, nothing else.',
'topic_sections_model6' => AIMOGEN_DEFAULT_MODEL,
'content_prompt6' => 'Write the content of a listicle section for the heading "%%current_section%%" in %%language%%. The title of the post is: "%%title%%". Don\'t add the title at the beginning of the created content. Be creative and unique. Don\'t repeat the heading in the created content. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Act as a Content Writer, not as a Virtual Assistant. Return only the content requested, without any additional comments or text. Writing Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_content_model6' => AIMOGEN_DEFAULT_MODEL,
'single_content_call-16' => 0,
'qa_prompt6' => 'Write a Q&A listicle for an article about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Each question should be clear and engaging, followed by a detailed and informative answer. Use HTML for formatting, include unnumbered lists and bold where applicable. Return only the Q&A content, nothing else.',
'topic_qa_model6' => AIMOGEN_DEFAULT_MODEL,
'outro_prompt6' => 'Write an outro for a listicle about "%%title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_outro_model6' => AIMOGEN_DEFAULT_MODEL,
'excerpt_prompt6' => 'Write a short excerpt for a listicle about "%%title%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters. Highlight the listicle nature of the article and what readers can expect to find.',
'topic_excerpt_model6' => AIMOGEN_DEFAULT_MODEL,
'strip_by_regex_prompts6' => '',
'replace_regex_prompts6' => '',
'run_regex_on6' => 'content',
'global_prepend6' => '',
'global_append6' => '',
'link_type6' => 'disabled',
'max_links6' => '',
'link_list6' => '',
'link_nofollow6' => 0,
'link_post_types6' => '',
'max_tokens6' => '',
'max_seed_tokens6' => '',
'temperature6' => '',
'top_p6' => '',
'presence_penalty6' => '',
'frequency_penalty6' => '',
'search_query_repetition6' => 0,
'enable_ai_images6' => 0,
'ai_command_image6' => 'Generate a high-resolution, visually compelling image that creatively interprets the theme encapsulated by this post title: "%%post_title%%". The image should be versatile enough to fit various niches, from technology and lifestyle to nature and science. It should feature a central, eye-catching element that abstractly represents the topic, surrounded by relevant, subtler motifs that provide context and depth. The composition should be balanced and aesthetically pleasing, with a harmonious color palette that complements the mood of the title. The artwork should be suitable for use as a captivating header image for a blog post.',
'model6' => '1024x1024',
'post_prepend6' => '',
'post_append6' => '',
'custom_shortcodes6' => '',
'strip_title6' => 0,
'skip_spin6' => 0,
'skip_translate6' => 0,
'strip_by_regex6' => '',
'replace_regex6' => '',
'model1b' => '1024x1024',
'ai_command1b' => 'Write a comprehensive and SEO-optimized article on the topic of "%%post_title%%". Incorporate relevant keywords naturally throughout the article to enhance search engine visibility. This article must provide valuable information to readers and be well-structured with proper headings, bullet points, and HTML formatting. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple.  Add an introductory and a conclusion section to the article. You can add also some other sections, when they fit the article\'s subject, like: benefits and practical tips, case studies, first had experience. Please ensure that the article is at least 1200 words in length and adheres to best SEO practices, including proper header tags (H1, H2, H3). The article will be automatically published on my WordPress site, as a post. Feel free to use a friendly, conversational tone and make the article as informative and engaging as possible while ensuring it remains factually accurate and well-researched.',
'min_char1b' => 500,
'title_model1b' => AIMOGEN_DEFAULT_MODEL,
'title_ai_command1b' => 'Craft an attention-grabbing and SEO-optimized article title for a dental health blog. This title must be concise, informative, and designed to pique the interest of readers while clearly conveying the topic of the article.',
'title_source1b' => 'keyword',
'headings1b' => '',
'headings_model1b' => AIMOGEN_DEFAULT_MODEL,
'headings_ai_command1b' => 'Generate %%needed_heading_count%% People Also Ask (PAA) related questions, each on a new line, that are relevant to the topic of the post title: "%%post_title%%".',
'images1b' => '',
'videos1b' => 0,
'headings_list1b' => '',
'images_list1b' => '',
'global_prepend1b' => '',
'global_append1b' => '',
'link_type1b' => 'disabled',
'max_links1b' => '',
'link_list1b' => '',
'link_nofollow1b' => 0,
'link_post_types1b' => '',
'max_tokens1b' => '',
'max_seed_tokens1b' => '',
'max_continue_tokens1b' => '',
'temperature1b' => '',
'top_p1b' => '',
'presence_penalty1b' => '',
'frequency_penalty1b' => '',
'search_query_repetition1b' => 0,
'enable_ai_images1b' => 0,
'ai_command_image1b' => 'Generate a high-resolution, visually compelling image that creatively interprets the theme encapsulated by this post title: "%%post_title%%". The image should be versatile enough to fit various niches, from technology and lifestyle to nature and science. It should feature a central, eye-catching element that abstractly represents the topic, surrounded by relevant, subtler motifs that provide context and depth. The composition should be balanced and aesthetically pleasing, with a harmonious color palette that complements the mood of the title. The artwork should be suitable for use as a captivating header image for a blog post.',
'post_prepend1b' => '',
'post_append1b' => '',
'custom_shortcodes1b' => '',
'strip_title1b' => 0,
'skip_spin1b' => 0,
'skip_translate1b' => 0,
'strip_by_regex1b' => '',
'replace_regex1b' => '',
'default_lang2' => '',
'max_caption2' => 3000,
'ai_titles2' => 0,
'post_sections_list2' => '',
'section_count2' => '3-4',
'sections_role2' => 'h2',
'paragraph_count2' => 2,
'topic_images2' => '',
'img_all_headings2' => 1,
'heading_img_location2' => 'heading',
'topic_videos2' => 0,
'title_outro2' => '{In Conclusion|To Conclude|In Summary|To Wrap It Up|Key Takeaways|Future Outlook|Closing Remarks|The Conclusion|Final Thoughts|In Retrospect|The Way Forward|Wrapping Up|Concluding Remarks|Insights and Conclusions}',
'enable_toc2' => 0,
'title_toc2' => 'Table of Contents',
'enable_qa2' => 0,
'title_qa2' => 'Q&A',
'content_language2' => 'English',
'writing_style2' => 'Creative',
'writing_tone2' => 'Neutral',
'title_prompt2' => 'Generate a title for a blog post discussing the topics covered in the YouTube video titled: "%%video_title%%", in %%language%% language. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.',
'topic_title_model2' => AIMOGEN_DEFAULT_MODEL,
'intro_prompt2' => 'Write an introduction for a blog post which talks about the topics discussed in the YouTube video with the following title: "%%video_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The YouTube video has the following transcript: "%%video_captions%%"',
'topic_intro_model2' => AIMOGEN_DEFAULT_MODEL,
'sections_prompt2' => 'Write %%sections_count%% consecutive headings that highlight specific aspects, provide detailed insights and specific recommendations for a blog post which talks about the topics discussed in the YouTube video with the following title: "%%video_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Don\'t add numbers to the headings, hyphens or any types of quotes. Return only the headings list, nothing else. Extract ideas from the following video transcript: "%%video_captions%%"',
'topic_sections_model2' => AIMOGEN_DEFAULT_MODEL,
'content_prompt2' => 'Write the content of a post section for the heading "%%current_section%%" in %%language%%. The title of the post is: "%%video_title%%". Don\'t repeat the heading in the created content. Don\'t add an intro or outro. Be creative and unique. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%. Extract content from the following video transcript: "%%video_captions%%"',
'topic_content_model2' => AIMOGEN_DEFAULT_MODEL,
'single_content_call-12' => 0,
'qa_prompt2' => 'Write a Q&A for a blog post which talks about the topics discussed in the YouTube video with the following title: "%%video_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The YouTube video has the following transcript: "%%video_captions%%"',
'topic_qa_model2' => AIMOGEN_DEFAULT_MODEL,
'outro_prompt2' => 'Write an outro for a blog post which talks about the topics discussed in the YouTube video with the following title: "%%video_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The YouTube video has the following transcript: "%%video_captions%%"',
'topic_outro_model2' => AIMOGEN_DEFAULT_MODEL,
'excerpt_prompt2' => 'Write a short excerpt for a blog post which talks about the topics discussed in the YouTube video with the following title: "%%video_title%%" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters. The YouTube video has the following transcript: "%%video_captions%%"',
'topic_excerpt_model2' => AIMOGEN_DEFAULT_MODEL,
'strip_by_regex_prompts2' => '',
'replace_regex_prompts2' => '',
'run_regex_on2' => 'content',
'global_prepend2' => '',
'global_append2' => '',
'link_type2' => 'disabled',
'max_links2' => '',
'link_list2' => '',
'link_nofollow2' => 0,
'link_post_types2' => '',
'max_tokens2' => '',
'max_seed_tokens2' => '',
'max_continue_tokens2' => '',
'temperature2' => '',
'top_p2' => '',
'presence_penalty2' => '',
'frequency_penalty2' => '',
'search_query_repetition2' => 0,
'enable_ai_images2' => 0,
'ai_command_image2' => 'Generate a high-resolution, visually compelling image that creatively interprets the theme encapsulated by this post title: "%%post_title%%". The image should be versatile enough to fit various niches, from technology and lifestyle to nature and science. It should feature a central, eye-catching element that abstractly represents the topic, surrounded by relevant, subtler motifs that provide context and depth. The composition should be balanced and aesthetically pleasing, with a harmonious color palette that complements the mood of the title. The artwork should be suitable for use as a captivating header image for a blog post.',
'model2' => '1024x1024',
'post_prepend2' => '',
'post_append2' => '',
'custom_shortcodes2' => '',
'strip_title2' => 0,
'skip_spin2' => 0,
'skip_translate2' => 0,
'no_random2' => 0,
'strip_by_regex2' => '',
'replace_regex2' => '',
'affiliate_id3' => '',
'source3' => 'com',
'min_price3' => '',
'max_price3' => '',
'max_products3' => '3-4',
'sort_results3' => 'none',
'shuffle_products3' => 1,
'cb_affiliate_id3' => '',
'max_products_cb3' => '3-4',
'neg_keywords3' => '',
'item_category3' => '',
'one_time3' => '',
'recurring3' => '',
'affiliate_tools3' => '',
'spotlight3' => '',
'mobile_hoplink3' => '',
'shipable_only3' => '',
'pitch_plus3' => '',
'item_gravity3' => '',
'dollar_sale3' => '',
'dollar_sale_av3' => '',
'rebill_total3' => '',
'per_sale3' => '',
'per_rebill3' => '',
'activation3' => '',
'item_language3' => 'All',
'sort_results_cb3' => 'relevance',
'first_hand3' => 0,
'sections_role3' => 'h2',
'paragraph_count3' => 2,
'topic_images3' => 1,
'no_headlink3' => 0,
'topic_videos3' => 0,
'title_outro3' => '{Experience the Difference|Unlock Your Potential|Elevate Your Lifestyle|Embrace a New Era|Seize the Opportunity|Discover the Power|Transform Your World|Unleash Your True Potential|Embody Excellence|Achieve New Heights|Experience Innovation|Ignite Your Passion|Reveal the Extraordinary}',
'enable_toc3' => 0,
'title_toc3' => 'Table of Contents',
'enable_qa3' => 0,
'title_qa3' => 'Q&A',
'enable_table3' => 0,
'content_language3' => 'English',
'writing_style3' => 'Creative',
'writing_tone3' => 'Neutral',
'title_prompt3' => 'Write a title for a product roundup blog post which talks about the following products: %%all_product_titles%%,  %%all_product_info%%, in %%language%% language. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.',
'topic_title_model3' => AIMOGEN_DEFAULT_MODEL,
'intro_prompt3' => 'Write an intro for a blog post which talks about the following products: %%all_product_titles%%,  %%all_product_info%%, in %%language%%. The title of the post is "%%post_title%%". Style: %%writing_style%%. Tone: %%writing_tone%%.',
'topic_intro_model3' => AIMOGEN_DEFAULT_MODEL,
'content_prompt3' => 'Write the content of a post section describing the product "%%product_title%%" in %%language%%. Include pros and cons of the product. Don\'t repeat the product title in the created content. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%. %%first_hand_experience_prompt%% Extract content from the following product description: "%%product_description%%"',
'topic_content_model3' => AIMOGEN_DEFAULT_MODEL,
'qa_prompt3' => 'Write a Q&A for a blog post with the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The blog post describes and compares multiple products: %%all_product_titles%%',
'topic_qa_model3' => AIMOGEN_DEFAULT_MODEL,
'outro_prompt3' => 'Write an outro for a blog post with the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The blog post describes and compares multiple products: %%all_product_titles%%',
'topic_outro_model3' => AIMOGEN_DEFAULT_MODEL,
'excerpt_prompt3' => 'Write a short excerpt for a blog post with the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. The blog post describes and compares multiple products: %%all_product_titles%%',
'topic_excerpt_model3' => AIMOGEN_DEFAULT_MODEL,
'table_prompt3' => 'Generate a HTML product comparison table, for a product review blog post. The post has the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. Don\'t add the entire description as a table entry, but instead, extract data from it, make matches between multiple products, be creative and also short and simple. The table must be in a WordPress friendly format and have modern styling (you can use WordPress table classes). Detail product information: %%all_product_info%%',
'topic_table_model3' => AIMOGEN_DEFAULT_MODEL,
'strip_by_regex_prompts3' => '',
'replace_regex_prompts3' => '',
'run_regex_on3' => 'content',
'global_prepend3' => '',
'global_append3' => '',
'link_type3' => 'disabled',
'max_links3' => '',
'link_list3' => '',
'link_nofollow3' => 0,
'link_post_types3' => '',
'max_tokens3' => '',
'max_seed_tokens3' => '',
'max_continue_tokens3' => '',
'temperature3' => '',
'top_p3' => '',
'presence_penalty3' => '',
'frequency_penalty3' => '',
'search_query_repetition3' => 0,
'enable_ai_images3' => 0,
'ai_command_image3' => 'A high detail image with no text of: "%%post_title%%"',
'model3' => '1024x1024',
'post_prepend3' => '',
'post_append3' => '',
'custom_shortcodes3' => '',
'strip_title3' => 0,
'skip_spin3' => 0,
'skip_translate3' => 0,
'strip_by_regex3' => '',
'replace_regex3' => '',
'affiliate_id4' => '',
'source4' => 'com',
'post_sections_list4' => '',
'section_count4' => '3-4',
'sections_role4' => 'h2',
'paragraph_count4' => 2,
'topic_images4' => 1,
'no_headlink4' => 0,
'topic_videos4' => 0,
'title_outro4' => '{Experience the Difference|Unlock Your Potential|Elevate Your Lifestyle|Embrace a New Era|Seize the Opportunity|Discover the Power|Transform Your World|Unleash Your True Potential|Embody Excellence|Achieve New Heights|Experience Innovation|Ignite Your Passion|Reveal the Extraordinary}',
'enable_toc4' => 0,
'title_toc4' => 'Table of Contents',
'enable_reviews4' => 0,
'title_reviews4' => 'Customer Reviews Analysis',
'enable_proscons4' => 0,
'title_proscons4' => 'Pros & Cons',
'enable_qa4' => 0,
'title_qa4' => 'Q&A',
'content_language4' => 'English',
'writing_style4' => 'Creative',
'writing_tone4' => 'Neutral',
'title_prompt4' => 'Write a title for a product review blog post of the following product: "%%product_title%%", in %%language%% language. Style: %%writing_style%%. Tone: %%writing_tone%%. Point of View: %%point_of_view%%. The title must be between 40 and 60 characters. The description of the product is: "%%product_description%%".',
'topic_title_model4' => AIMOGEN_DEFAULT_MODEL,
'intro_prompt4' => 'Write an introduction for a product review blog post of the following product: "%%product_title%%". The post is reviewing the product "%%product_title%%", in %%language%% language. Style: %%writing_style%%. Tone: %%writing_tone%%. Point of View: %%point_of_view%%. Write as if you had first-hand experience with the product you are describing. The description of the product is: "%%product_description%%".',
'topic_intro_model4' => AIMOGEN_DEFAULT_MODEL,
'sections_prompt4' => 'Write %%sections_count%% consecutive headings for a product review article of the "%%product_title%%" product, that starts with an overview, highlights specific features and aspects of the product, provides detailed insights and specific recommendations. The headings should be written in %%language%%, following a %%writing_style%% style and a %%writing_tone%% tone. Point of view: %%point_of_view%%. Don\'t add numbers to the headings, hyphens or any types of quotes. Write as if you had first-hand experience with the product you are describing. Return only the headings list, nothing else.',
'topic_sections_model4' => AIMOGEN_DEFAULT_MODEL,
'content_prompt4' => 'Write the content of a product review post, for the following section heading: "%%current_section%%". The post is reviewing the product "%%product_title%%" in %%language%%. Don\'t repeat the product title in the created content, also don\'t be repetitive in general. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. Extract content from the following product description: "%%product_description%%".',
'topic_content_model4' => AIMOGEN_DEFAULT_MODEL,
'reviews_prompt4' => 'Write the content of a "Customer Reviews Analysis" section for a product review blog post for the following product: "%%product_title%%". The title of the blog post is: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. Use HTML for formatting. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. List of customer reviews: "%%product_reviews%%".',
'topic_reviews_model4' => AIMOGEN_DEFAULT_MODEL,
'proscons_prompt4' => 'Write the content of a "Pros & Cons" section for a product review blog post for the following product: "%%product_title%%". The title of the blog post is: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%.Use HTML for formatting. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Product description: "%%product_description%%".',
'topic_proscons_model4' => AIMOGEN_DEFAULT_MODEL,
'qa_prompt4' => 'Write the content of a Q&A section for a product review blog post for the following product: "%%product_title%%". The title of the blog post is: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. Product description: "%%product_description%%".',
'topic_qa_model4' => AIMOGEN_DEFAULT_MODEL,
'outro_prompt4' => 'Write an outro for a product review blog post, for the product: "%%product_title%%". The post has the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. Product description: "%%product_description%%". Add also an engaging final call to action link, in a clickable HTML format (don\'t use markdown language), leading to the link of the product: "%%aff_url%%".',
'topic_outro_model4' => AIMOGEN_DEFAULT_MODEL,
'excerpt_prompt4' => 'Write a short excerpt for a product review blog post, for the product: "%%product_title%%". The post has the following title: "%%post_title%%", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Point Of View: %%point_of_view%%. The excerpt must be between 100 and 150 words.',
'topic_excerpt_model4' => AIMOGEN_DEFAULT_MODEL,
'strip_by_regex_prompts4' => '',
'replace_regex_prompts4' => '',
'run_regex_on4' => 'content',
'global_prepend4' => '',
'global_append4' => '',
'link_type4' => 'disabled',
'max_links4' => '',
'link_list4' => '',
'link_nofollow4' => 0,
'link_post_types4' => '',
'max_tokens4' => '',
'max_seed_tokens4' => '',
'max_continue_tokens4' => '',
'temperature4' => '',
'top_p4' => '',
'presence_penalty4' => '',
'frequency_penalty4' => '',
'search_query_repetition4' => 0,
'enable_ai_images4' => 0,
'ai_command_image4' => 'A high detail image with no text of: "%%post_title%%"',
'model4' => '1024x1024',
'post_prepend4' => '',
'post_append4' => '',
'custom_shortcodes4' => '',
'skip_spin4' => 0,
'skip_translate4' => 0,
'strip_by_regex4' => '',
'replace_regex4' => '',
'csv_separator5' => '',
'strip_title5' => 0,
'skip_spin5' => 0,
'skip_translate5' => 0,
'random_order5' => 0,
'strip_by_regex5' => '',
'replace_regex5' => '',
'link_type5' => 'disabled',
'max_links5' => '',
'link_list5' => '',
'link_nofollow5' => 0,
'link_post_types5' => '',
'image_model1a' => 'gpt-image-2',
'image_model1b' => 'gpt-image-2',
'image_model2' => 'gpt-image-2',
'image_model3' => 'gpt-image-2',
'image_model4' => 'gpt-image-2',
'image_model6' => 'gpt-image-2',
            );
        }
        else
        {
            $key = 'aiomatic_templates_advanced'; 
            $single = true; 
            $aiomatic_templates = get_user_meta( $user_id, $key, $single );
            if(!is_array($aiomatic_templates))
            {
                $aiomatic_templates = array();
            }
            if(!isset($aiomatic_templates[$template_name]))
            {
                wp_send_json_error( array( 'message' => 'Advanced template name not found in the database' ) );
            }
            $aiomatic_templates = $aiomatic_templates[$template_name];
        }
    }
    wp_send_json_success( array( 'content' => $aiomatic_templates ) );
    die();
}

add_action('wp_ajax_aiomatic_handle_vision_image_upload', 'aiomatic_handle_vision_image_upload');
add_action('wp_ajax_nopriv_aiomatic_handle_vision_image_upload', 'aiomatic_handle_vision_image_upload');
function aiomatic_handle_vision_image_upload() 
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Image uploaded successfully');
    if ( !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'openai-persistent-nonce'))
    {
        $aiomatic_result['msg'] = esc_html__('You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $allowed_file_types = ['image/jpeg', 'image/png', 'image/gif'];
    if(!isset($_FILES['image']))
    {
        $aiomatic_result['msg'] = esc_html__('No file sent for upload.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $file = $_FILES['image'];
    if ($file['size'] > 10000000) 
    {
        $aiomatic_result['msg'] = esc_html__('File size exceeds maximum limit.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if (!in_array($file['type'], $allowed_file_types)) 
    {
        $aiomatic_result['msg'] = esc_html__('Invalid file type submitted.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    add_filter('upload_dir', 'aiomatic_custom_vision_upload_dir');
    $upload = wp_handle_upload($file, ['test_form' => false]);
    remove_filter('upload_dir', 'aiomatic_custom_vision_upload_dir');
    if (!empty($upload['error'])) 
    {
        $aiomatic_result['msg'] = esc_html__('Upload error: ', 'aiomatic-automatic-ai-content-writer') . esc_html($upload['error']);
        wp_send_json($aiomatic_result);
        die();
    } 
    else 
    {
        $attachment_data = [
            'post_mime_type' => $upload['type'],
            'post_title' => sanitize_file_name($upload['file']),
            'post_content' => '',
            'post_status' => 'inherit'
        ];
        $attachment_id = wp_insert_attachment($attachment_data, $upload['file']);
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        require_once(ABSPATH . 'wp-admin/includes/media.php');
        $attach_data = wp_generate_attachment_metadata($attachment_id, $upload['file']);
        wp_update_attachment_metadata($attachment_id, $attach_data);
        $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
        if (isset($aiomatic_Chatbot_Settings['file_expiration']) && trim($aiomatic_Chatbot_Settings['file_expiration']) != '') 
        {
            $mytime = strtotime(trim($aiomatic_Chatbot_Settings['file_expiration']));
            if($mytime !== false)
            {
                $tdate = gmdate('Y-m-d', $mytime);
                update_post_meta($attachment_id, 'expiry_check', '1');
                update_post_meta($attachment_id, 'expiry_date', $tdate);
            }
        }
        $image_url = wp_get_attachment_url($attachment_id);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['image_url'] = $image_url;
        wp_send_json($aiomatic_result);
    }
    wp_die();
}

add_action('wp_ajax_aiomatic_handle_chat_pdf_upload', 'aiomatic_handle_chat_pdf_upload');
add_action('wp_ajax_nopriv_aiomatic_handle_chat_pdf_upload', 'aiomatic_handle_chat_pdf_upload');
function aiomatic_handle_chat_pdf_upload() 
{
    if ( !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'openai-persistent-nonce'))
    {
        $aiomatic_result['msg'] = esc_html__('You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if ( !isset($_POST['pdf_namespace']) || empty($_POST['pdf_namespace']))
    {
        $aiomatic_result['msg'] = esc_html__('Please specify also a namespace for embeddings.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'PDF file uploaded successfully');
    if(!function_exists('is_plugin_active'))
    {
        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    }
    if (!is_plugin_active('aiomatic-extension-pdf-files/aiomatic-extension-pdf-files.php')) 
    {
        $aiomatic_result['msg'] = esc_html__("This option requires the Aimogen Extension - PDF File Storage And Parsing to be active.", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = esc_html__("You need to enter an AI service API key for this feature to work.", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if (isset($aiomatic_Main_Settings['pinecone_app_id'])) {
        $pinecone_app_id = $aiomatic_Main_Settings['pinecone_app_id'];
    } else {
        $pinecone_app_id = '';
    }
    if (isset($aiomatic_Main_Settings['qdrant_app_id'])) {
        $qdrant_app_id = $aiomatic_Main_Settings['qdrant_app_id'];
    } else {
        $qdrant_app_id = '';
    }
    if (isset($aiomatic_Main_Settings['chroma_app_id'])) {
        $chroma_app_id = $aiomatic_Main_Settings['chroma_app_id'];
    } else {
        $chroma_app_id = '';
    }
    if($pinecone_app_id == '' && $qdrant_app_id == '' && $chroma_app_id == '')
    {
        $aiomatic_result['msg'] = esc_html__("You need to enter a Pinecone.io API, Chroma API or a Qdrant API key for this to work", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if (!isset($aiomatic_Main_Settings['embeddings_chat_short']) || trim($aiomatic_Main_Settings['embeddings_chat_short']) != 'on')
    {
        $aiomatic_result['msg'] = esc_html__("You need to enable Embeddings for the Chatbot for this to work", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if (!isset($aiomatic_Chatbot_Settings['upload_pdf']) || $aiomatic_Chatbot_Settings['upload_pdf'] != 'on') 
    {
        $aiomatic_result['msg'] = esc_html__("You need to enable PDF chat in plugin settings.", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $allowed_file_types = ['application/pdf'];
    if(!isset($_FILES['image']))
    {
        $aiomatic_result['msg'] = esc_html__('No file sent for upload.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $file = $_FILES['image'];
    if ($file['size'] > 50000000) 
    {
        $aiomatic_result['msg'] = esc_html__('File size exceeds maximum limit.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if (!in_array($file['type'], $allowed_file_types)) 
    {
        $aiomatic_result['msg'] = esc_html__('Invalid file type submitted.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    add_filter('upload_dir', 'aiomatic_custom_vision_upload_dir');
    $upload = wp_handle_upload($file, ['test_form' => false]);
    remove_filter('upload_dir', 'aiomatic_custom_vision_upload_dir');
    if (!empty($upload['error'])) 
    {
        $aiomatic_result['msg'] = esc_html__('Upload error: ', 'aiomatic-automatic-ai-content-writer') . esc_html($upload['error']);
        wp_send_json($aiomatic_result);
        die();
    } 
    else 
    {
        $attachment_data = [
            'post_mime_type' => $upload['type'],
            'post_title' => sanitize_file_name($upload['file']),
            'post_content' => '',
            'post_status' => 'inherit'
        ];
        $attachment_id = wp_insert_attachment($attachment_data, $upload['file']);
        require_once(ABSPATH . 'wp-admin/includes/image.php');
        require_once(ABSPATH . 'wp-admin/includes/media.php');
        $attach_data = wp_generate_attachment_metadata($attachment_id, $upload['file']);
        wp_update_attachment_metadata($attachment_id, $attach_data);
        if (isset($aiomatic_Chatbot_Settings['file_expiration_pdf']) && trim($aiomatic_Chatbot_Settings['file_expiration_pdf']) != '') 
        {
            $mytime = strtotime(trim($aiomatic_Chatbot_Settings['file_expiration_pdf']));
            if($mytime !== false)
            {
                $tdate = gmdate('Y-m-d', $mytime);
                update_post_meta($attachment_id, 'expiry_check', '1');
                update_post_meta($attachment_id, 'expiry_date', $tdate);
            }
        }
        $post_urlx = wp_get_attachment_url($attachment_id);
        $htmlc = aiomatic_extension_pdfext_getRemoteFile($post_urlx);
        if($htmlc === false)
        {
            $aiomatic_result['msg'] = esc_html__('Failed to upload and process file', 'aiomatic-automatic-ai-content-writer');
            wp_send_json($aiomatic_result);
            die();
        }
        if(aiomatic_is_base64($htmlc))
        {
            $htmlc = base64_decode($htmlc);
        }
        $file_data = '';
        if(class_exists('\Smalot\PdfParser\Parser'))
        {
            try
            {
                $pparser = new \Smalot\PdfParser\Parser();
                $document = $pparser->parseContent($htmlc);
                if (isset($aiomatic_Main_Settings['pdf_page']) && trim($aiomatic_Main_Settings['pdf_page']) != '')
                {
                    $page_range = '1-' . trim($aiomatic_Main_Settings['pdf_page']);
                }
                else
                {
                    $page_range = '';
                }
                if($page_range == '')
                {
                    $file_data = $document->getText();
                }
                else
                {
                    $page_range_arr = array_map('trim', aiomatic_extension_pdfext_extract_range($page_range));
                    $nr = 1;
                    $pages  = $document->getPages();
                    foreach ($pages as $page) {
                        if(count($page_range_arr) == 0)
                        {
                            break;
                        }
                        if(in_array($nr, $page_range_arr))
                        {
                            $page_range_arr = array_diff($page_range_arr, array($nr));
                            $file_data .= $page->getText();
                        }
                        $nr++;
                    }
                    $pages = count($pages);
                }
            }
            catch(Exception $e)
            {
                //failed to parse with PdfParser
            }
        }
        $pdflim = array();
        if(($file_data === false || empty(trim($file_data))) && class_exists('PdfToText'))
        {
            try
            {
                $pdf =  new PdfToText();
                $pdf->LoadFromString($htmlc);
                $aiomatic_stats = new Aiomatic_Statistics();
                $pdflim = $aiomatic_stats->get_pdf_limits();
                $maxp = -1;
                if(is_array($pdflim))
                {
                    if(isset($pdflim[0]) && is_array($pdflim[0]))
                    {
                        foreach($pdflim[0] as $pdfp)
                        {
                            if(intval($pdfp) > $maxp)
                            {
                                $maxp = intval($pdfp);
                            }
                        }
                    }
                }
                if($maxp > 0)
                {
                    $page_range = '1-' . $maxp;
                }
                else
                {
                    if (isset($aiomatic_Main_Settings['pdf_page']) && trim($aiomatic_Main_Settings['pdf_page']) != '')
                    {
                        $page_range = '1-' . trim($aiomatic_Main_Settings['pdf_page']);
                    }
                    else
                    {
                        $page_range = '';
                    }
                }
                if($page_range == '')
                {
                    $file_data = $pdf->Text;
                }
                else
                {
                    $page_range_arr = array_map('trim', aiomatic_extension_pdfext_extract_range($page_range));
                    foreach( $pdf->Pages as $page_number => $page_contents)
                    {
                        if(count($page_range_arr) == 0)
                        {
                            break;
                        }
                        if(in_array($page_number, $page_range_arr))
                        {
                            $page_range_arr = array_diff($page_range_arr, array($page_number));
                            $file_data .= $page_contents;
                        }
                    }
                }  
            }
            catch(Exception $e)
            {
                //failed to parse with PdfToText
            }          
        }
        if(($file_data === false || empty(trim($file_data))) && class_exists('\Com\Tecnick\Pdf\Parser\Parser'))
        {
            try
            {
                $cfg = [
                    'ignore_filter_errors' => true,
                ];
                $pdfx = new \Com\Tecnick\Pdf\Parser\Parser($cfg);
                $file_data = $pdfx->parse($htmlc);
                $pdfText = '';
                if($file_data !== false)
                foreach ($file_data as $object) {
                    $pdfText .= aiomatic_extractTextFromObject($object);
                }
                if(empty($pdfText))
                {
                    $aiomatic_result['msg'] = esc_html__('No textual data found in the PDF file', 'aiomatic-automatic-ai-content-writer');
                    wp_send_json($aiomatic_result);
                    die();
                }
            }
            catch(Exception $e)
            {
                //failed to parse with Tecnick
            }
        }
        $file_data = nl2br($file_data);
        $maxc = -1;
        if(is_array($pdflim))
        {
            if(isset($pdflim[1]) && is_array($pdflim[1]))
            {
                foreach($pdflim[1] as $pdfc)
                {
                    if(intval($pdfc) > $maxc)
                    {
                        $maxc = intval($pdfc);
                    }
                }
            }
        }
        if($maxc > 0)
        {
            $file_data = (aiomatic_strlen($file_data) > $maxc) ? aiomatic_substr($file_data, 0, $maxc) : $file_data;
        }
        else
        {
            if (isset($aiomatic_Main_Settings['pdf_character']) && trim($aiomatic_Main_Settings['pdf_character']) != '')
            {
                $max_l = intval($aiomatic_Main_Settings['pdf_character']);
                $file_data = (aiomatic_strlen($file_data) > $max_l) ? aiomatic_substr($file_data, 0, $max_l) : $file_data;
            }
        }
        if (isset($aiomatic_Main_Settings['embeddings_model']) && $aiomatic_Main_Settings['embeddings_model'] != '') 
        {
            $model = $aiomatic_Main_Settings['embeddings_model'];
        }
        else
        {
            $model = AIMOGEN_DEFAULT_MODEL_EMBEDDING;
        }
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        require_once(dirname(__FILE__) . "/res/Embeddings.php");
        $embdedding = new Aiomatic_Embeddings($token);
        $aiomatic_result = $embdedding->aimogenpro_save_embedding($file_data, '', '', false, $model, $_POST['pdf_namespace']);
        wp_send_json($aiomatic_result);
    }
    wp_die();
}

add_action('wp_ajax_aiomatic_handle_chat_file_upload', 'aiomatic_handle_chat_file_upload');
add_action('wp_ajax_nopriv_aiomatic_handle_chat_file_upload', 'aiomatic_handle_chat_file_upload');
function aiomatic_handle_chat_file_upload() 
{
    if ( !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'openai-persistent-nonce'))
    {
        $aiomatic_result['msg'] = esc_html__('You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $file_upload_type = '';
    if ( isset($_POST['file_upload_type']) && $_POST['file_upload_type'] != '')
    {
        $file_upload_type = $_POST['file_upload_type'];
    }
    $thread_id = '';
    if ( isset($_POST['thread_id']) && $_POST['thread_id'] != '')
    {
        $thread_id = $_POST['thread_id'];
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'File uploaded successfully');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Azure/Claude API is not currently supported for uploads.';
        wp_send_json($aiomatic_result);
        die();
    }
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = esc_html__("You need to enter an AI service API key for this feature to work.", 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if(!isset($_FILES['image']))
    {
        $aiomatic_result['msg'] = esc_html__('No file sent for upload.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $open_ai = new OpenAi($token);
    if(!$open_ai){
        $aiomatic_result['msg'] = 'Missing API Setting';
        wp_send_json($aiomatic_result);
        die();
    }
    if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
    {
        $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
    }
    if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
    {
        $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
    }

    $uploaded_file_ids = array();
    $files = $_FILES['image'];
    $file_count = is_array($files['name']) ? count($files['name']) : 1;

    for ($i = 0; $i < $file_count; $i++) {
        $name     = is_array($files['name']) ? $files['name'][$i] : $files['name'];
        $tmp_name = is_array($files['tmp_name']) ? $files['tmp_name'][$i] : $files['tmp_name'];
        $size     = is_array($files['size']) ? $files['size'][$i] : $files['size'];

        if ($size > 512000000) continue;

        $file_name = sanitize_file_name('chatbot-' . $name);
        $detected  = wp_check_filetype($file_name);
        $mime      = $detected['type'] ?: 'application/octet-stream';

        $c_file = curl_file_create($tmp_name, $mime, $file_name);
        $delay = '';
        if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
        {
            if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
            {
                $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                {
                    $delay = rand(trim($tempo[0]), trim($tempo[1]));
                }
            }
            else
            {
                if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                {
                    $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                }
            }
        }
        if($delay != '' && is_numeric($delay))
        {
            usleep($delay);
        }
        $upload_response = $open_ai->uploadFile(array(
            'purpose' => 'assistants',
            'file'    => $c_file,
        ));

        $upload_result = json_decode($upload_response);

        if (isset($upload_result->id)) {
            $uploaded_file_ids[] = $upload_result->id;
        }
    }
    if (empty($uploaded_file_ids)) {
        $aiomatic_result['msg'] = 'No files were successfully uploaded to AI service.';
        wp_send_json($aiomatic_result);
        die();
    }
    if($file_upload_type === 'responses')
    {
        $aiomatic_result['msg'] = implode(',', $uploaded_file_ids);
        $aiomatic_result['fid'] = implode(',', $uploaded_file_ids);
        $aiomatic_result['status'] = 'success';
        wp_send_json($aiomatic_result);
        wp_die();
    }
    require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
    try
    {
        $vs = aiomatic_openai_create_vector_store($token, 'New Vector Store', $uploaded_file_ids);
        if(isset($vs['id']))
        {
            if(!empty($thread_id))
            {
                $thread = aiomatic_openai_modify_thread($token, $thread_id, $vs['id']);
                if(!isset($thread['id']))
                {
                    $aiomatic_result['msg'] = 'Invalid thread format when modifying thread: ' . print_r($thread, true);
                    wp_send_json($aiomatic_result);
                    wp_die();
                }
            }
            $aiomatic_result['msg'] = $vs['id'];
            $aiomatic_result['fid'] = implode(',', $uploaded_file_ids);;
            $aiomatic_result['status'] = 'success';
        }
    }
    catch(Exception $e)
    {
        $aiomatic_result['msg'] = 'Exception in vector store creation: ' . $e->getMessage();
    }
    wp_send_json($aiomatic_result);
    wp_die();
}

add_action('wp_ajax_aiomatic_save_chat_data', 'aiomatic_save_chat_data');
add_action('wp_ajax_nopriv_aiomatic_save_chat_data', 'aiomatic_save_chat_data');
function aiomatic_save_chat_data() 
{
    if ( !isset($_POST['nonce']) || !wp_verify_nonce( $_POST['nonce'], 'openai-persistent-nonce'))
    {
        $aiomatic_result['msg'] = esc_html__('You are not allowed to do this.', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if ( !isset($_POST['uniqid']) || empty($_POST['uniqid']))
    {
        $aiomatic_result['msg'] = esc_html__('Missing parameter (uniqid)', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if ( !isset($_POST['input_text']))
    {
        $aiomatic_result['msg'] = esc_html__('Missing parameter (input_text)', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if ( !isset($_POST['remember_string']))
    {
        $aiomatic_result['msg'] = esc_html__('Missing parameter (remember_string)', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    if ( !isset($_POST['user_question']))
    {
        $aiomatic_result['msg'] = esc_html__('Missing parameter (user_question)', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
        die();
    }
    $setarr = array($_POST['input_text'], $_POST['remember_string'], $_POST['user_question']);
    if (isset($_POST['function_result']) && !empty($_POST['function_result']))
    {
        $setarr[] = $_POST['function_result'];
    }
    set_transient('aiomatic_ai_data_' . $_POST['uniqid'], $setarr, 900);
    $aiomatic_result = array('status' => 'success', 'msg' => 'OK');
    wp_send_json($aiomatic_result);
    wp_die();
}
add_action('wp_ajax_aiomatic_chat_submit', 'aiomatic_chat_submit');
add_action('wp_ajax_nopriv_aiomatic_chat_submit', 'aiomatic_chat_submit');
function aiomatic_chat_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with chat');
    if(!isset($_POST['input_text']) || !isset($_POST['model']) || !isset($_POST['temp']) || !isset($_POST['presence']) || !isset($_POST['frequency']) || !isset($_POST['remember_string']))
    {
        $aiomatic_result['msg'] = esc_html__('Incomplete POST request for chat', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    $is_modern_gpt = '0';
    if(isset($_POST['is_modern_gpt']))
    {
        $is_modern_gpt = $_POST['is_modern_gpt'];
    }
    $no_internet = false;
    if(isset($_POST['internet_access']) && ($_POST['internet_access'] === 'no' || $_POST['internet_access'] === '0' || $_POST['internet_access'] == 'off' || $_POST['internet_access'] == 'disabled' || $_POST['internet_access'] == 'Disabled' || $_POST['internet_access'] == 'disable' || $_POST['internet_access'] == "false"))
    {
        $no_internet = true;
    }
    $no_embeddings = false;
    if(isset($_POST['embeddings']) && ($_POST['embeddings'] === 'no' || $_POST['embeddings'] === '0' || $_POST['embeddings'] == 'off' || $_POST['embeddings'] == 'disabled' || $_POST['embeddings'] == 'disable' || $_POST['embeddings'] == 'Disabled' || $_POST['embeddings'] == "false"))
    {
        $no_embeddings = true;
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    if(isset($_POST['file_data']))
    {
        $file_data = stripslashes($_POST['file_data']);
    }
    else
    {
        $file_data = '';
    }
    if(isset($_POST['ai_thread_id']))
    {
        $thread_id = stripslashes($_POST['ai_thread_id']);
    }
    else
    {
        $thread_id = '';
    }
    if(isset($_POST['mcp_servers']))
    {
        $mcp_servers = stripslashes($_POST['mcp_servers']);
    }
    else
    {
        $mcp_servers = '';
    }
    if(isset($_POST['store_data']))
    {
        $store_data = stripslashes($_POST['store_data']);
    }
    else
    {
        $store_data = 'off';
    }
    if(isset($_POST['user_id']))
    {
        $user_id = stripslashes($_POST['user_id']);
    }
    else
    {
        $user_id = '';
    }

    $rcid  = isset($_POST['rcid']) ? absint($_POST['rcid']) : 0;
    $rcsig = isset($_POST['rcsig']) ? sanitize_text_field($_POST['rcsig']) : '';
    if ($rcid > 0) {

        $p = get_post($rcid);

        if ($p && $p->post_type === 'aiomatic_remote_chat') {

            $is_user_generated = get_post_meta($rcid, '_aimogen_user_generated', true);

            if ($is_user_generated === '1') {

                $secret = get_post_meta($rcid, '_aiomatic_remote_secret', true);
                $expected = hash_hmac('sha256', (string)$rcid, $secret);

                if (!hash_equals($expected, $rcsig)) {
                    $aiomatic_result['msg'] = esc_html__('Remote chatbot authentication failed.', 'aiomatic-automatic-ai-content-writer');
                    wp_send_json($aiomatic_result);
                }

                $user_id = (int) $p->post_author;
            }
        }
    }
    if(isset($_POST['pdf_data']))
    {
        $embedding_namespace = stripslashes($_POST['pdf_data']);
    }
    else
    {
        $embedding_namespace = '';
    }
    if(empty($embedding_namespace))
    {
        if (isset($aiomatic_Chatbot_Settings['persistent']) && $aiomatic_Chatbot_Settings['persistent'] == 'vector')
        {
            $embedding_namespace = 'persistentchat_' . $user_id . '_' . $thread_id;
        }
        else
        {
            if(isset($_POST['embeddings_namespace']) && !empty($_POST['embeddings_namespace']))
            {
                $embedding_namespace = $_POST['embeddings_namespace'];
            }
        }
    }
	$input_text = stripslashes($_POST['input_text']);
    if (isset($aiomatic_Chatbot_Settings['max_input_length']) && $aiomatic_Chatbot_Settings['max_input_length'] != '' && is_numeric($aiomatic_Chatbot_Settings['max_input_length'])) 
    {
        if(aiomatic_strlen($input_text) > intval($aiomatic_Chatbot_Settings['max_input_length']))
        {
            $input_text = aiomatic_substr($input_text, 0, intval($aiomatic_Chatbot_Settings['max_input_length']));
        }
    }
    if(isset($_POST['remember_string']))
    {
        $remember_string = stripslashes($_POST['remember_string']);
    }
    else
    {
        $remember_string = '';
    }
    if(isset($_POST['user_question']))
    {
        $user_question = stripslashes($_POST['user_question']);
    }
    else
    {
        $user_question = '';
    }
    if(isset($_POST['ai_assistant_id']))
    {
        $assistant_id = stripslashes($_POST['ai_assistant_id']);
    }
    else
    {
        $assistant_id = '';
    }
    if(isset($_POST['model']))
    {
        $model = stripslashes($_POST['model']);
    }
    else
    {
        $model = 'default';
    }
    if($model == 'default')
    {
        $model = AIMOGEN_DEFAULT_MODEL;
    }
    if(isset($_POST['temp']))
    {
        $temperature = stripslashes($_POST['temp']);
    }
    else
    {
        $temperature = '1';
    }
    if(isset($_POST['top_p']))
    {
        $top_p = stripslashes($_POST['top_p']);
    }
    else
    {
        $top_p = '1';
    }
    if(isset($_POST['presence']))
    {
        $presence_penalty = stripslashes($_POST['presence']);
    }
    else
    {
        $presence_penalty = '0';
    }
    if(isset($_POST['frequency']))
    {
        $frequency_penalty = stripslashes($_POST['frequency']);
    }
    else
    {
        $frequency_penalty = '0';
    }
    if($is_modern_gpt == '1')
    {
        if(!empty($remember_string))
        {
            $remember_string = json_decode($remember_string, true);
            if($remember_string === null)
            {
                $aiomatic_result['msg'] = esc_html__('Failed to decode conversation data in request!', 'aiomatic-automatic-ai-content-writer');
                wp_send_json($aiomatic_result);
            }
            if(!is_array($remember_string))
            {
                $remember_string = [];
            }
        }
        else
        {
            $remember_string = [];
        }
        if (isset($aiomatic_Chatbot_Settings['chatbot_prepend']) && trim($aiomatic_Chatbot_Settings['chatbot_prepend']) != '') 
        {
            array_unshift($remember_string, array ('role' => 'user', 'content' => trim($aiomatic_Chatbot_Settings['chatbot_prepend'])));
        }
        if (isset($aiomatic_Chatbot_Settings['chatbot_append']) && trim($aiomatic_Chatbot_Settings['chatbot_append']) != '') 
        {
            $remember_string[] = array ('role' => 'user', 'content' => trim($aiomatic_Chatbot_Settings['chatbot_append']));
        }
        $remember_string[] = array ('role' => 'user', 'content' => $input_text);
        $input_text = $remember_string;
    }
    else
    {
        if (isset($aiomatic_Chatbot_Settings['chatbot_prepend']) && trim($aiomatic_Chatbot_Settings['chatbot_prepend']) != '') 
        {
            $remember_string = trim($aiomatic_Chatbot_Settings['chatbot_prepend']) . ' ' . $remember_string;
        }
        if (isset($aiomatic_Chatbot_Settings['chatbot_append']) && trim($aiomatic_Chatbot_Settings['chatbot_append']) != '') 
        {
            $remember_string = $remember_string . ' ' . trim($aiomatic_Chatbot_Settings['chatbot_append']);
        }
        if(!empty(trim($remember_string)))
        {
            $input_text = trim($remember_string) . PHP_EOL . $input_text;
        }
    }
    $all_models = aiomatic_get_all_models();
    $models = $all_models;
    if(!in_array($model, $models))
    {
        $aiomatic_result['msg'] = esc_html__('Invalid model provided: ', 'aiomatic-automatic-ai-content-writer') . $model;
        wp_send_json($aiomatic_result);
    }
    $vision_file = '';
    if(isset($_REQUEST['vision_file']))
    {
        if(aiomatic_is_vision_model($model, $assistant_id))
        {
            $vision_file = stripslashes($_REQUEST['vision_file']);
        }
    }
    $temperature = floatval($temperature);
    $top_p = floatval($top_p);
    $presence_penalty = floatval($presence_penalty);
    $frequency_penalty = floatval($frequency_penalty);
    if (isset($aiomatic_Chatbot_Settings['restriction_time']) && $aiomatic_Chatbot_Settings['restriction_time'] != '' && is_numeric($aiomatic_Chatbot_Settings['restriction_time']) 
    && isset($aiomatic_Chatbot_Settings['restriction_count']) && $aiomatic_Chatbot_Settings['restriction_count'] != '' && is_numeric($aiomatic_Chatbot_Settings['restriction_count'])) 
    {
        $user_ip = $_SERVER['REMOTE_ADDR'];
        $restriction_time = esc_attr( $aiomatic_Chatbot_Settings['restriction_time'] );
        $user_hash = 'ai' . md5( $user_ip );
        $user_requests = get_transient( $user_hash );
        if($user_requests === false)
        {
            $user_requests = 0;
        }
        $user_max_requests = esc_attr( $aiomatic_Chatbot_Settings['restriction_count'] );
        if ( (int)$user_requests >= (int)$user_max_requests ) 
        {
            $restriction_message = '';
            if (isset($aiomatic_Chatbot_Settings['restriction_message']) && $aiomatic_Chatbot_Settings['restriction_message'] != '')
            {
                $restriction_message = $aiomatic_Chatbot_Settings['restriction_message'];
            }
            $aiomatic_result['msg'] = $restriction_message;
            wp_send_json($aiomatic_result);
        }
        set_transient( $user_hash, (int)$user_requests + 1, (int)$restriction_time );
    }
    if($temperature < 0 || $temperature > 2)
    {
        $aiomatic_result['msg'] = esc_html__('Invalid temperature provided: ', 'aiomatic-automatic-ai-content-writer') . $temperature;
        wp_send_json($aiomatic_result);
    }
    if($top_p < 0 || $top_p > 1)
    {
        $aiomatic_result['msg'] = esc_html__('Invalid top_p provided: ', 'aiomatic-automatic-ai-content-writer') . $top_p;
        wp_send_json($aiomatic_result);
    }
    if($presence_penalty < -2 || $presence_penalty > 2)
    {
        $aiomatic_result['msg'] = esc_html__('Invalid presence_penalty provided: ', 'aiomatic-automatic-ai-content-writer') . $presence_penalty;
        wp_send_json($aiomatic_result);
    }
    if($frequency_penalty < -2 || $frequency_penalty > 2)
    {
        $aiomatic_result['msg'] = esc_html__('Invalid frequency_penalty provided: ', 'aiomatic-automatic-ai-content-writer') . $frequency_penalty;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = esc_html__('You need to insert a valid AI service API Key for this to work!', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_chat_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = esc_html__('Daily token count of your user account was exceeded! Please try again tomorrow.', 'aiomatic-automatic-ai-content-writer');
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $max_tokens = aimogenpro_get_max_tokens($model);
    if (isset($aiomatic_Chatbot_Settings['max_tokens']) && $aiomatic_Chatbot_Settings['max_tokens'] !== '' && is_numeric($aiomatic_Chatbot_Settings['max_tokens']))
    {
        $max_tokens_chatbot = intval($aiomatic_Chatbot_Settings['max_tokens']);
        if(intval($max_tokens_chatbot) < $max_tokens)
        {
            $max_tokens = intval($max_tokens_chatbot);
            if($max_tokens <= 0)
            {
                $max_tokens = 1000;
            }
        }
    }
    if($is_modern_gpt == '1')
    {
        $aitext = '';
        foreach($input_text as $aimess)
        {
            if(isset($aimess['content']))
            {
                if(!is_array($aimess['content']))
                {
                    $aitext .= $aimess['content'] . '\n';
                }
                else
                {
                    foreach($aimess['content'] as $internalmess)
                    {
                        if($internalmess['type'] == 'text')
                        {
                            $aitext .= $internalmess['text'] . '\n';
                        }
                    }
                }
            }
        }
        $query_token_count = count(aiomatic_encode($aitext));
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $aitext, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $startIndex = intdiv(count($input_text), 2);
            $input_text = array_slice($input_text, $startIndex);
            $lastindex = end(array_keys($input_text));
            $string_len = aiomatic_strlen($input_text[$lastindex]['content']);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $input_text[$lastindex]['content'] = aiomatic_substr($input_text[$lastindex]['content'], 0, $string_len);
            $input_text[$lastindex]['content'] = trim($input_text[$lastindex]['content']);
            $aitext = '';
            foreach($input_text as $aimess)
            {
                if(isset($aimess['content']))
                {
                    if(!is_array($aimess['content']))
                    {
                        $aitext .= $aimess['content'] . '\n';
                    }
                    else
                    {
                        foreach($aimess['content'] as $internalmess)
                        {
                            if($internalmess['type'] == 'text')
                            {
                                $aitext .= $internalmess['text'] . '\n';
                            }
                        }
                    }
                }
            }
            $query_token_count = count(aiomatic_encode($aitext));
            $available_tokens = $max_tokens - $query_token_count;
        }
    }
    else
    {
        $query_token_count = count(aiomatic_encode($input_text));
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $input_text, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $string_len = aiomatic_strlen($input_text);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $input_text = aiomatic_substr($input_text, 0, $string_len);
            $input_text = trim($input_text);
            if(empty($input_text))
            {
                aiomatic_log_to_file('Empty API seed expression provided (after processing)');
                $aiomatic_result['msg'] = esc_html__('An internal error occurred, please try again later!', 'aiomatic-automatic-ai-content-writer');
                wp_send_json($aiomatic_result);
            }
            $query_token_count = count(aiomatic_encode($input_text));
            $available_tokens = $max_tokens - $query_token_count;
        }
    }
    $function_result = 'disabled';
    $forced_functions = array();
    if(isset($_POST['enable_god_mode']))
    {
        if($_POST['enable_god_mode'] === 'disabled' || $_POST['enable_god_mode'] === 'off')
        {
            $_POST['enable_god_mode'] = '';
        }
        if($_POST['enable_god_mode'] === 'on' || $_POST['enable_god_mode'] === 'yes' || $_POST['enable_god_mode'] === 'true' || $_POST['enable_god_mode'] === '1' ||  $_POST['enable_god_mode'] === 'enable' ||  $_POST['enable_god_mode'] === 'enabled')
        {
            $function_result = '';
        }
        elseif(is_string($_POST['enable_god_mode']) && !empty($_POST['enable_god_mode']))
        {
            $forced_functions = explode(',', $_POST['enable_god_mode']);
            $function_result = '';
        }
    }
	$error = '';
    $finish_reason = '';
    do_action('aiomatic_calling_chatbot', $input_text, $model);
    $response_text = aiomatic_generate_text($token, $model, $input_text, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, true, 'shortcodeChat', 0, $finish_reason, $error, $no_internet, $no_embeddings, false, $vision_file, $user_question, 'user', $assistant_id, $thread_id, $embedding_namespace, $function_result, $file_data, false, $store_data, $forced_functions, $mcp_servers, $rcid, $rcsig);    
    if($response_text === false)
    {
        $aiomatic_result['msg'] = $error;
        wp_send_json($aiomatic_result);
    }
    else
    {
        if($is_modern_gpt == '1')
        {
            $aitext = '';
            foreach($input_text as $aimess)
            {
                if(isset($aimess['content']))
                {
                    if(!is_array($aimess['content']))
                    {
                        $aitext .= $aimess['content'] . '\n';
                    }
                    else
                    {
                        foreach($aimess['content'] as $internalmess)
                        {
                            if($internalmess['type'] == 'text')
                            {
                                $aitext .= $internalmess['text'] . '\n';
                            }
                        }
                    }
                }
            }
            $inp_count = count(aiomatic_encode($aitext));
        }
        else
        {
            $inp_count = count(aiomatic_encode($input_text));
        }
        $resp_count = count(aiomatic_encode($response_text));
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + $inp_count + $resp_count;
            update_user_meta($user_id, 'aiomatic_used_chat_tokens', $used_token_count);
        }
    }
    $aiomatic_result['status'] = 'success';
	if (isset($aiomatic_Chatbot_Settings['enable_html'])) 
    {
        if(trim($aiomatic_Chatbot_Settings['enable_html']) == 'on')
        {
            if (isset($aiomatic_Chatbot_Settings['strip_js']) && trim($aiomatic_Chatbot_Settings['strip_js']) == 'on') 
            {
                $response_text = preg_replace('/<script([\s\S]*?)\/\s*script>/is', "", $response_text);
                $response_text = preg_replace('/on[a-zA-Z]*="([^"]*?)"/is', "", $response_text);
            }
            $aiomatic_result['data'] = trim($response_text);
        }
        else
        {
            $aiomatic_result['data'] = trim(esc_html($response_text));
        }
    }
    else
    {
        $aiomatic_result['data'] = trim(esc_html($response_text));
    }
    if ((isset($aiomatic_Main_Settings['markdown_parse']) && $aiomatic_Main_Settings['markdown_parse'] == 'on') || (isset($aiomatic_Chatbot_Settings['markdown_parse']) && $aiomatic_Chatbot_Settings['markdown_parse'] == 'on'))
    {
        if(aiomatic_containsMarkdown($aiomatic_result['data']))
        {
            $aiomatic_result['data'] = aiomatic_parse_markdown($aiomatic_result['data']);
        }
    }
    $aiomatic_result['thread_id'] = $thread_id;
    do_action('aiomatic_chat_reply', $aiomatic_result);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_add_prompt', function() {
    check_ajax_referer('update_prompt_nonce');

    $category = sanitize_text_field($_POST['category']);
    $prompt = sanitize_text_field($_POST['prompt']);

    $upload_dir = wp_upload_dir();
    $file = trailingslashit($upload_dir['basedir']) . 'aiomatic/prompts.json';

    if (!file_exists($file)) {
        wp_send_json_error(['message' => 'Prompt file not found.']);
    }

    $data = json_decode(file_get_contents($file), true);

    if (!isset($data[$category])) {
        $data[$category] = [];
    }

    if (in_array($prompt, $data[$category])) {
        wp_send_json_error(['message' => 'Prompt already exists.']);
    }

    $data[$category][] = $prompt;
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $wp_filesystem->put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    wp_send_json_success();
});

add_action('wp_ajax_delete_prompt', function() {
    check_ajax_referer('update_prompt_nonce');

    $category = sanitize_text_field($_POST['category']);
    $prompt = sanitize_text_field($_POST['prompt']);

    $upload_dir = wp_upload_dir();
    $file = trailingslashit($upload_dir['basedir']) . 'aiomatic/prompts.json';

    if (!file_exists($file)) {
        wp_send_json_error(['message' => 'Prompt file not found.']);
    }

    $data = json_decode(file_get_contents($file), true);

    if (!isset($data[$category])) {
        wp_send_json_error(['message' => 'Category not found.']);
    }

    $index = array_search($prompt, $data[$category]);
    if ($index === false) {
        wp_send_json_error(['message' => 'Prompt not found.']);
    }

    unset($data[$category][$index]);
    $data[$category] = array_values($data[$category]);

    if (empty($data[$category])) {
        unset($data[$category]);
    }
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $wp_filesystem->put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
    wp_send_json_success();
});

add_action('wp_ajax_update_prompt', function() {
    check_ajax_referer('update_prompt_nonce');

    $category = sanitize_text_field($_POST['category']);
    $old_prompt = sanitize_text_field($_POST['old_prompt']);
    $new_prompt = sanitize_text_field($_POST['new_prompt']);

    $upload_dir = wp_upload_dir();
    $prompts_dir = trailingslashit($upload_dir['basedir']) . 'aiomatic';
    $file = trailingslashit($prompts_dir) . 'prompts.json';

    if (!file_exists($file)) {
        $default_file = plugin_dir_path(__FILE__) . 'assets/prompts.json';
        if (file_exists($default_file)) {
            wp_mkdir_p($prompts_dir);
            copy($default_file, $file);
        } else {
            wp_send_json_error(['message' => 'Default prompts file not found']);
        }
    }
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $json = $wp_filesystem->get_contents($file);
    $data = json_decode($json, true);
    if (!isset($data[$category])) {
        wp_send_json_error(['message' => 'Category not found']);
    }

    $found = false;
    foreach ($data[$category] as $index => $prompt) {
        if ($prompt === $old_prompt) {
            $data[$category][$index] = $new_prompt;
            $found = true;
            break;
        }
    }

    if (!$found) {
        wp_send_json_error(['message' => 'Prompt not found in category']);
    }

    $result = $wp_filesystem->put_contents($file, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));

    if ($result === false) {
        wp_send_json_error(['message' => 'Failed to save updated prompt']);
    }

    wp_send_json_success(['message' => 'Prompt updated']);
});

add_action('wp_ajax_aiomatic_post_now', 'aiomatic_aiomatic_submit_post_callback');
function aiomatic_aiomatic_submit_post_callback()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $run_id = $_POST['id'];
    $wp_post = get_post($run_id);
    if($wp_post != null)
    {
        $template = 'skip';
        if(isset($_POST['template']) && !empty($_POST['template']))
        {
            $template = $_POST['template'];
        }
        aiomatic_do_post($wp_post, true, $template, false);
    }
    die();
}

add_action('wp_ajax_aiomatic_comparison', 'aiomatic_comparison');
add_action('wp_ajax_nopriv_aiomatic_comparison', 'aiomatic_comparison');
function aiomatic_comparison()
{
    $aiomatic_result = array('status' => 'error');
    if ( ! wp_verify_nonce( $_POST['_wpnonce'], 'aiomatic_comparison_generator' ) ) {
        $aiomatic_result['msg'] = esc_html__('Nonce verification failed', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if(empty($token))
    {
        $aiomatic_result['msg'] = esc_html__('A valid OpenAI API key is needed for this to work!', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['model']) || empty($_REQUEST['model']))
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['temperature']) || $_REQUEST['temperature'] === '')
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['max_tokens']) || empty($_REQUEST['max_tokens']))
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['top_p']) || $_REQUEST['top_p'] === '')
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['frequency_penalty']) || $_REQUEST['frequency_penalty'] === '')
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    if(!isset($_REQUEST['presence_penalty']) || $_REQUEST['presence_penalty'] === '')
    {
        $aiomatic_result['msg'] = esc_html__('Invalid request sent', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    $model = sanitize_text_field($_REQUEST['model']);
    $prompt = $_REQUEST['prompt'];
    $temperature = floatval(sanitize_text_field($_REQUEST['temperature']));
    $max_tokens = intval(sanitize_text_field($_REQUEST['max_tokens']));
    $top_p = floatval(sanitize_text_field($_REQUEST['top_p']));
    $frequency_penalty = floatval(sanitize_text_field($_REQUEST['frequency_penalty']));
    $presence_penalty = floatval(sanitize_text_field($_REQUEST['presence_penalty']));

    $assistant_id = '';
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $max_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'aiModelComparison', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
        $aiomatic_result['msg'] = esc_html__('Failed to generate AI content: ', 'aiomatic-automatic-ai-content-writer') . $aierror;
        wp_send_json($aiomatic_result);
	}
	else
	{
		$generated_text = aiomatic_sanitize_ai_result($generated_text);
	}
    global $aiomatic_stats;
    $aiomatic_estimated = 0;
    $aiomatic_tokens = 0;
    if(method_exists($aiomatic_stats, 'getDetails'))
    {
        $stop = null;
        $session = aiomatic_get_session_id();
        $mode = 'text';
        $maxResults = 1;
        $query = new Aiomatic_Query($prompt, $max_tokens, $model, $temperature, $stop, 'aiModelComparison', $mode, $token, $session, $maxResults, '', '');
        $estimates = $aiomatic_stats->getDetails($query, $generated_text);
        $aiomatic_estimated = $estimates['price'];
        $aiomatic_tokens = $estimates['units'];
    }
    $aiomatic_result['text'] = $generated_text;
    $aiomatic_result['tokens'] = $aiomatic_tokens;
    $aiomatic_result['words'] = str_word_count($generated_text);
    $aiomatic_result['cost'] = $aiomatic_estimated;
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
}

add_action('wp_ajax_aiomatic_delete_embedding', 'aiomatic_aiomatic_delete_embedding');
function aiomatic_aiomatic_delete_embedding()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong embedding deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['embeddingid']))
    {
        $aiomatic_result['msg'] = 'Field missing: embeddingid';
    }
    else
    {
        $embeddingid = $_POST['embeddingid'];
        if($embeddingid != '' && is_numeric($embeddingid))
        {
            $wp_post = get_post($embeddingid);
            if($wp_post != null)
            {
                $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
                {
                    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
                    $appids = array_filter($appids);
                    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
                    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
                    require_once(dirname(__FILE__) . "/res/Embeddings.php");
                    $embdedding = new Aiomatic_Embeddings($token);
                    $status = $embdedding->aiomatic_delete_embedding($embeddingid);
                    $aiomatic_result = $status;
                }
                else
                {
                    $aiomatic_result['msg'] = 'No app ID in plugin settings.';
                }
            }
            else
            {
                $aiomatic_result['msg'] = 'No post found with this ID: ' . $embeddingid;
            }
        }
        else
        {
            $aiomatic_result['msg'] = 'Blank embedding ID added';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_logs', 'aiomatic_delete_logs');
function aiomatic_delete_logs()
{
    $aiomatic_result = array('status' => 'success', 'msg' => 'Data deleted successfully');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $GLOBALS['aiomatic_stats']->clear_db();
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_user_logs', 'aiomatic_delete_user_logs');
function aiomatic_delete_user_logs()
{
    $aiomatic_result = array('status' => 'success', 'msg' => 'Data deleted successfully');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['delfor']) || empty($_POST['delfor']))
    {
        $aiomatic_result['status'] = 'fail';
        $aiomatic_result['msg'] = 'Empty username added in the request!';
        wp_send_json($aiomatic_result);
    }
    $userId = null;
    $user = get_user_by('login', $_POST['delfor']);
    if($user)
    {
        $userId = $user->ID;
    }
    else
    {
        $aiomatic_result['status'] = 'fail';
        $aiomatic_result['msg'] = 'Failed to get user ID for username: ' . $_POST['delfor'];
        wp_send_json($aiomatic_result);
    }
    $aiomatic_stats = new Aiomatic_Statistics();
    $aiomatic_stats->deleteUsageEntries('all', null, $userId, null, null);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_personas', 'aiomatic_personas');
function aiomatic_personas()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Personas');
    check_ajax_referer('aiomatic_personas', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['aiomatic-persona-title']) || empty($_POST['aiomatic-persona-title']))
    {
        $aiomatic_result['msg'] = 'Empty persona title added!';
        wp_send_json($aiomatic_result);
    }
    $title = $_POST['aiomatic-persona-title'];
    if(!isset($_POST['aiomatic-persona-prompt']) || empty($_POST['aiomatic-persona-prompt']))
    {
        $aiomatic_result['msg'] = 'Empty persona prompt added!';
        wp_send_json($aiomatic_result);
    }
    $prompt = $_POST['aiomatic-persona-prompt'];
    $description = '';
    if(isset($_POST['aiomatic-persona-description']) && !empty($_POST['aiomatic-persona-description']))
    {
        $description = $_POST['aiomatic-persona-description'];
    }
    $first_message = '';
    if(isset($_POST['aiomatic-persona-first-message']) && !empty($_POST['aiomatic-persona-first-message']))
    {
        $first_message = $_POST['aiomatic-persona-first-message'];
    }
    $avatar = '';
    if(isset($_POST['aiomatic-persona-avatar']) && !empty($_POST['aiomatic-persona-avatar']))
    {
        $avatar = $_POST['aiomatic-persona-avatar'];
    }
    $model = '';
    if(isset($_POST['aiomatic-persona-model']) && !empty($_POST['aiomatic-persona-model']))
    {
        $model = $_POST['aiomatic-persona-model'];
    }
    $aiomatic_result = aiomatic_save_persona($title, $prompt, $description, $first_message, $avatar, $model);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_list_assistant_files', 'aiomatic_list_assistant_files');
function aiomatic_list_assistant_files()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with finetuning files');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Claude API is not currently supported for assistants.';
        wp_send_json($aiomatic_result);
    }
    //assistant file listing is currently not supported for Azure
    if(aiomatic_check_if_azure($aiomatic_Main_Settings))
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = array();
        wp_send_json($aiomatic_result);
        die();
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        $open_ai = new OpenAi($token);
        if(!$open_ai){
            $aiomatic_result['msg'] = 'Missing API Setting';
            wp_send_json($aiomatic_result);
        }
        if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
        {
            $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
        }
        if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
        {
            $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
        }
        $delay = '';
        if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
        {
            if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
            {
                $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                {
                    $delay = rand(trim($tempo[0]), trim($tempo[1]));
                }
            }
            else
            {
                if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                {
                    $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                }
            }
        }
        if($delay != '' && is_numeric($delay))
        {
            usleep($delay);
        }
        $result = $open_ai->listFiles(array(
            'purpose' => 'assistants'
        ));
        $result = json_decode($result);
        if(isset($result->error)){
            $aiomatic_result['msg'] = $result->error->message;
        }
        else{
            if(isset($result->data) && is_array($result->data) && count($result->data))
            {
                foreach($result->data as $ind => $rd)
                {
                    if($rd->purpose != 'assistants')
                    {
                        unset($result->data[$ind]);
                    }
                }
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['data'] = $result->data;
            }
            else{
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['data'] = array();
            }
        }
        wp_send_json($aiomatic_result);
        die();
    }
}

add_action('wp_ajax_aiomatic_list_batch_files', 'aiomatic_list_batch_files');
function aiomatic_list_batch_files()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with batch files');
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if(aiomatic_check_if_azure($aiomatic_Main_Settings))
    {
        $headers = [
            'Content-Type: application/json',
            'api-key: ' . $token
        ];
        if (isset($aiomatic_Main_Settings['azure_api_selector_batch']) && $aiomatic_Main_Settings['azure_api_selector_batch'] != '' && $aiomatic_Main_Settings['azure_api_selector_batch'] != 'default')
        {
            $api_ver = '?api-version=' . $aiomatic_Main_Settings['azure_api_selector_batch'];
        }
        else
        {
            $api_ver = AIMOGEN_AZURE_BATCHES_API_VERSION;
        }
        $azure_endpoint = isset($aiomatic_Main_Settings['azure_endpoint']) && $aiomatic_Main_Settings['azure_endpoint'] ? trim($aiomatic_Main_Settings['azure_endpoint'], '/') : '';

        if (!$azure_endpoint) {
            $aiomatic_result['msg'] = 'Azure endpoint is missing in settings.';
            wp_send_json($aiomatic_result);
        }

        $url = $azure_endpoint . '/openai/files' . $api_ver;

        $ch = curl_init($url);
        if ($ch === false) {
            $aiomatic_result['msg'] = 'Failed to initialize cURL';
            wp_send_json($aiomatic_result);
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

        $response = curl_exec($ch);
        if ($response === false) {
            $error = curl_error($ch);
            curl_close($ch);
            $aiomatic_result['msg'] = "cURL error: $error";
            wp_send_json($aiomatic_result);
        }

        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        curl_close($ch);

        if ($http_code != 200) {
            $response_data = json_decode($response, true);
            $error_message = isset($response_data['error']['message']) ? $response_data['error']['message'] : 'Unknown error';
            $aiomatic_result['msg'] = "HTTP request failed with code $http_code: $error_message";
            wp_send_json($aiomatic_result);
        }

        $response_data = json_decode($response, true);
        if (json_last_error() !== JSON_ERROR_NONE) {
            $aiomatic_result['msg'] = 'Failed to decode JSON response from Azure.';
            wp_send_json($aiomatic_result);
        }

        $files = isset($response_data['data']) && is_array($response_data['data']) ? $response_data['data'] : [];
        $batch_files = array_filter($files, function ($file) {
            return isset($file['purpose']) && in_array($file['purpose'], ['batch', 'batch_output']);
        });

        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $batch_files;
        wp_send_json($aiomatic_result);
    }
    else
    {
        $open_ai = new OpenAi($token);
        if(!$open_ai){
            $aiomatic_result['msg'] = 'Missing API Setting';
            wp_send_json($aiomatic_result);
        }
        if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
        {
            $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
        }
        if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
        {
            $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
        }
        $delay = '';
        if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
        {
            if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
            {
                $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                {
                    $delay = rand(trim($tempo[0]), trim($tempo[1]));
                }
            }
            else
            {
                if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                {
                    $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                }
            }
        }
        if($delay != '' && is_numeric($delay))
        {
            usleep($delay);
        }
        $result = $open_ai->listFiles();
        $result = json_decode($result);
        if(isset($result->error)){
            $aiomatic_result['msg'] = $result->error->message;
        }
        else{
            if(isset($result->data) && is_array($result->data) && count($result->data))
            {
                foreach($result->data as $ind => $rd)
                {
                    if($rd->purpose != 'batch' && $rd->purpose != 'batch_output')
                    {
                        unset($result->data[$ind]);
                    }
                }
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['data'] = $result->data;
            }
            else{
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['data'] = array();
            }
        }
        wp_send_json($aiomatic_result);
    }
    die();
}
add_action('wp_ajax_aiomatic_download_file', 'aiomatic_download_file');
function aiomatic_download_file()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with file downloading');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Claude API is not currently supported for this feature.';
        wp_send_json($aiomatic_result);
        die();
    }
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if(aiomatic_check_if_azure($aiomatic_Main_Settings))
    {
        $azure_endpoint = isset($aiomatic_Main_Settings['azure_endpoint']) && $aiomatic_Main_Settings['azure_endpoint'] ? trim($aiomatic_Main_Settings['azure_endpoint'], '/') : '';
        if (isset($aiomatic_Main_Settings['azure_api_selector_batch']) && $aiomatic_Main_Settings['azure_api_selector_batch'] != '' && $aiomatic_Main_Settings['azure_api_selector_batch'] != 'default')
        {
            $api_ver = '?api-version=' . $aiomatic_Main_Settings['azure_api_selector_batch'];
        }
        else
        {
            $api_ver = AIMOGEN_AZURE_BATCHES_API_VERSION;
        }
        if (!$azure_endpoint) {
            $aiomatic_result['msg'] = 'Azure endpoint is missing in settings.';
            wp_send_json($aiomatic_result);
        }
        if (isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
            $file_id = sanitize_text_field($_REQUEST['id']);
            $url = $azure_endpoint . "/openai/files/{$file_id}/content{$api_ver}";
            $headers = [
                'api-key: ' . $token
            ];
            $ch = curl_init();
            if ($ch === false) {
                $aiomatic_result['msg'] = 'Failed to initialize cURL.';
                wp_send_json($aiomatic_result);
            }
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $response = curl_exec($ch);
            if ($response === false) {
                $error = curl_error($ch);
                curl_close($ch);
                $aiomatic_result['msg'] = "cURL error: $error";
                wp_send_json($aiomatic_result);
            }
            $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            if ($http_code == 200) {
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['data'] = $response;
                $aiomatic_result['filename'] = "{$file_id}.txt";
            } else {
                $response_data = json_decode($response, true);
                $error_message = isset($response_data['error']['message']) ? $response_data['error']['message'] : 'Unknown error';
                $aiomatic_result['msg'] = "HTTP request failed with code $http_code: $error_message";
            }
        } else {
            $aiomatic_result['msg'] = 'File ID is required.';
        }
    }
    else
    {
        $open_ai = new OpenAi($token);
        if(isset($_REQUEST['id']) && !empty($_REQUEST['id'])) {
            $id = sanitize_text_field($_REQUEST['id']);
            if (!$open_ai) {
                $aiomatic_result['msg'] = 'Missing API Setting';
                wp_send_json($aiomatic_result);
                die();
            } else {
                if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
                {
                    $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
                }
                if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
                {
                    $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
                }
                $delay = '';
                if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
                {
                    if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
                    {
                        $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                        if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                        {
                            $delay = rand(trim($tempo[0]), trim($tempo[1]));
                        }
                    }
                    else
                    {
                        if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                        {
                            $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                        }
                    }
                }
                if($delay != '' && is_numeric($delay))
                {
                    usleep($delay);
                }
                $result = $open_ai->retrieveFileContent($id);
                $json_parse = json_decode($result);
                if(isset($json_parse->error)){
                    $aiomatic_result['msg'] = 'Error: ' . $json_parse->error->message;
                    wp_send_json($aiomatic_result);
                    die();
                }
                else
                {
                    $file_info = $open_ai->retrieveFile($id); 
                    $json_info_parse = json_decode($file_info);
                    if(!isset($json_info_parse->filename))
                    {
                        $aiomatic_result['msg'] = 'Error in file downloading: ' . print_r($json_info_parse, true);
                        wp_send_json($aiomatic_result);
                        die();
                    }
                    else
                    {
                        $filename = $json_info_parse->filename;
                        $aiomatic_result['status'] = 'success';
                        $aiomatic_result['data'] = $result;
                        $aiomatic_result['filename'] = $filename;
                        wp_send_json($aiomatic_result);
                        die();
                    }
                }
            }
        }
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_parse_output', 'aiomatic_parse_output');
function aiomatic_parse_output()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with result parsing');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Azure/Claude API is not currently supported for finetunes.';
        wp_send_json($aiomatic_result);
        die();
    }
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    if(aiomatic_check_if_azure($aiomatic_Main_Settings))
    {
        $azure_endpoint = isset($aiomatic_Main_Settings['azure_endpoint']) && $aiomatic_Main_Settings['azure_endpoint'] ? trim($aiomatic_Main_Settings['azure_endpoint'], '/') : '';
        if (isset($aiomatic_Main_Settings['azure_api_selector_batch']) && $aiomatic_Main_Settings['azure_api_selector_batch'] != '' && $aiomatic_Main_Settings['azure_api_selector_batch'] != 'default')
        {
            $api_ver = '?api-version=' . $aiomatic_Main_Settings['azure_api_selector_batch'];
        }
        else
        {
            $api_ver = AIMOGEN_AZURE_BATCHES_API_VERSION;
        }
        if (!$azure_endpoint) {
            $aiomatic_result['msg'] = 'Azure endpoint is missing in settings.';
            wp_send_json($aiomatic_result);
        }
        if (isset($_REQUEST['id'], $_REQUEST['idin'], $_REQUEST['endpoint']) && !empty($_REQUEST['id']) && !empty($_REQUEST['idin']) && !empty($_REQUEST['endpoint'])) {
            $file_id_output = sanitize_text_field($_REQUEST['id']);
            $file_id_input = sanitize_text_field($_REQUEST['idin']);
            $endpoint = sanitize_text_field($_REQUEST['endpoint']);
            $output_url = $azure_endpoint . "/openai/files/{$file_id_output}/content{$api_ver}";
            $headers = [
                'api-key: ' . $token
            ];
            $output_content = aiomatic_fetch_azure_content($output_url, $headers);
            if (is_wp_error($output_content)) {
                $aiomatic_result['msg'] = 'Error retrieving output file: ' . $output_content->get_error_message();
                wp_send_json($aiomatic_result);
                die();
            }
            $input_url = $azure_endpoint . "/openai/files/{$file_id_input}/content{$api_ver}";
            $input_content = aiomatic_fetch_azure_content($input_url, $headers);
            if (is_wp_error($input_content)) {
                $aiomatic_result['msg'] = 'Error retrieving input file: ' . $input_content->get_error_message();
                wp_send_json($aiomatic_result);
                die();
            }
            $parsed_data = array(
                'input' => $input_content,
                'output' => $output_content
            );
            if ($endpoint === '/v1/chat/completions' || $endpoint === '/chat/completions') {
                $formatted_result = aiomatic_format_parsed_data($parsed_data);
            } else {
                $formatted_result = aiomatic_format_parsed_embeddings_data($parsed_data);
            }
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['data'] = $formatted_result;
            wp_send_json($aiomatic_result);
        } else {
            $aiomatic_result['msg'] = 'Incorrect request sent.';
            wp_send_json($aiomatic_result);
        }
    }
    else
    {
        $open_ai = new OpenAi($token);
        if(isset($_REQUEST['id']) && !empty($_REQUEST['id']) && isset($_REQUEST['idin']) && !empty($_REQUEST['idin']) && isset($_REQUEST['endpoint']) && !empty($_REQUEST['endpoint'])) 
        {
            $id = sanitize_text_field($_REQUEST['id']);
            $idin = sanitize_text_field($_REQUEST['idin']);
            $endpoint = sanitize_text_field($_REQUEST['endpoint']);
            if (!$open_ai) {
                $aiomatic_result['msg'] = 'Missing API Setting';
                wp_send_json($aiomatic_result);
                die();
            } else {
                if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
                {
                    $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
                }
                if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
                {
                    $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
                }
                $delay = '';
                if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
                {
                    if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
                    {
                        $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                        if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                        {
                            $delay = rand(trim($tempo[0]), trim($tempo[1]));
                        }
                    }
                    else
                    {
                        if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                        {
                            $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                        }
                    }
                }
                if($delay != '' && is_numeric($delay))
                {
                    usleep($delay);
                }
                $result = $open_ai->retrieveFileContent($id);
                $json_parse = json_decode($result);
                if(isset($json_parse->error)){
                    $aiomatic_result['msg'] = 'Error: ' . $json_parse->error->message;
                    wp_send_json($aiomatic_result);
                    die();
                }
                else
                {
                    if($delay != '' && is_numeric($delay))
                    {
                        usleep($delay);
                    }
                    $result_in = $open_ai->retrieveFileContent($idin);
                    $json_parse_in = json_decode($result_in);
                    if(isset($json_parse_in->error)){
                        $aiomatic_result['msg'] = 'Error in input file reading: ' . $json_parse_in->error->message;
                        wp_send_json($aiomatic_result);
                        die();
                    }
                    else
                    {
                        if($endpoint === '/v1/chat/completions' || $endpoint === '/chat/completions')
                        {
                            $parsed_data = array(
                                'input' => $result_in,
                                'output' => $result
                            );
                            $formatted_result = aiomatic_format_parsed_data($parsed_data);
                            $aiomatic_result['status'] = 'success';
                            $aiomatic_result['data'] = $formatted_result;
                            wp_send_json($aiomatic_result);
                            die();
                        }
                        else
                        {
                            $parsed_data = array(
                                'input' => $result_in,
                                'output' => $result
                            );
                            $formatted_result = aiomatic_format_parsed_embeddings_data($parsed_data);
                            $aiomatic_result['status'] = 'success';
                            $aiomatic_result['data'] = $formatted_result;
                            wp_send_json($aiomatic_result);
                            die();
                        }
                    }
                }
            }
        }
        else
        {
            $aiomatic_result['msg'] = 'Incorrect request sent.';
            wp_send_json($aiomatic_result);
            die();
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_template_data', 'aiomatic_get_template_data');
function aiomatic_get_template_data()
{
    check_ajax_referer('openai-omni-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with template data');
    if(!isset($_POST['id']) || empty($_POST['id']))
    {
        $aiomatic_result['msg'] = 'Empty template id added!';
        wp_send_json($aiomatic_result);
    }
    $themeid = $_POST['id'];
    $formid = $_POST['formid'];
    $aiomatic_theme = get_post(sanitize_text_field($themeid));
    if($aiomatic_theme === null || $aiomatic_theme === 0)
    {
        $aiomatic_result['msg'] = 'Failed to get template ID: ' . print_r($themeid, true);
        wp_send_json($aiomatic_result);
    }
    else 
    {
        $json_back = get_post_meta($aiomatic_theme->ID, 'aiomatic_json', true);
        if(!empty($json_back))
        {
            $aiomatic_theme->post_content = $json_back;
        }
        if(empty($formid))
        {
            aiomatic_update_option('aiomatic_dafault_omni_template', $aiomatic_theme->ID, false);
            $aiomatic_result['msg'] = $aiomatic_theme->post_content;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $aiomatic_result['msg'] = $aiomatic_theme->post_content;
            $aiomatic_result['status'] = 'success';
            wp_send_json($aiomatic_result);
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_save_seo_template', 'aiomatic_save_seo_template');
function aiomatic_save_seo_template() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $templates = get_option('aiomatic_templates', array());
    $template_name = sanitize_text_field($_POST['template_name']);
    $prompt = sanitize_textarea_field($_POST['prompt']);
    $aiomatic_target_selector = sanitize_textarea_field($_POST['aiomatic_target_selector']);
    $seo_assistant_id = sanitize_textarea_field($_POST['seo_assistant_id']);
    $aiomatic_model_selector = sanitize_textarea_field($_POST['aiomatic_model_selector']);
    $aiomatic_vision_selector = sanitize_textarea_field($_POST['aiomatic_vision_selector']);
    $aiomatic_rename_file_checkbox = sanitize_textarea_field($_POST['aiomatic_rename_file_checkbox']);
    $templates[$template_name] = array($prompt, $aiomatic_target_selector, $seo_assistant_id, $aiomatic_model_selector, $aiomatic_vision_selector, $aiomatic_rename_file_checkbox);
    aiomatic_update_option('aiomatic_templates', $templates);
    wp_send_json_success();
}
add_action('wp_ajax_aiomatic_load_seo_templates', 'aiomatic_load_seo_templates');
function aiomatic_load_seo_templates() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $templates = get_option('aiomatic_templates', array());
    wp_send_json_success($templates);
}
add_action('wp_ajax_aiomatic_load_seo_template', 'aiomatic_load_seo_template');
function aiomatic_load_seo_template() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $templates = get_option('aiomatic_templates', array());
    $template_name = sanitize_text_field($_POST['template_name']);
    if (isset($templates[$template_name])) {
        wp_send_json_success($templates[$template_name]);
    } else {
        wp_send_json_error();
    }
}
add_action('wp_ajax_aiomatic_delete_seo_template', 'aiomatic_delete_seo_template');
function aiomatic_delete_seo_template() {
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    
    $templates = get_option('aiomatic_templates', array());
    $template_name = sanitize_text_field($_POST['template_name']);
    
    if (isset($templates[$template_name])) {
        unset($templates[$template_name]);
        aiomatic_update_option('aiomatic_templates', $templates);
        wp_send_json_success();
    } else {
        wp_send_json_error();
    }
}
add_action('wp_ajax_aiomatic_get_template_cat_data', 'aiomatic_get_template_cat_data');
function aiomatic_get_template_cat_data()
{
    check_ajax_referer('openai-omni-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with template category selection');
    if(!isset($_POST['id']))
    {
        $aiomatic_result['msg'] = 'Empty template category id added!';
        wp_send_json($aiomatic_result);
    }
    if(empty($_POST['id']))
    {
        $args = array(
            'post_type' => 'aiomatic_omni_temp',
            'posts_per_page' => -1
        );
        $return_arr = array();
        $the_query = new WP_Query($args);
        if ($the_query->have_posts())
        {
            while ($the_query->have_posts())
            {
                $the_query->the_post();
                $return_arr[get_the_ID()] = get_the_title();
            }
            wp_reset_postdata();
        }
        $aiomatic_result['msg'] = $return_arr;
        $aiomatic_result['status'] = 'success';
        wp_send_json($aiomatic_result);
        die();
    }
    $themeid = $_POST['id'];
    $paged = 1; 
    $posts_per_page = 100; 
    $return_arr = array(); 
    do {
        $args = array(
            'post_type'      => 'aiomatic_omni_temp', 
            'posts_per_page' => $posts_per_page,  
            'paged'          => $paged,       
            'tax_query'      => array(
                array(
                    'taxonomy' => 'ai_template_categories',
                    'field'    => 'slug',                  
                    'terms'    => $themeid,              
                ),
            ),
        );

        $the_query = new WP_Query($args);

        if ($the_query->have_posts()) {
            while ($the_query->have_posts()) {
                $the_query->the_post();
                $return_arr[get_the_ID()] = get_the_title();
            }
            wp_reset_postdata();
        }

        $paged++;
    } while ($the_query->have_posts());
    $aiomatic_result['msg'] = $return_arr;
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_assistant_vector_store', 'aiomatic_delete_assistant_vector_store');
add_action('wp_ajax_nopriv_aiomatic_delete_assistant_vector_store', 'aiomatic_delete_assistant_vector_store');
function aiomatic_delete_assistant_vector_store()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with finetuning deletion');
    if(isset($_POST['id']) && !empty($_POST['id'])){
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
        {
            $aiomatic_result['msg'] = 'Claude API is not currently supported for assistants.';
            wp_send_json($aiomatic_result);
        }
        $file_upload_type = '';
        if ( isset($_POST['file_upload_type']) && $_POST['file_upload_type'] != '')
        {
            $file_upload_type = $_POST['file_upload_type'];
        }
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if($file_upload_type == 'assistants')
        {
            require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
            $storeid = $_POST['storeid'];
            try
            {
                $store_del = aiomatic_openai_delete_vector_store($token, $storeid);
                if(isset($store_del->error))
                {
                    $aiomatic_result['msg'] = $result->error->message;
                    wp_send_json($aiomatic_result);
                }
            }
            catch(Exception $e)
            {
                aiomatic_log_to_file('Failed to delete vector store ID: ' . $storeid . ', exception: ' . $e->getMessage());
            }
        }
        $open_ai = new OpenAi($token);
        if(!$open_ai){
            $aiomatic_result['msg'] = 'Missing API Setting';
            wp_send_json($aiomatic_result);
        }
        if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
        {
            $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
        }
        if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
        {
            $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
        }
        $delay = '';
        if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
        {
            if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
            {
                $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                {
                    $delay = rand(trim($tempo[0]), trim($tempo[1]));
                }
            }
            else
            {
                if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                {
                    $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                }
            }
        }
        $file_ids = explode(',', $_POST['id']);
        $success_count = 0;
        $errors = array();
        foreach($file_ids as $file_id) 
        {
            $file_id = trim($file_id);
            if(empty($file_id)) continue;
            if($delay != '' && is_numeric($delay))
            {
                usleep($delay);
            }
            $result_raw = $open_ai->deleteFile($file_id);
            $result = json_decode($result_raw);
            if(isset($result->error)) {
                $errors[] = $file_id . ': ' . $result->error->message;
            } else {
                $success_count++;
            }
        }
        if($success_count > 0) {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = sprintf(esc_html__('%d files deleted.', 'aiomatic-automatic-ai-content-writer'), $success_count);
        } else if (!empty($errors)) {
            $aiomatic_result['msg'] = implode(' | ', $errors);
        }
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_delete_assistant_file', 'aiomatic_delete_assistant_file');
function aiomatic_delete_assistant_file()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with finetuning deletion');
    if(isset($_POST['id']) && !empty($_POST['id'])){
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
        {
            $aiomatic_result['msg'] = 'Claude API is not currently supported for assistant files.';
            wp_send_json($aiomatic_result);
        }
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        
        if(aiomatic_check_if_azure($aiomatic_Main_Settings))
        {
            $azure_endpoint = isset($aiomatic_Main_Settings['azure_endpoint']) && $aiomatic_Main_Settings['azure_endpoint'] ? trim($aiomatic_Main_Settings['azure_endpoint'], '/') : '';
            if (isset($aiomatic_Main_Settings['azure_api_selector_batch']) && $aiomatic_Main_Settings['azure_api_selector_batch'] != '' && $aiomatic_Main_Settings['azure_api_selector_batch'] != 'default')
            {
                $api_ver = '?api-version=' . $aiomatic_Main_Settings['azure_api_selector_batch'];
            }
            else
            {
                $api_ver = AIMOGEN_AZURE_BATCHES_API_VERSION;
            }
            if (!$azure_endpoint) {
                $aiomatic_result['msg'] = 'Azure endpoint is missing in settings.';
                wp_send_json($aiomatic_result);
            }
            $file_id = sanitize_text_field($_POST['id']);
            $url = $azure_endpoint . "/openai/files/{$file_id}{$api_ver}";
            $headers = [
                'Content-Type: application/json',
                'api-key: ' . $token
            ];
            $ch = curl_init();
            if ($ch === false) {
                $aiomatic_result['msg'] = 'Failed to initialize cURL.';
                wp_send_json($aiomatic_result);
            }
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'DELETE');
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            $response = curl_exec($ch);
            if ($response === false) {
                $error = curl_error($ch);
                curl_close($ch);
                $aiomatic_result['msg'] = "cURL error: $error";
                wp_send_json($aiomatic_result);
            }
            $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);
            if ($http_code == 204 || $http_code == 200) {
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['msg'] = 'File deleted successfully.';
            } else {
                $response_data = json_decode($response, true);
                $error_message = isset($response_data['error']['message']) ? $response_data['error']['message'] : 'Unknown error';
                $aiomatic_result['msg'] = "HTTP request failed with code $http_code: $error_message";
            }
        }
        else
        {
            $open_ai = new OpenAi($token);
            if(!$open_ai){
                $aiomatic_result['msg'] = 'Missing API Setting';
                wp_send_json($aiomatic_result);
            }
            if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
            {
                $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
            }
            if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
            {
                $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
            }
            $delay = '';
            if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
            {
                if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
                {
                    $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                    if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                    {
                        $delay = rand(trim($tempo[0]), trim($tempo[1]));
                    }
                }
                else
                {
                    if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                    {
                        $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                    }
                }
            }
            if($delay != '' && is_numeric($delay))
            {
                usleep($delay);
            }
            $result = $open_ai->deleteFile($_POST['id']);
            $result = json_decode($result);
            if(isset($result->error))
            {
                $aiomatic_result['msg'] = $result->error->message;
            }
            else
            {
                $aiomatic_result['status'] = 'success';
            }
        }
    }
    wp_send_json($aiomatic_result);
}
add_action('wp_ajax_aiomatic_assistant_file_upload', 'aiomatic_assistant_file_upload');
function aiomatic_assistant_file_upload()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with file uploading');
    if(isset($_FILES['file']) && empty($_FILES['file']['error'])){
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_if_azure($aiomatic_Main_Settings) && aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
        {
            $aiomatic_result['msg'] = 'Claude API is not currently supported for assistants.';
            wp_send_json($aiomatic_result);
        }
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        $open_ai = new OpenAi($token);
        if(!$open_ai){
            $aiomatic_result['msg'] = 'Missing API Setting';
            wp_send_json($aiomatic_result);
        }
        if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
        {
            $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
        }
        if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
        {
            $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
        }
        $file_name = sanitize_file_name( basename( $_FILES['file']['name'] ) );
        $tmp_file  = $_FILES['file']['tmp_name'];

        $real_type = wp_check_filetype( $file_name );
        $mime      = empty( $real_type['type'] ) ? 'application/octet-stream' : $real_type['type'];

        $c_file = curl_file_create( $tmp_file, $mime, $file_name );
        $delay = '';
        if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
        {
            if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
            {
                $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                {
                    $delay = rand(trim($tempo[0]), trim($tempo[1]));
                }
            }
            else
            {
                if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                {
                    $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                }
            }
        }
        if($delay != '' && is_numeric($delay))
        {
            usleep($delay);
        }
        $purpose = 'assistants';
        $result = $open_ai->uploadFile(array(
            'purpose' => $purpose,
            'file' => $c_file,
        ));
        $result = json_decode($result);
        if(isset($result->error)){
            $aiomatic_result['msg'] = $result->error->message;
        }
        else
        {
            $aiomatic_result['msg'] = $result->id;
            $aiomatic_result['status'] = 'success';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_batch_file_upload', 'aiomatic_batch_file_upload');
function aiomatic_batch_file_upload()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with file uploading');
    if(isset($_FILES['file']) && empty($_FILES['file']['error'])){
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(aiomatic_check_if_azure($aiomatic_Main_Settings))
        {
            $azure_endpoint = isset($aiomatic_Main_Settings['azure_endpoint']) && $aiomatic_Main_Settings['azure_endpoint'] ? trim($aiomatic_Main_Settings['azure_endpoint'], '/') : '';
            if (isset($aiomatic_Main_Settings['azure_api_selector_batch']) && $aiomatic_Main_Settings['azure_api_selector_batch'] != '' && $aiomatic_Main_Settings['azure_api_selector_batch'] != 'default')
            {
                $api_ver = '?api-version=' . $aiomatic_Main_Settings['azure_api_selector_batch'];
            }
            else
            {
                $api_ver = AIMOGEN_AZURE_BATCHES_API_VERSION;
            }

            if (!$azure_endpoint) {
                $aiomatic_result['msg'] = 'Azure endpoint is missing in settings.';
                wp_send_json($aiomatic_result);
            }

            $file_name = sanitize_file_name(basename($_FILES['file']['name']));
            $tmp_file = $_FILES['file']['tmp_name'];

            $url = $azure_endpoint . '/openai/files' . $api_ver;

            $headers = [
                'api-key: ' . $token
            ];

            $fields = [
                'purpose' => 'batch',
                'file' => new CURLFile($tmp_file, $_FILES['file']['type'], $file_name),
            ];

            $ch = curl_init();
            if ($ch === false) {
                $aiomatic_result['msg'] = 'Failed to initialize cURL.';
                wp_send_json($aiomatic_result);
            }

            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

            $response = curl_exec($ch);

            if ($response === false) {
                $error = curl_error($ch);
                curl_close($ch);
                $aiomatic_result['msg'] = "cURL error: $error";
                wp_send_json($aiomatic_result);
            }

            $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
            curl_close($ch);

            if ($http_code != 200 && $http_code != 201) {
                $response_data = json_decode($response, true);
                $error_message = isset($response_data['error']['message']) ? $response_data['error']['message'] : 'Unknown error';
                $aiomatic_result['msg'] = "HTTP request failed with code $http_code: $error_message";
                wp_send_json($aiomatic_result);
            }

            $response_data = json_decode($response, true);
            if (json_last_error() !== JSON_ERROR_NONE) {
                $aiomatic_result['msg'] = 'Failed to decode JSON response from Azure.';
                wp_send_json($aiomatic_result);
            }

            $aiomatic_result['status'] = 'success';
            $aiomatic_result['data'] = $response_data;
        }
        else
        {
            $open_ai = new OpenAi($token);
            if(!$open_ai){
                $aiomatic_result['msg'] = 'Missing API Setting';
                wp_send_json($aiomatic_result);
            }
            if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
            {
                $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
            }
            if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
            {
                $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
            }
            $file_name = sanitize_file_name( basename( $_FILES['file']['name'] ) );
            $tmp_file  = $_FILES['file']['tmp_name'];

            $real = wp_check_filetype( $file_name );
            $mime = $real['type'] ?: 'application/octet-stream';

            $c_file = curl_file_create( $tmp_file, $mime, $file_name );
            $delay = '';
            if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
            {
                if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
                {
                    $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
                    if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
                    {
                        $delay = rand(trim($tempo[0]), trim($tempo[1]));
                    }
                }
                else
                {
                    if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
                    {
                        $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
                    }
                }
            }
            if($delay != '' && is_numeric($delay))
            {
                usleep($delay);
            }
            $purpose = 'batch';
            $result = $open_ai->uploadFile(array(
                'purpose' => $purpose,
                'file' => $c_file,
            ));
            $result = json_decode($result);
            if(isset($result->error)){
                $aiomatic_result['msg'] = $result->error->message;
            }
            else
            {
                $aiomatic_result['msg'] = $result->id;
                $aiomatic_result['status'] = 'success';
            }
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_assistants', 'aiomatic_assistants');
function aiomatic_assistants()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Assistants');
    check_ajax_referer('aiomatic_assistants', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $token = '';
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['aiomatic-assistant-title']) || empty($_POST['aiomatic-assistant-title']))
    {
        $aiomatic_result['msg'] = 'Empty assistant title added!';
        wp_send_json($aiomatic_result);
    }
    $title = stripslashes($_POST['aiomatic-assistant-title']);
    if(!isset($_POST['aiomatic-assistant-prompt']) || empty($_POST['aiomatic-assistant-prompt']))
    {
        $prompt = '';
    }
    else
    {
        $prompt = stripslashes($_POST['aiomatic-assistant-prompt']);
    }
    if(!isset($_POST['aiomatic-assistant-model']) || empty($_POST['aiomatic-assistant-model']))
    {
        $aiomatic_result['msg'] = 'Empty assistant model added!';
        wp_send_json($aiomatic_result);
    }
    $code_interpreter = false;
    if(isset($_POST['aiomatic-assistant-code-interpreter']) && $_POST['aiomatic-assistant-code-interpreter'] == 'on')
    {
        $code_interpreter = true;
    }
    $file_search = false;
    if(isset($_POST['aiomatic-assistant-file_search']) && $_POST['aiomatic-assistant-file_search'] == 'on')
    {
        $file_search = true;
    }
    $model = stripslashes($_POST['aiomatic-assistant-model']);
    $description = '';
    if(isset($_POST['aiomatic-assistant-description']) && !empty($_POST['aiomatic-assistant-description']))
    {
        $description = stripslashes($_POST['aiomatic-assistant-description']);
    }
    $topp = '';
    if(isset($_POST['aiomatic-assistant-topp']) && !empty($_POST['aiomatic-assistant-topp']))
    {
        $topp = stripslashes($_POST['aiomatic-assistant-topp']);
    }
    $temperature = '';
    if(isset($_POST['aiomatic-assistant-temperature']) && !empty($_POST['aiomatic-assistant-temperature']))
    {
        $temperature = stripslashes($_POST['aiomatic-assistant-temperature']);
    }
    $assistant_first_message = '';
    if(isset($_POST['aiomatic-assistant-first-message']) && !empty($_POST['aiomatic-assistant-first-message']))
    {
        $assistant_first_message = stripslashes($_POST['aiomatic-assistant-first-message']);
    }
    $avatar = '';
    if(isset($_POST['aiomatic-assistant-avatar']))
    {
        $avatar = stripslashes($_POST['aiomatic-assistant-avatar']);
    }
    $functions = '';
    if(isset($_POST['aiomatic-assistant-functions']) && !empty($_POST['aiomatic-assistant-functions']))
    {
        $functions = stripslashes($_POST['aiomatic-assistant-functions']);
    }
    $assistant_files = [];
    if(isset($_POST['aiomatic-assistant-files']) && !empty($_POST['aiomatic-assistant-files']))
    {
        $assistant_files = $_POST['aiomatic-assistant-files'];
        if(is_array($assistant_files) && $code_interpreter === true && count($assistant_files) > 20)
        {
            $assistant_files = array_slice($assistant_files, 0, 20);
        }
    }
    $aiomatic_result = aiomatic_save_assistant($token, $title, $model, $prompt, $description, $temperature, $topp, $assistant_first_message, $avatar, $code_interpreter, $file_search, $assistant_files, $functions);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_batches', 'aiomatic_batches');
function aiomatic_batches()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Batch Requests');
    check_ajax_referer('aiomatic_batches', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $token = '';
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['aiomatic-batch-file']) || empty($_POST['aiomatic-batch-file']))
    {
        $aiomatic_result['msg'] = 'Empty AI Batch Request File added!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_batch_file = $_POST['aiomatic-batch-file'];
    if(!isset($_POST['aiomatic-completion-window']) || empty($_POST['aiomatic-completion-window']))
    {
        $aiomatic_result['msg'] = 'Empty AI Completion Window added!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_completion_window = $_POST['aiomatic-completion-window'];
    if(!isset($_POST['aiomatic-endpoint']) || empty($_POST['aiomatic-endpoint']))
    {
        $aiomatic_result['msg'] = 'Empty AI Endpoint added!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_endpoint = $_POST['aiomatic-endpoint'];
    $aiomatic_result = aiomatic_save_batch($token, $aiomatic_batch_file, $aiomatic_completion_window, $aiomatic_endpoint);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_assistants_edit', 'aiomatic_assistants_edit');
function aiomatic_assistants_edit()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Assistants editing');
    check_ajax_referer('aiomatic_assistants', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $token = '';
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['aiomatic-assistant-title']) || empty($_POST['aiomatic-assistant-title']))
    {
        $aiomatic_result['msg'] = 'Empty assistant title added!';
        wp_send_json($aiomatic_result);
    }
    $title = stripslashes($_POST['aiomatic-assistant-title']);
    if(!isset($_POST['assistant_id']) || empty($_POST['assistant_id']))
    {
        $aiomatic_result['msg'] = 'Incorrect request provided!';
        wp_send_json($aiomatic_result);
    }
    $assistant_id_local = stripslashes($_POST['assistant_id']);
    $assistant_id = get_post_meta($assistant_id_local, '_assistant_id', true);
    if(empty($assistant_id))
    {
        $aiomatic_result['msg'] = 'Assistant ID was not found in the database!';
        wp_send_json($aiomatic_result);
    }
    if(!isset($_POST['aiomatic-assistant-prompt']) || empty($_POST['aiomatic-assistant-prompt']))
    {
        $prompt = '';
    }
    else
    {
        $prompt = stripslashes($_POST['aiomatic-assistant-prompt']);
    }
    if(!isset($_POST['aiomatic-assistant-model']) || empty($_POST['aiomatic-assistant-model']))
    {
        $aiomatic_result['msg'] = 'Empty assistant model added!';
        wp_send_json($aiomatic_result);
    }
    $code_interpreter = false;
    if(isset($_POST['aiomatic-assistant-code-interpreter']) && $_POST['aiomatic-assistant-code-interpreter'] == 'on')
    {
        $code_interpreter = true;
    }
    $file_search = false;
    if(isset($_POST['aiomatic-assistant-file_search']) && $_POST['aiomatic-assistant-file_search'] == 'on')
    {
        $file_search = true;
    }
    $model = stripslashes($_POST['aiomatic-assistant-model']);
    $description = '';
    if(isset($_POST['aiomatic-assistant-description']) && !empty($_POST['aiomatic-assistant-description']))
    {
        $description = stripslashes($_POST['aiomatic-assistant-description']);
    }
    $topp = '';
    if(isset($_POST['aiomatic-assistant-topp']) && !empty($_POST['aiomatic-assistant-topp']))
    {
        $topp = stripslashes($_POST['aiomatic-assistant-topp']);
    }
    $temperature = '';
    if(isset($_POST['aiomatic-assistant-temperature']) && !empty($_POST['aiomatic-assistant-temperature']))
    {
        $temperature = stripslashes($_POST['aiomatic-assistant-temperature']);
    }
    $assistant_first_message = '';
    if(isset($_POST['aiomatic-assistant-first-message']) && !empty($_POST['aiomatic-assistant-first-message']))
    {
        $assistant_first_message = stripslashes($_POST['aiomatic-assistant-first-message']);
    }
    $avatar = '';
    if(isset($_POST['aiomatic-assistant-avatar']))
    {
        $avatar = stripslashes($_POST['aiomatic-assistant-avatar']);
    }
    $functions = '';
    if(isset($_POST['aiomatic-assistant-functions']) && !empty($_POST['aiomatic-assistant-functions']))
    {
        $functions = stripslashes($_POST['aiomatic-assistant-functions']);
    }
    $assistant_files = [];
    if(isset($_POST['aiomatic-assistant-files']) && !empty($_POST['aiomatic-assistant-files']))
    {
        $assistant_files = $_POST['aiomatic-assistant-files'];
        if(is_array($assistant_files) && $code_interpreter === true && count($assistant_files) > 20)
        {
            $assistant_files = array_slice($assistant_files, 0, 20);
        }
    }
    $aiomatic_result = aiomatic_update_assistant($token, $assistant_id, $assistant_id_local, $title, $model, $prompt, $description, $temperature, $topp, $assistant_first_message, $avatar, $code_interpreter, $file_search, $assistant_files, $functions);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_forms', 'aiomatic_forms');
function aiomatic_forms()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI forms');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    check_ajax_referer('aiomatic_forms', 'nonce');
    $formid = '';
    if(isset($_POST['formid']) && !empty($_POST['formid']))
    {
        $formid = $_POST['formid'];
    }
    if(!isset($_POST['aiomatic-form-title']) || empty($_POST['aiomatic-form-title']))
    {
        $aiomatic_result['msg'] = 'Empty form title added!';
        wp_send_json($aiomatic_result);
    }
    $title = $_POST['aiomatic-form-title'];
    if(!isset($_POST['aiomatic-form-prompt']) || empty($_POST['aiomatic-form-prompt']))
    {
        $aiomatic_result['msg'] = 'Empty form prompt added!';
        wp_send_json($aiomatic_result);
    }
    $prompt = $_POST['aiomatic-form-prompt'];
    if(!isset($_POST['aiomatic-form-model']) || empty($_POST['aiomatic-form-model']))
    {
        $aiomatic_result['msg'] = 'Empty form model added!';
        wp_send_json($aiomatic_result);
    }
    $model = $_POST['aiomatic-form-model'];
    if(!isset($_POST['aiomatic-form-stream']) || (empty($_POST['aiomatic-form-stream']) && $_POST['aiomatic-form-stream'] !== '0'))
    {
        $aiomatic_result['msg'] = 'Empty form stream added!';
        wp_send_json($aiomatic_result);
    }
    $streaming_enabled = $_POST['aiomatic-form-stream'];
    if(isset($_POST['aiomatic-form-assistant-id']))
    {
        $assistant_id = $_POST['aiomatic-form-assistant-id'];
    }
    else
    {
        $assistant_id = '';
    }
    if(isset($_POST['aiomatic-html']))
    {
        $html = $_POST['aiomatic-html'];
    }
    else
    {
        $html = '0';
    }
    if(!isset($_POST['aiomatic-header']) || empty($_POST['aiomatic-header']))
    {
        $aiomatic_result['msg'] = 'Empty form header state added!';
        wp_send_json($aiomatic_result);
    }
    $header = $_POST['aiomatic-header'];
    if(!isset($_POST['aiomatic-editor']) || empty($_POST['aiomatic-editor']))
    {
        $_POST['aiomatic-editor'] = 'textarea';
    }
    $editor = $_POST['aiomatic-editor'];
    if(!isset($_POST['aiomatic-form-wait-omniblock']) || empty($_POST['aiomatic-form-wait-omniblock']))
    {
        $_POST['aiomatic-form-wait-omniblock'] = 'no';
    }
    $wait = $_POST['aiomatic-form-wait-omniblock'];
    if(!isset($_POST['aiomatic-trigger-id']) || empty($_POST['aiomatic-trigger-id']))
    {
        $_POST['aiomatic-trigger-id'] = 'no';
    }
    $trigger_id = $_POST['aiomatic-trigger-id'];
    if(!isset($_POST['aiomatic-trigger-api-key']) || empty($_POST['aiomatic-trigger-api-key']))
    {
        $_POST['aiomatic-trigger-api-key'] = 'no';
    }
    $trigger_api_key = $_POST['aiomatic-trigger-api-key'];
    if(!isset($_POST['aiomatic-advanced']) || empty($_POST['aiomatic-advanced']))
    {
        $_POST['aiomatic-advanced'] = 'hide';
    }
    $advanced = $_POST['aiomatic-advanced'];
    if(!isset($_POST['aiomatic-submit']) || empty($_POST['aiomatic-submit']))
    {
        $aiomatic_result['msg'] = 'Empty form submit text added!';
        wp_send_json($aiomatic_result);
    }
    $submit = $_POST['aiomatic-submit'];
    $description = '';
    if(isset($_POST['aiomatic-form-description']) && !empty($_POST['aiomatic-form-description']))
    {
        $description = $_POST['aiomatic-form-description'];
    }
    $response = '';
    if(isset($_POST['aiomatic-form-response']) && !empty($_POST['aiomatic-form-response']))
    {
        $response = $_POST['aiomatic-form-response'];
    }
    $max = '';
    if(isset($_POST['aiomatic-max']) && !empty($_POST['aiomatic-max']))
    {
        $max = $_POST['aiomatic-max'];
    }
    $temperature = '';
    if(isset($_POST['aiomatic-temperature']) && !empty($_POST['aiomatic-temperature']))
    {
        $temperature = $_POST['aiomatic-temperature'];
    }
    $topp = '';
    if(isset($_POST['aiomatic-topp']) && !empty($_POST['aiomatic-topp']))
    {
        $topp = $_POST['aiomatic-topp'];
    }
    $presence = '';
    if(isset($_POST['aiomatic-presence']) && !empty($_POST['aiomatic-presence']))
    {
        $presence = $_POST['aiomatic-presence'];
    }
    $frequency = '';
    if(isset($_POST['aiomatic-frequency']) && !empty($_POST['aiomatic-frequency']))
    {
        $frequency = $_POST['aiomatic-frequency'];
    }
    $type = '';
    if(isset($_POST['aiomatic-type']) && !empty($_POST['aiomatic-type']))
    {
        $type = $_POST['aiomatic-type'];
    }
    $aiomaticfields = array();
    if(isset($_POST['aiomaticfields']) && !empty($_POST['aiomaticfields']))
    {
        $aiomaticfields = $_POST['aiomaticfields'];
    }
    $aiomatic_result = aiomatic_save_forms($formid, $title, $prompt, $model, $header, $submit, $description, $response, $max, $temperature, $topp, $presence, $frequency, $type, $aiomaticfields, $assistant_id, $streaming_enabled, $editor, $advanced, $html, $wait, $trigger_api_key, $trigger_id);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_deleteall_forms', 'aiomatic_deleteall_forms');
function aiomatic_deleteall_forms()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with general form deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $allposts = get_posts( array('post_type'=>'aiomatic_forms','numberposts'=>-1) );
    foreach ($allposts as $eachpost) {
        wp_delete_post( $eachpost->ID, true );
    }
    $aiomatic_result['msg'] = 'Successfully deleted all forms!';
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_deleteall_personas', 'aiomatic_deleteall_personas');
function aiomatic_deleteall_personas()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with general persona deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $allposts = get_posts( array('post_type'=>'aiomatic_personas','numberposts'=>-1) );
    foreach ($allposts as $eachpost) {
        wp_delete_post( $eachpost->ID, true );
    }
    $aiomatic_result['msg'] = 'Successfully deleted all personas!';
    $aiomatic_result['status'] = 'success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_save_theme', 'aiomatic_save_theme');
function aiomatic_save_theme()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with theme saving');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['post_title']) || empty($_POST['post_title']))
    {
        $aiomatic_result['msg'] = 'Empty form post_title added!';
        wp_send_json($aiomatic_result);
    }
    $post_title = $_POST['post_title'];
    if(!isset($_POST['general_background']) || empty($_POST['general_background']))
    {
        $aiomatic_result['msg'] = 'Empty form general_background added!';
        wp_send_json($aiomatic_result);
    }
    $general_background = $_POST['general_background'];
    if(!isset($_POST['background']) || empty($_POST['background']))
    {
        $aiomatic_result['msg'] = 'Empty form background added!';
        wp_send_json($aiomatic_result);
    }
    $background = $_POST['background'];
    if(!isset($_POST['input_border_color']) || empty($_POST['input_border_color']))
    {
        $aiomatic_result['msg'] = 'Empty form input_border_color added!';
        wp_send_json($aiomatic_result);
    }
    $input_border_color = $_POST['input_border_color'];
    if(!isset($_POST['input_text_color']) || empty($_POST['input_text_color']))
    {
        $aiomatic_result['msg'] = 'Empty form input_text_color added!';
        wp_send_json($aiomatic_result);
    }
    $input_text_color = $_POST['input_text_color'];
    if(!isset($_POST['persona_name_color']) || empty($_POST['persona_name_color']))
    {
        $aiomatic_result['msg'] = 'Empty form persona_name_color added!';
        wp_send_json($aiomatic_result);
    }
    $persona_name_color = $_POST['persona_name_color'];
    if(!isset($_POST['persona_role_color']) || empty($_POST['persona_role_color']))
    {
        $aiomatic_result['msg'] = 'Empty form persona_role_color added!';
        wp_send_json($aiomatic_result);
    }
    $persona_role_color = $_POST['persona_role_color'];
    if(!isset($_POST['input_placeholder_color']) || empty($_POST['input_placeholder_color']))
    {
        $aiomatic_result['msg'] = 'Empty form input_placeholder_color added!';
        wp_send_json($aiomatic_result);
    }
    $input_placeholder_color = $_POST['input_placeholder_color'];
    if(!isset($_POST['user_background_color']) || empty($_POST['user_background_color']))
    {
        $aiomatic_result['msg'] = 'Empty form user_background_color added!';
        wp_send_json($aiomatic_result);
    }
    $user_background_color = $_POST['user_background_color'];
    if(!isset($_POST['ai_background_color']) || empty($_POST['ai_background_color']))
    {
        $aiomatic_result['msg'] = 'Empty form ai_background_color added!';
        wp_send_json($aiomatic_result);
    }
    $ai_background_color = $_POST['ai_background_color'];
    if(!isset($_POST['ai_font_color']) || empty($_POST['ai_font_color']))
    {
        $aiomatic_result['msg'] = 'Empty form ai_font_color added!';
        wp_send_json($aiomatic_result);
    }
    $ai_font_color = $_POST['ai_font_color'];
    if(!isset($_POST['user_font_color']) || empty($_POST['user_font_color']))
    {
        $aiomatic_result['msg'] = 'Empty form user_font_color added!';
        wp_send_json($aiomatic_result);
    }
    $user_font_color = $_POST['user_font_color'];
    if(!isset($_POST['submit_color']) || empty($_POST['submit_color']))
    {
        $aiomatic_result['msg'] = 'Empty form submit_color added!';
        wp_send_json($aiomatic_result);
    }
    $submit_color = $_POST['submit_color'];
    if(!isset($_POST['voice_color']) || empty($_POST['voice_color']))
    {
        $aiomatic_result['msg'] = 'Empty form voice_color added!';
        wp_send_json($aiomatic_result);
    }
    $voice_color = $_POST['voice_color'];
    if(!isset($_POST['voice_color_activated']) || empty($_POST['voice_color_activated']))
    {
        $aiomatic_result['msg'] = 'Empty form voice_color_activated added!';
        wp_send_json($aiomatic_result);
    }
    $voice_color_activated = $_POST['voice_color_activated'];
    if(!isset($_POST['submit_text_color']) || empty($_POST['submit_text_color']))
    {
        $aiomatic_result['msg'] = 'Empty form submit_text_color added!';
        wp_send_json($aiomatic_result);
    }
    $submit_text_color = $_POST['submit_text_color'];
    $encode_arr = array(
        'general_background' => $general_background,
        'background' => $background,
        'input_border_color' => $input_border_color,
        'input_text_color' => $input_text_color,
        'persona_name_color' => $persona_name_color,
        'persona_role_color' => $persona_role_color,
        'input_placeholder_color' => $input_placeholder_color,
        'user_background_color' => $user_background_color,
        'ai_background_color' => $ai_background_color,
        'ai_font_color' => $ai_font_color,
        'user_font_color' => $user_font_color,
        'submit_color' => $submit_color,
        'voice_color' => $voice_color,
        'voice_color_activated' => $voice_color_activated,
        'submit_text_color' => $submit_text_color,
    );
    $json_save = json_encode($encode_arr);
    $themes_data = array(
        'post_type' => 'aiomatic_themes',
        'post_title' => $post_title,
        'post_content' => $json_save,
        'post_status' => 'publish'
    );
    remove_filter('content_save_pre', 'wp_filter_post_kses');
    remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
    $themes_id = wp_insert_post($themes_data);
    add_filter('content_save_pre', 'wp_filter_post_kses');
    add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
    if(is_wp_error($themes_id))
    {
        $aiomatic_result['msg'] = 'Failed to import form: ' . $themes_id->get_error_message();
        wp_send_json($aiomatic_result);
    }
    elseif($themes_id === 0)
    {
        $aiomatic_result['msg'] = 'Failed to insert form to database: ' . print_r($themes_data, true);
        wp_send_json($aiomatic_result);
    }
    else 
    {
        $aiomatic_result['msg'] = 'Successfully deleted all personas!';
        $aiomatic_result['status'] = 'success';
        wp_send_json($aiomatic_result);
    }
    die();
}
add_action('wp_ajax_aiomatic_get_theme', 'aiomatic_get_theme');
function aiomatic_get_theme()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with theme getting');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['themeid']) || empty($_POST['themeid']))
    {
        $aiomatic_result['msg'] = 'Empty form themeid added!';
        wp_send_json($aiomatic_result);
    }
    $themeid = $_POST['themeid'];
    $aiomatic_theme = get_post(sanitize_text_field($themeid));
    if($aiomatic_theme === null || $aiomatic_theme === 0)
    {
        $aiomatic_result['msg'] = 'Failed to get theme ID: ' . print_r($themeid, true);
        wp_send_json($aiomatic_result);
    }
    else 
    {
        $aiomatic_result['msg'] = $aiomatic_theme->post_content;
        $aiomatic_result['status'] = 'success';
        wp_send_json($aiomatic_result);
    }
    die();
}
add_action('wp_ajax_aiomatic_delete_theme', 'aiomatic_delete_theme');
function aiomatic_delete_theme()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with theme deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['themeid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        wp_delete_post($_POST['themeid']);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'Theme deleted successfully';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_form', 'aiomatic_delete_selected_form');
function aiomatic_delete_selected_form()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $deleted = false;
        if(count($_POST['ids'])) 
        {
            foreach ($_POST['ids'] as $id)
            {
                wp_delete_post($id);
                $deleted = true;
            }
        }
        if($deleted === true)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'Forms deleted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_personas', 'aiomatic_delete_selected_personas');
function aiomatic_delete_selected_personas()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with persona deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $deleted = false;
        if(count($_POST['ids'])) 
        {
            foreach ($_POST['ids'] as $id)
            {
                wp_delete_post($id);
                $deleted = true;
            }
        }
        if($deleted === true)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'Personas deleted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_assistants', 'aiomatic_delete_selected_assistants');
function aiomatic_delete_selected_assistants()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with assistant deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $token = '';
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        }
        else
        {
            $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
            wp_send_json($aiomatic_result);
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        $deleted = false;
        $errors = '';
        if(count($_POST['ids'])) 
        {
            foreach ($_POST['ids'] as $id)
            {
                $assistant_id = get_post_meta($id, '_assistant_id', true);
                if(!empty($assistant_id))
                {
                    try
                    {
                        aiomatic_openai_delete_assistant($token, $assistant_id);
                    }
                    catch(Exception $e)
                    {
                        $errors .= 'Failed to delete assistant ID: ' . $assistant_id . ', exception: ' . $e->getMessage() . '\n';
                    }
                }
                $vector_store_id = get_post_meta($id, '_assistant_vector_store_id', true);
                if(!empty($vector_store_id))
                {
                    try
                    {
                        aiomatic_openai_delete_vector_store($token, $vector_store_id);
                    }
                    catch(Exception $e)
                    {
                        aiomatic_log_to_file('Failed to delete vector store ID: ' . $vector_store_id . ', exception: ' . $e->getMessage());
                    }
                }
                wp_delete_post($id);
                $deleted = true;
            }
        }
        if(!empty($errors))
        {
            $aiomatic_result['msg'] = 'Assistant failed to be deleted: ' . $errors;
            wp_send_json($aiomatic_result);
        }
        if($deleted === true)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'Assistant deleted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_templates', 'aiomatic_delete_selected_templates');
function aiomatic_delete_selected_templates()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock template deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $deleted = false;
        if(count($_POST['ids'])) 
        {
            foreach ($_POST['ids'] as $id)
            {
                wp_delete_post($id);
                $deleted = true;
            }
        }
        if($deleted === true)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'OmniBlock templates deleted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_all_templates', 'aiomatic_delete_all_templates');
function aiomatic_delete_all_templates()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock template deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $deleted = false;
    $allposts = get_posts(array('post_type'=>'aiomatic_omni_temp','numberposts'=>-1));
    foreach ($allposts as $eachpost) 
    {
        wp_delete_post($eachpost->ID, true);
        $deleted = true;
    }
    if($deleted === true)
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'OmniBlock templates deleted successfully';
    }
    else
    {
        $aiomatic_result['msg'] = 'No OmniBlock templates found to be deleted';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_files', 'aiomatic_delete_selected_files');
function aiomatic_delete_selected_files()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock file deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $deleted = false;
        if(count($_POST['ids'])) 
        {
            foreach ($_POST['ids'] as $id)
            {
                $this_post = get_post($id);
                if($this_post === null)
                {
                    $aiomatic_result['msg'] = 'Incorrect post_id sent';
                    wp_send_json($aiomatic_result);
                }
                $local_id = get_post_meta($id, 'local_id', true);
                if(empty($local_id))
                {
                    $aiomatic_result['msg'] = 'Local file path not found';
                    wp_send_json($aiomatic_result);
                }
                $file_type = '';
                $terms = wp_get_object_terms( $id, 'ai_file_type' );
                if(!is_wp_error($terms))
                {
                    foreach($terms as  $tm)
                    {
                        $file_type = $tm->slug;
                        break;
                    }
                }
                if($file_type == 'local')
                {
                    global $wp_filesystem;
                    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
                        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
                        wp_filesystem($creds);
                    }
                    $wp_filesystem->delete($local_id);
                }
                elseif($file_type == 'amazon')
                {
                    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                    if(!function_exists('is_plugin_active'))
                    {
                        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    }
                    if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
                    {
                        $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['bucket_name']) || trim($aiomatic_Main_Settings['bucket_name']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Amazon S3 bucket_name for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['s3_user']) || trim($aiomatic_Main_Settings['s3_user']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Amazon S3 s3_user for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['s3_pass']) || trim($aiomatic_Main_Settings['s3_pass']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Amazon S3 s3_pass for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['bucket_region']) || trim($aiomatic_Main_Settings['bucket_region']) == '')
                    {
                        $aiomatic_Main_Settings['bucket_region'] = 'eu-central-1';
                    }
                    try
                    {
                        $credentials = array('key' => trim($aiomatic_Main_Settings['s3_user']), 'secret' => trim($aiomatic_Main_Settings['s3_pass']));
                        $s3 = new S3Client([
                            'version' => 'latest',
                            'region'  => trim($aiomatic_Main_Settings['bucket_region']),
                            'credentials' => $credentials
                        ]);
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to initialize Amazon S3 API: ' . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                    try 
                    {
                        $obj_arr = [
                            'Bucket' => trim($aiomatic_Main_Settings['bucket_name']),
                            'Key'    => $local_id,
                        ];
                        $awsret = $s3->deleteObject($obj_arr);
                        if(!isset($awsret['DeleteMarker']))
                        {
                            $aiomatic_result['msg'] = "Failed to decode Amazon S3 API response: " . print_r($awsret, true);
                            wp_send_json($aiomatic_result);
                        }
                    } 
                    catch (Exception $e) 
                    {
                        $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    } 
                }
                elseif($file_type == 'wasabi')
                {
                    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                    if(!function_exists('is_plugin_active'))
                    {
                        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    }
                    if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
                    {
                        $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['wasabi_bucket']) || trim($aiomatic_Main_Settings['wasabi_bucket']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_bucket for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['wasabi_user']) || trim($aiomatic_Main_Settings['wasabi_user']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_user for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['wasabi_pass']) || trim($aiomatic_Main_Settings['wasabi_pass']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_pass for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['wasabi_region']) || trim($aiomatic_Main_Settings['wasabi_region']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_region for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    try
                    {
                        $credentials = array('key' => trim($aiomatic_Main_Settings['wasabi_user']), 'secret' => trim($aiomatic_Main_Settings['wasabi_pass']));
                        $s3 = new S3Client([
                            'endpoint' => "https://" . trim($aiomatic_Main_Settings['wasabi_bucket']) . ".s3." . trim($aiomatic_Main_Settings['wasabi_region']) . ".wasabisys.com/",
                            'bucket_endpoint' => true,
                            'version' => 'latest',
                            'region'  => trim($aiomatic_Main_Settings['wasabi_region']),
                            'credentials' => $credentials
                        ]);
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to initialize Wasabi API: ' . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                    try 
                    {
                        $obj_arr = [
                            'Bucket' => trim($aiomatic_Main_Settings['wasabi_bucket']),
                            'Key'    => $local_id,
                        ];
                        $awsret = $s3->deleteObject($obj_arr);
                        if(!isset($awsret['DeleteMarker']))
                        {
                            $aiomatic_result['msg'] = "Failed to decode Wasabi API response: " . print_r($awsret, true);
                            wp_send_json($aiomatic_result);
                        }
                    } 
                    catch (Exception $e) 
                    {
                        $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Wasabi: " . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    } 
                }
                elseif($file_type == 'generics3')
                {
                    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                    if(!function_exists('is_plugin_active'))
                    {
                        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    }
                    if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
                    {
                        $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['generic_url']) || trim($aiomatic_Main_Settings['generic_url']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage URL for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['generic_bucket']) || trim($aiomatic_Main_Settings['generic_bucket']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage bucket for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['generic_user']) || trim($aiomatic_Main_Settings['generic_user']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage user for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['generic_pass']) || trim($aiomatic_Main_Settings['generic_pass']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage password for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['generic_region']) || trim($aiomatic_Main_Settings['generic_region']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage region for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    try
                    {
                        $credentials = array('key' => trim($aiomatic_Main_Settings['generic_user']), 'secret' => trim($aiomatic_Main_Settings['generic_pass']));
                        $s3 = new S3Client([
                            'endpoint' => trim($aiomatic_Main_Settings['generic_url']),
                            'bucket_endpoint' => true,
                            'version' => 'latest',
                            'region'  => trim($aiomatic_Main_Settings['generic_region']),
                            'credentials' => $credentials
                        ]);
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to initialize Generic S3 Storage API: ' . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                    try 
                    {
                        $obj_arr = [
                            'Bucket' => trim($aiomatic_Main_Settings['generic_bucket']),
                            'Key'    => $local_id,
                        ];
                        $awsret = $s3->deleteObject($obj_arr);
                        if(!isset($awsret['DeleteMarker']))
                        {
                            $aiomatic_result['msg'] = "Failed to decode Generic S3 Storage API response: " . print_r($awsret, true);
                            wp_send_json($aiomatic_result);
                        }
                    } 
                    catch (Exception $e) 
                    {
                        $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Generic S3 Storage: " . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    } 
                }
                elseif($file_type == 'cloudflare')
                {
                    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                    if(!function_exists('is_plugin_active'))
                    {
                        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    }
                    if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
                    {
                        $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['cloud_bucket']) || trim($aiomatic_Main_Settings['cloud_bucket']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_bucket for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['cloud_user']) || trim($aiomatic_Main_Settings['cloud_user']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_user for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['cloud_pass']) || trim($aiomatic_Main_Settings['cloud_pass']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_pass for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['cloud_account']) || trim($aiomatic_Main_Settings['cloud_account']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_account for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    try
                    {
                        $credentials = array('key' => trim($aiomatic_Main_Settings['cloud_user']), 'secret' => trim($aiomatic_Main_Settings['cloud_pass']));
                        $s3 = new S3Client([
                            'endpoint' => "https://" . trim($aiomatic_Main_Settings['cloud_account']) . ".r2.cloudflarestorage.com",
                            'bucket_endpoint' => true,
                            'version' => 'latest',
                            'region' => 'us-east-1',
                            'credentials' => $credentials
                        ]);
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to initialize CloudFlare API: ' . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                    try 
                    {
                        $obj_arr = [
                            'Bucket' => trim($aiomatic_Main_Settings['cloud_bucket']),
                            'Key'    => $local_id,
                        ];
                        $awsret = $s3->deleteObject($obj_arr);
                        if(!isset($awsret['DeleteMarker']))
                        {
                            $aiomatic_result['msg'] = "Failed to decode CloudFlare API response: " . print_r($awsret, true);
                            wp_send_json($aiomatic_result);
                        }
                    } 
                    catch (Exception $e) 
                    {
                        $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to CloudFlare: " . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    } 
                }
                elseif($file_type == 'digital')
                {
                    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
                    if(!function_exists('is_plugin_active'))
                    {
                        include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                    }
                    if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
                    {
                        $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['digital_endpoint']) || trim($aiomatic_Main_Settings['digital_endpoint']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_endpoint for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['digital_user']) || trim($aiomatic_Main_Settings['digital_user']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_user for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    if (!isset($aiomatic_Main_Settings['digital_pass']) || trim($aiomatic_Main_Settings['digital_pass']) == '')
                    {
                        $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_pass for this to work!';
                        wp_send_json($aiomatic_result);
                    }
                    $bucket_name = '';
                    preg_match_all('#https:\/\/([^.]*?)\.(?:[^.]*?)\.digitaloceanspaces\.com#i', trim($aiomatic_Main_Settings['digital_endpoint']), $zmatches);
                    if(isset($zmatches[1][0]))
                    {
                        $bucket_name = $zmatches[1][0];
                    }
                    else
                    {
                        $aiomatic_result['msg'] = 'Failed to parse Digital Ocean Spaces URL: ' . trim($aiomatic_Main_Settings['digital_endpoint']);
                        wp_send_json($aiomatic_result);
                    }
                    $endpoint_plain_url = preg_replace('#https?:\/\/([^.]*?\.)([^.]*?)\.digitaloceanspaces\.com#i', 'https://$2.digitaloceanspaces.com', trim($aiomatic_Main_Settings['digital_endpoint']));
                    try
                    {
                        $credentials = array('key' => trim($aiomatic_Main_Settings['digital_user']), 'secret' => trim($aiomatic_Main_Settings['digital_pass']));
                        $s3 = new S3Client([
                            'version' => 'latest',
                            'region'  => 'us-east-1',
                            'endpoint' => $endpoint_plain_url,
                            'use_path_style_endpoint' => false,
                            'credentials' => $credentials
                        ]);
                    }
                    catch(Exception $e)
                    {
                        $aiomatic_result['msg'] = 'Failed to initialize Digital Ocean API: ' . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    }
                    try 
                    {
                        $obj_arr = [
                            'Bucket' => trim($bucket_name),
                            'Key'    => $local_id,
                        ];
                        $awsret = $s3->deleteObject($obj_arr);
                        if(!isset($awsret['DeleteMarker']))
                        {
                            $aiomatic_result['msg'] = "Failed to decode Digital Ocean API response: " . print_r($awsret, true);
                            wp_send_json($aiomatic_result);
                        }
                    } 
                    catch (Exception $e) 
                    {
                        $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Digital Ocean: " . $e->getMessage();
                        wp_send_json($aiomatic_result);
                    } 
                }
                wp_delete_post($id);
                $deleted = true;
            }
        }
        if($deleted === true)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'OmniBlock files deleted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_save_omni_template', 'aiomatic_save_omni_template');
function aiomatic_save_omni_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock template creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['omni_template_new'])  || empty($_POST['omni_template_new']) || !isset($_POST['omni_template_cat_new']) || !isset($_POST['sortable_cards_new']) || empty($_POST['sortable_cards_new']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $ai_data = array(
            'post_type' => 'aiomatic_omni_temp',
            'post_title' => $_POST['omni_template_new'],
            'post_content' => $_POST['sortable_cards_new'],
            'post_status' => 'publish'
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $omni_id = wp_insert_post($ai_data);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($omni_id))
        {
            aiomatic_log_to_file('Failed to insert OmniBlock template: ' . $omni_id->get_error_message());
        }
        elseif($omni_id === 0)
        {
            aiomatic_log_to_file('Failed to insert OmniBlock template: ' . print_r($ai_data, true));
        }
        else 
        {
            update_post_meta($omni_id, 'aiomatic_json', $_POST['sortable_cards_new']);
            if(trim($_POST['omni_template_cat_new']) != '')
            {
                $cats = $_POST['omni_template_cat_new'];
                $cat_arr = explode(';', $cats);
                $cat_arr = array_map('trim', $cat_arr);
                wp_set_object_terms($omni_id, $cat_arr, 'ai_template_categories');
            }
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'OmniBlock template inserted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_save_editor_template', 'aiomatic_save_editor_template');
function aiomatic_save_editor_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Content Editor template creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['editor_template_new'])  || empty($_POST['editor_template_new']) || !isset($_POST['extractedData']) || empty($_POST['extractedData']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $safe_json = $_POST['extractedData'];
        $ai_data = array(
            'post_type' => 'aiomatic_editor_temp',
            'post_title' => $_POST['editor_template_new'],
            'post_content' => '',
            'post_status' => 'publish'
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $omni_id = wp_insert_post($ai_data);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($omni_id))
        {
            aiomatic_log_to_file('Failed to insert AI Content Editor template: ' . $omni_id->get_error_message());
        }
        elseif($omni_id === 0)
        {
            aiomatic_log_to_file('Failed to insert AI Content Editor template: ' . print_r($ai_data, true));
        }
        else 
        {
            update_post_meta($omni_id, 'aiomatic_json', $safe_json);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'AI Content Editor template inserted successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_rename_editor_template', 'aiomatic_rename_editor_template');
function aiomatic_rename_editor_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Content Editor template creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['editor_template_new'])  || empty($_POST['editor_template_new']) || !isset($_POST['selected']) || empty($_POST['selected']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $template_name = sanitize_text_field( stripslashes($_POST['editor_template_new']) );
        $selected = $_POST['selected'];
        if ( get_post_status( $selected ) ) 
        {
            $ai_data = array(
                'ID' => $selected,
                'post_title' => $template_name,
            );
            remove_filter('content_save_pre', 'wp_filter_post_kses');
            remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
            $za_id = wp_update_post($ai_data);
            add_filter('content_save_pre', 'wp_filter_post_kses');
            add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
            if(is_wp_error($za_id))
            {
                aiomatic_log_to_file('Failed to insert AI Content Editor template: ' . $za_id->get_error_message());
            }
            elseif($za_id === 0)
            {
                aiomatic_log_to_file('Failed to insert AI Content Editor template: ' . print_r($ai_data, true));
            }
            else 
            {
                $aiomatic_result['status'] = 'success';
                $aiomatic_result['msg'] = 'AI Content Editor template updated successfully';
            }
        }
        else
        {
            $aiomatic_result['msg'] = 'AI Content Editor template ID not found: ' . $selected;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_overwrite_editor_template', 'aiomatic_overwrite_editor_template');
function aiomatic_overwrite_editor_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Content Editor template overwriting');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['selected']) || empty($_POST['selected']) || !isset($_POST['extractedData']) || empty($_POST['extractedData']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $safe_json = $_POST['extractedData'];
        $selected = $_POST['selected'];
        if ( get_post_status( $selected ) ) 
        {
            update_post_meta($selected, 'aiomatic_json', $safe_json);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'AI Content Editor template overwritten successfully';
        }
        else
        {
            $aiomatic_result['msg'] = 'AI Content Editor template ID not found: ' . $selected;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_editor_template', 'aiomatic_delete_editor_template');
function aiomatic_delete_editor_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Content Editor template deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['selected'])  || empty($_POST['selected']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $selected = $_POST['selected'];
        if ( get_post_status( $selected ) ) 
        {
            wp_delete_post($selected, true);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'AI Content Editor template deleted successfully';
        }
        else
        {
            $aiomatic_result['msg'] = 'AI Content Editor template ID not found: ' . $selected;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_load_editor_template', 'aiomatic_load_editor_template');
function aiomatic_load_editor_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with AI Content Editor template loading');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['selected'])  || empty($_POST['selected']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $selected = $_POST['selected'];
        if ( get_post_status( $selected ) ) 
        {
            $json_back = get_post_meta($selected, 'aiomatic_json', true);
            if(empty($json_back))
            {
                $aiomatic_result['msg'] = 'Incorrect template structure found for ID: ' . $selected;
                wp_send_json($aiomatic_result);
            }
            if(is_string($json_back))
            {
                $json_back = str_replace("\\'", "'", $json_back); 
                $json_back = str_replace("'", "\\\\'", $json_back);
                $saved_tmp = json_decode($json_back, true);
            }
            else
            {
                $saved_tmp = $json_back;
            }
            if($saved_tmp === null)
            {
                if (json_last_error() !== JSON_ERROR_NONE) 
                {
                    aiomatic_log_to_file("Failed to decode templaet json: " . json_last_error_msg());
                }
                $aiomatic_result['msg'] = 'AI Content Editor edit template failed to be decoded: ' . $selected;
                wp_send_json($aiomatic_result);
            }
            $aiomatic_Spinner_Settings = get_option('aiomatic_Spinner_Settings', array());
            foreach($saved_tmp as $theindex => $thevalue)
            {
                $aiomatic_Spinner_Settings[$theindex] = $thevalue;
            }
            aiomatic_update_option('aiomatic_Spinner_Settings', $aiomatic_Spinner_Settings);
            aiomatic_update_option('aiomatic_loaded_template', $selected);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'AI Content Editor template loaded successfully';
        }
        else
        {
            $aiomatic_result['msg'] = 'AI Content Editor template ID not found: ' . $selected;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_save_omni_template_edit', 'aiomatic_save_omni_template_edit');
function aiomatic_save_omni_template_edit()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with OmniBlock template creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['omni_template_edit'])  || empty($_POST['omni_template_edit']) || !isset($_POST['omni_template_cat_edit']) || !isset($_POST['sortable_cards_edit']) || empty($_POST['sortable_cards_edit']) || !isset($_POST['omni_template_id']) || empty($_POST['omni_template_id']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $ai_data = array(
            'post_type'    => 'aiomatic_omni_temp',
            'post_title'   => $_POST['omni_template_edit'],
            'post_content' => $_POST['sortable_cards_edit'],
            'ID'           => $_POST['omni_template_id'],
            'post_status'  => 'publish'
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $omni_id = wp_update_post($ai_data);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($omni_id))
        {
            aiomatic_log_to_file('Failed to update OmniBlock template: ' . $omni_id->get_error_message());
        }
        elseif($omni_id === 0)
        {
            aiomatic_log_to_file('Failed to update OmniBlock template: ' . print_r($ai_data, true));
        }
        else 
        {
            update_post_meta($omni_id, 'aiomatic_json', $_POST['sortable_cards_edit']);
            if(trim($_POST['omni_template_cat_edit']) != '')
            {
                $cats = $_POST['omni_template_cat_edit'];
                $cat_arr = explode(';', $cats);
                $cat_arr = array_map('trim', $cat_arr);
                wp_set_object_terms($omni_id, $cat_arr, 'ai_template_categories');
            }
            else
            {
                wp_set_object_terms($omni_id, array(), 'ai_template_categories');
            }
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = 'OmniBlock template updated successfully';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_sync_assistants', 'aiomatic_sync_assistants');
function aiomatic_sync_assistants()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with assistant importing');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $token = '';
    $imported = false;
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
        wp_send_json($aiomatic_result);
    }
    require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
    try
    {
        $all_assistants = aiomatic_openai_list_assistants($token);
        if(empty($all_assistants))
        {
            $aiomatic_result['msg'] = 'No assistants to import.';
            wp_send_json($aiomatic_result);
        }
        foreach($all_assistants as $my_assistant)
        {
            $vector_store_id = '';
            if(isset($my_assistant['tool_resources']['file_search']['vector_store_ids'][0]))
            {
                $vector_store_id = $my_assistant['tool_resources']['file_search']['vector_store_ids'][0];
            }
            $file_ids = array();
            if(isset($my_assistant['tool_resources']['code_interpreter']['file_ids'][0]))
            {
                $file_ids = $my_assistant['tool_resources']['code_interpreter']['file_ids'];
            }
            $result = aiomatic_save_assistant_only_local($token, $my_assistant['name'], $my_assistant['model'], $my_assistant['instructions'], $my_assistant['description'], $my_assistant['temperature'], $my_assistant['top_p'], '', '', $file_ids, $my_assistant['id'], $my_assistant['created_at'], $my_assistant['tools'], $vector_store_id);
            if(!isset($result['id']))
            {
                $aiomatic_result['msg'] = 'Failed to import assistant: ' . print_r($result, true);
            }
            else
            {
                $imported = true;
            }
        }
    }
    catch(Exception $e)
    {
        $aiomatic_result['msg'] = 'Exception while importing assistants: ' . $e->getMessage();
    }
    if($imported === true)
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'Assistant imported successfully';
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_sync_batches', 'aiomatic_sync_batches');
function aiomatic_sync_batches()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with batch request importing');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $token = '';
    $imported = false;
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up your API key in the plugin\' settings.';
        wp_send_json($aiomatic_result);
    }
    require_once (dirname(__FILE__) . "/res/aiomatic-batch-api.php"); 
    try
    {
        $all_batches = aiomatic_openai_list_batches($token);
        if(empty($all_batches))
        {
            $aiomatic_result['msg'] = 'No batch requests to import.';
            wp_send_json($aiomatic_result);
        }
        foreach($all_batches as $my_batch)
        {
            $result = aiomatic_save_batch_only_local($token, $my_batch);
            if(!isset($result['id']))
            {
                $aiomatic_result['msg'] = 'Failed to import AI Batch Request: ' . print_r($result, true);
            }
            else
            {
                $imported = true;
            }
        }
    }
    catch(Exception $e)
    {
        $aiomatic_result['msg'] = 'Exception while importing batch requests: ' . $e->getMessage();
    }
    if($imported === true)
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'AI Batch Requests imported successfully';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_all_batches', 'aiomatic_delete_all_batches');
function aiomatic_delete_all_batches()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with batch request importing');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $post_type = 'aiomatic_batches';
    $paged = 1; 
    $posts_per_page = 100; 
    $posts = array();
    do {
        $args = array(
            'post_type'      => $post_type, 
            'posts_per_page' => $posts_per_page,
            'post_status'    => 'any',  
            'paged'          => $paged,  
        );
        $query = new WP_Query($args);
        if ($query->have_posts()) {
            while ($query->have_posts()) {
                $query->the_post();
                $posts[] = $query->post;
            }
        }

        $paged++;
        wp_reset_postdata(); 
    } while ($query->have_posts());
    $deleted = false;
    foreach ($posts as $post) {
        wp_delete_post($post->ID, true);
        $deleted = true;
    }
    if($deleted === true)
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'AI Batch Requests deleted successfully';
    }
    if($deleted === false)
    {
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = 'No AI Batch Requests to delete';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_persona', 'aiomatic_get_persona');
function aiomatic_get_persona()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with persona query');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $persona = false;
        $aiomatic_persona = get_post($_POST['ids'], ARRAY_A);
        if ($aiomatic_persona) 
        {
            $avatar = get_the_post_thumbnail_url($aiomatic_persona['ID'], 'thumbnail');
            $avatarid = get_post_thumbnail_id($aiomatic_persona['ID']);
            $message = get_post_meta($aiomatic_persona['ID'], '_persona_first_message', true);
            $model = get_post_meta($aiomatic_persona['ID'], '_persona_model', true);
            $onlyKeys = ['ID', 'post_content','post_title', 'post_excerpt'];
            $aiomatic_persona = array_filter($aiomatic_persona, function($v) use ($onlyKeys) 
            {
                return in_array($v, $onlyKeys);
            }, ARRAY_FILTER_USE_KEY);
            $aiomatic_persona['avatar'] = $avatar;
            $aiomatic_persona['avatarid'] = $avatarid;
            $aiomatic_persona['message'] = $message;
            $aiomatic_persona['model'] = $model;
            $persona = json_encode($aiomatic_persona);
        }
        if($persona !== false)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['msg'] = $persona;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_duplicate_form', 'aiomatic_duplicate_form');
function aiomatic_duplicate_form()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form duplication');
    if(!isset($_POST['formid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $formid = $_POST['formid'];
        $original_form = get_post($formid);
        if($original_form == null)
        {
            $aiomatic_result['msg'] = 'Form id was not found!';
            wp_send_json($aiomatic_result);
        }
        $prompt = get_post_meta($formid, 'prompt', true);
        $model = get_post_meta($formid, 'model', true);
        $assistant_id = get_post_meta($formid, 'assistant_id', true);
        if(empty($assistant_id))
        {
            $assistant_id = '';
        }
        $header = get_post_meta($formid, 'header', true);
        $advanced = get_post_meta($formid, 'advanced', true);
        $editor = get_post_meta($formid, 'editor', true);
        $submit = get_post_meta($formid, 'submit', true);
        $max = get_post_meta($formid, 'max', true);
        $temperature = get_post_meta($formid, 'temperature', true);
        $topp = get_post_meta($formid, 'topp', true);
        $presence = get_post_meta($formid, 'presence', true);
        $frequency = get_post_meta($formid, 'frequency', true);
        $response = get_post_meta($formid, 'response', true);
        $type = get_post_meta($formid, 'type', true);
        $aiomaticfields = get_post_meta($formid, '_aiomaticfields', true);
        if(!is_array($aiomaticfields))
        {
            $aiomaticfields = array();
        }
        $original_form->post_date = wp_date('Y-m-d H:i:s');
        unset($original_form->ID);
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $forms_id = wp_insert_post($original_form);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($forms_id))
        {
            $aiomatic_result['msg'] = 'Failed to save duplicated form!';
            wp_send_json($aiomatic_result);
        }
        update_post_meta($forms_id, 'prompt', $prompt);
        update_post_meta($forms_id, 'model', $model);
        update_post_meta($forms_id, 'assistant_id', $assistant_id);
        update_post_meta($forms_id, 'header', $header);
        update_post_meta($forms_id, 'advanced', $advanced);
        update_post_meta($forms_id, 'editor', $editor);
        update_post_meta($forms_id, 'submit', $submit);
        update_post_meta($forms_id, 'max', $max);
        update_post_meta($forms_id, 'temperature', $temperature);
        update_post_meta($forms_id, 'topp', $topp);
        update_post_meta($forms_id, 'presence', $presence);
        update_post_meta($forms_id, 'frequency', $frequency);
        update_post_meta($forms_id, 'response', $response);
        update_post_meta($forms_id, 'type', $type);
        update_post_meta($forms_id, '_aiomaticfields', $aiomaticfields);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $forms_id;
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_duplicate_persona', 'aiomatic_duplicate_persona');
function aiomatic_duplicate_persona()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with persona duplication');
    if(!isset($_POST['personaid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $personaid = $_POST['personaid'];
        $original_persona = get_post($personaid);
        if($original_persona == null)
        {
            $aiomatic_result['msg'] = 'Persona id was not found!';
            wp_send_json($aiomatic_result);
        }
        $first_message = get_post_meta($personaid, '_persona_first_message', true);
        $model = get_post_meta($personaid, '_persona_model', true);
        $original_persona->post_date = wp_date('Y-m-d H:i:s');
        $avatar = get_post_thumbnail_id($original_persona->ID);
        unset($original_persona->ID);
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $persona_id = wp_insert_post($original_persona);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($persona_id))
        {
            $aiomatic_result['msg'] = 'Failed to save duplicated persona!';
            wp_send_json($aiomatic_result);
        }
        if($avatar > 0)
        {
            require_once(ABSPATH . 'wp-admin/includes/image.php');
            require_once(ABSPATH . 'wp-admin/includes/media.php');
            set_post_thumbnail( $persona_id, $avatar );
        }
        if(!empty($first_message))
        {
            update_post_meta($persona_id, '_persona_first_message', $first_message);
        }
        if(!empty($model))
        {
            update_post_meta($persona_id, '_persona_model', $model);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $persona_id;
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_convert_persona', 'aiomatic_convert_persona');
function aiomatic_convert_persona()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with persona duplication');
    if(!isset($_POST['personaid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        $personaid = $_POST['personaid'];
        $original_persona = get_post($personaid);
        if($original_persona == null)
        {
            $aiomatic_result['msg'] = 'Persona id was not found!';
            wp_send_json($aiomatic_result);
        }
        $assistant_first_message = get_post_meta($personaid, '_persona_first_message', true);
        $xmodel = get_post_meta($personaid, '_persona_model', true);
        if(empty($xmodel))
        {
            $xmodel = AIMOGEN_DEFAULT_MODEL;
        }
        $featured_image = get_post_thumbnail_id($personaid);
        $vector_store_id = '';
        $metadata = '';
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        $assistantData = aiomatic_openai_save_assistant(
            $token,
            AIMOGEN_DEFAULT_MODEL,
            $original_persona->post_title,
            $original_persona->post_excerpt,
            0,
            0,
            $original_persona->post_content,
            [],
            [],
            $metadata,
            $vector_store_id
        );
        if($assistantData === false)
        {
            $aiomatic_result['msg'] = 'Failed to save assistant using the API';
            wp_send_json($aiomatic_result);
        }
        else
        {
            if(isset($assistantData['id']))
            {
                $assistant_id = $assistantData['id'];
            }
            else
            {
                $aiomatic_result['msg'] = 'Failed to save assistant using the API!';
                wp_send_json($aiomatic_result);
            }
        }
        $errors = '';
        try
        {
            $file_ids = array();
            $result = aiomatic_save_assistant_only_local($token, $original_persona->post_title, $xmodel, $original_persona->post_content, $original_persona->post_excerpt, 0, 0, $assistant_first_message, $featured_image, $file_ids, $assistant_id, time(), [], $vector_store_id);
            if(!isset($result['id']))
            {
                $aiomatic_result['msg'] = 'Failed to import assistant: ' . print_r($result, true);
            }
        }
        catch(Exception $e)
        {
            $errors .= 'Failed to duplicate assistant ID locally: ' . $assistant_id . ', exception: ' . $e->getMessage() . '\n';
        }
        if(!empty($errors))
        {
            $aiomatic_result['msg'] = 'Assistant failed to be duplicated: ' . $errors;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $assistant_id;
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_add_remote_realtime_chatbot', 'aiomatic_add_remote_realtime_chatbot');
function aiomatic_add_remote_realtime_chatbot()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with remote realtime chatbot creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $page_title = 'Realtime Chatbot ' . uniqid(); 
    $page_id = wp_insert_post(array(
        'post_title'     => $page_title,
        'post_status'    => 'publish',
        'post_type'      => 'aiomatic_remote_chat',
        'post_content'   => '[aimogen-realtime-chat]'
    ));
    if ($page_id && !is_wp_error($page_id)) 
    {
        $myop = get_option('aiomatic_realtime_chat_page_id', false);
        if($myop === false)
        {
            $myop = array();
        }
        if(is_numeric($myop))
        {
            $myop = array($myop);
        }
        $myop[] = $page_id;
        aiomatic_update_option('aiomatic_realtime_chat_page_id', $myop);
    }
    else
    {
        $aiomatic_result['msg'] = 'Failed to save remote realtime chatbot instance!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['status'] = 'success';
    $aiomatic_result['id'] = $page_id;
    $aiomatic_result['msg'] = 'Success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_add_remote_chatbot', 'aiomatic_add_remote_chatbot');
function aiomatic_add_remote_chatbot()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with remote chatbot creation');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $page_title = 'Chatbot ' . uniqid(); 
    $page_id = wp_insert_post(array(
        'post_title'     => $page_title,
        'post_status'    => 'publish',
        'post_type'      => 'aiomatic_remote_chat',
        'post_content'   => '[aimogen-chat-form]'
    ));
    if ($page_id && !is_wp_error($page_id)) 
    {
        $myop = get_option('aiomatic_chat_page_id', false);
        if($myop === false)
        {
            $myop = array();
        }
        if(is_numeric($myop))
        {
            $myop = array($myop);
        }
        $myop[] = $page_id;
        aiomatic_update_option('aiomatic_chat_page_id', $myop);
    }
    else
    {
        $aiomatic_result['msg'] = 'Failed to save remote chatbot instance!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['status'] = 'success';
    $aiomatic_result['id'] = $page_id;
    $aiomatic_result['msg'] = 'Success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_remote_chatbot', 'aiomatic_delete_remote_chatbot');
function aiomatic_delete_remote_chatbot()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with remote chatbot deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['dataid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
        wp_send_json($aiomatic_result);
    }
    $dataid = $_POST['dataid'];
    wp_delete_post($dataid, true);
    $aiomatic_result['status'] = 'success';
    $aiomatic_result['msg'] = 'Success';
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_form', 'aiomatic_delete_form');
function aiomatic_delete_form()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular form deletion');
    if(!isset($_POST['formid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('manage_options') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $_POST['formid'];
        $aiomatic_result['msg'] = 'Success';
        wp_delete_post($_POST['formid']);
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_assistant', 'aiomatic_delete_assistant');
function aiomatic_delete_assistant()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular assistant deletion');
    if(!isset($_POST['assistantid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('manage_options') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        $errors = '';
        $assistant_id = get_post_meta($_POST['assistantid'], '_assistant_id', true);
        if(!empty($assistant_id))
        {
            try
            {
                aiomatic_openai_delete_assistant($token, $assistant_id);
            }
            catch(Exception $e)
            {
                $errors .= 'Failed to delete assistant ID: ' . $assistant_id . ', exception: ' . $e->getMessage() . '\n';
            }
        }
        $vector_store_id = get_post_meta($_POST['assistantid'], '_assistant_vector_store_id', true);
        if(!empty($vector_store_id))
        {
            try
            {
                aiomatic_openai_delete_vector_store($token, $vector_store_id);
            }
            catch(Exception $e)
            {
                aiomatic_log_to_file('Failed to delete vector store ID: ' . $vector_store_id . ', exception: ' . $e->getMessage());
            }
        }
        wp_delete_post($_POST['assistantid']);
        if(!empty($errors))
        {
            $aiomatic_result['msg'] = 'Assistant failed to be deleted: ' . $errors;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $_POST['assistantid'];
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_convert_assistant', 'aiomatic_convert_assistant');
function aiomatic_convert_assistant()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular assistant convertion');
    if(!isset($_POST['assistantid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $assitant_id = $_POST['assistantid'];
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        $this_post = get_post($assitant_id);
        if($this_post === null)
        {
            $aiomatic_result['msg'] = 'Incorrect post_id sent';
            wp_send_json($aiomatic_result);
        }
        $personas_data = array(
            'post_type' => 'aiomatic_personas',
            'post_title' => $this_post->post_title,
            'post_excerpt' => $this_post->post_excerpt,
            'post_content' => $this_post->post_content,
            'post_status' => 'publish'
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $personas_id = wp_insert_post($personas_data);
        if(is_wp_error($personas_id))
        {
            $aiomatic_result['msg'] = 'Failed to save persona!';
            wp_send_json($aiomatic_result);
        }
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        $assistant_first_message = get_post_meta($assitant_id, '_assistant_first_message', true);
        $model = get_post_meta($assitant_id, '_assistant_model', true);
        $featured_image = get_post_thumbnail_id($assitant_id);
        if(is_numeric($featured_image))
        {
            if($featured_image > 0)
            {
                require_once(ABSPATH . 'wp-admin/includes/image.php');
                require_once(ABSPATH . 'wp-admin/includes/media.php');
                set_post_thumbnail( $personas_id, $featured_image );
            }
        }
        update_post_meta($personas_id, '_persona_first_message', $assistant_first_message);
        update_post_meta($personas_id, '_persona_model', $model);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $personas_id;
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_omni_template', 'aiomatic_delete_omni_template');
function aiomatic_delete_omni_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular OmniBlock Template deletion');
    if(!isset($_POST['id']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('manage_options') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        wp_delete_post($_POST['id']);
        if(!empty($errors))
        {
            $aiomatic_result['msg'] = 'OmniBlock Template failed to be deleted: ' . $errors;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $_POST['id'];
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_omni_file', 'aiomatic_delete_omni_file');
function aiomatic_delete_omni_file()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular OmniBlock File deletion');
    if(!isset($_POST['id']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('manage_options') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $this_post = get_post($_POST['id']);
        if($this_post === null)
        {
            $aiomatic_result['msg'] = 'Incorrect post_id sent';
            wp_send_json($aiomatic_result);
        }

        $local_id = get_post_meta($_POST['id'], 'local_id', true);
        if(empty($local_id))
        {
            $aiomatic_result['msg'] = 'Local file path not found';
            wp_send_json($aiomatic_result);
        }
        $file_type = '';
        $terms = wp_get_object_terms( $_POST['id'], 'ai_file_type' );
        if(!is_wp_error($terms))
        {
            foreach($terms as  $tm)
            {
                $file_type = $tm->slug;
                break;
            }
        }
        if($file_type == 'local')
        {
            global $wp_filesystem;
            if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
                include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
                wp_filesystem($creds);
            }
            $wp_filesystem->delete($local_id);
        }
        elseif($file_type == 'amazon')
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if(!function_exists('is_plugin_active'))
            {
                include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            }
            if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
            {
                $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['bucket_name']) || trim($aiomatic_Main_Settings['bucket_name']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Amazon S3 bucket_name for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['s3_user']) || trim($aiomatic_Main_Settings['s3_user']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Amazon S3 s3_user for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['s3_pass']) || trim($aiomatic_Main_Settings['s3_pass']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Amazon S3 s3_pass for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['bucket_region']) || trim($aiomatic_Main_Settings['bucket_region']) == '')
            {
                $aiomatic_Main_Settings['bucket_region'] = 'eu-central-1';
            }
            try
            {
                $credentials = array('key' => trim($aiomatic_Main_Settings['s3_user']), 'secret' => trim($aiomatic_Main_Settings['s3_pass']));
                $s3 = new S3Client([
                    'version' => 'latest',
                    'region'  => trim($aiomatic_Main_Settings['bucket_region']),
                    'credentials' => $credentials
                ]);
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to initialize Amazon S3 API: ' . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
            try 
            {
                $obj_arr = [
                    'Bucket' => trim($aiomatic_Main_Settings['bucket_name']),
                    'Key'    => $local_id,
                ];
                $awsret = $s3->deleteObject($obj_arr);
                if(!isset($awsret['DeleteMarker']))
                {
                    $aiomatic_result['msg'] = "Failed to decode Amazon S3 API response: " . print_r($awsret, true);
                    wp_send_json($aiomatic_result);
                }
            } 
            catch (Exception $e) 
            {
                $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Amazon S3: " . $e->getMessage();
                wp_send_json($aiomatic_result);
            } 
        }
        elseif($file_type == 'wasabi')
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if(!function_exists('is_plugin_active'))
            {
                include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            }
            if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
            {
                $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['wasabi_bucket']) || trim($aiomatic_Main_Settings['wasabi_bucket']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_bucket for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['wasabi_user']) || trim($aiomatic_Main_Settings['wasabi_user']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_user for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['wasabi_pass']) || trim($aiomatic_Main_Settings['wasabi_pass']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_pass for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['wasabi_region']) || trim($aiomatic_Main_Settings['wasabi_region']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Wasabi wasabi_region for this to work!';
                wp_send_json($aiomatic_result);
            }
            try
            {
                $credentials = array('key' => trim($aiomatic_Main_Settings['wasabi_user']), 'secret' => trim($aiomatic_Main_Settings['wasabi_pass']));
                $s3 = new S3Client([
                    'endpoint' => "https://" . trim($aiomatic_Main_Settings['wasabi_bucket']) . ".s3." . trim($aiomatic_Main_Settings['wasabi_region']) . ".wasabisys.com/",
                    'bucket_endpoint' => true,
                    'version' => 'latest',
                    'region'  => trim($aiomatic_Main_Settings['wasabi_region']),
                    'credentials' => $credentials
                ]);
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to initialize Wasabi API: ' . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
            try 
            {
                $obj_arr = [
                    'Bucket' => trim($aiomatic_Main_Settings['wasabi_bucket']),
                    'Key'    => $local_id,
                ];
                $awsret = $s3->deleteObject($obj_arr);
                if(!isset($awsret['DeleteMarker']))
                {
                    $aiomatic_result['msg'] = "Failed to decode Wasabi API response: " . print_r($awsret, true);
                    wp_send_json($aiomatic_result);
                }
            } 
            catch (Exception $e) 
            {
                $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Wasabi: " . $e->getMessage();
                wp_send_json($aiomatic_result);
            } 
        }
        elseif($file_type == 'generics3')
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if(!function_exists('is_plugin_active'))
            {
                include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            }
            if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
            {
                $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['generic_bucket']) || trim($aiomatic_Main_Settings['generic_bucket']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage bucket for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['generic_user']) || trim($aiomatic_Main_Settings['generic_user']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage user for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['generic_pass']) || trim($aiomatic_Main_Settings['generic_pass']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage password for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['generic_region']) || trim($aiomatic_Main_Settings['generic_region']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage region for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['generic_url']) || trim($aiomatic_Main_Settings['generic_url']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Generic S3 Storage URL for this to work!';
                wp_send_json($aiomatic_result);
            }
            try
            {
                $credentials = array('key' => trim($aiomatic_Main_Settings['generic_user']), 'secret' => trim($aiomatic_Main_Settings['generic_pass']));
                $s3 = new S3Client([
                    'endpoint' => trim($aiomatic_Main_Settings['generic_url']),
                    'bucket_endpoint' => true,
                    'version' => 'latest',
                    'region'  => trim($aiomatic_Main_Settings['generic_region']),
                    'credentials' => $credentials
                ]);
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to initialize Generic S3 Storage API: ' . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
            try 
            {
                $obj_arr = [
                    'Bucket' => trim($aiomatic_Main_Settings['generic_bucket']),
                    'Key'    => $local_id,
                ];
                $awsret = $s3->deleteObject($obj_arr);
                if(!isset($awsret['DeleteMarker']))
                {
                    $aiomatic_result['msg'] = "Failed to decode Generic S3 Storage API response: " . print_r($awsret, true);
                    wp_send_json($aiomatic_result);
                }
            } 
            catch (Exception $e) 
            {
                $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Generic S3 Storage: " . $e->getMessage();
                wp_send_json($aiomatic_result);
            } 
        }
        elseif($file_type == 'cloudflare')
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if(!function_exists('is_plugin_active'))
            {
                include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            }
            if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
            {
                $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['cloud_bucket']) || trim($aiomatic_Main_Settings['cloud_bucket']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_bucket for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['cloud_user']) || trim($aiomatic_Main_Settings['cloud_user']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_user for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['cloud_pass']) || trim($aiomatic_Main_Settings['cloud_pass']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_pass for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['cloud_account']) || trim($aiomatic_Main_Settings['cloud_account']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a CloudFlare cloud_account for this to work!';
                wp_send_json($aiomatic_result);
            }
            try
            {
                $credentials = array('key' => trim($aiomatic_Main_Settings['cloud_user']), 'secret' => trim($aiomatic_Main_Settings['cloud_pass']));
                $s3 = new S3Client([
                    'endpoint' => "https://" . trim($aiomatic_Main_Settings['cloud_account']) . ".r2.cloudflarestorage.com",
                    'bucket_endpoint' => true,
                    'version' => 'latest',
                    'region' => 'us-east-1',
                    'credentials' => $credentials
                ]);
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to initialize CloudFlare API: ' . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
            try 
            {
                $obj_arr = [
                    'Bucket' => trim($aiomatic_Main_Settings['cloud_bucket']),
                    'Key'    => $local_id,
                ];
                $awsret = $s3->deleteObject($obj_arr);
                if(!isset($awsret['DeleteMarker']))
                {
                    $aiomatic_result['msg'] = "Failed to decode CloudFlare API response: " . print_r($awsret, true);
                    wp_send_json($aiomatic_result);
                }
            } 
            catch (Exception $e) 
            {
                $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to CloudFlare: " . $e->getMessage();
                wp_send_json($aiomatic_result);
            } 
        }
        elseif($file_type == 'digital')
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if(!function_exists('is_plugin_active'))
            {
                include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
            }
            if (!is_plugin_active('aiomatic-extension-amazon-s3-images/aiomatic-extension-amazon-s3-images.php')) 
            {
                $aiomatic_result['msg'] = 'You need enable the "Aimogen Extension: Amazon S3 Storage" plugin for this feature to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['digital_endpoint']) || trim($aiomatic_Main_Settings['digital_endpoint']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_endpoint for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['digital_user']) || trim($aiomatic_Main_Settings['digital_user']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_user for this to work!';
                wp_send_json($aiomatic_result);
            }
            if (!isset($aiomatic_Main_Settings['digital_pass']) || trim($aiomatic_Main_Settings['digital_pass']) == '')
            {
                $aiomatic_result['msg'] = 'You need to enter a Digital Ocean digital_pass for this to work!';
                wp_send_json($aiomatic_result);
            }
            $bucket_name = '';
            preg_match_all('#https:\/\/([^.]*?)\.(?:[^.]*?)\.digitaloceanspaces\.com#i', trim($aiomatic_Main_Settings['digital_endpoint']), $zmatches);
            if(isset($zmatches[1][0]))
            {
                $bucket_name = $zmatches[1][0];
            }
            else
            {
                $aiomatic_result['msg'] = 'Failed to parse Digital Ocean Spaces URL: ' . trim($aiomatic_Main_Settings['digital_endpoint']);
                wp_send_json($aiomatic_result);
            }
            $endpoint_plain_url = preg_replace('#https?:\/\/([^.]*?\.)([^.]*?)\.digitaloceanspaces\.com#i', 'https://$2.digitaloceanspaces.com', trim($aiomatic_Main_Settings['digital_endpoint']));
            try
            {
                $credentials = array('key' => trim($aiomatic_Main_Settings['digital_user']), 'secret' => trim($aiomatic_Main_Settings['digital_pass']));
                $s3 = new S3Client([
                    'version' => 'latest',
                    'region'  => 'us-east-1',
                    'endpoint' => $endpoint_plain_url,
                    'use_path_style_endpoint' => false,
                    'credentials' => $credentials
                ]);
            }
            catch(Exception $e)
            {
                $aiomatic_result['msg'] = 'Failed to initialize Digital Ocean API: ' . $e->getMessage();
                wp_send_json($aiomatic_result);
            }
            try 
            {
                $obj_arr = [
                    'Bucket' => trim($bucket_name),
                    'Key'    => $local_id,
                ];
                $awsret = $s3->deleteObject($obj_arr);
                if(!isset($awsret['DeleteMarker']))
                {
                    $aiomatic_result['msg'] = "Failed to decode Digital Ocean API response: " . print_r($awsret, true);
                    wp_send_json($aiomatic_result);
                }
            } 
            catch (Exception $e) 
            {
                $aiomatic_result['msg'] = "There was an error uploading the file " . $image_url . " to Digital Ocean: " . $e->getMessage();
                wp_send_json($aiomatic_result);
            } 
        }
        wp_delete_post($_POST['id']);
        if(!empty($errors))
        {
            $aiomatic_result['msg'] = 'OmniBlock File failed to be deleted: ' . $errors;
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $_POST['id'];
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_duplicate_assistant', 'aiomatic_duplicate_assistant');
function aiomatic_duplicate_assistant()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular assistant duplication');
    if(!isset($_POST['assistantid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $vector_store_id = '';
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        $original_assistant = get_post($_POST['assistantid'], ARRAY_A);
        if($original_assistant == null)
        {
            $aiomatic_result['msg'] = 'Assistant id was not found!';
            wp_send_json($aiomatic_result);
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        $errors = '';
        $assistant_id = get_post_meta($_POST['assistantid'], '_assistant_id', true);
        $assistant_first_message = get_post_meta($_POST['assistantid'], '_assistant_first_message', true);
        $featured_image = get_post_thumbnail_id($_POST['assistantid']);
        $new_id = false;
        if(!empty($assistant_id))
        {
            try
            {
                $new_id = aiomatic_openai_duplicate_assistant($token, $assistant_id, $vector_store_id);
            }
            catch(Exception $e)
            {
                $errors .= 'Failed to duplicate assistant ID: ' . $assistant_id . ', exception: ' . $e->getMessage() . '\n';
            }
        }
        if($new_id !== false)
        {
            try
            {
                $file_ids = array();
                if(isset($new_id['tool_resources']['code_interpreter']['file_ids'][0]))
                {
                    $file_ids = $new_id['tool_resources']['code_interpreter']['file_ids'];
                }
                $result = aiomatic_save_assistant_only_local($token, $new_id['name'], $new_id['model'], $new_id['instructions'], $new_id['description'], $new_id['temperature'], $new_id['top_p'], $assistant_first_message, $featured_image, $file_ids, $new_id['id'], $new_id['created_at'], $new_id['tools'], $vector_store_id);
                if(!isset($result['id']))
                {
                    $aiomatic_result['msg'] = 'Failed to import assistant: ' . print_r($result, true);
                }
            }
            catch(Exception $e)
            {
                $errors .= 'Failed to duplicate assistant ID locally: ' . $assistant_id . ', exception: ' . $e->getMessage() . '\n';
            }
            if(!empty($errors))
            {
                $aiomatic_result['msg'] = 'Assistant failed to be duplicated: ' . $errors;
                wp_send_json($aiomatic_result);
            }
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['id'] = $_POST['assistantid'];
            $aiomatic_result['msg'] = 'Success';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_duplicate_omni_template', 'aiomatic_duplicate_omni_template');
function aiomatic_duplicate_omni_template()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular OmniBlock template duplication');
    if(!isset($_POST['id']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $id = $_POST['id'];
        $original_temp = get_post($id);
        if($original_temp == null)
        {
            $aiomatic_result['msg'] = 'OmniBlock template id was not found!';
            wp_send_json($aiomatic_result);
        }
        $original_temp->post_date = wp_date('Y-m-d H:i:s');
        $original_temp->post_title .= ' - Copy';
        $original_temp->post_content = addslashes($original_temp->post_content);
        unset($original_temp->ID);
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $zaid = wp_insert_post($original_temp);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($zaid))
        {
            $aiomatic_result['msg'] = 'Failed to save duplicated OmniBlock template!';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $category_detail = get_the_terms($id, 'ai_template_categories');
            $categories_list = array();
            if(is_array($category_detail))
            {
                foreach($category_detail as $cd){
                    $categories_list[] = $cd->slug;
                }
            }
            if(!empty($categories_list))
            {
                wp_set_object_terms($zaid, $categories_list, 'ai_template_categories');
            }
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $zaid;
        $aiomatic_result['msg'] = 'Success';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_omni_data', 'aiomatic_get_omni_data');
function aiomatic_get_omni_data()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular OmniBlock template query');
    if(!isset($_POST['theID']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $id = $_POST['theID'];
        $original_temp = get_post($id);
        if($original_temp == null)
        {
            $aiomatic_result['msg'] = 'OmniBlock edit template id was not found!';
            wp_send_json($aiomatic_result);
        }
        $json_back = get_post_meta($id, 'aiomatic_json', true);
        if(!empty($json_back))
        {
            $original_temp->post_content = $json_back;
        }
        $saved_cards = json_decode($original_temp->post_content, true);
        if($saved_cards === null)
        {
            aiomatic_log_to_file('Decode fail: ' . json_last_error_msg());
            $aiomatic_result['msg'] = 'OmniBlock edit template failed to be decoded!';
            wp_send_json($aiomatic_result);
        }
        $data = aiomatic_get_omniblock_data($saved_cards, $original_temp);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['msg'] = $data;
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_assistant', 'aiomatic_get_assistant_ajax');
function aiomatic_get_assistant_ajax()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular assistant getting');
    if(!isset($_POST['assistantid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $original_assistant = get_post($_POST['assistantid'], ARRAY_A);
        if($original_assistant == null)
        {
            $aiomatic_result['msg'] = 'Assistant id was not found!';
            wp_send_json($aiomatic_result);
        }
        $code_interpreter = false;
        $file_search = false;
        $functions = [];
        $tools = get_post_meta($original_assistant['ID'], '_assistant_tools', true);
        if(!empty($tools))
        {
            foreach($tools as $tool)
            {
                if($tool['type'] == 'code_interpreter')
                {
                    $code_interpreter = true;
                }
                elseif($tool['type'] == 'file_search')
                {
                    $file_search = true;
                }
                elseif($tool['type'] == 'function')
                {
                    $functions[] = $tool['function'];
                }
            }
        }
        $assistant_first_message = get_post_meta($original_assistant['ID'], '_assistant_first_message', true);
        $assistant_model = get_post_meta($original_assistant['ID'], '_assistant_model', true);
        $assistant_files = get_post_meta($original_assistant['ID'], '_assistant_files', true);
        $assistant_id = get_post_meta($original_assistant['ID'], '_assistant_id', true);
        $temperature = get_post_meta($original_assistant['ID'], '_assistant_temperature', true);
        $topp = get_post_meta($original_assistant['ID'], '_assistant_topp', true);
        $vector_store_id = get_post_meta($original_assistant['ID'], '_assistant_vector_store_id', true);
        $original_assistant['code_interpreter'] = $code_interpreter;
        $original_assistant['file_search'] = $file_search;
        $original_assistant['functions'] = $functions;
        $original_assistant['assistant_first_message'] = $assistant_first_message;
        $original_assistant['assistant_model'] = $assistant_model;
        $original_assistant['assistant_files'] = $assistant_files;
        $original_assistant['assistant_id'] = $assistant_id;
        $original_assistant['temperature'] = $temperature;
        $original_assistant['topp'] = $topp;
        $original_assistant['vector_store_id'] = $vector_store_id;
        $original_assistant['featured_image'] = get_post_thumbnail_id($original_assistant['ID']);
        if($original_assistant['featured_image'] === false)
        {
            $original_assistant['featured_image'] = 0;
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $original_assistant;
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_batch', 'aiomatic_get_batch_ajax');
function aiomatic_get_batch_ajax()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular batch getting');
    if(!isset($_POST['batchid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $original_batch = get_post($_POST['batchid'], ARRAY_A);
        if($original_batch == null)
        {
            $aiomatic_result['msg'] = 'Batch id was not found!';
            wp_send_json($aiomatic_result);
        }
        $batch_id = get_post_meta($_POST['batchid'], '_batch_id', true);
        $batch_endpoint = get_post_meta($_POST['batchid'], '_batch_endpoint', true);
        $batch_completion_window = get_post_meta($_POST['batchid'], '_batch_completion_window', true);
        $batch_errors = get_post_meta($_POST['batchid'], '_batch_errors', true);
        $batch_input_file_id = get_post_meta($_POST['batchid'], '_batch_input_file_id', true);
        $batch_status = get_post_meta($_POST['batchid'], '_batch_status', true);
        $batch_output_file_id = get_post_meta($_POST['batchid'], '_batch_output_file_id', true);
        $batch_created_at = get_post_meta($_POST['batchid'], '_batch_created_at', true);
        $batch_in_progress_at = get_post_meta($_POST['batchid'], '_batch_in_progress_at', true);
        $batch_expires_at = get_post_meta($_POST['batchid'], '_batch_expires_at', true);
        $batch_finalizing_at = get_post_meta($_POST['batchid'], '_batch_finalizing_at', true);
        $batch_completed_at = get_post_meta($_POST['batchid'], '_batch_completed_at', true);
        $batch_failed_at = get_post_meta($_POST['batchid'], '_batch_failed_at', true);
        $batch_expired_at = get_post_meta($_POST['batchid'], '_batch_expired_at', true);
        $batch_cancelling_at = get_post_meta($_POST['batchid'], '_batch_cancelling_at', true);
        $batch_cancelled_at = get_post_meta($_POST['batchid'], '_batch_cancelled_at', true);
        $batch_request_count = get_post_meta($_POST['batchid'], '_batch_request_count', true);
        $batch_request_completed = get_post_meta($_POST['batchid'], '_batch_request_completed', true);
        $batch_request_failed = get_post_meta($_POST['batchid'], '_batch_request_failed', true);
        $batch_error_file_id = get_post_meta($_POST['batchid'], '_batch_error_file_id', true);
        $original_batch['batch_endpoint'] = $batch_endpoint;
        $original_batch['batch_id'] = $batch_id;
        $original_batch['batch_completion_window'] = $batch_completion_window;
        $original_batch['batch_errors'] = $batch_errors;
        $original_batch['batch_input_file_id'] = $batch_input_file_id;
        $original_batch['batch_status'] = $batch_status;
        $original_batch['batch_output_file_id'] = $batch_output_file_id;
        $original_batch['batch_expires_at'] = $batch_expires_at;
        $original_batch['batch_created_at'] = $batch_created_at;
        $original_batch['batch_in_progress_at'] = $batch_in_progress_at;
        $original_batch['batch_cancelling_at'] = $batch_cancelling_at;
        $original_batch['batch_cancelled_at'] = $batch_cancelled_at;
        $original_batch['batch_finalizing_at'] = $batch_finalizing_at;
        $original_batch['batch_completed_at'] = $batch_completed_at;
        $original_batch['batch_failed_at'] = $batch_failed_at;
        $original_batch['batch_expired_at'] = $batch_expired_at;
        $original_batch['batch_request_count'] = $batch_request_count;
        $original_batch['batch_request_failed'] = $batch_request_failed;
        $original_batch['batch_request_completed'] = $batch_request_completed;
        $original_batch['batch_error_file_id'] = $batch_error_file_id;
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $original_batch;
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_cancel_batch', 'aiomatic_cancel_batch_ajax');
function aiomatic_cancel_batch_ajax()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular batch cancel');
    if(!isset($_POST['batchid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $batch_id = $_POST['batchid'];
        $open_batch_id = get_post_meta($batch_id, '_batch_id', true);
        if(empty($open_batch_id))
        {
            $aiomatic_result['msg'] = 'Batch ID not found in the database!';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-batch-api.php"); 
        try
        {
            $batch = aiomatic_openai_cancel_batch($token, $open_batch_id);
            if(!isset($batch['id']))
            {
                throw new Exception('Incorrect response from batch cancelling: ' . print_r($batch, true));
            }
            else
            {
                $original_batch = get_post($batch_id, ARRAY_A);
                if($original_batch == null)
                {
                    $aiomatic_result['msg'] = 'Batch Request id was not found!';
                    wp_send_json($aiomatic_result);
                }
                $batch_data = array(
                    'post_type' => 'aiomatic_batches',
                    'post_title' => $batch['id'],
                    'post_status' => 'publish',
                    'ID' => $original_batch['ID']
                );
                remove_filter('content_save_pre', 'wp_filter_post_kses');
                remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
                $local_batch_id = wp_update_post($batch_data);
                add_filter('content_save_pre', 'wp_filter_post_kses');
                add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
                if(is_wp_error($local_batch_id))
                {
                    $aiomatic_result['msg'] = $local_batch_id->get_error_message();
                }
                elseif($local_batch_id === 0)
                {
                    $aiomatic_result['msg'] = 'Failed to update assistant to database: ' . $assistant['name'];
                }
                else 
                {
                    update_post_meta($local_batch_id, '_batch_id', $batch['id']);
                    update_post_meta($local_batch_id, '_batch_endpoint', $batch['endpoint']);
                    update_post_meta($local_batch_id, '_batch_completion_window', $batch['completion_window']);
                    update_post_meta($local_batch_id, '_batch_errors', $batch['errors']);
                    update_post_meta($local_batch_id, '_batch_input_file_id', $batch['input_file_id']);
                    update_post_meta($local_batch_id, '_batch_status', $batch['status']);
                    update_post_meta($local_batch_id, '_batch_output_file_id', $batch['output_file_id']);
                    update_post_meta($local_batch_id, '_batch_created_at', $batch['created_at']);
                    update_post_meta($local_batch_id, '_batch_in_progress_at', $batch['in_progress_at']);
                    update_post_meta($local_batch_id, '_batch_expires_at', $batch['expires_at']);
                    update_post_meta($local_batch_id, '_batch_finalizing_at', $batch['finalizing_at']);
                    update_post_meta($local_batch_id, '_batch_completed_at', $batch['completed_at']);
                    update_post_meta($local_batch_id, '_batch_failed_at', $batch['failed_at']);
                    update_post_meta($local_batch_id, '_batch_expired_at', $batch['expired_at']);
                    update_post_meta($local_batch_id, '_batch_cancelling_at', $batch['cancelling_at']);
                    update_post_meta($local_batch_id, '_batch_cancelled_at', $batch['cancelled_at']);
                    update_post_meta($local_batch_id, '_batch_request_count', $batch['request_counts']['total']);
                    update_post_meta($local_batch_id, '_batch_request_completed', $batch['request_counts']['completed']);
                    update_post_meta($local_batch_id, '_batch_request_failed', $batch['request_counts']['failed']);
                    update_post_meta($local_batch_id, '_batch_error_file_id', $batch['error_file_id']);
                    $aiomatic_result['status'] = 'success';
                    $aiomatic_result['id'] = $local_batch_id;
                }
            }
        }
        catch(Exception $e)
        {
            $aiomatic_result['msg'] = 'Exception in batch cancelling: ' . $e->getMessage();
            wp_send_json($aiomatic_result);
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $batch;
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_sync_assistant', 'aiomatic_sync_assistant_ajax');
function aiomatic_sync_assistant_ajax()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular assistant sync');
    if(!isset($_POST['assistantid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $original_assistant = get_post($_POST['assistantid'], ARRAY_A);
        if($original_assistant == null)
        {
            $aiomatic_result['msg'] = 'Assistant id was not found!';
            wp_send_json($aiomatic_result);
        }
        $ass_id = get_post_meta($original_assistant['ID'], '_assistant_id', true);
        if(empty($ass_id))
        {
            $aiomatic_result['msg'] = 'OpenAI assistant id was not found!';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
        try
        {
            $assistant = aiomatic_openai_retrieve_assistant($token, $ass_id);
            if(!isset($assistant['id']))
            {
                throw new Exception('Incorrect response from assistant grabbing: ' . print_r($assistant, true));
            }
        }
        catch(Exception $e)
        {
            $aiomatic_result['msg'] = 'Exception in assistant grabbing: ' . $e->getMessage();
            wp_send_json($aiomatic_result);
        }
        if(empty($assistant['name']))
        {
            $assistant['name'] = 'Untitled Assistant';
        }
        if(empty($assistant['instructions']))
        {
            $assistant['instructions'] = '';
        }
        if(empty($assistant['description']))
        {
            $assistant['description'] = '';
        }
        $assistant_data = array(
            'post_type' => 'aiomatic_assistants',
            'post_title' => $assistant['name'],
            'post_content' => $assistant['instructions'],
            'post_excerpt' => $assistant['description'],
            'post_status' => 'publish',
            'ID' => $original_assistant['ID']
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $local_assistant_id = wp_update_post($assistant_data);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($local_assistant_id))
        {
            $aiomatic_result['msg'] = $local_assistant_id->get_error_message();
        }
        elseif($local_assistant_id === 0)
        {
            $aiomatic_result['msg'] = 'Failed to update assistant to database: ' . $assistant['name'];
        }
        else 
        {
            update_post_meta($local_assistant_id, '_assistant_model', $assistant['model']);
            update_post_meta($local_assistant_id, '_assistant_tools', (array) $assistant['tools']);
            $file_ids = array();
            if(isset($assistant['tool_resources']['code_interpreter']['file_ids'][0]))
            {
                $file_ids = $assistant['tool_resources']['code_interpreter']['file_ids'];
            }
            update_post_meta($local_assistant_id, '_assistant_files', $file_ids);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['id'] = $local_assistant_id;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_sync_batch', 'aiomatic_sync_batch_ajax');
function aiomatic_sync_batch_ajax()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular batch request sync');
    if(!isset($_POST['batchid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('edit_posts') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $original_batch = get_post($_POST['batchid'], ARRAY_A);
        if($original_batch == null)
        {
            $aiomatic_result['msg'] = 'Batch Request id was not found!';
            wp_send_json($aiomatic_result);
        }
        $batch_id = get_post_meta($original_batch['ID'], '_batch_id', true);
        if(empty($batch_id))
        {
            $aiomatic_result['msg'] = 'OpenAI Batch Request id was not found!';
            wp_send_json($aiomatic_result);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
            $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
            wp_send_json($aiomatic_result);
        }
        else
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            if(empty($token))
            {
                $aiomatic_result['msg'] = 'Invalid API key submitted';
                wp_send_json($aiomatic_result);
            }
        }
        require_once (dirname(__FILE__) . "/res/aiomatic-batch-api.php"); 
        try
        {
            $batch = aiomatic_openai_retrieve_batch($token, $batch_id);
            if(!isset($batch['id']))
            {
                throw new Exception('Incorrect response from batch grabbing: ' . print_r($batch, true));
            }
        }
        catch(Exception $e)
        {
            $aiomatic_result['msg'] = 'Exception in batch grabbing: ' . $e->getMessage();
            wp_send_json($aiomatic_result);
        }
        $batch_data = array(
            'post_type' => 'aiomatic_batches',
            'post_title' => $batch['id'],
            'post_status' => 'publish',
            'ID' => $original_batch['ID']
        );
        remove_filter('content_save_pre', 'wp_filter_post_kses');
        remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');remove_filter('title_save_pre', 'wp_filter_kses');
        $local_batch_id = wp_update_post($batch_data);
        add_filter('content_save_pre', 'wp_filter_post_kses');
        add_filter('content_filtered_save_pre', 'wp_filter_post_kses');add_filter('title_save_pre', 'wp_filter_kses');
        if(is_wp_error($local_batch_id))
        {
            $aiomatic_result['msg'] = $local_batch_id->get_error_message();
        }
        elseif($local_batch_id === 0)
        {
            $aiomatic_result['msg'] = 'Failed to update assistant to database: ' . $assistant['name'];
        }
        else 
        {
            update_post_meta($local_batch_id, '_batch_id', $batch['id']);
            update_post_meta($local_batch_id, '_batch_endpoint', $batch['endpoint']);
            update_post_meta($local_batch_id, '_batch_completion_window', $batch['completion_window']);
            update_post_meta($local_batch_id, '_batch_errors', $batch['errors']);
            update_post_meta($local_batch_id, '_batch_input_file_id', $batch['input_file_id']);
            update_post_meta($local_batch_id, '_batch_status', $batch['status']);
            update_post_meta($local_batch_id, '_batch_output_file_id', $batch['output_file_id']);
            update_post_meta($local_batch_id, '_batch_created_at', $batch['created_at']);
            update_post_meta($local_batch_id, '_batch_in_progress_at', $batch['in_progress_at']);
            update_post_meta($local_batch_id, '_batch_expires_at', $batch['expires_at']);
            update_post_meta($local_batch_id, '_batch_finalizing_at', $batch['finalizing_at']);
            update_post_meta($local_batch_id, '_batch_completed_at', $batch['completed_at']);
            update_post_meta($local_batch_id, '_batch_failed_at', $batch['failed_at']);
            update_post_meta($local_batch_id, '_batch_expired_at', $batch['expired_at']);
            update_post_meta($local_batch_id, '_batch_cancelling_at', $batch['cancelling_at']);
            update_post_meta($local_batch_id, '_batch_cancelled_at', $batch['cancelled_at']);
            update_post_meta($local_batch_id, '_batch_request_count', $batch['request_counts']['total']);
            update_post_meta($local_batch_id, '_batch_request_completed', $batch['request_counts']['completed']);
            update_post_meta($local_batch_id, '_batch_request_failed', $batch['request_counts']['failed']);
            update_post_meta($local_batch_id, '_batch_error_file_id', $batch['error_file_id']);
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['id'] = $local_batch_id;
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_persona', 'aiomatic_delete_persona');
function aiomatic_delete_persona()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with singular persona deletion');
    if(!isset($_POST['personaid']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        check_ajax_referer('openai-ajax-nonce', 'nonce');
        if ( ! current_user_can('manage_options') ) {
            wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['id'] = $_POST['personaid'];
        $aiomatic_result['msg'] = 'Success';
        wp_delete_post($_POST['personaid']);
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_download_embeddings', 'aiomatic_download_embeddings');
function aiomatic_download_embeddings()
{
    global $wpdb;
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with embedding downloading');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $ids = $wpdb->get_results("SELECT ID FROM ".$wpdb->posts." WHERE post_type='aiomatic_embeddings'");
    $ids = wp_list_pluck($ids,'ID');
    $ret_arr = array();
    if(count($ids)) 
    {
        foreach($ids as $my_postid)
        {
            $content_post = get_post($my_postid);
            if(isset($content_post->post_content))
            {
                $ret_arr[] = array($content_post->post_content);
            }
        }
        if(count($ret_arr) > 0)
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['rows'] = $ret_arr;
        }
        else
        {
            $aiomatic_result['msg'] = 'No embeddings can be downloaded.';
        }
    }
    else
    {
        $aiomatic_result['msg'] = 'No embeddings found to download.';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_get_form', 'aiomatic_get_form');
function aiomatic_get_form()
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form getting');
    if(isset($_POST['id']) && !empty($_POST['id'])){
        $aiomatic_form = get_post(sanitize_text_field($_POST['id']));
        if($aiomatic_form)
        {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            $prompt = get_post_meta($aiomatic_form->ID, 'prompt', true);
            $assistant_id = get_post_meta($aiomatic_form->ID, 'assistant_id', true);
            if(empty($assistant_id))
            {
                $assistant_id = '';
            }
            $model = get_post_meta($aiomatic_form->ID, 'model', true);
            $header = get_post_meta($aiomatic_form->ID, 'header', true);
            $html = get_post_meta($aiomatic_form->ID, 'html', true);
            $editor = get_post_meta($aiomatic_form->ID, 'editor', true);
            $wait = get_post_meta($aiomatic_form->ID, 'wait_omniblock', true);
            $advanced = get_post_meta($aiomatic_form->ID, 'advanced', true);
            $submit = get_post_meta($aiomatic_form->ID, 'submit', true);
            $max = get_post_meta($aiomatic_form->ID, 'max', true);
            $temperature = get_post_meta($aiomatic_form->ID, 'temperature', true);
            $topp = get_post_meta($aiomatic_form->ID, 'topp', true);
            $presence = get_post_meta($aiomatic_form->ID, 'presence', true);
            $frequency = get_post_meta($aiomatic_form->ID, 'frequency', true);
            $response = get_post_meta($aiomatic_form->ID, 'response', true);
            $trigger_api_key = get_post_meta($aiomatic_form->ID, 'trigger_api_key', true);
            $trigger_id = get_post_meta($aiomatic_form->ID, 'trigger_id', true);
            $type = get_post_meta($aiomatic_form->ID, 'type', true);
            $streaming_enabled = get_post_meta($aiomatic_form->ID, 'streaming_enabled', true);
            $aiomaticfields = get_post_meta($aiomatic_form->ID, '_aiomaticfields', true);
            $omniblock = get_post_meta($aiomatic_form->ID, 'editor', true);
            if(!is_array($aiomaticfields))
            {
                $aiomaticfields = array();
            }
            if($html != '1')
            {
                $html = '0';
            }
            $aiomaticfields = array_values($aiomaticfields);
            $result = '<form action="#" method="post" id="aiomatic_forms_form_edit">
            <input type="hidden" name="action" value="aiomatic_forms">
            <input type="hidden" name="formid" value="' . esc_attr($aiomatic_form->ID) . '">
            <input type="hidden" name="nonce" value="' . wp_create_nonce('aiomatic_forms') . '">
            <div class="main-form-header-holder"><h2>' . esc_html__("Input Fields", 'aiomatic-automatic-ai-content-writer') . ':</h2><span class="header-al-right">' . esc_html__("Hide Input Fields", 'aiomatic-automatic-ai-content-writer') . '</span></div>
              <div class="main-form-holder">
              <button class="aiomatic-create-form-field button">' . esc_html__("Add A New Form Input Field", 'aiomatic-automatic-ai-content-writer') . '</button>
              <br/><br/>
              <div class="aiomatic-template-fields">';
              foreach($aiomaticfields as $inx => $aifield)
              {
                    $result .= '<div class="aiomatic-template-form-field">
                    <div class="aiomatic-template-form-field">
                    <div>
                       <div>
                             <strong class="aiomatic-label-top marginbottom-5">Label*<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
                             <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the input field Label (textual hint).", 'aiomatic-automatic-ai-content-writer') . '</div>
                             </div></strong>
                             <input type="text" name="aiomaticfields[' . $inx . '][label]" required placeholder="The label which will be shown next to the input field" value="' . (isset($aifield['label']) ? esc_attr($aifield['label']) : '') . '" class="aiomatic-create-template-field-label aiomatic-full-size">
                       </div>
                       <div>
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("ID*", 'aiomatic-automatic-ai-content-writer') . '<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
                             <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the input field ID. This is important, as you will be able to get the value entered by users on the front end for this input field, using this ID. You will be able to use this in the 'Prompt' settings field from below, in the following format: %%ID_YOU_ENTER_HERE%%.", 'aiomatic-automatic-ai-content-writer') . '</div>
                             </div></strong>
                             <input placeholder="my_unique_input_id" type="text" name="aiomaticfields[' . $inx . '][id]" required value="' . (isset($aifield['id']) ? esc_attr($aifield['id']) : '') . '" class="aiomatic-create-template-field-id aiomatic-full-size">
                             <small class="aiomatic-full-center">' . esc_html__("You can add the value of this field to the form prompt from below, using this shortcode", 'aiomatic-automatic-ai-content-writer') . ': <b>%%my_unique_input_id%%</b></small>
                       </div>
                       <div>
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Required*", 'aiomatic-automatic-ai-content-writer') . '<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
                             <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set this input field as required (form cannot be submitted unless this is filled up).", 'aiomatic-automatic-ai-content-writer') . '</div>
                             </div></strong>
                             <select name="aiomaticfields[' . $inx . '][required]" class="aiomatic-create-template-field-required aiomatic-full-size">
                                <option value="no"';
                                if(isset($aifield['required']) && $aifield['required'] == 'no')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>No</option>
                                <option value="yes"';
                                if(isset($aifield['required']) && $aifield['required'] == 'yes')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Yes</option>
                             </select>
                       </div>
                       <div>
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Field Type*", 'aiomatic-automatic-ai-content-writer') . '<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
                             <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the field type for this input field.", 'aiomatic-automatic-ai-content-writer') . '</div>
                             </div></strong>
                             <select name="aiomaticfields[' . $inx . '][type]" class="aiomatic-create-template-field-type aiomatic-full-size">
                                <option value="text"';
                                if(isset($aifield['type']) && $aifield['type'] == 'text')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Text</option>
                                <option value="select"';
                                if(isset($aifield['type']) && $aifield['type'] == 'select')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Drop-Down</option>
                                <option value="number"';
                                if(isset($aifield['type']) && $aifield['type'] == 'number')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Number</option>
                                <option value="range"';
                                if(isset($aifield['type']) && $aifield['type'] == 'range')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Range</option>
                                <option value="email"';
                                if(isset($aifield['type']) && $aifield['type'] == 'email')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Email</option>
                                <option value="url"';
                                if(isset($aifield['type']) && $aifield['type'] == 'url')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>URL</option>
                                <option value="scrape"';
                                if(isset($aifield['type']) && $aifield['type'] == 'scrape')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>URL Scraper</option>
                                <option value="textarea"';
                                if(isset($aifield['type']) && $aifield['type'] == 'textarea')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Textarea</option>
                                <option value="checkbox"';
                                if(isset($aifield['type']) && $aifield['type'] == 'checkbox')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Checkbox</option>
                                <option value="radio"';
                                if(isset($aifield['type']) && $aifield['type'] == 'radio')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Radio</option>
                                <option value="color"';
                                if(isset($aifield['type']) && $aifield['type'] == 'color')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Color</option>
                                <option value="date"';
                                if(isset($aifield['type']) && $aifield['type'] == 'date')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Date</option>
                                <option value="time"';
                                if(isset($aifield['type']) && $aifield['type'] == 'time')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Time</option>
                                <option value="datetime"';
                                if(isset($aifield['type']) && $aifield['type'] == 'datetime')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>DateTime</option>
                                <option value="month"';
                                if(isset($aifield['type']) && $aifield['type'] == 'month')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Month</option>
                                <option value="week"';
                                if(isset($aifield['type']) && $aifield['type'] == 'week')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Week</option>
                                <option value="file"';
                                if(isset($aifield['type']) && $aifield['type'] == 'file')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>File Upload</option>
                                <option value="html"';
                                if(isset($aifield['type']) && $aifield['type'] == 'html')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>HTML</option>
                                <option value="captcha"';
                                if(isset($aifield['type']) && $aifield['type'] == 'captcha')
                                {
                                    $result .= ' selected';
                                }
                                $result .= '>Captcha</option>
                             </select>
                       </div>
                       <div class="aiomatic-create-template-field-placeholder-main';
                       if(isset($aifield['type']) && ($aifield['type'] == 'captcha' || $aifield['type'] == 'html' || $aifield['type'] == 'radio' || $aifield['type'] == 'color' || $aifield['type'] == 'date' || $aifield['type'] == 'time' || $aifield['type'] == 'datetime' || $aifield['type'] == 'month' || $aifield['type'] == 'week' || $aifield['type'] == 'file' || $aifield['type'] == 'checkbox' || $aifield['type'] == 'select'))
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Placeholder Text", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Placeholder text" type="text" name="aiomaticfields[' . $inx . '][placeholder]" value="' . (isset($aifield['placeholder']) ? esc_attr($aifield['placeholder']) : '') . '" class="aiomatic-create-template-field-placeholder aiomatic-full-size">
                       </div>
                       <div class="aiomatic-create-template-field-limit-main';
                       if(isset($aifield['type']) && ($aifield['type'] == 'captcha' || $aifield['type'] == 'html' || $aifield['type'] == 'radio' || $aifield['type'] == 'color' || $aifield['type'] == 'date' || $aifield['type'] == 'time' || $aifield['type'] == 'datetime' || $aifield['type'] == 'month' || $aifield['type'] == 'week' || $aifield['type'] == 'checkbox' || $aifield['type'] == 'select'))
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Max Character Input Limit", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Max input character count" type="text" name="aiomaticfields[' . $inx . '][limit]" value="' . (isset($aifield['limit']) ? esc_attr($aifield['limit']) : '') . '" class="aiomatic-create-template-field-limit aiomatic-full-size">
                       </div>
                       <div class="aiomatic-create-template-field-min-main';
                       if(isset($aifield['type']) && $aifield['type'] != 'number' && $aifield['type'] != 'range')
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Min", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Minimum value (optional)" type="number" name="aiomaticfields[' . $inx . '][min]" value="' . (isset($aifield['min']) ? esc_attr($aifield['min']) : '') . '" class="aiomatic-create-template-field-min aiomatic-full-size">
                       </div>
                       <div class="aiomatic-create-template-field-max-main';
                       if(isset($aifield['type']) && $aifield['type'] != 'number' && $aifield['type'] != 'range')
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Max", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Maximum value (optional)" type="number" name="aiomaticfields[' . $inx . '][max]" value="' . (isset($aifield['max']) ? esc_attr($aifield['max']) : '') . '" class="aiomatic-create-template-field-max aiomatic-full-size">
                       </div>
                       <div class="aiomatic-create-template-field-rows-main';
                       if(isset($aifield['type']) && $aifield['type'] != 'textarea')
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Rows", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Textarea rows count (optional)" type="number" name="aiomaticfields[' . $inx . '][rows]" value="' . (isset($aifield['rows']) ? esc_attr($aifield['rows']) : '') . '" class="aiomatic-create-template-field-rows aiomatic-full-size">
                       </div>
                       <div class="aiomatic-create-template-field-cols-main';
                       if(isset($aifield['type']) && $aifield['type'] != 'textarea')
                       {
                           $result .= ' aiomatic-hidden-form';
                       }
                       $result .= '">
                             <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Cols", 'aiomatic-automatic-ai-content-writer') . '</strong>
                             <input placeholder="Textarea columns count (optional)" type="number" name="aiomaticfields[' . $inx . '][cols]" value="' . (isset($aifield['cols']) ? esc_attr($aifield['cols']) : '') . '" class="aiomatic-create-template-field-cols aiomatic-full-size">
                       </div>
                    </div>
                    <div class="aiomatic-create-template-field-options-main';
                    if(isset($aifield['type']) && $aifield['type'] != 'radio' && $aifield['type'] != 'file' && $aifield['type'] != 'checkbox' && $aifield['type'] != 'select' && $aifield['type'] != 'html')
                    {
                        $result .= ' aiomatic-hidden-form';
                    }
                    $result .= '">
                       <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Options", 'aiomatic-automatic-ai-content-writer') . '</strong>
                       <textarea name="aiomaticfields[' . $inx . '][options]" class="aiomatic-create-template-field-options aiomatic-full-size" placeholder="Possible values, separated by a new line">' . (isset($aifield['options']) ? esc_textarea($aifield['options']) : '') . '</textarea>
                    </div>
                    <div class="aiomatic-create-template-field-value-main">
                       <strong class="aiomatic-label-top marginbottom-5">' . esc_html__("Predefined Value", 'aiomatic-automatic-ai-content-writer') . '</strong>
                       <textarea name="aiomaticfields[' . $inx . '][value]" class="aiomatic-create-template-field-value aiomatic-full-size" placeholder="Predefined Value">' . (isset($aifield['value']) ? esc_textarea($aifield['value']) : '') . '</textarea>
                    </div>
                    <div class="aiomatic-form-controls">
                    <span class="aiomatic-field-up">' . esc_html__("Move Up", 'aiomatic-automatic-ai-content-writer') . '</span>&nbsp;
                    <span class="aiomatic-field-down">' . esc_html__("Move Down", 'aiomatic-automatic-ai-content-writer') . '</span>&nbsp;
                    <span class="aiomatic-field-delete">' . esc_html__("Delete", 'aiomatic-automatic-ai-content-writer') . '</span>&nbsp;
                    <span class="aiomatic-field-duplicate">' . esc_html__("Duplicate", 'aiomatic-automatic-ai-content-writer') . '</span>
                    </div>
                    </div>
                    </div>';
              }
              $result .= '</div></div>
              <hr/>
              <h2>' . esc_html__("Form Options", 'aiomatic-automatic-ai-content-writer') . ':</h2>
              <h4>' . esc_html__("Type*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the type of this form. If you select the 'OmniBlock Triggering Form' type, you must create an OmniBlock rule sequence, containing an OmniBlock of the 'AI Forms Trigger' type. You can use in the OmniBlocks following the 'AI Forms Trigger' OmniBlock, the following shortcode: %%ai_forms_full_input_OmniBlockID%%. Besides of this, you can use shortcodes with the IDs of the AI Form's input field names which triggered the OmniBlock sequence, in this format: %%inputID%%. For example, if you have an input field in your AI Form named colorx, you can use in OmniBlocks the following shortcode, to get the value of this input field: %%colorx%%. In this case, the AI form will always return the value provided by the last OmniBlock which is executed.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-type" id="aiomatic-edit-type" class="aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="text"';
                 if($type == 'text')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("Text", 'aiomatic-automatic-ai-content-writer') . '</option>
                 <option value="gpt-image-1"';
                 if($type == 'gpt-image-1')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("gpt-image-1", 'aiomatic-automatic-ai-content-writer') . '</option>
                 <option value="gpt-image-1-mini"';
                 if($type == 'gpt-image-1-mini')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("gpt-image-1-mini", 'aiomatic-automatic-ai-content-writer') . '</option>
                 <option value="gpt-image-1.5"';
                 if($type == 'gpt-image-1.5')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("gpt-image-1.5", 'aiomatic-automatic-ai-content-writer') . '</option>
                 <option value="gpt-image-2"';
                 if($type == 'gpt-image-2')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("gpt-image-2", 'aiomatic-automatic-ai-content-writer') . '</option>
                 <option value="chatgpt-image-latest"';
                 if($type == 'chatgpt-image-latest')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("chatgpt-image-latest", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 if (isset($aiomatic_Main_Settings['stability_app_id']) && trim($aiomatic_Main_Settings['stability_app_id']) != '') 
                 {
                    $result .= '<option value="image2"';
                    if($type == 'image2')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Stable Diffusion Image", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 if (isset($aiomatic_Main_Settings['midjourney_app_id']) && trim($aiomatic_Main_Settings['midjourney_app_id']) != '') 
                 {
                    $result .= '<option value="image-mid"';
                    if($type == 'image-mid')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Midjourney", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 if (isset($aiomatic_Main_Settings['replicate_app_id']) && trim($aiomatic_Main_Settings['replicate_app_id']) != '') 
                 {
                    $result .= '<option value="image-rep"';
                    if($type == 'image-rep')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Replicate", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 if (isset($aiomatic_Main_Settings['app_id_google']) && trim($aiomatic_Main_Settings['app_id_google']) != '') 
                 {
                    $result .= '<option value="image-goo"';
                    if($type == 'image-goo')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Google", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 if (isset($aiomatic_Main_Settings['ideogram_app_id']) && trim($aiomatic_Main_Settings['ideogram_app_id']) != '') 
                 {
                    $result .= '<option value="image-ide"';
                    if($type == 'image-ide')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Ideogram", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 if (isset($aiomatic_Main_Settings['cloudflare_worker_url']) && trim($aiomatic_Main_Settings['cloudflare_worker_url']) != '' && isset($aiomatic_Main_Settings['cloudflare_worker_secret']) && trim($aiomatic_Main_Settings['cloudflare_worker_secret']) != '') 
                 {
                    $result .= '<option value="image-cf"';
                    if($type == 'image-cf')
                    {
                       $result .= ' selected';
                    }
                    $result .= '>' . esc_html__("Cloudflare", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 }
                 $result .= '<option value="omniblock-form"';
                 if($type == 'omniblock-form')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>' . esc_html__("OmniBlock Triggering Form", 'aiomatic-automatic-ai-content-writer') . '</option>';
                 $result .= '</select>
              <br/>
              <h4 class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">' . esc_html__("AI Forms Trigger OmniBlock ID", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the OmniBlock Rule ID and AI Forms Trigger OmniBlock ID which will be called. Use this format: <Rule-ID>_<OmniBlock-ID> - example: 0_aF", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input id="aiomatic-trigger-id_edit" name="aiomatic-trigger-id" class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= ' aiomatic-full-size" placeholder="<Rule-ID>_<OmniBlock-ID>" value="' . esc_attr($trigger_id) . '">
              <br  class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '"/>
              <h4 class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">' . esc_html__("OmniBlock Trigger API Key", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the same API key as you set in your 'AI Forms Trigger' OmniBlock type.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input id="aiomatic-trigger-api-key_edit" name="aiomatic-trigger-api-key" class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= ' aiomatic-full-size" placeholder="Trigger API Key" value="' . esc_attr($trigger_api_key) . '">
              <br  class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '"/>
              <h4>' . esc_html__("Title*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the title of this form.", 'aiomatic-automatic-ai-content-writer'). '</div>
              </div></h4>
              <input id="aiomatic-form-title_edit" name="aiomatic-form-title" class="aiomatic-full-size" placeholder="Your form name" value="' . esc_attr($aiomatic_form->post_title) . '" required>
              <br/>
              <h4>' . esc_html__("Description", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the description of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <textarea id="aiomatic-form-description" name="aiomatic-form-description" class="aiomatic-full-size" placeholder="Your form description">' . esc_textarea($aiomatic_form->post_content) . '</textarea>
              <br/>
              <h4>' . esc_html__("Prompt*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the prompt which will be sent to the AI content writer. You can use shortcodes to get the input values entered by users in the form. The shortcodes need to be in the following format: %%ID_of_the_input_field%% - New feature: List of additional shortcodes: %%post_title%%, %%post_content%%, %%post_content_plain_text%%, %%post_excerpt%%, %%post_cats%%, %%post_tags%%, %%featured_image%%, %%blog_title%%, %%author_name%%, %%current_date_time%%, %%post_link%%, %%random_sentence%%, %%random_sentence2%%, %%user_name%%, %%user_email%%, %%user_display_name%%, %%user_role%%, %%user_id%%, %%user_firstname%%, %%user_lastname%%, %%user_url%%, %%user_description%%. You can also use custom fields (post meta) that it's assigned to posts using custom shortcodes in this format: %%!custom_field_slug!%%. You can also use custom user meta fields (user meta) which is assigned to users using custom shortcodes in this format: %%~custom_field_slug~%%. Example: if you wish to add data that is imported from the custom field post_data, you should use this shortcode: %%!post_data!%%. The length of this command should not be greater than the max token count set in the settings for the seed command - Update: %%related_questions_KEYWORD%% is also supported, to get a list of PAA questions for the KEYWORD you want to use. Update: nested shortcodes also supported (shortcodes generated by rules from other plugins).", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <textarea id="aiomatic-form-prompt_edit" name="aiomatic-form-prompt" class="aiomatic-full-size" placeholder="The prompt which will be sent to the AI content writer" required>' . esc_textarea($prompt) . '</textarea>
              <br/>
              <h4>' . esc_html__("Sample Response", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set an example response for this form, this can be shown to users.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <textarea name="aiomatic-form-response" id="aiomatic-form-response" class="aiomatic-full-size" placeholder="A sample response to show for this form">' . esc_textarea($response) . '</textarea>
              <hr/>
              <div class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">
              <h2>' . esc_html__("AI Model Options", 'aiomatic-automatic-ai-content-writer') . ':</h2>
              <h4>' . esc_html__("AI Assistant ID*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select the AI assistant to be used for this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-form-assistant-id" class="aiomatic-create-template-field-type aiomatic-full-size">';
$all_assistants = aiomatic_get_all_assistants();
if($all_assistants === false)
{
    $result .= '<option val="" selected disabled>' . esc_html__("Only OpenAI API is supported for Assistants API", 'aiomatic-automatic-ai-content-writer') . '</option>';
}
else
{
    if(count($all_assistants) == 0)
    {
        $result .= '<option val="" selected disabled>' . esc_html__("No Assistans added, go to the plugin's 'AI Assistans' menu to add new assistants!", 'aiomatic-automatic-ai-content-writer') . '</option>';
    }
    else
    {
        $result .= '<option value=""';
        if($assistant_id == '')
        {
            $result .= ' selected';
        }
        $result .= '>' . esc_html__("Don't use assistants, use AI models instead", 'aiomatic-automatic-ai-content-writer') . '</option>';
        foreach($all_assistants as $myassistant)
        {
            $result .= '<option value="' . $myassistant->ID .'"';
            if($assistant_id == $myassistant->ID)
            {
                $result .= ' selected';
            }
            $result .= '>' . esc_html($myassistant->post_title);
            $result .= '</option>';
        }
    }
}
$result .= '</select>
              <br/>
              <h4>' . esc_html__("AI Model*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select the AI model to be used for this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-form-model" class="aiomatic-create-template-field-type aiomatic-full-size">';
$all_models = aiomatic_get_all_models();
foreach($all_models as $modl)
{
    $result .= '<option value="' . $modl . '"';
    if($modl == $model)
    {
        $result .= ' selected';
    }
    $result .= '>' . $modl . esc_html(aiomatic_get_model_provider($modl)) . '</option>';
}
$result .= '</select>
            <br/>
            <h4>' . esc_html__("Response Streaming*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
            <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to enable response streaming for your AI form.", 'aiomatic-automatic-ai-content-writer') . '</div>
            </div></h4>
            <select name="aiomatic-form-stream" class="aiomatic-create-template-field-type aiomatic-full-size"><option value="stream"';
            if('stream' === $streaming_enabled)
            {
            $result .= ' selected';
            }
            $result .= '>' . esc_html__("Enabled", 'aiomatic-automatic-ai-content-writer') . '</option>
            <option value="0"';
            if('stream' !== $streaming_enabled)
            {
            $result .= ' selected';
            }
            $result .= '>' . esc_html__("Disabled", 'aiomatic-automatic-ai-content-writer') . '</option></select>
              <br/>
              <br/>
              <button class="aiomatic-show-hide-field button">' . esc_html__("Show/Hide Advanced Model Settings", 'aiomatic-automatic-ai-content-writer') . '</button>
              <br/>
              <div class="aiomatic-hidden-form" id="hideAdv_edit">
              <h4>' . esc_html__("Max Token Count", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the AI maximum token count of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input type="number" min="1" step="1" name="aiomatic-max" value="' . esc_attr($max) . '" placeholder="Maximum token count to be used" class="cr_width_full">
              <br/>
              <h4>' . esc_html__("Temperature", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the AI temperature of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input type="number" min="0" step="0.01" max="2" name="aiomatic-temperature" value="' . esc_attr($temperature) . '" placeholder="AI Temperature" class="cr_width_full">
              <br/>
              <h4>' . esc_html__("Top_p", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the AI top_p parameter of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input type="number" min="0" max="1" step="0.01" name="aiomatic-topp" value="' . esc_attr($topp) . '" placeholder="AI Top_p" class="cr_width_full">
              <br/>
              <h4>' . esc_html__("Presence Penalty", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the AI presence penalty parameter of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input type="number" min="-2" step="0.01" max="2" name="aiomatic-presence" value="' . esc_attr($presence) . '" placeholder="AI Presence Penalty" class="cr_width_full">
              <br/>
              <h4>' . esc_html__("Frequency Penalty", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the AI frequency penalty parameter of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input type="number" min="0" max="2" step="0.01" name="aiomatic-frequency" value="' . esc_attr($frequency) . '" placeholder="AI Frequency penalty" class="cr_width_full">
              </div>
              <hr/>
              </div>
              <h2>' . esc_html__("Front End Options", 'aiomatic-automatic-ai-content-writer') . ':</h2>
              <h4>' . esc_html__("Allow HTML Responses*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to allow the rendering of HTML responses in the forms.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-html" class="aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="0"';
                 if($html == '0')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Don\'t Allow</option>
                 <option value="1"';
                 if($html == '1')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Allow</option>
              </select>
              <br/>
              <h4>' . esc_html__("Show Header On Front End*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to show the form header to users.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-header" class="aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="show"';
                 if($header == 'show')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Show</option>
                 <option value="hide"';
                 if($header == 'hide')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Hide</option>
              </select>
              <br/>
              <h4 class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">' . esc_html__("Display AI Form Results In", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to show the form results in a modern WP Editor instead of a plain textarea.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-editor" class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= ' aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="textarea"';
                 if($editor == 'textarea')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Textarea</option>
                 <option value="wpeditor"';
                 if($editor == 'wpeditor')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>WP Editor</option>
              </select>
              <h4 class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">' . esc_html__("Wait For OmniBlock Return Value*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to show the OmniBlock's return text in the form result field.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-form-wait-omniblock" class="aiomatic-hide-omniblock-form';
              if($type != 'omniblock-form')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= ' aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="yes"';
                 if($wait == 'yes')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Yes</option>
                 <option value="no"';
                 if($wait == 'no')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>No</option>
              </select>
              <br class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '"/>
              <h4 class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '">' . esc_html__("Show Advanced Form Options", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Select if you want to show the advanced form options to users.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <select name="aiomatic-advanced" class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= ' aiomatic-create-template-field-type aiomatic-full-size">
                 <option value="hide"';
                 if($advanced == 'hide')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Hide</option>
                 <option value="show"';
                 if($advanced == 'show')
                 {
                    $result .= ' selected';
                 }
                 $result .= '>Show</option>
              </select>
              <br class="aiomatic-hide-not-text';
              if($type != 'text')
              {
                $result .= ' aiomatic-hidden-form';
              }
              $result .= '"/>
              <h4>' . esc_html__("Submit Button Text*", 'aiomatic-automatic-ai-content-writer') . ':<div class="bws_help_box bws_help_box_right dashicons dashicons-editor-help cr_align_middle">
              <div class="bws_hidden_help_text cr_min_260px">' . esc_html__("Set the submit button text of this form.", 'aiomatic-automatic-ai-content-writer') . '</div>
              </div></h4>
              <input id="aiomatic-submit_edit" name="aiomatic-submit" value="' . esc_attr($submit) . '" class="aiomatic-full-size" placeholder="Submit" required>
            <br/><br/>
            <button type="submit" id="aiomatic-form-save-button_edit" class="button button-primary">' . esc_html__("Save", 'aiomatic-automatic-ai-content-writer') . '</button>
           <div class="aiomatic-forms-success"></div>
        </form>';
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['data'] = $result;
        }
        else{
            $aiomatic_result['msg'] = 'Form not found';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_scrape_url_embeddings', 'aiomatic_scrape_url_embeddings');
function aiomatic_scrape_url_embeddings()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with embeddings scraping');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['xurl']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
        {
            $namespace = '';
            if(isset($_POST['namespace']))
            {
                $namespace = $_POST['namespace'];
            }
            $file_data = aiomatic_scrape_page(trim($_POST['xurl']), '0', 'auto', '');
            if($file_data === false)
            {
                $aiomatic_result['msg'] = 'Incorrect AJAX call';
            }
            else
            {
                $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
                $appids = array_filter($appids);
                if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
                $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
                require_once(dirname(__FILE__) . "/res/Embeddings.php");
                $embdedding = new Aiomatic_Embeddings($token);
                $aiomatic_result = $embdedding->aiomatic_create_single_embedding_nojson(stripslashes($file_data), $namespace);
            }
        }
        else
        {
            $aiomatic_result['msg'] = 'Please set up API key';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_embeddings_upload', 'aiomatic_embeddings_upload');
function aiomatic_embeddings_upload()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with embeddings uploading');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['xfile']) || !isset($_POST['filename']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        $namespace = '';
        if(isset($_POST['namespace']))
        {
            $namespace = sanitize_text_field($_POST['namespace']);
        }
        $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
        if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
        {
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            $file_name = sanitize_file_name($_POST['filename']);
            $filetype  = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
            $raw_data = base64_decode($_POST['xfile']);
            $embedtext = '';
            if($filetype == 'pdf')
            {
                if(!function_exists('is_plugin_active'))
                {
                    include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
                }
                if (!is_plugin_active('aiomatic-extension-pdf-files/aiomatic-extension-pdf-files.php')) 
                {
                    $aiomatic_result['msg'] = esc_html__("To create embeddings from PDF files, you need the Aimogen Extension - PDF File Storage And Parsing to be active.", 'aiomatic-automatic-ai-content-writer');
                    wp_send_json($aiomatic_result);
                    die();
                }
            }
            switch($filetype) {
                case 'csv':
                    $lines = [];
                    $rows = preg_split('/\r\n|\r|\n/', $raw_data);
                    foreach ($rows as $row) {
                        $cols = str_getcsv($row, ',');
                        $lines[] = implode(' ', $cols);
                    }
                    $embedtext = implode("\n", $lines);
                    break;
                case 'txt':
                    $embedtext = aiomatic_safe_utf8($raw_data);
                    break;
                case 'json':
                    $json_string = aiomatic_safe_utf8($raw_data);
                    $data = json_decode($json_string, true);
                    $lines = [];

                    if (json_last_error() === JSON_ERROR_NONE && is_array($data)) {
                        foreach ($data as $item) {
                            if (is_array($item)) {
                                $lines[] = implode(' ', array_map('wp_strip_all_tags', $item));
                            } else {
                                $lines[] = wp_strip_all_tags((string)$item);
                            }
                        }
                    } else {
                        $lines = preg_split('/\r\n|\r|\n/', $json_string);
                    }
                    $lines = array_map('trim', $lines);
                    $lines = array_filter($lines);
                    $embedtext = implode("\n", $lines);
                    break;

                case 'pdf':
                    $text = '';
                    if (class_exists('\Smalot\PdfParser\Parser')) {
                        try {
                            $parser = new \Smalot\PdfParser\Parser();
                            $pdf = $parser->parseContent($raw_data);
                            $text = $pdf->getText();
                        } catch (\Exception $e) {
                            $text = '';
                        }
                    }
                    if (empty(trim($text)) && class_exists('PdfToText')) {
                        try {
                            $pdf = new PdfToText();
                            $pdf->LoadFromString($raw_data);
                            $text = $pdf->Text;
                        } catch (\Exception $e) {
                            $text = '';
                        }
                    }
                    if (empty(trim($text)) && class_exists('\Com\Tecnick\Pdf\Parser\Parser')) {
                        try {
                            $cfg = ['ignore_filter_errors' => true];
                            $parser = new \Com\Tecnick\Pdf\Parser\Parser($cfg);
                            $objects = $parser->parse($raw_data);
                            $pdfText = '';
                            foreach ($objects as $object) {
                                $pdfText .= aiomatic_extractTextFromObject($object);
                            }
                            $text = $pdfText;
                        } catch (\Exception $e) {
                            $text = '';
                        }
                    }
                    if (empty(trim($text))) {
                        wp_send_json([
                            'status' => 'error',
                            'msg' => 'No textual data could be extracted from the PDF file'
                        ]);
                    }
                    $lines = preg_split('/\r\n|\r|\n/', $text);
                    $lines = array_map('trim', $lines);
                    $lines = array_filter($lines);
                    $embedtext = implode("\n", $lines);
                    $embedtext = aiomatic_safe_utf8($embedtext);
                    break;
                case 'docx':
                    $tmp = wp_tempnam();
                    global $wp_filesystem;
                    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
                        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
                        wp_filesystem($creds);
                    }
                    $wp_filesystem->put_contents($tmp, $raw_data);
                    $zip = new ZipArchive;
                    $text = '';
                    if ($zip->open($tmp) === true) {
                        $xml = $zip->getFromName('word/document.xml');
                        $text = strip_tags($xml);
                        $zip->close();
                    }
                    unlink($tmp);
                    $lines = preg_split('/\r\n|\r|\n/', $text);
                    $lines = array_map('trim', $lines);
                    $lines = array_filter($lines);
                    $embedtext = implode("\n", $lines);
                    $embedtext = aiomatic_safe_utf8($embedtext);
                    break;

                default:
                    wp_send_json(['status'=>'error','msg'=>'Unsupported file type']);
            }

            require_once(dirname(__FILE__) . "/res/Embeddings.php");
            $embdedding = new Aiomatic_Embeddings($token);
            $aiomatic_result = $embdedding->aiomatic_create_embeddings($embedtext, $namespace);
        }
        else
        {
            $aiomatic_result['msg'] = 'Please set up API key';
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_deleteall_embedding', 'aiomatic_deleteall_embedding');
function aiomatic_deleteall_embedding()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with general embeddings deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        require_once(dirname(__FILE__) . "/res/Embeddings.php");
        $embdedding = new Aiomatic_Embeddings($token);
        $aiomatic_result = $embdedding->aiomatic_deleteall_embeddings();
    }
    else
    {
        $aiomatic_result['msg'] = 'Please set up API key for embeddings';
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_delete_selected_embedding', 'aiomatic_delete_selected_embedding');
function aiomatic_delete_selected_embedding()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with embeddings deletion');
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['ids']))
    {
        $aiomatic_result['msg'] = 'Incorrect AJAX call';
    }
    else
    {
        if(count($_POST['ids'])) {
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if (isset($aiomatic_Main_Settings['app_id']) && trim($aiomatic_Main_Settings['app_id']) != '') 
            {
                $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
                $appids = array_filter($appids);
                if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
                $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
                require_once(dirname(__FILE__) . "/res/Embeddings.php");
                $embdedding = new Aiomatic_Embeddings($token);
                $aiomatic_result = $embdedding->aiomatic_delete_embeddings_ids($_POST['ids']);
            }
            else
            {
                $aiomatic_result['msg'] = 'Please set up API key for embeddings deletion';
            }
        }
    }
    wp_send_json($aiomatic_result);
    die();
}
add_action('wp_ajax_aiomatic_erase_action', 'aiomatic_erase_action');
function aiomatic_erase_action()
{
    check_ajax_referer('openai-run-nonce', 'nonce');
    if ( ! current_user_can('manage_options') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $param = '';
    if(isset($_POST['id']))
    {
        $param = $_POST['id'];
    }
    else
    {
        aiomatic_log_to_file('Incorrect POST request sent');
        echo 'fail';
        die();
    }
    $rules = get_option('aiomatic_omni_list', array());
    if (!empty($rules)) 
    {
        $found = 0;
        $cont = 0;
        $main_keywords = '';
        foreach ($rules as $request => $bundle[]) 
        {
            if ($cont == $param) 
            {
                $bundle_values    = array_values($bundle);
                $myValues         = $bundle_values[$cont];
                $array_my_values  = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
                $main_keywords    = isset($array_my_values[4]) ? $array_my_values[4] : '';
                $found            = 1;
                break;
            }
            $cont = $cont + 1;
        }
        if($found === 0)
        {
            aiomatic_log_to_file('Rule ID not found in rules list: ' . $param);
            echo 'fail';
            die();
        }
        $keyword_arr = preg_split('/\r\n|\r|\n/', trim($main_keywords));
        aiomatic_remove_processed_keywords($keyword_arr);
        echo 'ok';
        die();
    } 
    else 
    {
        aiomatic_log_to_file('No rules found for aiomatic_omni_list!');
        echo 'fail';
        die();
    }
}
add_action('wp_ajax_aiomatic_my_action', 'aiomatic_my_action_callback');
function aiomatic_my_action_callback()
{
    check_ajax_referer('openai-run-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    $failed                 = false;
    $del_id                 = $_POST['id'];
    if(isset($_POST['type']))
    {
        $type                   = $_POST['type'];
    }
    else
    {
        $type                   = '0';
    }
    $how                    = $_POST['how'];
    if($how == 'duplicate')
    {
        if($type == 0)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_rules_list', 'options');
            if (!get_option('aiomatic_rules_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_rules_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[109] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_rules_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_rules_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_rules_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 1)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_youtube_list', 'options');
            if (!get_option('aiomatic_youtube_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_youtube_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[97] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_youtube_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_youtube_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_youtube_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 2)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_amazon_list', 'options');
            if (!get_option('aiomatic_amazon_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_amazon_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[94] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_amazon_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_amazon_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_amazon_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 3)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_review_list', 'options');
            if (!get_option('aiomatic_review_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_review_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[88] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_review_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_review_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_review_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 4)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_csv_list', 'options');
            if (!get_option('aiomatic_csv_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_csv_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[31] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_csv_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_csv_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_csv_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 5)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
            if (!get_option('aiomatic_omni_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_omni_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[7] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_omni_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_omni_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_omni_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 6)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_listicle_list', 'options');
            if (!get_option('aiomatic_listicle_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_listicle_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[95] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_listicle_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_listicle_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_listicle_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 7)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_forum_list', 'options');
            if (!get_option('aiomatic_forum_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_forum_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[37] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_forum_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_forum_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_forum_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 8)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_reply_list', 'options');
            if (!get_option('aiomatic_reply_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_reply_list');
            }
            if (!empty($rules)) {
                $found            = 0;
                $cont = 0;
                foreach ($rules as $request => $bundle[]) {
                    if ($cont == $del_id) {
                        $copy_bundle = $rules[$request];
                        $copy_bundle[2] = '1988-01-27 00:00:00';
                        $copy_bundle[22] = uniqid('', true);
                        $rules[] = $copy_bundle;
                        $found   = 1;
                        break;
                    }
                    $cont = $cont + 1;
                }
                if($found == 0)
                {
                    aiomatic_log_to_file('aiomatic_reply_list index not found: ' . $del_id);
                    echo 'nochange';
                    die();
                }
                else
                {
                    aiomatic_update_option('aiomatic_reply_list', $rules, false);
                    echo 'ok';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_reply_list empty!');
                echo 'nochange';
                die();
            }
        }
        else
        {
            aiomatic_log_to_file('Unknown type submitted: ' . $type);
            echo 'nochange';
            die();
        }
    }
    $force_delete           = true;
    $number                 = 0;
    if ($how == 'trash') 
    {
        $force_delete = false;
    }
    else
    {
        if($type == 7)
        {
            if(function_exists('aiomatic_delete_wpforo_topics_by_meta'))
            {
                $returns = aiomatic_delete_wpforo_topics_by_meta('aiomatic_parent_rule', $del_id);
                if(is_wp_error($returns))
                {
                    aiomatic_log_to_file('Error deleting wpForo topics for rule id ' . esc_html($del_id) . ': ' . $returns->get_error_message());
                    echo 'fail';
                    die();
                }
                else
                {
                    echo esc_html($returns);
                    die();
                }
            }
            else
            {
                aiomatic_log_to_file('Aimogen\'s wpForo integration is not active!');
                echo 'fail';
                die();
            }
        }
        if($type == 5)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
            if (!get_option('aiomatic_omni_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_omni_list');
            }
            $cont = 0;
            foreach ($rules as $request => $bundle) 
            {
                if (isset($bundle[7]) && $bundle[7] == $del_id && isset($bundle[4])) 
                {
                    $keyword_arr = preg_split('/\r\n|\r|\n/', trim($bundle[4]));
                    aiomatic_remove_processed_keywords($keyword_arr);
                    break;
                }
                $cont = $cont + 1;
            }
        }
    }
    $post_list = array();
    $postsPerPage = 50000;
    $paged = 0;
    do
    {
        $postOffset = $paged * $postsPerPage;
        $query = array(
            'post_status' => array(
                'publish',
                'draft',
                'pending',
                'trash',
                'private',
                'future'
            ),
            'post_type' => array(
                'any'
            ),
            'numberposts' => $postsPerPage,
            'meta_key' => 'aiomatic_parent_rule',
            'fields' => 'ids',
            'offset'  => $postOffset
        );
        $got_me = get_posts($query);
        $post_list = array_merge($post_list, $got_me);
        $paged++;
    }while(!empty($got_me));
    wp_suspend_cache_addition(true);
    foreach ($post_list as $post) {
        $index = get_post_meta($post, 'aiomatic_parent_rule', true);
        if ($index == $type . '-' . $del_id || $index == $del_id) 
        {
            $args             = array(
                'post_parent' => $post
            );
            $post_attachments = get_children($args);
            if (isset($post_attachments) && !empty($post_attachments)) {
                foreach ($post_attachments as $attachment) {
                    wp_delete_attachment($attachment->ID, true);
                }
            }
            $res = wp_delete_post($post, $force_delete);
            if ($res === false) {
                $failed = true;
            } else {
                $number++;
            }
        }
    }
    wp_suspend_cache_addition(false);
    if ($failed === true) {
        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
            aiomatic_log_to_file('[PostDelete] Failed to delete all posts for rule id: ' . esc_html($del_id) . '!');
        }
        echo 'failed';
    } else {
        if (isset($aiomatic_Main_Settings['enable_detailed_logging'])) {
            aiomatic_log_to_file('[PostDelete] Successfuly deleted ' . esc_html($number) . ' posts for rule id: ' . esc_html($del_id) . '!');
        }
        if ($number == 0) {
            echo 'nochange';
        } else {
            echo 'ok';
        }
    }
    die();
}

add_action('wp_ajax_aiomatic_my_action_move', 'aiomatic_my_action_move_callback');
function aiomatic_my_action_move_callback()
{
    check_ajax_referer('openai-run-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $del_id                 = $_POST['id'];
    if(isset($_POST['type']))
    {
        $type                   = $_POST['type'];
    }
    else
    {
        $type                   = '0';
    }
    $how                    = $_POST['how'];
    if($how == 'up')
    {
        if($type == 0)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_rules_list', 'options');
            if (!get_option('aiomatic_rules_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_rules_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_rules_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_rules_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_rules_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 1)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_youtube_list', 'options');
            if (!get_option('aiomatic_youtube_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_youtube_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_youtube_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_youtube_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_youtube_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 2)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_amazon_list', 'options');
            if (!get_option('aiomatic_amazon_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_amazon_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_amazon_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_amazon_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_amazon_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 3)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_review_list', 'options');
            if (!get_option('aiomatic_review_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_review_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_review_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_review_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_review_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 4)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_csv_list', 'options');
            if (!get_option('aiomatic_csv_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_csv_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_csv_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_csv_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_csv_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 5)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
            if (!get_option('aiomatic_omni_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_omni_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_omni_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_omni_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_omni_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 6)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_listicle_list', 'options');
            if (!get_option('aiomatic_listicle_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_listicle_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_listicle_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_listicle_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_listicle_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 7)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_forum_list', 'options');
            if (!get_option('aiomatic_forum_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_forum_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_forum_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_forum_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_forum_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 8)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_reply_list', 'options');
            if (!get_option('aiomatic_reply_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_reply_list');
            }
            if (!empty($rules)) {
                if ($del_id > 0 && $del_id < count($rules)) {
                    $temp = $rules[$del_id - 1];
                    $rules[$del_id - 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    aiomatic_update_option('aiomatic_reply_list', $rules, false);
                    echo 'ok';
                    die();
                }
                else {
                    aiomatic_log_to_file('aiomatic_reply_list index out of bounds for move up: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_reply_list empty!');
                echo 'nochange';
                die();
            }
        }
        else
        {
            aiomatic_log_to_file('Unknown type submitted: ' . $type);
            echo 'nochange';
            die();
        }
    }
    elseif($how == 'down')
    {
        if($type == 0)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_rules_list', 'options');
            if (!get_option('aiomatic_rules_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_rules_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_rules_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_rules_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_rules_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 1)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_youtube_list', 'options');
            if (!get_option('aiomatic_youtube_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_youtube_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_youtube_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_youtube_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_youtube_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 2)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_amazon_list', 'options');
            if (!get_option('aiomatic_amazon_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_amazon_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_amazon_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_amazon_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_amazon_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 3)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_review_list', 'options');
            if (!get_option('aiomatic_review_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_review_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_review_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_review_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_review_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 4)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_csv_list', 'options');
            if (!get_option('aiomatic_csv_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_csv_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_csv_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_csv_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_csv_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 5)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_omni_list', 'options');
            if (!get_option('aiomatic_omni_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_omni_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_omni_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_omni_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_omni_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 6)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_listicle_list', 'options');
            if (!get_option('aiomatic_listicle_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_listicle_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_listicle_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_listicle_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_listicle_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 7)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_forum_list', 'options');
            if (!get_option('aiomatic_forum_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_forum_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_forum_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_forum_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_forum_list empty!');
                echo 'nochange';
                die();
            }
        }
        elseif($type == 8)
        {
            $GLOBALS['wp_object_cache']->delete('aiomatic_reply_list', 'options');
            if (!get_option('aiomatic_reply_list')) {
                $rules = array();
            } else {
                $rules = get_option('aiomatic_reply_list');
            }
            if (!empty($rules)) {
                if ($del_id >= 0 && $del_id < count($rules) - 1) {
                    $temp = $rules[$del_id + 1];
                    $rules[$del_id + 1] = $rules[$del_id];
                    $rules[$del_id] = $temp;
                    
                    aiomatic_update_option('aiomatic_reply_list', $rules, false);
                    echo 'ok';
                    die();
                } else {
                    aiomatic_log_to_file('aiomatic_reply_list index out of bounds for move down: ' . $del_id);
                    echo 'nochange';
                    die();
                }
            } else {
                aiomatic_log_to_file('aiomatic_reply_list empty!');
                echo 'nochange';
                die();
            }
        }
        else
        {
            aiomatic_log_to_file('Unknown type submitted: ' . $type);
            echo 'nochange';
            die();
        }
    }
    else
    {
        aiomatic_log_to_file('Unknown action submitted: ' . $how);
        echo 'nochange';
        die();
    }
    die();
}
add_action('wp_ajax_aiomatic_delete_rules_bulk','aiomatic_delete_rules_bulk_callback');
function aiomatic_delete_rules_bulk_callback()
{
    check_ajax_referer('openai-run-nonce','nonce');

    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error('Unauthorized',403);
    }

    $ids  = isset($_POST['ids']) ? (array)$_POST['ids'] : array();
    $type = isset($_POST['type']) ? intval($_POST['type']) : 0;

    $lists = array(
        0=>'aiomatic_rules_list',
        1=>'aiomatic_youtube_list',
        2=>'aiomatic_amazon_list',
        3=>'aiomatic_review_list',
        4=>'aiomatic_csv_list',
        5=>'aiomatic_omni_list',
        6=>'aiomatic_listicle_list',
        7=>'aiomatic_forum_list',
        8=>'aiomatic_reply_list',
    );

    if(!isset($lists[$type]))
        wp_die('nochange');

    $option_name = $lists[$type];

    $rules = get_option($option_name,array());

    if(empty($rules))
        wp_die('nochange');

    foreach($ids as $id)
    {
        $id = intval($id);

        if(isset($rules[$id]))
            unset($rules[$id]);
    }

    $rules = array_values($rules);

    aiomatic_update_option($option_name,$rules,false);

    echo 'ok';
    wp_die();
}
add_action('wp_ajax_aiomatic_toggle_rules_bulk', 'aiomatic_toggle_rules_bulk_callback');
function aiomatic_toggle_rules_bulk_callback()
{
    check_ajax_referer('openai-run-nonce', 'nonce');

    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error(array('message'=>'Unauthorized'),403);
    }

    $rule_ids = isset($_POST['ids']) ? (array)$_POST['ids'] : array();
    $type     = isset($_POST['type']) ? intval($_POST['type']) : 0;
    $state    = isset($_POST['state']) ? intval($_POST['state']) : 0;

    if(empty($rule_ids))
        wp_send_json_error('no rules');

    $lists = array(
        0=>'aiomatic_rules_list',
        1=>'aiomatic_youtube_list',
        2=>'aiomatic_amazon_list',
        3=>'aiomatic_review_list',
        4=>'aiomatic_csv_list',
        5=>'aiomatic_omni_list',
        6=>'aiomatic_listicle_list',
        7=>'aiomatic_forum_list',
        8=>'aiomatic_reply_list',
    );

    if(!isset($lists[$type]))
        wp_send_json_error('invalid type');

    $option_name = $lists[$type];

    $rules = get_option($option_name, array());

    if(empty($rules))
        wp_send_json_error('empty');

    foreach($rule_ids as $id)
    {
        $id = intval($id);

        if(isset($rules[$id]))
            $rules[$id][1] = $state ? '1' : '0';
    }

    aiomatic_update_option($option_name, $rules, false);

    wp_send_json_success();
}
add_action('wp_ajax_aiomatic_run_my_action', 'aiomatic_run_my_action_callback');
function aiomatic_run_my_action_callback()
{
    check_ajax_referer('openai-run-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['id']))
    {
        die();
    }
    $run_id = $_POST['id'];
    if(isset($_POST['type']))
    {
        $type                   = $_POST['type'];
    }
    else
    {
        $type                   = 0;
    }
    echo esc_html(aiomatic_run_rule($run_id, $type, 0, 0, null, '', '', false, []));
    die();
}
add_action('wp_ajax_aiomatic_run_omniblock', 'aiomatic_run_omniblock_callback');
function aiomatic_run_omniblock_callback()
{
    check_ajax_referer('openai-omni-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if(!isset($_POST['id']) || !isset($_POST['uniquid']))
    {
        die();
    }
    $run_id = $_POST['id'];
    if(isset($_POST['type']))
    {
        $type                   = $_POST['type'];
    }
    else
    {
        $type                   = 0;
    }
    $uniquid = $_POST['uniquid'];
    $rezult = aiomatic_run_rule($run_id, $type, 0, 0, null, $uniquid, '', false, []);
    if(is_array($rezult))
    {
        echo json_encode($rezult);
    }
    else
    {
        echo esc_html($rezult);
    }
    die();
}
add_action('wp_ajax_nopriv_aiomatic_editor', 'aiomatic_editor');
add_action('wp_ajax_aiomatic_editor', 'aiomatic_editor');
function aiomatic_editor() {
	check_ajax_referer('wp_rest', 'nonce');
	$aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['assistant_not_logged']) || $aiomatic_Main_Settings['assistant_not_logged'] == 'disable')
    {
        if(!is_user_logged_in())
        {
            wp_send_json_error( array( 'message' => esc_html__("You need to log in to perform this action!", 'aiomatic-automatic-ai-content-writer') ) );
        }
    }
    if(!isset($_POST['prompt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prompt)' ) );
	}
	$prompt = stripslashes(sanitize_text_field( $_POST['prompt'] ));
    if (isset($aiomatic_Main_Settings['assistant_disable']) && $aiomatic_Main_Settings['assistant_disable'] == 'on')
    {
        wp_send_json_error( array( 'message' => 'Assistant disabled in plugin settings' ) );
    }
    if (!isset($aiomatic_Main_Settings['aiomatic_enabled']) || $aiomatic_Main_Settings['aiomatic_enabled'] != 'on')
    {
        wp_send_json_error( array( 'message' => 'Aimogen plugin disabled' ) );
    }
    if(isset($aiomatic_Main_Settings['assistant_model']) && $aiomatic_Main_Settings['assistant_model'] != '')
    {
        $model = $aiomatic_Main_Settings['assistant_model'];
    }
    else
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
    if(isset($aiomatic_Main_Settings['wizard_assistant_id']) && $aiomatic_Main_Settings['wizard_assistant_id'] != '')
    {
        $wizard_assistant_id = $aiomatic_Main_Settings['wizard_assistant_id'];
    }
    else
    {
        $wizard_assistant_id = '';
    }
	$temperature = 1;
    if(isset($aiomatic_Main_Settings['assistant_temperature']) && $aiomatic_Main_Settings['assistant_temperature'] != '')
    {
        $temperature = intval($aiomatic_Main_Settings['assistant_temperature']);
    }
	$top_p = 1;
    if(isset($aiomatic_Main_Settings['assistant_top_p']) && $aiomatic_Main_Settings['assistant_top_p'] != '')
    {
        $top_p = intval($aiomatic_Main_Settings['assistant_top_p']);
    }
	$fpenalty = 0;
    if(isset($aiomatic_Main_Settings['assistant_fpenalty']) && $aiomatic_Main_Settings['assistant_fpenalty'] != '')
    {
        $fpenalty = intval($aiomatic_Main_Settings['assistant_fpenalty']);
    }
	$ppenalty = 0;
    if(isset($aiomatic_Main_Settings['assistant_ppenalty']) && $aiomatic_Main_Settings['assistant_ppenalty'] != '')
    {
        $ppenalty = intval($aiomatic_Main_Settings['assistant_ppenalty']);
    }
	$max_tokens = aimogenpro_get_max_tokens($model);
	$all_models = aiomatic_get_all_models(true);
	if(!in_array($model, $all_models))
    {
        $model = aiomatic_get_default_model_name($aiomatic_Main_Settings);
    }
	if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
	$new_post_content = '';
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
	$query_token_count = count(aiomatic_encode($prompt));
	$available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
	if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
	{
		$string_len = aiomatic_strlen($prompt);
		$string_len = $string_len / 2;
		$string_len = intval(0 - $string_len);
        $aicontent = aiomatic_substr($prompt, 0, $string_len);
		$aicontent = trim($aicontent);
		if(empty($aicontent))
		{
			wp_send_json_error( array( 'message' => 'Incorrect prompt provided!' ) );
		}
		$query_token_count = count(aiomatic_encode($aicontent));
		$available_tokens = $max_tokens - $query_token_count;
	}
    $thread_id = '';
	$aierror = '';
	$finish_reason = '';
	$generated_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, $top_p, $ppenalty, $fpenalty, false, 'aiAssistantWriter', 0, $finish_reason, $aierror, false, false, false, '', '', 'user', $wizard_assistant_id, $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
	if($generated_text === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI content: ' . $aierror) );
	}
	else
	{
		$new_post_content = aiomatic_sanitize_ai_result($generated_text);
	}
    do_action('aiomatic_assistant_text_reply', $new_post_content);
	wp_send_json_success( array( 'content' => $new_post_content ) );
    die();
}
add_action('wp_ajax_nopriv_aiomatic_shortcode_replacer', 'aiomatic_shortcode_replacer');
add_action('wp_ajax_aiomatic_shortcode_replacer', 'aiomatic_shortcode_replacer');
function aiomatic_shortcode_replacer() 
{
	check_ajax_referer('wp_rest', 'nonce');
    if(!isset($_POST['postId']) || !isset($_POST['send_prompt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (postId or send_prompt)' ) );
	}
	$postId = stripslashes(sanitize_text_field( $_POST['postId'] ));
	$send_prompt = stripslashes( $_POST['send_prompt'] );
    $post = get_post($postId);
    if($post === null)
    {
        wp_send_json_error( array( 'message' => 'Incorrect postId sent' ) );
        exit;
    }
    $blog_title = html_entity_decode(get_bloginfo('title'));
    $post_link = get_permalink($postId);
    $post_title = $post->post_title;
    $post_excerpt = $post->post_excerpt;
    $post_content = $post->post_content;
    $author_obj       = get_user_by('id', $post->post_author);
    if($author_obj !== false && isset($author_obj->user_nicename))
    {
        $user_name        = $author_obj->user_nicename;
    }
    else
    {
        $user_name        = '';
    }
    $featured_image   = '';
    wp_suspend_cache_addition(true);
    $metas = get_post_custom($post->ID);
    wp_suspend_cache_addition(false);
    if(is_array($metas))
    {
        $rez_meta = aiomatic_preg_grep_keys('#.+?_featured_ima?ge?#i', $metas);
    }
    else
    {
        $rez_meta = array();
    }
    if(count($rez_meta) > 0)
    {
        foreach($rez_meta as $rm)
        {
            if(isset($rm[0]) && filter_var($rm[0], FILTER_VALIDATE_URL))
            {
                $featured_image = $rm[0];
                break;
            }
        }
    }
    if($featured_image == '')
    {
        $featured_image = aiomatic_generate_thumbmail($post->ID);
    }
    if($featured_image == '' && $post_content != '')
    {
        $dom     = new DOMDocument();
        $internalErrors = libxml_use_internal_errors(true);
        $dom->loadHTML($post_content);
        libxml_use_internal_errors($internalErrors);
        $tags      = $dom->getElementsByTagName('img');
        foreach ($tags as $tag) {
            $temp_get_img = $tag->getAttribute('src');
            if ($temp_get_img != '') {
                $temp_get_img = strtok($temp_get_img, '?');
                $featured_image = rtrim($temp_get_img, '/');
            }
        }
    }
    $post_cats = '';
    $post_categories = wp_get_post_categories( $post->ID );
    foreach($post_categories as $c){
        $cat = get_category( $c );
        $post_cats .= $cat->name . ',';
    }
    $post_cats = trim($post_cats, ',');
    if($post_cats != '')
    {
        $post_categories = explode(',', $post_cats);
    }
    else
    {
        $post_categories = array();
    }
    if(count($post_categories) == 0)
    {
        $terms = get_the_terms( $post->ID, 'product_cat' );
        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $post_categories[] = $term->slug;
            }
            $post_cats = implode(',', $post_categories);
        }
        
    }
    $post_tagz = '';
    $post_tags = wp_get_post_tags( $post->ID );
    foreach($post_tags as $t){
        $post_tagz .= $t->name . ',';
    }
    $post_tagz = trim($post_tagz, ',');
    if($post_tagz != '')
    {
        $post_tags = explode(',', $post_tagz);
    }
    else
    {
        $post_tags = array();
    }
    if(count($post_tags) == 0)
    {
        $terms = get_the_terms( $post->ID, 'product_tag' );
        if ( ! empty( $terms ) && ! is_wp_error( $terms ) ){
            foreach ( $terms as $term ) {
                $post_tags[] = $term->slug;
            }
            $post_tagz = implode(',', $post_tags);
        }
        
    }
    $send_prompt = aiomatic_replaceAIPostShortcodes($send_prompt, $post_link, $post_title, $blog_title, $post_excerpt, $post_content, $user_name, $featured_image, $post_cats, $post_tagz, $postId, '', '', '', '', '', '');
	wp_send_json_success( array( 'content' => $send_prompt ) );
    die();
}
add_action('wp_ajax_aiomatic_imager', 'aiomatic_imager');
function aiomatic_imager() {
	check_ajax_referer('wp_rest', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }

    if(!isset($_POST['prompt']))
	{
		wp_send_json_error( array( 'message' => 'Incorrect query sent (prompt)' ) );
	}
	$prompt = stripslashes(sanitize_text_field( $_POST['prompt'] ));

	$aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (isset($aiomatic_Main_Settings['assistant_disable']) && $aiomatic_Main_Settings['assistant_disable'] == 'on')
    {
        wp_send_json_error( array( 'message' => 'Assistant disabled in plugin settings' ) );
    }
    if (!isset($aiomatic_Main_Settings['aiomatic_enabled']) || $aiomatic_Main_Settings['aiomatic_enabled'] != 'on')
    {
        wp_send_json_error( array( 'message' => 'Aimogen plugin disabled' ) );
    }
	if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
		wp_send_json_error( array( 'message' => 'You need to enter an AI service API key in plugin settings!' ) );
	}
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
	$appids = array_filter($appids);
	if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $image_size = '1024x1024';
	if (isset($aiomatic_Main_Settings['assistant_image_size']) && trim($aiomatic_Main_Settings['assistant_image_size']) != '') 
    {
        $image_size = $aiomatic_Main_Settings['assistant_image_size'];
    }
    $image_model = 'gpt-image-2';
	if (isset($aiomatic_Main_Settings['assistant_image_model']) && trim($aiomatic_Main_Settings['assistant_image_model']) != '') 
    {
        $image_model = $aiomatic_Main_Settings['assistant_image_model'];
    }
    if($image_model == 'stability')
    {
        $echo_ok = false;
        $height = '1024';
        $width = '1024';
        if ($image_size == '1024x1024') 
        {
            $height = '1024';
            $width = '1024';
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_stability_image($prompt, $height, $width, 'aiAssistantStableImage', 0, false, $ierror, false, false, '', 0);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling Stability.ai API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text[1]))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling Stability.ai API in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text[1] . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from Stability.ai API call: ' . $prompt) );
        }
    }
    elseif($image_model == 'google')
    {
        $echo_ok = false;
        if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '') 
        {
            $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
        }
        else
        {
            $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_image_google($prompt, $model, 'aiAssistantGoogleImage', false, $ierror, false);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling Stability.ai API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text[1]))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling Stability.ai API in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text[1] . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from Stability.ai API call: ' . $prompt) );
        }
    }
    elseif($image_model == 'midjourney')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if ($image_size == '1024x1024') 
        {
            $height = '1024';
            $width = '1024';
        }
        elseif ($image_size == '1024x1792') 
        {
            $height = '1792';
            $width = '1024';
        }
        elseif ($image_size == '1792x1024') 
        {
            $height = '1024';
            $width = '1792';
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_ai_image_midjourney($prompt, $width, $height, 'aiAssistantMidjourneyImage', false, $ierror);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling GoAPI (Midjourney) API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling GoAPI (Midjourney) in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from GoAPI (Midjourney) API call: ' . $prompt) );
        }
    }
    elseif($image_model == 'replicate')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if ($image_size == '1024x1024') 
        {
            $height = '1024';
            $width = '1024';
        }
        elseif ($image_size == '1024x1792') 
        {
            $height = '1792';
            $width = '1024';
        }
        elseif ($image_size == '1792x1024') 
        {
            $height = '1024';
            $width = '1792';
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_replicate_image($prompt, $width, $height, 'aiAssistantReplicateImage', false, $ierror);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling Replicate API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling Replicate in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from Replicate API call: ' . $prompt) );
        }
    }
    elseif($image_model == 'ideogram')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if ($image_size == '1024x1024') 
        {
            $height = '1024';
            $width = '1024';
        }
        elseif ($image_size == '1024x1792') 
        {
            $height = '1792';
            $width = '1024';
        }
        elseif ($image_size == '1792x1024') 
        {
            $height = '1024';
            $width = '1792';
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_ideogram_image($prompt, $width, $height, 'aiAssistantIdeogramImage', false, $ierror);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling Ideogram API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling Ideogram in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from Ideogram API call: ' . $prompt) );
        }
    }
    elseif($image_model == 'cloudflare')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if ($image_size == '1024x1024') 
        {
            $height = '1024';
            $width = '1024';
        }
        elseif ($image_size == '1024x1792') 
        {
            $height = '1792';
            $width = '1024';
        }
        elseif ($image_size == '1792x1024') 
        {
            $height = '1024';
            $width = '1792';
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_cloudflare_image($prompt, $width, $height, 'aiAssistantCloudflareImage', false, $ierror);
        if($arr_response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling Cloudflare API in image assistant: ' . $ierror) );
        }
        else
        {
            if(!isset($arr_response_text))
            {
                wp_send_json_error( array( 'message' => 'Error occurred when calling Cloudflare in image assistant, incorrect reply!') );
            }
            $image = '<img src="' . $arr_response_text . '">';
            $echo_ok = true;
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from Cloudflare API call: ' . $prompt) );
        }
    }
    else
    {
        $error = '';
        $image = '';
        $echo_ok = false;
        $response_text = aiomatic_generate_ai_image($token, 1, $prompt, $image_size, 'aiAssistantImage', true, 0, $error, $image_model, $prompt);
        if($response_text === false)
        {
            wp_send_json_error( array( 'message' => 'Error occurred when calling API in image assistant: ' . $error) );
        }
        else
        {
            foreach($response_text as $tmpimg)
            {
                $attached_id = '';
                $localpath = aiomatic_copy_image_locally($tmpimg, $attached_id, '', false, $prompt);
                if($localpath !== false)
                {
                    $image = '<img src="' . $localpath[0] . '">';
                    $echo_ok = true;
                    break;
                }
                else
                {
                    wp_send_json_error( array( 'message' => 'Failed to copy image file locally: ' . $tmpimg) );
                }
            }
        }
        if($echo_ok === false)
        {
            wp_send_json_error( array( 'message' => 'No image returned from API call: ' . $prompt) );
        }
    }
	if($image === false)
	{
		wp_send_json_error( array( 'message' => 'Failed to generate AI image: ' . $error) );
	}
    do_action('aiomatic_assistant_image_reply', $image);
	wp_send_json_success( array( 'content' => $image ) );
    die();
}

add_action('wp_ajax_aiomatic_form_submit', 'aiomatic_form_submit');
add_action('wp_ajax_nopriv_aiomatic_form_submit', 'aiomatic_form_submit');
function aiomatic_form_submit() {

	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form submission');
    $response_text = '';
    if(!isset($_POST['presence']) || !isset($_POST['input_text']) || !isset($_POST['model']) || !isset($_POST['temp']) || !isset($_POST['top_p']) || !isset($_POST['frequency']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for form submission';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
    if(isset($_POST['aiomaticType']))
    {
	    $aiomaticType = sanitize_text_field($_POST['aiomaticType']);
    }
    else
    {
        $aiomaticType = 'none';
    }
	$input_text = stripslashes($_POST['input_text']);
    if($aiomaticType !== 'omniblock-form')
    {
        if (isset($aiomatic_Main_Settings['form_prepend']) && trim($aiomatic_Main_Settings['form_prepend']) != '') 
        {
            $input_text = trim($aiomatic_Main_Settings['form_prepend']) . ' ' . $input_text;
        }
        if (isset($aiomatic_Main_Settings['form_append']) && trim($aiomatic_Main_Settings['form_append']) != '') 
        {
            $input_text = $input_text . ' ' . trim($aiomatic_Main_Settings['form_append']);
        }
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
	$model = sanitize_text_field(stripslashes($_POST['model']));
    if($model == 'default')
    {
        $model = AIMOGEN_DEFAULT_MODEL;
    }
	$assistant_id = sanitize_text_field(stripslashes($_POST['assistant_id']));
	$temperature = sanitize_text_field($_POST['temp']);
	$top_p = sanitize_text_field($_POST['top_p']);
	$presence_penalty = sanitize_text_field($_POST['presence']);
	$frequency_penalty = sanitize_text_field($_POST['frequency']);
    $all_models = aiomatic_get_all_models(true);
    $models = $all_models;
    if(!in_array($model, $models))
    {
        $aiomatic_result['msg'] = 'Invalid model provided: ' . $model;
        wp_send_json($aiomatic_result);
    }
    $temperature = floatval($temperature);
    $top_p = floatval($top_p);
    $presence_penalty = floatval($presence_penalty);
    $frequency_penalty = floatval($frequency_penalty);
    if($temperature < 0 || $temperature > 2)
    {
        $aiomatic_result['msg'] = 'Invalid temperature provided: ' . $temperature;
        wp_send_json($aiomatic_result);
    }
    if($top_p < 0 || $top_p > 1)
    {
        $aiomatic_result['msg'] = 'Invalid top_p provided: ' . $top_p;
        wp_send_json($aiomatic_result);
    }
    if($presence_penalty < -2 || $presence_penalty > 2)
    {
        $aiomatic_result['msg'] = 'Invalid presence_penalty provided: ' . $presence_penalty;
        wp_send_json($aiomatic_result);
    }
    if($frequency_penalty < -2 || $frequency_penalty > 2)
    {
        $aiomatic_result['msg'] = 'Invalid frequency_penalty provided: ' . $frequency_penalty;
        wp_send_json($aiomatic_result);
    }
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid AI service API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'Daily token count of your user account has been exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    do_action('aiomatic_calling_forms', $input_text, $model);

    $form_id = '';
    if(isset($_POST['form_id']) && $_POST['form_id'] != '')
    {
        $form_id = sanitize_text_field($_POST['form_id']);
    }
    $post_id = get_the_ID();
    if($aiomaticType == 'omniblock-form')
    {
        $input_text = str_replace('{', '%%LEFTBRACE%%', $input_text);
        $input_text = str_replace('}', '%%RIGHTBRACE%%', $input_text);
    }
    $input_text = aiomatic_replaceEmbeddingsAIPostShortcodes($input_text, $post_id);
    if($aiomaticType == 'omniblock-form')
    {
        $input_text = str_replace('%%LEFTBRACE%%', '{', $input_text);
        $input_text = str_replace('%%RIGHTBRACE%%', '}', $input_text);
    }
    $current_user = wp_get_current_user();
    if ( !($current_user instanceof WP_User) || !is_user_logged_in()) 
    {
        $input_text = str_replace('%%user_name%%', '', $input_text);
        $input_text = str_replace('%%user_email%%', '' , $input_text);
        $input_text = str_replace('%%user_display_name%%', '', $input_text);
        $input_text = str_replace('%%user_role%%', '', $input_text);
        $input_text = str_replace('%%user_id%%', '' , $input_text);
        $input_text = str_replace('%%user_firstname%%', '' , $input_text);
        $input_text = str_replace('%%user_lastname%%', '' , $input_text);
        $input_text = str_replace('%%user_description%%', '' , $input_text);
        $input_text = str_replace('%%user_url%%', '' , $input_text);
    }
    else
    {
        $input_text = str_replace('%%user_name%%', $current_user->user_login, $input_text);
        $input_text = str_replace('%%user_email%%', $current_user->user_email , $input_text);
        $input_text = str_replace('%%user_display_name%%', $current_user->display_name, $input_text);
        $input_text = str_replace('%%user_role%%', implode(',', $current_user->roles), $input_text);
        $input_text = str_replace('%%user_id%%', $current_user->ID , $input_text);
        $input_text = str_replace('%%user_firstname%%', $current_user->user_firstname , $input_text);
        $input_text = str_replace('%%user_lastname%%', $current_user->user_lastname , $input_text);
        $user_desc = get_the_author_meta( 'description', $current_user->ID );
        $input_text = str_replace('%%user_description%%', $user_desc , $input_text);
        $user_url = get_the_author_meta( 'user_url', $current_user->ID );
        $input_text = str_replace('%%user_url%%', $user_url , $input_text);
    }

    if($aiomaticType == 'text' || $aiomaticType == 'none')
    {
        $max_tokens = aimogenpro_get_max_tokens($model);
        $input_text = preg_replace('#<br\s*/?>#i', "\n", $input_text);
        $input_text = htmlspecialchars_decode($input_text, ENT_QUOTES);
        $input_text = stripslashes($input_text);
        $input_text = preg_replace('#<div><span class="highlight-none">([\s\S]*?)<\/span><\/div>#i', PHP_EOL . '$1', $input_text);
        $input_text = preg_replace('#<span class="highlight-none">([\s\S]*?)<\/span>#i', '$1', $input_text);
        $query_token_count = count(aiomatic_encode($input_text));
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $input_text, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $string_len = aiomatic_strlen($input_text);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $input_text = aiomatic_substr($input_text, 0, $string_len);
            $input_text = trim($input_text);
            if(empty($input_text))
            {
                $aiomatic_result['msg'] = 'Empty API seed expression provided (after processing)';
                wp_send_json($aiomatic_result);
                wp_die();
            }
            $query_token_count = count(aiomatic_encode($input_text));
            $available_tokens = $max_tokens - $query_token_count;
        }
        $thread_id = '';
        $error = '';
        $finish_reason = '';
        if(isset($aiomatic_Main_Settings['store_data_forms']) && $aiomatic_Main_Settings['store_data_forms'] == 'on')
        {
            $store_data = 'on';
        }
        else
        {
            $store_data = 'off';
        }
        if($aiomaticType == 'text')
        {
            $response_text = aiomatic_generate_text($token, $model, $input_text, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'formsText', 0, $finish_reason, $error, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '', '', '');
        }
        else
        {

            $response_text = aiomatic_generate_text($token, $model, $input_text, $available_tokens, $temperature, $top_p, $presence_penalty, $frequency_penalty, false, 'shortcodeCompletion', 0, $finish_reason, $error, false, false, false, '', '', 'user', $assistant_id, $thread_id, '', 'disabled', '', true, $store_data, array(), '', '', '');
        }
        if($response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $inp_count = count(aiomatic_encode($input_text));
            $resp_count = count(aiomatic_encode($response_text));
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + $inp_count + $resp_count;
                update_user_meta($user_id, 'aiomatic_used_tokens', $used_token_count);
            }
        }
    }
    elseif($aiomaticType == 'image')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '512x512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-2';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-new')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-2';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'gpt-image-1')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-1';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'gpt-image-2')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-2';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'gpt-image-1-mini')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-1-mini';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'gpt-image-1.5')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'gpt-image-1.5';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'chatgpt-image-latest')
    {
        $echo_ok = false;
        $error = '';
        $image_size = '1024x1024';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            $image_size = trim($aiomatic_Main_Settings['ai_image_size']);
        }
        $image_model = 'chatgpt-image-latest';
        $arr_response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'formsImage', false, 0, $error, $image_model, $input_text);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            foreach($arr_response_text as $tmpimg)
            {
                $response_text = $tmpimg;
                $echo_ok = true;
            }
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image2')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            if(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1024')
            {
                $height = '1024';
                $width = '1024';
            }
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_stability_image($input_text, $height, $width, 'formsStableImage', 0, true, $ierror, false, false, '', 0);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-mid')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            if(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1024')
            {
                $height = '1024';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1792')
            {
                $height = '1792';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1792x1024')
            {
                $height = '1024';
                $width = '1792';
            }
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_ai_image_midjourney($input_text, $width, $height, 'formsMidjourneyImage', true, $ierror);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-rep')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            if(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1024')
            {
                $height = '1024';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1792')
            {
                $height = '1792';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1792x1024')
            {
                $height = '1024';
                $width = '1792';
            }
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_replicate_image($input_text, $width, $height, 'formsReplicateImage', true, $ierror);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-goo')
    {
        $echo_ok = false;
        if (isset($aiomatic_Main_Settings['google_ai_image_model']) && trim($aiomatic_Main_Settings['google_ai_image_model']) != '') 
        {
            $model = trim($aiomatic_Main_Settings['google_ai_image_model']);
        }
        else
        {
            $model = AIMOGEN_GOOGLE_IMAGE_DEFAULT_MODEL;
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_image_google($input_text, $model, 'formsGoogleImage', true, $ierror, false);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-ide')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            if(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1024')
            {
                $height = '1024';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1792')
            {
                $height = '1792';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1792x1024')
            {
                $height = '1024';
                $width = '1792';
            }
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_ideogram_image($input_text, $width, $height, 'formsIdeogramImage', true, $ierror);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'image-cf')
    {
        $echo_ok = false;
        $height = '512';
        $width = '512';
        if (isset($aiomatic_Main_Settings['ai_image_size']) && trim($aiomatic_Main_Settings['ai_image_size']) != '') 
        {
            if(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1024')
            {
                $height = '1024';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1024x1792')
            {
                $height = '1792';
                $width = '1024';
            }
            elseif(trim($aiomatic_Main_Settings['ai_image_size']) == '1792x1024')
            {
                $height = '1024';
                $width = '1792';
            }
        }
        $ierror = '';
        $arr_response_text = aiomatic_generate_cloudflare_image($input_text, $width, $height, 'formsCloudflareImage', true, $ierror);
        if($arr_response_text === false)
        {
            $aiomatic_result['msg'] = $ierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $response_text = $arr_response_text;
            $echo_ok = true;
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + 1000;
                update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
            }
        }
        if($echo_ok === false)
        {
            $aiomatic_result['msg'] = 'No image returned from API call: ' . $input_text;
            wp_send_json($aiomatic_result);
        }
    }
    elseif($aiomaticType == 'omniblock-form')
    {
        $input_text = json_decode($input_text, true);
        if($input_text === null || $input_text === false || $input_text === true)
        {
            $input_text = [];
        }
        $received_api_key = get_post_meta($form_id, 'trigger_api_key', true);
        $omniblockid = get_post_meta($form_id, 'trigger_id', true);
        if(empty($omniblockid))
        {
            $aiomatic_result['msg'] = 'You need to submit an OmniBlock ID in the request';
            wp_send_json($aiomatic_result);
        }
        require_once (dirname(__FILE__) . "/aiomatic-automation.php"); 
        $cont = 0;
        $id_parts = explode('_', $omniblockid);
        if(!isset($id_parts[1]))
        {
            $aiomatic_result['msg'] = 'Incorrect OmniBlock ID submitted in request';
            wp_send_json($aiomatic_result);
        }
        if(count($id_parts) > 2)
        {
            $aiomatic_result['msg'] = 'Incorrect format for OmniBlock IDs';
            wp_send_json($aiomatic_result);
        }
        $param = $id_parts[0];
        $omniTriggerID = $id_parts[1];
        $rules = get_option('aiomatic_omni_list', array());
        $found = false;
        if (!empty($rules)) 
        {
            $default_block_types = aiomatic_omniblocks_default_block_types(); 
            foreach ($rules as $request => $bundle[]) 
            {
                if ($cont == $param) 
                {
                    $bundle_values    = array_values($bundle);
                    $myValues         = $bundle_values[$cont];
                    $array_my_values  = array_values($myValues);for($iji=0;$iji<count($array_my_values);++$iji){if(is_string($array_my_values[$iji])){$array_my_values[$iji]=stripslashes($array_my_values[$iji]);}}
                    $schedule         = isset($array_my_values[0]) ? $array_my_values[0] : '';
                    $active           = isset($array_my_values[1]) ? $array_my_values[1] : '';
                    $last_run         = isset($array_my_values[2]) ? $array_my_values[2] : '';
                    $max              = isset($array_my_values[3]) ? $array_my_values[3] : '';
                    $main_keywords    = isset($array_my_values[4]) ? $array_my_values[4] : '';
                    $title_once       = isset($array_my_values[5]) ? $array_my_values[5] : '';
                    $rule_description = isset($array_my_values[6]) ? $array_my_values[6] : '';
                    $rule_unique_id   = isset($array_my_values[7]) ? $array_my_values[7] : '';
                    $sortable_cards   = isset($array_my_values[8]) ? $array_my_values[8] : '';
                    $more_keywords    = isset($array_my_values[9]) ? $array_my_values[9] : '';
                    $days_no_run      = isset($array_my_values[10]) ? $array_my_values[10] : '';
                    $block_data       = json_decode($sortable_cards, true);
                    if(!empty($block_data))
                    {
                        foreach($block_data as $index => $current_block)
                        {
                            $card_type_found = array();
                            foreach($default_block_types as $def_card)
                            {
                                if($current_block['type'] == $def_card['id'])
                                {
                                    $card_type_found = $def_card;
                                    break;
                                }
                            }
                            if(!empty($card_type_found) && $omniTriggerID == $current_block['identifier'] && $current_block['type'] === 'ai_forms_omniblock')
                            {
                                if(isset($current_block['parameters']['api_key']) && !empty(trim($current_block['parameters']['api_key'])))
                                {
                                    $api_key = $current_block['parameters']['api_key'];
                                    if(empty($received_api_key))
                                    {
                                        $aiomatic_result['msg'] = 'You need to specify an API key for this request';
                                        wp_send_json($aiomatic_result);
                                    }
                                    if($received_api_key != $api_key)
                                    {
                                        $aiomatic_result['msg'] = 'Invalid API key provided';
                                        wp_send_json($aiomatic_result);
                                    }
                                    if(!isset($_REQUEST['apikey']) || empty($_REQUEST['apikey']))
                                    {
                                        $_REQUEST['apikey'] = $received_api_key;
                                    }
                                }
                                $return_last_result = true;
                                $return_me = aiomatic_run_rule($cont, 5, 1, 0, null, '', $omniTriggerID, $return_last_result, $input_text);
                                if($return_me == 'fail')
                                {
                                    aiomatic_log_to_file('OmniBlock AI Forms Trigger rule running failed, rule ID: ' . $param . ' OmniBlock ID: ' . $omniTriggerID);
                                }
                                $aiomatic_result['status'] = 'success';
                                $aiomatic_result['unique_index'] = '';
                                $html = '0';
                                if ($form_id != '')
                                {
                                    $html = get_post_meta($form_id, 'html', true);
                                }
                                if($html == '1')
                                {
                                    $aiomatic_result['data'] = wp_kses_post($return_me);
                                }
                                else
                                { 
                                    $aiomatic_result['data'] = esc_html($return_me);
                                }
                                do_action('aiomatic_form_reply', $return_me);
                                wp_send_json($aiomatic_result);
                                die();
                            }
                        }
                    }
                }
                $cont = $cont + 1;
            }
            if($found == false)
            {
                $aiomatic_result['msg'] = 'Specified \'AI Forms Trigger\' OmniBlock ID not found';
                wp_send_json($aiomatic_result);
            }
        } 
        else 
        {
            $aiomatic_result['msg'] = 'No rules found for aiomatic_omni_list!';
            wp_send_json($aiomatic_result);
        }
    }
    else
    {
        $aiomatic_result['msg'] = 'Unknown request type submitted: ' . esc_html($aiomaticType);
        wp_send_json($aiomatic_result);
    }
    $unique_index = '';
    if(isset($_POST['isForm']) && $_POST['isForm'] == '1')
    {
        if($aiomaticType == 'text' || $aiomaticType == 'none')
        {
            if(!empty($user_id) && $user_id != 0 && is_numeric($user_id))
            {
                if($form_id != '')
                {
                    if (!isset($aiomatic_Main_Settings['disable_ai_form_saving']) || trim($aiomatic_Main_Settings['disable_ai_form_saving']) != 'on')
                    {
                        $history_key = 'aiomatic_form_history_' . $form_id;
                        $history = get_user_meta($user_id, $history_key, true);
                        if (!is_array($history)) {
                            $history = [];
                        }
                        if (isset($aiomatic_Main_Settings['form_max_user']) && trim($aiomatic_Main_Settings['form_max_user']) != '') 
                        {
                            $max_allowed = (int) trim($aiomatic_Main_Settings['form_max_user']);
                            if (count($history) >= $max_allowed) {
                                $history = array_slice($history, -($max_allowed - 1), null, true);
                            }
                        }
                        $unique_index = uniqid('aiomatic_', true);
                        $history[$unique_index] = $response_text;
                        update_user_meta($user_id, $history_key, $history);
                    }
                }
            }
        }
    }

    $aiomatic_result['status'] = 'success';
    $aiomatic_result['unique_index'] = $unique_index;
    $html = '0';
    if ($form_id != '')
    {
        $html = get_post_meta($form_id, 'html', true);
    }
    if($html == '1')
    {
        $aiomatic_result['data'] = wp_kses_post($response_text);
    }
    else
    { 
        $aiomatic_result['data'] = esc_html($response_text);
    }
    do_action('aiomatic_form_reply', $response_text);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_add_form_history_entry', 'aiomatic_add_form_history_entry');
function aiomatic_add_form_history_entry() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    $index = '';
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['disable_ai_form_saving']) || trim($aiomatic_Main_Settings['disable_ai_form_saving']) != 'on')
    {
        if(isset($_POST['form_id']))
        {
            $form_id = sanitize_text_field($_POST['form_id']);
        }
        else
        {
            $form_id = '';
        }
        $response_text = sanitize_text_field($_POST['response_text']);
        $user_id = get_current_user_id();
        if ($user_id == 0 || empty($form_id)) {
            wp_send_json_error(['message' => 'Invalid request.']);
        }
        $history_key = 'aiomatic_form_history_' . $form_id;
        $history = get_user_meta($user_id, $history_key, true);
        if (!is_array($history)) {
            $history = [];
        }
        if (isset($aiomatic_Main_Settings['form_max_user']) && trim($aiomatic_Main_Settings['form_max_user']) != '') 
        {
            $max_allowed = (int) trim($aiomatic_Main_Settings['form_max_user']);
            if (count($history) >= $max_allowed) {
                $history = array_slice($history, -($max_allowed - 1), null, true);
            }
        }
        $index = uniqid('aiomatic_', true);
        $history[$index] = $response_text;
        update_user_meta($user_id, $history_key, $history);
    }
    wp_send_json_success(['status' => 'success', 'unique_index' => $index]);
}

add_action('wp_ajax_aiomatic_delete_form_history_entry', 'aiomatic_delete_form_history_entry');
function aiomatic_delete_form_history_entry() {
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }

    $form_id = sanitize_text_field($_POST['form_id']);
    $index = $_POST['index'];
    $user_id = get_current_user_id();

    if ($user_id == 0 || empty($form_id)) {
        wp_send_json_error(['message' => 'Invalid request.']);
    }

    $history_key = 'aiomatic_form_history_' . $form_id;
    $history = get_user_meta($user_id, $history_key, true);
    if (!is_array($history) || !isset($history[$index])) {
        wp_send_json_error(['message' => 'Entry not found.']);
    }

    unset($history[$index]);
    $history = array_values($history);
    update_user_meta($user_id, $history_key, $history);

    wp_send_json_success(['message' => 'Entry deleted successfully.']);
}

add_action('wp_ajax_aiomatic_scrape_form_submit', 'aiomatic_scrape_form_submit');
add_action('wp_ajax_nopriv_aiomatic_scrape_form_submit', 'aiomatic_scrape_form_submit');
function aiomatic_scrape_form_submit() 
{
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with form submission');
    if(!isset($_POST['scrapeurl']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for text scraping';
        wp_send_json($aiomatic_result);
    }
    $scraped_data = '';
    $scrapeurl = trim($_POST['scrapeurl']);
    if(filter_var($scrapeurl, FILTER_VALIDATE_URL))
    {
        $scraped_data = aiomatic_scrape_page($scrapeurl, '0', 'auto', '');
        if($scraped_data === false)
        {
            $aiomatic_result['msg'] = 'Incorrect AJAX call';
        }
        else
        {
            $aiomatic_result['status'] = 'success';
            $aiomatic_result['data'] = $scraped_data;
        }
    }
    else
    {
        $aiomatic_result['msg'] = 'Unknown URL format submitted: ' . esc_url($scrapeurl);
    }
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_edit_submit', 'aiomatic_edit_submit');
add_action('wp_ajax_nopriv_aiomatic_edit_submit', 'aiomatic_edit_submit');

function aiomatic_edit_submit() {
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with edit submission');
    if(!isset($_POST['instruction']) || !isset($_POST['input_text']) || !isset($_POST['model']) || !isset($_POST['temp']) || !isset($_POST['top_p']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for text editing';
        wp_send_json($aiomatic_result);
    }
	$instruction = stripslashes($_POST['instruction']);
	$input_text = stripslashes($_POST['input_text']);
	$model = sanitize_text_field($_POST['model']);
	$temperature = sanitize_text_field($_POST['temp']);
	$top_p = sanitize_text_field($_POST['top_p']);
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
    $temperature = floatval($temperature);
    $top_p = floatval($top_p);
    $models = aiomatic_get_all_models(true);
    if(!in_array($model, $models))
    {
        $aiomatic_result['msg'] = 'Invalid editing model provided: ' . $model;
        wp_send_json($aiomatic_result);
    }
    if($temperature < 0 || $temperature > 2)
    {
        $aiomatic_result['msg'] = 'Invalid temperature provided: ' . $temperature;
        wp_send_json($aiomatic_result);
    }
    if($top_p < 0 || $top_p > 1)
    {
        $aiomatic_result['msg'] = 'Invalid top_p provided: ' . $top_p;
        wp_send_json($aiomatic_result);
    }
    if(empty($instruction))
    {
        $aiomatic_result['msg'] = 'You need to add an instruction for the text editing!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'You need to insert a valid AI service API Key for this to work!';
        wp_send_json($aiomatic_result);
    }
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_edit_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = 'Daily token count of your user account is exceeded! Please try again tomorrow.';
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $completionmodels = $models;
    if(in_array($model, $completionmodels))
    {
        if(!aiomatic_endsWith(trim($instruction), ':'))
        {
            $prompt = $instruction . ': ' . $input_text;
        }
        else
        {
            $prompt = $instruction . $input_text;
        }
        $thread_id = '';
        $error = '';
        $finish_reason = '';
        $max_tokens = aimogenpro_get_max_tokens($model);
        $prompt = stripslashes($prompt);
        $query_token_count = count(aiomatic_encode($prompt));
        $available_tokens = aiomatic_compute_available_tokens($model, $max_tokens, $prompt, $query_token_count);
        if($available_tokens <= AIMOGEN_MINIMUM_TOKENS_FOR_COMPLETIONS)
        {
            $string_len = aiomatic_strlen($prompt);
            $string_len = $string_len / 2;
            $string_len = intval(0 - $string_len);
            $prompt = aiomatic_substr($prompt, 0, $string_len);
            $prompt = trim($prompt);
            if(empty($prompt))
            {
                $aiomatic_result['msg'] = 'Empty API seed expression provided (after processing)';
                wp_send_json($aiomatic_result);
            }
            else
            {
                $query_token_count = count(aiomatic_encode($prompt));
                $available_tokens = $max_tokens - $query_token_count;
            }
        }
        $response_text = aiomatic_generate_text($token, $model, $prompt, $available_tokens, $temperature, $top_p, 0, 0, false, 'shortcodeCEditor', 0, $finish_reason, $error, false, false, false, '', '', 'user', '', $thread_id, '', 'disabled', '', false, false, array(), '', '', '');
        if($response_text === false)
        {
            $aiomatic_result['msg'] = $error;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $inp_count = count(aiomatic_encode($prompt));
            $resp_count = count(aiomatic_encode($response_text));
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + $inp_count + $resp_count;
                update_user_meta($user_id, 'aiomatic_used_tokens', $used_token_count);
            }
        }
        $response_text = trim($response_text);
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $response_text;
        do_action('aiomatic_editor_reply', $response_text);
        wp_send_json($aiomatic_result);
    }
    else
    {
        $aierror = '';
        $input_text = stripslashes($input_text);
        $instruction = stripslashes($instruction);
        $response_text = aiomatic_edit_text($token, $model, $instruction, $input_text, $temperature, $top_p, 'shortcodeEditor', 0, $aierror);
        if($response_text === false)
        {
            $aiomatic_result['msg'] = $aierror;
            wp_send_json($aiomatic_result);
        }
        else
        {
            $instr_count = count(aiomatic_encode($instruction));
            $inp_count = count(aiomatic_encode($input_text));
            $resp_count = count(aiomatic_encode($response_text));
            if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
            {
                $used_token_count = intval($used_token_count) + $instr_count + $inp_count + $resp_count;
                update_user_meta($user_id, 'aiomatic_used_edit_tokens', $used_token_count);
            }
        }
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $response_text;
        do_action('aiomatic_editor_reply', $response_text);
        wp_send_json($aiomatic_result);
    }
    die();
}

add_action('wp_ajax_aiomatic_image_chat_submit', 'aiomatic_image_chat_submit');
add_action('wp_ajax_nopriv_aiomatic_image_chat_submit', 'aiomatic_image_chat_submit');

function aiomatic_image_chat_submit() 
{
    $echo_ok = false;
	check_ajax_referer('openai-ajax-images-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with chat submission');
    if(!isset($_POST['input_text']))
    {
        $aiomatic_result['msg'] = 'Incomplete POST request for image chat';
        wp_send_json($aiomatic_result);
    }
    $user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!empty($user_token_cap_per_day))
    {
        $user_token_cap_per_day = intval($user_token_cap_per_day);
    }
	$user_id = sanitize_text_field($_POST['user_id']);
	$input_text = stripslashes($_POST['input_text']);
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = esc_html__('You need to insert a valid AI service API Key for this to work!', 'aiomatic-automatic-ai-content-writer');
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
    $used_token_count = 0;
    if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result['msg'] = sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) );
            wp_send_json($aiomatic_result);
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_image_chat_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result['msg'] = esc_html__('Daily token limit for your user account was exceeded! Please try again tomorrow.', 'aiomatic-automatic-ai-content-writer');
                wp_send_json($aiomatic_result);
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
	$appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
	$error = '';
    $image_size = '1024x1024';
	if (isset($aiomatic_Chatbot_Settings['image_chat_size']) && trim($aiomatic_Chatbot_Settings['image_chat_size']) != '') 
    {
        $image_size = $aiomatic_Chatbot_Settings['image_chat_size'];
    }
    $image_chat_model = 'gpt-image-2';
	if (isset($aiomatic_Chatbot_Settings['image_chat_model']) && trim($aiomatic_Chatbot_Settings['image_chat_model']) != '') 
    {
        $image_chat_model = $aiomatic_Chatbot_Settings['image_chat_model'];
    }
    $response_text = aiomatic_generate_ai_image($token, 1, $input_text, $image_size, 'shortcodeImageChat', false, 0, $error, $image_chat_model, $input_text);
    if($response_text === false)
    {
        $aiomatic_result['msg'] = $error;
        wp_send_json($aiomatic_result);
    }
    else
    {
        foreach($response_text as $tmpimg)
        {
            $aiomatic_result['status'] = 'success';
            if(isset($aiomatic_result['data']))
            {
                $aiomatic_result['data'] .= '<a href="' . $tmpimg . '" target="_blank"><img class="image_max_w_ai" src="' . $tmpimg . '"></a>';
            }
            else
            {
                $aiomatic_result['data'] = '<a href="' . $tmpimg . '" target="_blank"><img class="image_max_w_ai" src="' . $tmpimg . '"></a>';
            }
            $echo_ok = true;
        }
        if(!empty($user_token_cap_per_day) && is_numeric($user_token_cap_per_day))
        {
            $used_token_count = intval($used_token_count) + 1000;
            update_user_meta($user_id, 'aiomatic_used_image_chat_tokens', $used_token_count);
        }
    }
    if($echo_ok === false)
    {
        $aiomatic_result['msg'] = esc_html__('No image returned from API call: ', 'aiomatic-automatic-ai-content-writer') . $input_text;
        wp_send_json($aiomatic_result);
    }
    do_action('aiomatic_image_chat_reply', $aiomatic_result);
    wp_send_json($aiomatic_result);
    die();
}

add_action('wp_ajax_aiomatic_user_meta_save', 'aiomatic_user_meta_save');
add_action('wp_ajax_nopriv_aiomatic_user_meta_save', 'aiomatic_user_meta_save');

function aiomatic_user_meta_save() 
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong');
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['x_input_text']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no x_input_text');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    if(!isset($_POST['user_id']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no user_id');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$user_id = sanitize_text_field($_POST['user_id']);
    if(!isset($_POST['persistent']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no persistentid');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$persistent = sanitize_text_field($_POST['persistent']);
    if(empty($user_id) || $user_id == 0)
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, user_id is not valid');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    if(isset($_POST['thread_id']))
    {
	    $thread_id = stripslashes($_POST['thread_id']);
    }
    else
    {
        $thread_id = '';
    }
    if($persistent == 'history')
    {
        if(empty($thread_id))
        {
            if(isset($_POST['saving_index']))
            {
                $saving_index = stripslashes($_POST['saving_index']);
            }
            if(empty($saving_index))
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, saving_index is not valid');
                wp_send_json($aiomatic_result);
                wp_die();
            }
            if(isset($_POST['main_index']))
            {
                $main_index = stripslashes($_POST['main_index']);
            }
            if(empty($main_index))
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, main_index is not valid');
                wp_send_json($aiomatic_result);
                wp_die();
            }
            $x_input_text = stripslashes($_POST['x_input_text']);
            if(!empty($x_input_text))
            {
                if(is_numeric($user_id))
                {
                    $pmeta = get_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, true);
                    if(!is_array($pmeta))
                    {
                        $pmeta = array();
                    }
                    $is_existing = false;
                    if(isset($pmeta[$saving_index]))
                    {
                        $is_existing = true;
                    }
                    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
                    if(isset($aiomatic_Chatbot_Settings['max_chat_log_login']) && $aiomatic_Chatbot_Settings['max_chat_log_login'] !== '' && is_numeric($aiomatic_Chatbot_Settings['max_chat_log_login']))
                    {
                        $xmax = intval($aiomatic_Chatbot_Settings['max_chat_log_login']) - 1;
                        if($xmax < 0)
                        {
                            $xmax = 0;
                        }
                        if(count($pmeta) >= $xmax)
                        {
                            uasort($pmeta, function ($a, $b) 
                            {
                                return $b['time'] <=> $a['time'];
                            });
                            $pmeta = array_slice($pmeta, 0, $xmax);
                        }
                    }
                    $x_input_text = remove_ai_typing_indicator($x_input_text);
                    $name = aiomatic_generate_conversation_title($x_input_text);
                    $pmeta[$saving_index]['name'] = $name;
                    $pmeta[$saving_index]['main_index'] = $main_index;
                    $pmeta[$saving_index]['time'] = time();
                    $pmeta[$saving_index]['data'] = $x_input_text;
                    update_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, $pmeta);
                    if(!$is_existing)
                    {
                        $aiomatic_result = array('status' => 'success', 'name' => $name, 'msg' => 'true');
                        wp_send_json($aiomatic_result);
                        wp_die();
                    }
                    $aiomatic_result = array('status' => 'success', 'msg' => 'ok');
                    wp_send_json($aiomatic_result);
                    wp_die();
                }
                else
                {
                    $pmeta = get_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id);
                    if(!is_array($pmeta))
                    {
                        $pmeta = array();
                    }
                    $is_existing = false;
                    if(isset($pmeta[$saving_index]))
                    {
                        $is_existing = true;
                    }
                    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
                    if(isset($aiomatic_Chatbot_Settings['max_chat_log_not_login']) && $aiomatic_Chatbot_Settings['max_chat_log_not_login'] !== '' && is_numeric($aiomatic_Chatbot_Settings['max_chat_log_not_login']))
                    {
                        $xmax = intval($aiomatic_Chatbot_Settings['max_chat_log_not_login']) - 1;
                        if($xmax < 0)
                        {
                            $xmax = 0;
                        }
                        if(count($pmeta) >= $xmax)
                        {
                            uasort($pmeta, function ($a, $b) 
                            {
                                return $b['time'] <=> $a['time'];
                            });
                            $pmeta = array_slice($pmeta, 0, $xmax);
                        }
                    }
                    $x_input_text = remove_ai_typing_indicator($x_input_text);
                    $name = aiomatic_generate_conversation_title($x_input_text);
                    $pmeta[$saving_index]['name'] = $name;
                    $pmeta[$saving_index]['main_index'] = $main_index;
                    $pmeta[$saving_index]['time'] = time();
                    $pmeta[$saving_index]['data'] = $x_input_text;
                    if(isset($aiomatic_Chatbot_Settings['remember_chat_transient']) && $aiomatic_Chatbot_Settings['remember_chat_transient'] !== '' && is_numeric($aiomatic_Chatbot_Settings['remember_chat_transient']))
                    {
                        $remember_time = intval($aiomatic_Chatbot_Settings['remember_chat_transient']);
                    }
                    else
                    {
                        $remember_time = 0;
                    }
                    set_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id, $pmeta, $remember_time);
                    if(!$is_existing)
                    {
                        $aiomatic_result = array('status' => 'success', 'name' => $name, 'msg' => 'true');
                        wp_send_json($aiomatic_result);
                        wp_die();
                    }
                    $aiomatic_result = array('status' => 'success', 'msg' => 'ok');
                    wp_send_json($aiomatic_result);
                    wp_die();
                }
            }
        }
    }
    elseif($persistent == 'vector')
    {
        $x_input_text = stripslashes($_POST['x_input_text']);
        if(!empty($x_input_text))
        {
            $namespace = 'persistentchat_' . $user_id . '_' . $thread_id;
            $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
            if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) 
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'You need to enter an AI service API key for this feature to work.');
                wp_send_json($aiomatic_result);
                wp_die();
            }
            if (isset($aiomatic_Main_Settings['pinecone_app_id'])) {
                $pinecone_app_id = $aiomatic_Main_Settings['pinecone_app_id'];
            } else {
                $pinecone_app_id = '';
            }
            if (isset($aiomatic_Main_Settings['qdrant_app_id'])) {
                $qdrant_app_id = $aiomatic_Main_Settings['qdrant_app_id'];
            } else {
                $qdrant_app_id = '';
            }
            if (isset($aiomatic_Main_Settings['chroma_app_id'])) {
                $chroma_app_id = $aiomatic_Main_Settings['chroma_app_id'];
            } else {
                $chroma_app_id = '';
            }
            if($pinecone_app_id == '' && $qdrant_app_id == '' && $chroma_app_id == '')
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'You need to enter a Pinecone.io API, Chroma API or a Qdrant API key for this to work');
                wp_send_json($aiomatic_result);
                wp_die();
            }
            if (!isset($aiomatic_Main_Settings['embeddings_chat_short']) || trim($aiomatic_Main_Settings['embeddings_chat_short']) != 'on')
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'You need to enable Embeddings for the Chatbot for this to work');
                wp_send_json($aiomatic_result);
                wp_die();
            }
            if (isset($aiomatic_Main_Settings['embeddings_model']) && $aiomatic_Main_Settings['embeddings_model'] != '') 
            {
                $model = $aiomatic_Main_Settings['embeddings_model'];
            }
            else
            {
                $model = AIMOGEN_DEFAULT_MODEL_EMBEDDING;
            }
            $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
            $appids = array_filter($appids);
            if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
            $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
            require_once(dirname(__FILE__) . "/res/Embeddings.php");
            $embdedding = new Aiomatic_Embeddings($token);
            $aiomatic_result = $embdedding->aimogenpro_save_embedding($x_input_text, '', '', false, $model, $namespace);
            if($aiomatic_result['status'] == 'error')
            {
                $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save user chat log embeddings');
                wp_send_json($aiomatic_result);
                wp_die();
            }
        }
    }
    else
    {
        if(empty($thread_id))
        {
            $x_input_text = stripslashes($_POST['x_input_text']);
            $x_input_text = remove_ai_typing_indicator($x_input_text);
            if(!empty($x_input_text))
            {
                if(is_numeric($user_id))
                {
                    update_user_meta($user_id, 'aiomatic_chat_history_' . $persistent, $x_input_text);
                }
                else
                {
                    $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
                    if(isset($aiomatic_Chatbot_Settings['remember_chat_transient']) && $aiomatic_Chatbot_Settings['remember_chat_transient'] !== '' && is_numeric($aiomatic_Chatbot_Settings['remember_chat_transient']))
                    {
                        $remember_time = intval($aiomatic_Chatbot_Settings['remember_chat_transient']);
                    }
                    else
                    {
                        $remember_time = 0;
                    }
                    set_transient('aiomatic_chat_history_' . $persistent . '_' . $user_id, $x_input_text, $remember_time);
                }
            }
        }
        else
        {
            if(is_numeric($user_id))
            {
                update_user_meta($user_id, 'aiomatic_assistant_history_thread', $thread_id);
            }
            else
            {
                $aiomatic_Chatbot_Settings = get_option('aiomatic_Chatbot_Settings', false);
                if(isset($aiomatic_Chatbot_Settings['remember_chat_transient']) && $aiomatic_Chatbot_Settings['remember_chat_transient'] !== '' && is_numeric($aiomatic_Chatbot_Settings['remember_chat_transient']))
                {
                    $remember_time = intval($aiomatic_Chatbot_Settings['remember_chat_transient']);
                }
                else
                {
                    $remember_time = 0;
                }
                set_transient('aiomatic_assistant_history_thread_' . $user_id, $thread_id, $remember_time);
            }
        }
    }
    $aiomatic_result = array('status' => 'success', 'msg' => 'ok');
    wp_send_json($aiomatic_result);
	wp_die();
}

add_action('wp_ajax_aiomatic_record_user_usage', 'aiomatic_record_user_usage');
add_action('wp_ajax_nopriv_aiomatic_record_user_usage', 'aiomatic_record_user_usage');
function aiomatic_record_user_usage() 
{
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['input_text']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no input_text');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$input_text = stripslashes($_POST['input_text']);
    if(!isset($_POST['user_id']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no user_id');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$user_id = sanitize_text_field($_POST['user_id']);
    if(!isset($_POST['user_token_cap_per_day']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no user_token_cap_per_day');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    if(!isset($_POST['response_text']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no response_text');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$response_text = sanitize_text_field($_POST['response_text']);
    if(!isset($_POST['model']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no model');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$model = sanitize_text_field($_POST['model']);
    if(!isset($_POST['temp']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no temp');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    $vision_file = '';
    if(isset($_POST['vision_file']))
    {
        $vision_file = $_POST['vision_file'];
    }
    $source = 'chat';
    if(isset($_POST['source']))
    {
        $source = $_POST['source'];
    }
    if($source === 'forms')
    {
        $source = 'shortcodeForms';
    }
    else
    {
        $source = 'shortcodeChat';
    }
	$temperature = sanitize_text_field($_POST['temp']);
    $inp_count = count(aiomatic_encode($input_text));
    $resp_count = count(aiomatic_encode($response_text));
    if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0)
        {
            aiomatic_log_to_file('Failed to save persistent conversation, invalid user ID sent');
        }
        else
        {
            $used_token_count = 0;
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_chat_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
            }
            else
            {
                $used_token_count = 0;
            }
            $used_token_count = intval($used_token_count) + $inp_count + $resp_count;
            update_user_meta($user_id, 'aiomatic_used_chat_tokens', $used_token_count);
        }
    }
    $session = aiomatic_get_session_id();
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $query = new Aiomatic_Query($input_text, 0, $model, $temperature, null, $source, 'text', $token, $session, 1, '', '');
    apply_filters( 'aiomatic_ai_reply', $response_text, $query );
    if($vision_file != '')
    {
        $stats = [
            "env" => $query->env,
            "session" => $query->session,
            "mode" => 'image',
            "model" => $query->model,
            "apiRef" => $query->apiKey,
            "units" => 1,
            "type" => 'images',
        ];
        if (empty($stats["price"])) {
            if (aiomatic_is_aiomaticapi_key($query->apiKey)) {
                $stats["price"] = 0;
            } else {
                $stats["price"] = $GLOBALS['aiomatic_stats']->getVisionPrice($query->model);
            }
        }
        $log_id = $GLOBALS['aiomatic_stats']->add($stats);
        if ($log_id && isset($aiomatic_Main_Settings['enable_detailed_tracking']) 
            && $aiomatic_Main_Settings['enable_detailed_tracking'] === 'on') {

            $GLOBALS['aiomatic_stats']->add_log_meta(
                $log_id,
                'prompt',
                aiomatic_normalize_log_payload($input_text ?? null) 
            );

            $GLOBALS['aiomatic_stats']->add_log_meta(
                $log_id,
                'result',
                aiomatic_normalize_log_payload($response_text ?? null)
            );
        }
    }
    $aiomatic_result = array('status' => 'success', 'msg' => 'ok');
    wp_send_json($aiomatic_result);
	wp_die();
}

add_action('wp_ajax_aiomatic_call_ai_function', 'aiomatic_call_ai_function');
//add_action('wp_ajax_nopriv_aiomatic_call_ai_function', 'aiomatic_call_ai_function');
function aiomatic_call_ai_function() 
{
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['func_call']))
    {
        aiomatic_log_to_file('Failed to call function, no func_call');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    if (!is_user_logged_in() || !current_user_can('manage_options')) {
        aiomatic_log_to_file('Blocked AI tool call: unauthorized user');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
        wp_die();
    }
    require_once(dirname(__FILE__) . "/aiomatic-god-mode-parser.php");

    $func_call = wp_unslash($_POST['func_call']);
    $func_call = json_decode(json_encode($func_call), FALSE);

    if (isset($func_call->tool_calls) && is_array($func_call->tool_calls)) {
        foreach ($func_call->tool_calls as $tool_call) {
            $name = isset($tool_call->function->name) && is_string($tool_call->function->name)
                ? sanitize_text_field($tool_call->function->name)
                : '';

            if (empty($name) || !aiomatic_is_restricted_function_allowed($name)) {
                aiomatic_log_to_file('Blocked tool (AJAX): ' . $name);
                wp_send_json(['scope' => 'fail', 'data' => 'fail']);
                wp_die();
            }
        }
    }

    $func_call = apply_filters( 'aiomatic_ai_reply_raw', $func_call, '');
    if(isset($func_call->init_data) && isset($func_call->aiomatic_tool_results))
    {
        $func_call_copy = clone $func_call;
        if(isset($func_call_copy->init_data))
        {
            unset($func_call_copy->init_data);
        }
        if(isset($func_call_copy->aiomatic_tool_results))
        {
            unset($func_call_copy->aiomatic_tool_results);
        }
        for($i = 0; $i < count($func_call->aiomatic_tool_results);$i++)
        {
            $func_call->aiomatic_tool_results[$i]['assistant_message'] = $func_call_copy;
        }
        $json_me = json_encode(array('scope' => 'response', 'data' => $func_call->aiomatic_tool_results));
        echo $json_me;
        wp_die();
    }
    elseif(isset($func_call->init_data) && isset($func_call->aiomatic_tool_direct_message))
    {
        $my_message = '';
        foreach($func_call->aiomatic_tool_direct_message as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'user_message', 'data' => $my_message));
        wp_die();
    }
    elseif(isset($func_call->init_data) && isset($func_call->aiomatic_tool_end_conversation))
    {
        $my_message = '';
        foreach($func_call->aiomatic_tool_end_conversation as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'end_message', 'data' => $my_message));
        wp_die();
    }
    else
    {
        aiomatic_log_to_file('Failed to parse result call: ' . print_r($func_call, true));
    }
    wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	wp_die();
}
add_action('wp_ajax_aiomatic_call_ai_function_responses', 'aiomatic_call_ai_function_responses');
//add_action('wp_ajax_nopriv_aiomatic_call_ai_function_responses', 'aiomatic_call_ai_function_responses');
function aiomatic_call_ai_function_responses() 
{
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['func_call']))
    {
        aiomatic_log_to_file('Failed to call function, no func_call');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    if (!is_user_logged_in() || !current_user_can('manage_options')) {
        aiomatic_log_to_file('Blocked AI tool call: unauthorized user');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
        wp_die();
    }
    if(!isset($_POST['function_name']))
    {
        aiomatic_log_to_file('Failed to call function, no function_name');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    if(!isset($_POST['function_call_id']))
    {
        aiomatic_log_to_file('Failed to call function, no function_call_id');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    require_once(dirname(__FILE__) . "/aiomatic-god-mode-parser.php");
    $function_name = wp_unslash($_POST['function_name']);
    $func_call = wp_unslash($_POST['func_call']);
    $function_call_id = wp_unslash($_POST['function_call_id']);

    if (!aiomatic_is_restricted_function_allowed($function_name)) {
        aiomatic_log_to_file('Blocked tool (central filter): ' . $function_name);
        wp_send_json(array('scope' => 'fail', 'data'  => 'fail'));
        wp_die();
    }

    $simulate_ai_response = new stdClass();
    $simulate_ai_response->output = array();
    $simulated_function_call = new stdClass();
    $simulated_function_call->type = 'function_call';
    $simulated_function_call->arguments = $func_call;
    $simulated_function_call->name = $function_name;
    $simulated_function_call->id = $function_call_id;
    $simulate_ai_response->output[] = $simulated_function_call;
    $simulate_ai_response = apply_filters( 'aiomatic_ai_responses_reply_raw', $simulate_ai_response, '');
    if(isset($simulate_ai_response->aiomatic_tool_results))
    {
        $simulate_ai_response_copy = clone $simulate_ai_response;
        if(isset($simulate_ai_response_copy->aiomatic_tool_results))
        {
            unset($simulate_ai_response_copy->aiomatic_tool_results);
        }
        for($i = 0; $i < count($simulate_ai_response->aiomatic_tool_results);$i++)
        {
            $simulate_ai_response->aiomatic_tool_results[$i]['assistant_message'] = $simulate_ai_response_copy;
        }
        $json_me = json_encode(array('scope' => 'response', 'data' => $simulate_ai_response->aiomatic_tool_results));
        echo $json_me;
        wp_die();
    }
    elseif(isset($simulate_ai_response->aiomatic_tool_direct_message))
    {
        $my_message = '';
        foreach($simulate_ai_response->aiomatic_tool_direct_message as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'user_message', 'data' => $my_message));
        wp_die();
    }
    elseif(isset($simulate_ai_response->aiomatic_tool_end_conversation))
    {
        $my_message = '';
        foreach($simulate_ai_response->aiomatic_tool_end_conversation as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'end_message', 'data' => $my_message));
        wp_die();
    }
    else
    {
        aiomatic_log_to_file('Failed to parse result: ' . print_r($simulate_ai_response, true));
    }
    wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	wp_die();
}

add_action('wp_ajax_aiomatic_call_ai_function_realtime', 'aiomatic_call_ai_function_realtime');
//add_action('wp_ajax_nopriv_aiomatic_call_ai_function_realtime', 'aiomatic_call_ai_function_realtime');
function aiomatic_call_ai_function_realtime() 
{
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['name']))
    {
        aiomatic_log_to_file('Failed to call realtime function, no name');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    if (!is_user_logged_in() || !current_user_can('manage_options')) {
        aiomatic_log_to_file('Blocked AI tool call: unauthorized user');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
        wp_die();
    }
    if(!isset($_POST['args']))
    {
        aiomatic_log_to_file('Failed to call realtime function, no args');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    if(!isset($_POST['call_id']))
    {
        aiomatic_log_to_file('Failed to call realtime function, no call_id');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    require_once(dirname(__FILE__) . "/aiomatic-god-mode-parser.php");
    $name    = sanitize_key(wp_unslash($_POST['name']));
    $call_id = sanitize_text_field(wp_unslash($_POST['call_id']));
    $args = wp_unslash($_POST['args']);
    
    if (!aiomatic_is_restricted_function_allowed($name)) {
        aiomatic_log_to_file('Blocked tool (central filter): ' . $name);
        wp_send_json(array('scope' => 'fail', 'data'  => 'fail'));
        wp_die();
    }

    $reply = new stdClass();
    $reply->tool_calls = array();
    $the_function = new stdClass();
    $the_function->type = 'function';
    $func_details = new stdClass();
    $func_details->name = $name;
    $func_details->arguments = $args;
    $the_function->id = $call_id;
    $the_function->function = $func_details;
    $reply->tool_calls[] = $the_function;

    $reply = apply_filters( 'aiomatic_ai_reply_raw', $reply, '');
    if(isset($reply->aiomatic_tool_results))
    {
        $func_call_copy = clone $reply;
        if(isset($func_call_copy->aiomatic_tool_results))
        {
            unset($func_call_copy->aiomatic_tool_results);
        }
        for($i = 0; $i < count($reply->aiomatic_tool_results);$i++)
        {
            $reply->aiomatic_tool_results[$i]['assistant_message'] = $func_call_copy;
        }
        $json_me = json_encode(array('scope' => 'response', 'data' => $reply->aiomatic_tool_results));
        echo $json_me;
        wp_die();
    }
    elseif(isset($reply->aiomatic_tool_direct_message))
    {
        $my_message = '';
        foreach($reply->aiomatic_tool_direct_message as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'user_message', 'data' => $my_message));
        wp_die();
    }
    elseif(isset($reply->aiomatic_tool_end_conversation))
    {
        $my_message = '';
        foreach($reply->aiomatic_tool_end_conversation as $dm)
        {
            $my_message .= $dm['content'] . ' ';
        }
        $my_message = trim($my_message);
        wp_send_json(array('scope' => 'end_message', 'data' => $my_message));
        wp_die();
    }
    else
    {
        aiomatic_log_to_file('Failed to parse realtime result: ' . print_r($reply, true));
    }
    
    wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	wp_die();
}

add_action('wp_ajax_aiomatic_call_google_ai_function', 'aiomatic_call_google_ai_function');
add_action('wp_ajax_nopriv_aiomatic_call_google_ai_function', 'aiomatic_call_google_ai_function');
function aiomatic_call_google_ai_function() 
{
	check_ajax_referer('openai-persistent-nonce', 'nonce');
    if(!isset($_POST['func_call']))
    {
        aiomatic_log_to_file('Failed to call google function, no func_call');
        wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	    wp_die();
    }
    require_once(dirname(__FILE__) . "/aiomatic-god-mode-parser.php");
    $func_call = $_POST['func_call'];
    $current_data = json_decode(json_encode($func_call), true);
    if (
        !is_array($current_data) ||
        empty($current_data['name']) ||
        !is_string($current_data['name'])
    ) {
        aiomatic_log_to_file('Invalid tool call structure');
        wp_send_json(['scope' => 'fail', 'data' => 'fail']);
        wp_die();
    }
    $tool_name = sanitize_text_field($current_data['name']);
    
    if(!aiomatic_is_restricted_function_allowed($tool_name))
    {
        aiomatic_log_to_file('Blocked private realtime tool call: ' . $tool_name);
        wp_send_json(array('scope' => 'fail', 'data'  => 'fail'));
        wp_die();
    }

    $filter_tool_object = new stdClass();
    $filter_tool_object->tool_calls = array();
    $tool_oarr = new stdClass();
    $tool_oarr->type = 'function';
    $tool_oarr->id = uniqid();
    $tool_oarr->function = new stdClass();
    $tool_oarr->function->name = $tool_name;
    $tool_oarr->function->arguments = (object) $current_data['args'];
    $filter_tool_object->tool_calls[] = $tool_oarr;
    $filter_tool_object = apply_filters('aiomatic_google_tools_ai_reply_raw', $filter_tool_object);
    if(isset($filter_tool_object->aiomatic_tool_direct_message) && isset($filter_tool_object->aiomatic_tool_direct_message[0]) && isset($filter_tool_object->aiomatic_tool_direct_message[0]['content']) && !empty($filter_tool_object->aiomatic_tool_direct_message[0]['content']))
    {
        $json_me = json_encode(array('scope' => 'user_message', 'data' => $filter_tool_object->aiomatic_tool_direct_message[0]['content']));
        echo $json_me;
        wp_die();
    }
    if(isset($filter_tool_object->aiomatic_tool_end_conversation) && isset($filter_tool_object->aiomatic_tool_end_conversation[0]) && isset($filter_tool_object->aiomatic_tool_end_conversation[0]['content']) && !empty($filter_tool_object->aiomatic_tool_end_conversation[0]['content']))
    {
        $json_me = json_encode(array('scope' => 'end_message', 'data' => $filter_tool_object->aiomatic_tool_end_conversation[0]['content']));
        echo $json_me;
        wp_die();
    }
    if(isset($filter_tool_object->aiomatic_tool_results) && isset($filter_tool_object->aiomatic_tool_results[0]) && isset($filter_tool_object->aiomatic_tool_results[0]['content']) && !empty($filter_tool_object->aiomatic_tool_results[0]['content']))
    {
        remove_filter('aiomatic_ai_functions', 'aiomatic_add_god_mode', 999);
        $json_me = json_encode(array('scope' => 'response', 'data' => $filter_tool_object->aiomatic_tool_results[0]['content']));
        echo $json_me;
        wp_die();
    }
    wp_send_json(array('scope' => 'fail', 'data' => 'fail'));
	wp_die();
}

add_action('wp_ajax_aiomatic_create_thread', 'aiomatic_create_thread');
add_action('wp_ajax_nopriv_aiomatic_create_thread', 'aiomatic_create_thread');
function aiomatic_create_thread() 
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with assistant thread creation');
	check_ajax_referer('openai-ajax-nonce', 'nonce');
    $assistant_id = isset($_POST['assistantid']) ? sanitize_text_field($_POST['assistantid']) : '';
    $file_data = isset($_POST['file_data']) ? sanitize_text_field($_POST['file_data']) : '';
    if(empty($assistant_id))
    {
        $aiomatic_result['msg'] = 'No assistant ID passed';
        wp_send_json($aiomatic_result);
        wp_die();
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
        $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
        wp_die();
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
            wp_die();
        }
        if(aiomatic_is_aiomaticapi_key($token))
        {
            $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for audio processing.';
            wp_send_json($aiomatic_result);
            wp_die();
        }
    }
    $assistant_first_message = '';
    require_once (dirname(__FILE__) . "/res/aiomatic-assistants-api.php"); 
    try
    {
        $local_assistant_id = aiomatic_find_local_assistant_id($assistant_id);
        if($local_assistant_id === false)
        {
            $aiomatic_result['msg'] = 'Failed to find local assistant ID for: ' . $assistant_id;
            wp_send_json($aiomatic_result);
            wp_die();
        }
        $assistant_first_message = get_post_meta($local_assistant_id, '_assistant_first_message', true);
        if(!empty($assistant_first_message))
        {
            $simulate_conv = array();
            $simulate_conv[] = array("role" => 'assistant', "content" => $assistant_first_message);
        }
        else
        {
            $simulate_conv = [];
        }
        $thread = aiomatic_openai_create_thread($token, $simulate_conv, $file_data);
        if(!isset($thread['id']))
        {
            $aiomatic_result['msg'] = 'Invalid thread format: ' . print_r($thread, true);
            wp_send_json($aiomatic_result);
            wp_die();
        }
        $thread_id = $thread['id'];
        $aiomatic_result['status'] = 'success';
        $aiomatic_result['data'] = $thread_id;
    }
    catch(Exception $e)
    {
        $aiomatic_result['msg'] = 'Exception in thread creation: ' . $e->getMessage();
    }
    wp_send_json($aiomatic_result);
	wp_die();
}

add_action('wp_ajax_aiomatic_log_realtime_usage', 'aiomatic_log_realtime_usage');
add_action('wp_ajax_nopriv_aiomatic_log_realtime_usage', 'aiomatic_log_realtime_usage');
function aiomatic_log_realtime_usage() 
{
    check_ajax_referer('openai-ajax-nonce', 'nonce');
    if(!isset($_POST['usage_data']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Usage data not provided');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    if(!isset($_POST['model']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Model not provided');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    $usage_data_json = isset($_POST['usage_data']) ? stripslashes($_POST['usage_data']) : '';
    $usage_data = json_decode($usage_data_json, true);
    if($usage_data === null)
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to decode usage data');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
    $model = $_POST['model'];
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if(!isset($_POST['user_id']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no user_id');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$user_id = sanitize_text_field($_POST['user_id']);
    if(!isset($_POST['user_token_cap_per_day']))
    {
        $aiomatic_result = array('status' => 'error', 'msg' => 'Failed to save persistent conversation, no user_token_cap_per_day');
        wp_send_json($aiomatic_result);
	    wp_die();
    }
	$user_token_cap_per_day = sanitize_text_field($_POST['user_token_cap_per_day']);
    $inp_count = 0;
    if(isset($usage_data['usage']['input_tokens']))
    {
        $inp_count = intval($usage_data['usage']['input_tokens']);
    }
    $resp_count = 0;
    if(isset($usage_data['usage']['output_tokens']))
    {
        $resp_count = intval($usage_data['usage']['output_tokens']);
    }
    $aiomatic_Limit_Settings = get_option('aiomatic_Limit_Settings', false);
    $dummy_query = new stdClass();
    $ok = apply_filters( 'aiomatic_ai_allowed', true, $aiomatic_Limit_Settings, $dummy_query );
    if ( $ok !== true ) {
        $aiomatic_result = array('status' => 'usage', 'msg' => esc_html__('Chatbot is rate limited:', 'aiomatic-automatic-ai-content-writer') . ' ' . $ok);
        wp_send_json($aiomatic_result);
        wp_die();
    }
    $used_token_count = 0;
    if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0 || !is_numeric($user_id))
        {
            /* translators: %s: URL */
            $aiomatic_result = array('status' => 'usage', 'msg' => sprintf( wp_kses( __( 'You are not allowed to access this form if you are not logged in. Please <a href="%s" target="_blank">log in</a> to continue.', 'aiomatic-automatic-ai-content-writer'), array(  'a' => array( 'href' => array(), 'target' => array() ) ) ), wp_login_url(get_permalink()) ));
            wp_send_json($aiomatic_result);
            wp_die();
        }
        $used_token_count = get_user_meta($user_id, 'aiomatic_used_realtime_chat_tokens', true);
        if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
        {
            $used_token_count = intval($used_token_count);
            if($used_token_count > $user_token_cap_per_day)
            {
                $aiomatic_result = array('status' => 'usage', 'msg' => esc_html__('Daily token count of your user account was exceeded! Please try again tomorrow.', 'aiomatic-automatic-ai-content-writer'));
                wp_send_json($aiomatic_result);
                wp_die();
            }
        }
        else
        {
            $used_token_count = 0;
        }
    }
    if($user_token_cap_per_day != '' && is_numeric($user_token_cap_per_day))
    {
        if(empty($user_id) || $user_id == 0)
        {
            aiomatic_log_to_file('Failed to save persistent conversation, invalid user ID sent');
        }
        else
        {
            $used_token_count = 0;
            $used_token_count = get_user_meta($user_id, 'aiomatic_used_realtime_chat_tokens', true);
            if($used_token_count !== '' && $used_token_count !== false && is_numeric($used_token_count))
            {
                $used_token_count = intval($used_token_count);
            }
            else
            {
                $used_token_count = 0;
            }
            $used_token_count = intval($used_token_count) + $inp_count + $resp_count;
            update_user_meta($user_id, 'aiomatic_used_realtime_chat_tokens', $used_token_count);
        }
    }
    $session = aiomatic_get_session_id();
    $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
    $appids = array_filter($appids);
    if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
    $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
    $stats = [
        "env" => 'realtimeChat',
        "session" => $session,
        "mode" => 'voice',
        "model" => $model,
        "apiRef" => $token,
        "units" => $inp_count + $resp_count,
        "type" => 'tokens',
    ];
    if (aiomatic_is_aiomaticapi_key($token)) {
        $stats["price"] = 0;
    } else {
        $stats["price"] = $GLOBALS['aiomatic_stats']->calculateRealtimeCost($usage_data['usage'], $model);
    }
    $GLOBALS['aiomatic_stats']->add($stats);
    $aiomatic_result = array('status' => 'success', 'msg' => 'ok');
    wp_send_json($aiomatic_result);
	wp_die();
}

add_action( 'wp_ajax_aiomatic_audio_converter', 'aiomatic_audio_converter' );
add_action( 'wp_ajax_nopriv_aiomatic_audio_converter', 'aiomatic_audio_converter' );
function aiomatic_audio_converter()
{
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with audio converter');
    if ( !wp_verify_nonce( $_POST['nonce'], 'openai-audio-nonce' ) ) {
        $aiomatic_result['msg'] = 'You are not allowed to execute this action!';
        wp_send_json($aiomatic_result);
    }
    $purpose = isset($_REQUEST['audio_purpose']) && !empty($_REQUEST['audio_purpose']) ? sanitize_text_field($_REQUEST['audio_purpose']) : 'transcriptions';
    $prompt = isset($_REQUEST['prompt']) && !empty($_REQUEST['prompt']) ? sanitize_text_field($_REQUEST['prompt']) : '';
    $type = isset($_REQUEST['type']) && !empty($_REQUEST['type']) ? sanitize_text_field($_REQUEST['type']) : 'upload';
    $url = isset($_REQUEST['url']) && !empty($_REQUEST['url']) ? sanitize_text_field($_REQUEST['url']) : '';
    $model = isset($_REQUEST['model']) && !empty($_REQUEST['model']) ? sanitize_text_field($_REQUEST['model']) : 'whisper-1';
    $temperature = isset($_REQUEST['temperature']) && !empty($_REQUEST['temperature']) ? sanitize_text_field($_REQUEST['temperature']) : 0;
    $language = isset($_REQUEST['language']) && !empty($_REQUEST['language']) ? sanitize_text_field($_REQUEST['language']) : 'en';
    $mime_types = ['mp3' => 'audio/mpeg','mp4' => 'video/mp4','mpeg' => 'video/mpeg','m4a' => 'audio/m4a','wav' => 'audio/wav','webm' => 'video/webm'];
    if($purpose != 'transcriptions' && $purpose != 'translations')
    {
        $aiomatic_result['msg'] = 'Unknown purpose submitted.';
        wp_send_json($aiomatic_result);
    }
    if($type == 'upload' && !isset($_FILES['file'])){
        $aiomatic_result['msg'] = 'An audio file is mandatory.';
        wp_send_json($aiomatic_result);
    }
    if($type == 'record' && !isset($_FILES['recorded_audio'])){
        $aiomatic_result['msg'] = 'An audio recording is mandatory.';
        wp_send_json($aiomatic_result);
    }
    if($type == 'upload'){
        $file = $_FILES['file'];
        $file_name = sanitize_file_name(basename($file['name']));
        $filetype = wp_check_filetype($file_name);
        if(!in_array($filetype['type'], $mime_types)){
            $aiomatic_result['msg'] = 'We only accept mp3, mp4, mpeg, mpga, m4a, wav, or webm.';
            wp_send_json($aiomatic_result);
        }
        if($file['size'] > 26214400){
            $aiomatic_result['msg'] = 'Audio file maximum 25MB';
            wp_send_json($aiomatic_result);
        }
    }
    if($type == 'record'){
        $file = $_FILES['recorded_audio'];
        $file_name = sanitize_file_name(basename($file['name']));
        $filetype = wp_check_filetype($file_name);
        if(!in_array($filetype['type'], $mime_types)){
            $aiomatic_result['msg'] = 'We only accept mp3, mp4, mpeg, mpga, m4a, wav, or webm.';
            wp_send_json($aiomatic_result);
        }
        if($file['size'] > 26214400){
            $aiomatic_result['msg'] = 'Audio file maximum 25MB';
            wp_send_json($aiomatic_result);
        }
        $tmp_file = $file['tmp_name'];
    }
    if($type == 'url'){
        if(empty($url)){
            $aiomatic_result['msg'] = 'The audio URL is required';
            wp_send_json($aiomatic_result);
        }
        $remoteFile = get_headers($url, 1);
        $file_name = basename($url);
        $is_in_mime_types = false;
        $file_ext = '';
        foreach($mime_types as $key=>$mime_type){
            if((is_array($remoteFile['Content-Type']) && in_array($mime_type,$remoteFile['Content-Type'])) || strpos($remoteFile['Content-Type'], $mime_type) !== false){
                $is_in_mime_types = true;
                $file_ext = '.'.$key;
                break;
            }
        }
        if(!$is_in_mime_types){
            $aiomatic_result['msg'] = 'We only accept mp3, mp4, mpeg, mpga, m4a, wav, or webm.';
            wp_send_json($aiomatic_result);
        }

        if(strpos($file_name, $file_ext) === false){
            $file_name = md5(uniqid() . time()) . $file_ext;
        }
        if($remoteFile['Content-Length'] > 26214400){
            $aiomatic_result['msg'] = 'Audio file maximum 25MB';
            wp_send_json($aiomatic_result);
        }
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
        $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
        if(aiomatic_is_aiomaticapi_key($token))
        {
            $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for audio processing.';
            wp_send_json($aiomatic_result);
        }
    }
    if (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Azure/Claude API is not currently supported for audio conversion.';
        wp_send_json($aiomatic_result);
    }
    require_once (dirname(__FILE__) . "/res/openai/Url.php"); 
    require_once (dirname(__FILE__) . "/res/openai/OpenAi.php"); 
    $open_ai = new OpenAi($token);
    if(!$open_ai){
        $aiomatic_result['msg'] = 'Missing API Setting';
        wp_send_json($aiomatic_result);
    }
    if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
    {
        $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
    }
    if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
    {
        $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
    }
    if($type == 'url'){
        if(!function_exists('download_url')){
            include_once( ABSPATH . 'wp-admin/includes/file.php' );
        }
        $tmp_file = download_url($url);
        if ( is_wp_error( $tmp_file ) ){
            $aiomatic_result['msg'] = $tmp_file->get_error_message();
            wp_send_json($aiomatic_result);
        }
    }
    if($type == 'upload'){
        $tmp_file = $file['tmp_name'];
    }
    $response_format = 'text';
    global $wp_filesystem;
    if ( ! is_a( $wp_filesystem, 'WP_Filesystem_Base') ){
        include_once(ABSPATH . 'wp-admin/includes/file.php');$creds = request_filesystem_credentials( site_url() );
        wp_filesystem($creds);
    }
    $data_request = array(
        'audio' => array(
            'filename' => $file_name,
            'data' => $wp_filesystem->get_contents($tmp_file)
        ),
        'model' => $model,
        'temperature' => $temperature,
        'response_format' => $response_format,
        'prompt' => $prompt
    );
    if($purpose == 'transcriptions' && !empty($language)){
        $data_request['language'] = $language;
    }
    $delay = '';
    if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
    {
        if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
        {
            $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
            if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
            {
                $delay = rand(trim($tempo[0]), trim($tempo[1]));
            }
        }
        else
        {
            if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
            {
                $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
            }
        }
    }
    if($delay != '' && is_numeric($delay))
    {
        usleep($delay);
    }
    if($purpose == 'transcriptions')
    {
        $completion = $open_ai->transcribe($data_request);
    }
    elseif($purpose == 'translations')
    {
        $completion = $open_ai->translate($data_request);
    }
    $result = json_decode($completion);
    if($result && isset($result->error)){
        $aiomatic_result['msg'] = $result->error->message;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['status'] = 'success';
    $text_generated = $completion;
    $aiomatic_result['data'] = $text_generated;
    if(empty($text_generated)){
        $aiomatic_result['msg'] = 'OpenAI returned empty content';
        wp_send_json($aiomatic_result);
    }
    do_action('aiomatic_audio_converter_reply', $aiomatic_result);
    wp_send_json($aiomatic_result);
    die();
}
add_action( 'wp_ajax_aiomatic_moderate_text', 'aiomatic_moderate_text' );
add_action( 'wp_ajax_nopriv_aiomatic_moderate_text', 'aiomatic_moderate_text' );
function aiomatic_moderate_text()
{
    check_ajax_referer('openai-moderation-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'msg' => 'Something went wrong with text moderation');
    $text = isset($_REQUEST['text']) && !empty($_REQUEST['text']) ? sanitize_text_field($_REQUEST['text']) : '';
    if(empty($text))
    {
        $aiomatic_result['msg'] = 'You need to enter a text to moderate!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!aiomatic_check_api_keys($aiomatic_Main_Settings)) {
        $aiomatic_result['msg'] = 'You need to add an AI service API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['app_id']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
        if(aiomatic_is_aiomaticapi_key($token))
        {
            $aiomatic_result['msg'] = 'Currently only OpenAI API is supported for text moderation.';
            wp_send_json($aiomatic_result);
        }
    }
    if (aiomatic_check_if_azure_or_others($aiomatic_Main_Settings)) 
    {
        $aiomatic_result['msg'] = 'Azure/Claude API is not currently supported for moderation.';
        wp_send_json($aiomatic_result);
    }
    require_once (dirname(__FILE__) . "/res/openai/Url.php"); 
    require_once (dirname(__FILE__) . "/res/openai/OpenAi.php"); 
    $open_ai = new OpenAi($token);
    if(!$open_ai){
        $aiomatic_result['msg'] = 'Missing API Setting';
        wp_send_json($aiomatic_result);
    }
    if (isset($aiomatic_Main_Settings['api_selector']) && trim($aiomatic_Main_Settings['api_selector']) == 'custom' && isset($aiomatic_Main_Settings['custom_endpoint']) && trim($aiomatic_Main_Settings['custom_endpoint'], ' /') != '') 
    {
        $open_ai->setCustomURL(trim($aiomatic_Main_Settings['custom_endpoint'], ' /'));
    }
    if (isset($aiomatic_Main_Settings['openai_organization']) && $aiomatic_Main_Settings['openai_organization'] != '')
    {
        $open_ai->setORG($aiomatic_Main_Settings['openai_organization']);
    }
    $data_request = array(
        'input' => $text
    );
    if(isset($_REQUEST['model']) && !empty(trim($_REQUEST['model'])))
    {
        $data_request['model'] = trim($_REQUEST['model']);
    }
    $delay = '';
    if (isset($aiomatic_Main_Settings['request_delay']) && $aiomatic_Main_Settings['request_delay'] != '') 
    {
        if(stristr($aiomatic_Main_Settings['request_delay'], ',') !== false)
        {
            $tempo = explode(',', $aiomatic_Main_Settings['request_delay']);
            if(isset($tempo[1]) && is_numeric(trim($tempo[1])) && is_numeric(trim($tempo[0])))
            {
                $delay = rand(trim($tempo[0]), trim($tempo[1]));
            }
        }
        else
        {
            if(is_numeric(trim($aiomatic_Main_Settings['request_delay'])))
            {
                $delay = intval(trim($aiomatic_Main_Settings['request_delay']));
            }
        }
    }
    if($delay != '' && is_numeric($delay))
    {
        usleep($delay);
    }
    $moderation = $open_ai->moderation($data_request);
    $result = json_decode($moderation);
    if($result && isset($result->error)){
        $aiomatic_result['msg'] = $result->error->message;
        wp_send_json($aiomatic_result);
    }
    $aiomatic_result['status'] = 'success';
    $aiomatic_result['data'] = $moderation;
    do_action('aiomatic_text_moderation_reply', $aiomatic_result);
    wp_send_json($aiomatic_result);
    die();
}

add_action( 'wp_ajax_aiomatic_aidetector_check_text', 'aiomatic_aidetector_check_text' );
add_action( 'wp_ajax_nopriv_aiomatic_aidetector_check_text', 'aiomatic_aidetector_check_text' );
function aiomatic_aidetector_check_text()
{
    check_ajax_referer('openai-aidetector-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'message' => 'Something went wrong with text AI content check');
    $text = isset($_REQUEST['text']) && !empty($_REQUEST['text']) ? sanitize_text_field($_REQUEST['text']) : '';
    if(empty($text))
    {
        $aiomatic_result['message'] = 'You need to enter a text to check!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['plagiarism_api']) || trim($aiomatic_Main_Settings['plagiarism_api']) == '') {
        $aiomatic_result['message'] = 'You need to add a PlagiarismCheck API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['plagiarism_api']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token) && !aiomatic_check_if_azure($aiomatic_Main_Settings))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
    }
    $status_check = false;
    $language = 'en';
    if(isset($_POST['language']))
	{
		$language = trim($_POST['language']);
	}
    $postData = [
        'language' => $language,
        'text' => $text,
    ];
    $requestData = [];
    foreach ($postData as $name => $value) {
        $requestData[] = $name.'='.urlencode($value);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['plagiarism_api']) || trim($aiomatic_Main_Settings['plagiarism_api']) == '') 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'You need to enter a PlagiarismCheck API key in plugin settings!' ) );
        exit;
	}
    $ch = curl_init();
    if ($ch === false) 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in aidetector check request' ) );
        exit;
	}  
    curl_setopt($ch, CURLOPT_URL, 'https://plagiarismcheck.org/api/v1/chat-gpt/');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $requestData)); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-API-TOKEN:'. trim($aiomatic_Main_Settings['plagiarism_api'])
    ));
    
    $result = curl_exec($ch);
    if ($result === false) 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in ai detector check request result' ) );
        exit;
	}  
    curl_close($ch);

    $response = json_decode($result);
    if ($response === null) 
    {
        aiomatic_log_to_file('Failed to decode initial ai detector checker API result: ' . $result);
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode initial ai detector checker API result' ) );
        exit;
	} 
    if ($response->success) 
    {
        $timeout = 0;
        $max_time = 120;
        if(isset($response->data->id))
        {
            $id = $response->data->id;
        }
        else
        {
            aiomatic_log_to_file('Failed to decode ai detector checker API result: ' . print_r($response, true));
            wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode ai detector checker API result' ) );
            exit;
        }
        $ok_done = false;
        while($timeout < $max_time && !$ok_done)
        {
            sleep(5);
            $ch = curl_init();
            if ($ch === false) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in ai detector check request result success' ) );
                exit;
            }  
            curl_setopt($ch, CURLOPT_URL, 'https://plagiarismcheck.org/api/v1/chat-gpt/' . $id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_POST, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-API-TOKEN:'. trim($aiomatic_Main_Settings['plagiarism_api'])
            ));
            $status_check = curl_exec($ch);
            if ($status_check === false) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to execute curl in ai detector check request result success' ) );
                exit;
            }  
            curl_close($ch);
            $status = json_decode($status_check);
            if ($status === null) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode response in ai detector check request result success' ) );
                exit;
            }
            if (isset($status->data->conclusion_type) && !empty($status->data->conclusion_type)) 
            {
                $ok_done = true;
                $data = array();
                $data['status'] = 200;
                if(isset($status->data->likely_percent) && !empty($status->data->likely_percent))
                {
                    $data['percentage'] = $status->data->likely_percent;
                }
                else
                {
                    $data['percentage'] = $status->data->percent;
                }
                $data['report'] = $status->data->conclusion;
                $detected_chunks = array();
                if ($status && !empty($status->data->chunks)) 
                {
                    $content = $status->data->content;
                    $chunks = $status->data->chunks;
                    foreach ($chunks as $chunk) 
                    {
                        $start = $chunk->position[0];
                        $end = $chunk->position[1];
                        $extracted_text = aiomatic_substr($content, $start, $end - $start);
                        $detected_chunks[] =$extracted_text;
                    }
                }
                $data['detected_chunks'] = $detected_chunks;
                wp_send_json_success( array('status' => 'success', 'result' => $data) );
            }
            else
            {
                $timeout += 5;
            }
        }
        if(!$ok_done)
        {
            aiomatic_log_to_file('Failed to get status from ai detector checker API result: ' . $status_check);
            wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to get status from ai detector checker API result' ) );
            exit;
        }
    }
    else
    {
        if ($response->message) 
        {
            aiomatic_log_to_file('Error in AI detector API call: ' . $response->message);
            wp_send_json_error( array( 'status' => 'error', 'message' => 'Error in AI detector API call: ' . esc_html($response->message) ) );
            exit;
        }
        aiomatic_log_to_file('Failed to interpret ai detector checker API result: ' . $result);
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to interpret ai detector checker API result' ) );
        exit;
    }
    aiomatic_log_to_file('Failed to get status from ai detector checker API result (timeout)');
    wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to get status from ai detector checker API result' ) );
    exit;
}
add_action( 'wp_ajax_aiomatic_plagiarism_check_text', 'aiomatic_plagiarism_check_text' );
add_action( 'wp_ajax_nopriv_aiomatic_plagiarism_check_text', 'aiomatic_plagiarism_check_text' );
function aiomatic_plagiarism_check_text()
{
    check_ajax_referer('openai-plagiarism-nonce', 'nonce');
    $aiomatic_result = array('status' => 'error', 'message' => 'Something went wrong with text plagiarism check');
    $text = isset($_REQUEST['text']) && !empty($_REQUEST['text']) ? sanitize_text_field($_REQUEST['text']) : '';
    if(empty($text))
    {
        $aiomatic_result['message'] = 'You need to enter a text to check!';
        wp_send_json($aiomatic_result);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['plagiarism_api']) || trim($aiomatic_Main_Settings['plagiarism_api']) == '') {
        $aiomatic_result['message'] = 'You need to add a PlagiarismCheck API key in plugin settings for this shortcode to work.';
        wp_send_json($aiomatic_result);
    }
    else
    {
        $appids = preg_split('/\r\n|\r|\n/', trim($aiomatic_Main_Settings['plagiarism_api']));
        $appids = array_filter($appids);
        if(empty($appids)){$token = '';}else{$token = $appids[array_rand($appids)];}
        $token = apply_filters('aimogen_openai_api_key', $token);$token = apply_filters('aiomatic_openai_api_key', $token);
        if(empty($token) && !aiomatic_check_if_azure($aiomatic_Main_Settings))
        {
            $aiomatic_result['msg'] = 'Invalid API key submitted';
            wp_send_json($aiomatic_result);
        }
    }
    $status_check = false;
    $language = 'en';
    if(isset($_POST['language']))
	{
		$language = trim($_POST['language']);
	}
    $postData = [
        'language' => $language,
        'text' => $text,
    ];
    $requestData = [];
    foreach ($postData as $name => $value) {
        $requestData[] = $name.'='.urlencode($value);
    }
    $aiomatic_Main_Settings = get_option('aiomatic_Main_Settings', false);
    if (!isset($aiomatic_Main_Settings['plagiarism_api']) || trim($aiomatic_Main_Settings['plagiarism_api']) == '') 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'You need to enter a PlagiarismCheck API key in plugin settings!' ) );
        exit;
	}
    $ch = curl_init();
    if ($ch === false) 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in plagiarism check request' ) );
        exit;
	}  
    curl_setopt($ch, CURLOPT_URL, 'https://plagiarismcheck.org/api/v1/text');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, implode('&', $requestData)); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'X-API-TOKEN:'. trim($aiomatic_Main_Settings['plagiarism_api'])
    ));
    
    $result = curl_exec($ch);
    if ($result === false) 
    {
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in plagiarism check request result' ) );
        exit;
	}  
    curl_close($ch);

    $response = json_decode($result);
    if ($response === null) 
    {
        aiomatic_log_to_file('Failed to decode plagiarism checker API result: ' . $result);
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode plagiarism checker API result' ) );
        exit;
	} 
    if ($response->success) 
    {
        $timeout = 0;
        $max_time = 120;
        $id = $response->data->text->id;
        $ok_done = false;
        while($timeout < $max_time && !$ok_done)
        {
            sleep(5);
            $ch = curl_init();
            if ($ch === false) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in plagiarism check request result success' ) );
                exit;
            }  
            curl_setopt($ch, CURLOPT_URL, 'https://plagiarismcheck.org/api/v1/text/' . $id);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
            curl_setopt($ch, CURLOPT_POST, false);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'X-API-TOKEN:'. trim($aiomatic_Main_Settings['plagiarism_api'])
            ));
            $status_check = curl_exec($ch);
            if ($status_check === false) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to execute curl in plagiarism check request result success' ) );
                exit;
            }  
            curl_close($ch);
            $status = json_decode($status_check);
            if ($status === null) 
            {
                wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode response in plagiarism check request result success' ) );
                exit;
            }  
            if (isset($status->data->state) && $status->data->state === 5) 
            {
                $ch = curl_init();
                if ($ch === false) 
                {
                    wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to init curl in plagiarism check request result state' ) );
                    exit;
                }  
                curl_setopt($ch, CURLOPT_URL, 'https://plagiarismcheck.org/api/v1/text/report/' . $id);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
                curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
                curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
                curl_setopt($ch, CURLOPT_POST, false);
                curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                    'X-API-TOKEN:'. trim($aiomatic_Main_Settings['plagiarism_api'])
                ));
                $report_check = curl_exec($ch);
                if ($ch === false) 
                {
                    wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to exec curl in plagiarism check request result state' ) );
                    exit;
                }  
                curl_close($ch);
                $report = json_decode($report_check);
                if ($ch === null) 
                {
                    wp_send_json_error( array('status' => 'error',  'message' => 'Failed to decode request in plagiarism check request result state' ) );
                    exit;
                }
                $ok_done = true;
                $data = array();
                $data['status'] = 200;
                $data['percentage'] = $report->data->report->percent;
                $data['report'] = json_encode($report->data->report_data->sources);
                wp_send_json_success( array('status' => 'success', 'result' => $data) );
            }
            else
            {
                $timeout += 5;
            }
        }
        if(!$ok_done)
        {
            aiomatic_log_to_file('Failed to get status from plagiarism checker API result: ' . $status_check);
            wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to get status from plagiarism checker API result' ) );
            exit;
        }
    }
    else
    {
        if ($response->message) 
        {
            aiomatic_log_to_file('Error in plagiarism checker API call: ' . $response->message);
            wp_send_json_error( array( 'status' => 'error', 'message' => 'Error in plagiarism checker API call: ' . esc_html($response->message) ) );
            exit;
        }
        aiomatic_log_to_file('Failed to interpret plagiarism checker API result: ' . $result);
		wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to decode plagiarism checker API result' ) );
        exit;
    }
    aiomatic_log_to_file('Failed to get status from plagiarism checker API result (timeout)');
    wp_send_json_error( array( 'status' => 'error', 'message' => 'Failed to get status from plagiarism checker API result' ) );
    exit;
}

add_action( 'wp_ajax_aiomatic_execute_single_advanced_job', 'aiomatic_execute_single_advanced_job' );
function aiomatic_execute_single_advanced_job() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['post_data']))
	{
		wp_send_json_error( array( 'message' => 'post_data is required!' ) );
	}
	$post_data = $_POST['post_data'];
    if(empty($post_data))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid post_data!' ) );
    }
	if(!isset($_POST['selected']))
	{
		wp_send_json_error( array( 'message' => 'Selected options are required!' ) );
	}
	$selected = $_POST['selected'];
	if($selected != '1a' && $selected != '1a-' && $selected != '1b' && $selected != '2' && $selected != '3' && $selected != '4' && $selected != '5' && $selected != '6')
    {
        wp_send_json_error( array( 'message' => 'Selected job options are invalid: ' . $selected) );
    }
    $job_id = uniqid('job_', true);
    if($selected == '1a')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = 'test';//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '500';//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = '';//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = '';//max_continue_tokens
        $inner_arr[] = '';//model
        $inner_arr[] = '';//headings
        $inner_arr[] = '';//images
        $inner_arr[] = '';//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//headings_list
        $inner_arr[] = '';//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//title_model
        $inner_arr[] = '';//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = 'keyword';//title_source
        $inner_arr[] = '';//headings_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//headings_model
        $inner_arr[] = 'topic';//posting_mode
        $inner_arr[] = $post_data['post_topic_list'];//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $inner_arr[] = '';//enable_ai_images_article
        $inner_arr[] = '';//ai_command_image_article
        $inner_arr[] = '';//image_model_article
        $inner_arr[] = '';//image_size_article
        $type = 0;
    }
    elseif($selected == '1a-')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '500';//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = '';//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = '';//max_continue_tokens
        $inner_arr[] = '';//model
        $inner_arr[] = '';//headings
        $inner_arr[] = '';//images
        $inner_arr[] = '';//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//headings_list
        $inner_arr[] = '';//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//title_model
        $inner_arr[] = '';//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = 'keyword';//title_source
        $inner_arr[] = '';//headings_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//headings_model
        $inner_arr[] = 'topic';//posting_mode
        $inner_arr[] = '';//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $inner_arr[] = '';//enable_ai_images_article
        $inner_arr[] = '';//ai_command_image_article
        $inner_arr[] = '';//image_model_article
        $inner_arr[] = '';//image_size_article
        $type = 0;
    }
    elseif($selected == '1b')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = $post_data['min_char'];//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['ai_command'];//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['model'];//model
        $inner_arr[] = $post_data['headings'];//headings
        $inner_arr[] = $post_data['images'];//images
        $inner_arr[] = $post_data['videos'];//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = $post_data['headings_list'];//headings_list
        $inner_arr[] = $post_data['images_list'];//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['title_model'];//title_model
        $inner_arr[] = $post_data['title_ai_command'];//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['title_source'];//title_source
        $inner_arr[] = $post_data['headings_ai_command'];//headings_ai_command
        $inner_arr[] = $post_data['headings_model'];//headings_model
        $inner_arr[] = 'title';//posting_mode
        $inner_arr[] = '';//post_topic_list
        $inner_arr[] = '';//post_sections_list
        $inner_arr[] = 'English';//content_language
        $inner_arr[] = 'Creative';//writing_style
        $inner_arr[] = 'Neutral';//writing_tone
        $inner_arr[] = 'Write a title for an article about \"%%topic%%\" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.';//title_prompt
        $inner_arr[] = 'Write %%sections_count%% consecutive headings for an article about \"%%title%%\" that highlight specific aspects, provide detailed insights and specific recommendations. The headings must be written in %%language%%, following a %%writing_style%% style and a %%writing_tone%% tone. Don\'t add numbers to the headings, hyphens or any types of quotes. Return only the headings list, nothing else.';//sections_prompt
        $inner_arr[] = 'Write the content of a post section for the heading \"%%current_section%%\" in %%language%%. The title of the post is: \"%%title%%\". Don\'t add the title at the beginning of the created content. Be creative and unique. Don\'t repeat the heading in the created content. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%.';//content_prompt
        $inner_arr[] = 'Write a short excerpt for an article about \"%%title%%\" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters.';//excerpt_prompt
        $inner_arr[] = '3-4';//section_count
        $inner_arr[] = '2';//paragraph_count
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_title_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_sections_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_content_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_excerpt_model
        $inner_arr[] = '0';//single_content_call
        $inner_arr[] = 'Craft an introduction for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//intro_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_intro_model
        $inner_arr[] = 'Write an outro for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//outro_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_outro_model
        $inner_arr[] = '';//topic_images
        $inner_arr[] = 'h2';//sections_role
        $inner_arr[] = '';//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = '';//strip_by_regex_prompts
        $inner_arr[] = '';//replace_regex_prompts
        $inner_arr[] = 'content';//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = '0';//enable_toc
        $inner_arr[] = 'Table of Contents';//title_toc
        $inner_arr[] = 'Write a Q&A for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//qa_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_qa_model
        $inner_arr[] = '0';//enable_qa
        $inner_arr[] = 'Q&A';//title_qa
        $inner_arr[] = 'In Conclusion';//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '1';//img_all_headings
        $inner_arr[] = 'top';//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = 'ai';//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $inner_arr[] = '';//enable_ai_images_article
        $inner_arr[] = '';//ai_command_image_article
        $inner_arr[] = '';//image_model_article
        $inner_arr[] = '';//image_size_article
        $type = 0;
    }
    elseif($selected == '2')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['url_list'];//url_list
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['default_lang'];//default_lang
        $inner_arr[] = $post_data['ai_titles'];//ai_titles
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['max_caption'];//max_caption
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = $post_data['no_random'];//no_random
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 1;
    }
    elseif($selected == '3')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['amazon_keyword'];//url_list
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['affiliate_id'];//affiliate_id
        $inner_arr[] = $post_data['first_hand'];//first_hand
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['max_products'];//max_products
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['target_country'];//target_country
        $inner_arr[] = $post_data['min_price'];//min_price
        $inner_arr[] = $post_data['max_price'];//max_price
        $inner_arr[] = $post_data['sort_results'];//sort_results
        $inner_arr[] = $post_data['shuffle_products'];//shuffle_products
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['no_headlink'];//no_headlink
        $inner_arr[] = $post_data['enable_table'];//enable_table
        $inner_arr[] = $post_data['table_prompt'];//table_prompt
        $inner_arr[] = $post_data['topic_table_model'];//topic_table_model
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $inner_arr[] = $post_data['product_source'];//product_source
        $inner_arr[] = $post_data['neg_keywords'];//neg_keywords
        $inner_arr[] = $post_data['item_category'];//item_category
        $inner_arr[] = $post_data['one_time'];//one_time
        $inner_arr[] = $post_data['recurring'];//recurring
        $inner_arr[] = $post_data['affiliate_tools'];//affiliate_tools
        $inner_arr[] = $post_data['spotlight'];//spotlight
        $inner_arr[] = $post_data['mobile_hoplink'];//mobile_hoplink
        $inner_arr[] = $post_data['shipable_only'];//shipable_only
        $inner_arr[] = $post_data['pitch_plus'];//pitch_plus
        $inner_arr[] = $post_data['item_gravity'];//item_gravity
        $inner_arr[] = $post_data['dollar_sale'];//dollar_sale
        $inner_arr[] = $post_data['dollar_sale_av'];//dollar_sale_av
        $inner_arr[] = $post_data['rebill_total'];//rebill_total
        $inner_arr[] = $post_data['per_sale'];//per_sale
        $inner_arr[] = $post_data['per_rebill'];//per_rebill
        $inner_arr[] = $post_data['item_language'];//item_language
        $inner_arr[] = $post_data['sort_results_cb'];//sort_results_cb
        $inner_arr[] = $post_data['activation'];//activation
        $inner_arr[] = $post_data['cb_affiliate_id'];//cb_affiliate_id
        $inner_arr[] = $post_data['max_products_cb'];//max_products_cb
        $type = 2;
    }
    elseif($selected == '4')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['review_keyword'];//review_keyword
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['affiliate_id'];//affiliate_id
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['target_country'];//target_country
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['point_of_view'];//point_of_view
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['proscons_prompt'];//proscons_prompt
        $inner_arr[] = $post_data['topic_proscons_model'];//topic_proscons_model
        $inner_arr[] = $post_data['title_proscons'];//title_proscons
        $inner_arr[] = $post_data['enable_proscons'];//enable_proscons
        $inner_arr[] = $post_data['title_reviews'];//title_reviews
        $inner_arr[] = $post_data['enable_reviews'];//enable_reviews
        $inner_arr[] = $post_data['reviews_prompt'];//reviews_prompt
        $inner_arr[] = $post_data['topic_reviews_model'];//topic_reviews_model
        $inner_arr[] = $post_data['no_headlink'];//no_headlink
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $inner_arr[] = $post_data['product_source'];//product_source
        $inner_arr[] = $post_data['neg_keywords'];//neg_keywords
        $inner_arr[] = $post_data['item_category'];//item_category
        $inner_arr[] = $post_data['one_time'];//one_time
        $inner_arr[] = $post_data['recurring'];//recurring
        $inner_arr[] = $post_data['affiliate_tools'];//affiliate_tools
        $inner_arr[] = $post_data['spotlight'];//spotlight
        $inner_arr[] = $post_data['mobile_hoplink'];//mobile_hoplink
        $inner_arr[] = $post_data['shipable_only'];//shipable_only
        $inner_arr[] = $post_data['pitch_plus'];//pitch_plus
        $inner_arr[] = $post_data['item_gravity'];//item_gravity
        $inner_arr[] = $post_data['dollar_sale'];//dollar_sale
        $inner_arr[] = $post_data['dollar_sale_av'];//dollar_sale_av
        $inner_arr[] = $post_data['rebill_total'];//rebill_total
        $inner_arr[] = $post_data['per_sale'];//per_sale
        $inner_arr[] = $post_data['per_rebill'];//per_rebill
        $inner_arr[] = $post_data['item_language'];//item_language
        $inner_arr[] = $post_data['sort_results_cb'];//sort_results_cb
        $inner_arr[] = $post_data['activation'];//activation
        $inner_arr[] = $post_data['cb_affiliate_id'];//cb_affiliate_id
        $type = 3;
    }
    elseif($selected == '5')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['random_order'];//random_order
        $inner_arr[] = $post_data['csv_separator'];//csv_separator
        $type = 4;
    }
    elseif($selected == '6')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = 'test';//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['post_topic_list'];//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = $job_id;//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 6;
    }
    $rules = array($inner_arr);
    aiomatic_job_set_status_pending($job_id, array('step' => 'Job started'));

    $response = json_encode(array('success' => true, 'data' => array('job_id' => $job_id)));

    // Send the response headers
    if (!headers_sent()) {
        header('Content-Type: application/json; charset=utf-8');
        header('Content-Length: ' . aiomatic_strlen($response));
        header('Connection: close');
        header('Cache-Control: no-cache, must-revalidate');
        header('X-Accel-Buffering: no');
    }
    if (session_id()) {
        session_write_close();
    }

    // Clear all other buffers
    while (ob_get_level() > 0) {
        ob_end_clean();
    }
    
    // Turn off compression on the server
    if (function_exists('apache_setenv')) {
        apache_setenv('no-gzip', 1);
    }
    
    ini_set('zlib.output_compression', 0);

    // Flush all output to the client. The script will continue to run but the client connection will close
    echo $response;
    if (ob_get_level() > 0) 
    {
        ob_flush();
    }
    flush();

    // If you're running PHP-FPM, this will finish the request and allow the script to run in the background
    if (function_exists('fastcgi_finish_request')) {
        fastcgi_finish_request();
    }
    register_shutdown_function('aiomatic_fatal_clear_job', $job_id);
    //start actual work
    $return_me = aiomatic_run_rule(0, $type, 0, 1, $rules, '', '', false, []);
    if(!is_array($return_me) || !isset($return_me[0]) || !isset($return_me[1]))
    {
        aiomatic_job_set_status_failed($job_id, 'Rule running failed: ' . print_r($return_me, true));
        wp_die();
    }
    else
    {
        aiomatic_job_set_status_completed($job_id, array('content' => $return_me[0], 'title' => $return_me[1] ));
        wp_die();
    }
}

add_action( 'wp_ajax_aiomatic_execute_single_advanced', 'aiomatic_execute_single_advanced' );
function aiomatic_execute_single_advanced() {
	check_ajax_referer( 'openai-single-nonce', 'nonce' );
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
	if(!isset($_POST['post_data']))
	{
		wp_send_json_error( array( 'message' => 'post_data is required!' ) );
	}
	$post_data = $_POST['post_data'];
    if(empty($post_data))
    {
        wp_send_json_error( array( 'message' => 'You need to enter a valid post_data!' ) );
    }
	if(!isset($_POST['selected']))
	{
		wp_send_json_error( array( 'message' => 'Selected options are required!' ) );
	}
	$selected = $_POST['selected'];
	if($selected != '1a' && $selected != '1a-' && $selected != '1b' && $selected != '2' && $selected != '3' && $selected != '4' && $selected != '5' && $selected != '6')
    {
        wp_send_json_error( array( 'message' => 'Selected options are invalid: ' . $selected ) );
    }
    if($selected == '1a')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = 'test';//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '500';//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = '';//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = '';//max_continue_tokens
        $inner_arr[] = '';//model
        $inner_arr[] = '';//headings
        $inner_arr[] = '';//images
        $inner_arr[] = '';//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//headings_list
        $inner_arr[] = '';//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//title_model
        $inner_arr[] = '';//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = 'keyword';//title_source
        $inner_arr[] = '';//headings_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//headings_model
        $inner_arr[] = 'topic';//posting_mode
        $inner_arr[] = $post_data['post_topic_list'];//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 0;
    }
    elseif($selected == '1a-')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '500';//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = '';//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = '';//max_continue_tokens
        $inner_arr[] = '';//model
        $inner_arr[] = '';//headings
        $inner_arr[] = '';//images
        $inner_arr[] = '';//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//headings_list
        $inner_arr[] = '';//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//title_model
        $inner_arr[] = '';//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = 'keyword';//title_source
        $inner_arr[] = '';//headings_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//headings_model
        $inner_arr[] = 'topic';//posting_mode
        $inner_arr[] = '';//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 0;
    }
    elseif($selected == '1b')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = $post_data['min_char'];//min_char
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['ai_command'];//ai_command
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['model'];//model
        $inner_arr[] = $post_data['headings'];//headings
        $inner_arr[] = $post_data['images'];//images
        $inner_arr[] = $post_data['videos'];//videos
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = $post_data['headings_list'];//headings_list
        $inner_arr[] = $post_data['images_list'];//images_list
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['title_model'];//title_model
        $inner_arr[] = $post_data['title_ai_command'];//title_ai_command
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['title_source'];//title_source
        $inner_arr[] = $post_data['headings_ai_command'];//headings_ai_command
        $inner_arr[] = $post_data['headings_model'];//headings_model
        $inner_arr[] = 'title';//posting_mode
        $inner_arr[] = '';//post_topic_list
        $inner_arr[] = '';//post_sections_list
        $inner_arr[] = 'English';//content_language
        $inner_arr[] = 'Creative';//writing_style
        $inner_arr[] = 'Neutral';//writing_tone
        $inner_arr[] = 'Write a title for an article about \"%%topic%%\" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 40 and 60 characters.';//title_prompt
        $inner_arr[] = 'Write %%sections_count%% consecutive headings for an article about \"%%title%%\" that highlight specific aspects, provide detailed insights and specific recommendations. The headings must be written in %%language%%, following a %%writing_style%% style and a %%writing_tone%% tone. Don\'t add numbers to the headings, hyphens or any types of quotes. Return only the headings list, nothing else.';//sections_prompt
        $inner_arr[] = 'Write the content of a post section for the heading \"%%current_section%%\" in %%language%%. The title of the post is: \"%%title%%\". Don\'t add the title at the beginning of the created content. Be creative and unique. Don\'t repeat the heading in the created content. Don\'t add an intro or outro. Write %%paragraphs_per_section%% paragraphs in the section. Use HTML for formatting, include unnumbered lists and bold. If needed, you can use WordPress related CSS styling for the article. When applicable, add also HTML tables with WordPress styling (you can use WordPress table classes). If added, table data must be relevant, creative, short and simple. Writing Style: %%writing_style%%. Tone: %%writing_tone%%.';//content_prompt
        $inner_arr[] = 'Write a short excerpt for an article about \"%%title%%\" in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%. Must be between 150 and 250 characters.';//excerpt_prompt
        $inner_arr[] = '3-4';//section_count
        $inner_arr[] = '2';//paragraph_count
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_title_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_sections_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_content_model
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_excerpt_model
        $inner_arr[] = '0';//single_content_call
        $inner_arr[] = 'Craft an introduction for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//intro_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_intro_model
        $inner_arr[] = 'Write an outro for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//outro_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_outro_model
        $inner_arr[] = '';//topic_images
        $inner_arr[] = 'h2';//sections_role
        $inner_arr[] = '';//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = '';//strip_by_regex_prompts
        $inner_arr[] = '';//replace_regex_prompts
        $inner_arr[] = 'content';//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = '0';//enable_toc
        $inner_arr[] = 'Table of Contents';//title_toc
        $inner_arr[] = 'Write a Q&A for an article about \"%%title%%\", in %%language%%. Style: %%writing_style%%. Tone: %%writing_tone%%.';//qa_prompt
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//topic_qa_model
        $inner_arr[] = '0';//enable_qa
        $inner_arr[] = 'Q&A';//title_qa
        $inner_arr[] = 'In Conclusion';//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '1';//img_all_headings
        $inner_arr[] = 'top';//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = 'ai';//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 0;
    }
    elseif($selected == '2')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['url_list'];//url_list
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['default_lang'];//default_lang
        $inner_arr[] = $post_data['ai_titles'];//ai_titles
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['max_caption'];//max_caption
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = $post_data['no_random'];//no_random
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 1;
    }
    elseif($selected == '3')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['amazon_keyword'];//url_list
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['affiliate_id'];//affiliate_id
        $inner_arr[] = $post_data['first_hand'];//first_hand
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['max_products'];//max_products
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['target_country'];//target_country
        $inner_arr[] = $post_data['min_price'];//min_price
        $inner_arr[] = $post_data['max_price'];//max_price
        $inner_arr[] = $post_data['sort_results'];//sort_results
        $inner_arr[] = $post_data['shuffle_products'];//shuffle_products
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['no_headlink'];//no_headlink
        $inner_arr[] = $post_data['enable_table'];//enable_table
        $inner_arr[] = $post_data['table_prompt'];//table_prompt
        $inner_arr[] = $post_data['topic_table_model'];//topic_table_model
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 2;
    }
    elseif($selected == '4')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = $post_data['review_keyword'];//review_keyword
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['max_continue_tokens'];//max_continue_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['affiliate_id'];//affiliate_id
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['target_country'];//target_country
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = $post_data['point_of_view'];//point_of_view
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['proscons_prompt'];//proscons_prompt
        $inner_arr[] = $post_data['topic_proscons_model'];//topic_proscons_model
        $inner_arr[] = $post_data['title_proscons'];//title_proscons
        $inner_arr[] = $post_data['enable_proscons'];//enable_proscons
        $inner_arr[] = $post_data['title_reviews'];//title_reviews
        $inner_arr[] = $post_data['enable_reviews'];//enable_reviews
        $inner_arr[] = $post_data['reviews_prompt'];//reviews_prompt
        $inner_arr[] = $post_data['topic_reviews_model'];//topic_reviews_model
        $inner_arr[] = $post_data['no_headlink'];//no_headlink
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 3;
    }
    elseif($selected == '5')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = $post_data['post_title'];//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '0';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = '';//parent_id
        $inner_arr[] = uniqid();//rule_unique_id
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['random_order'];//random_order
        $inner_arr[] = $post_data['csv_separator'];//csv_separator
        $type = 4;
    }
    elseif($selected == '6')
    {
        $inner_arr = array();
        $inner_arr[] = '24';//schedule
        $inner_arr[] = '1';//active
        $inner_arr[] = '1988-01-27 00:00:00';//last_run
        $inner_arr[] = '1';//max
        $inner_arr[] = 'publish';//post_status
        $inner_arr[] = 'post';//post_type
        $inner_arr[] = 'rand';//post_user_name
        $inner_arr[] = '';//item_create_tag
        $inner_arr[] = array('aiomatic_no_category_12345678');//default_category
        $inner_arr[] = 'disabled';//auto_categories
        $inner_arr[] = 'disabled';//can_create_tag
        $inner_arr[] = '0';//enable_comments
        $inner_arr[] = '';//image_url
        $inner_arr[] = 'test';//post_title
        $inner_arr[] = '0';//enable_pingback
        $inner_arr[] = 'post-format-standard';//post_format
        $inner_arr[] = '';//custom_fields
        $inner_arr[] = '';//custom_tax
        $inner_arr[] = $post_data['temperature'];//temperature
        $inner_arr[] = $post_data['top_p'];//top_p
        $inner_arr[] = $post_data['presence_penalty'];//presence_penalty
        $inner_arr[] = $post_data['frequency_penalty'];//frequency_penalty
        $inner_arr[] = '0';//royalty_free
        $inner_arr[] = $post_data['max_tokens'];//max_tokens
        $inner_arr[] = $post_data['max_seed_tokens'];//max_seed_tokens
        $inner_arr[] = $post_data['post_prepend'];//post_prepend
        $inner_arr[] = $post_data['post_append'];//post_append
        $inner_arr[] = $post_data['enable_ai_images'];//enable_ai_images
        $inner_arr[] = $post_data['ai_command_image'];//ai_command_image
        $inner_arr[] = $post_data['image_size'];//image_size
        $inner_arr[] = '';//wpml_lang
        $inner_arr[] = '1';//remove_default
        $inner_arr[] = $post_data['strip_title'];//strip_title
        $inner_arr[] = '0';//title_once
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//category_model
        $inner_arr[] = '';//category_ai_command
        $inner_arr[] = AIMOGEN_DEFAULT_MODEL;//tag_model
        $inner_arr[] = '';//tag_ai_command
        $inner_arr[] = '';//min_time
        $inner_arr[] = '';//max_time
        $inner_arr[] = $post_data['skip_spin'];//skip_spin
        $inner_arr[] = $post_data['skip_translate'];//skip_translate
        $inner_arr[] = $post_data['post_topic_list'];//post_topic_list
        $inner_arr[] = $post_data['post_sections_list'];//post_sections_list
        $inner_arr[] = $post_data['content_language'];//content_language
        $inner_arr[] = $post_data['writing_style'];//writing_style
        $inner_arr[] = $post_data['writing_tone'];//writing_tone
        $inner_arr[] = $post_data['title_prompt'];//title_prompt
        $inner_arr[] = $post_data['sections_prompt'];//sections_prompt
        $inner_arr[] = $post_data['content_prompt'];//content_prompt
        $inner_arr[] = $post_data['excerpt_prompt'];//excerpt_prompt
        $inner_arr[] = $post_data['section_count'];//section_count
        $inner_arr[] = $post_data['paragraph_count'];//paragraph_count
        $inner_arr[] = $post_data['topic_title_model'];//topic_title_model
        $inner_arr[] = $post_data['topic_sections_model'];//topic_sections_model
        $inner_arr[] = $post_data['topic_content_model'];//topic_content_model
        $inner_arr[] = $post_data['topic_excerpt_model'];//topic_excerpt_model
        $inner_arr[] = $post_data['single_content_call'];//single_content_call
        $inner_arr[] = $post_data['intro_prompt'];//intro_prompt
        $inner_arr[] = $post_data['topic_intro_model'];//topic_intro_model
        $inner_arr[] = $post_data['outro_prompt'];//outro_prompt
        $inner_arr[] = $post_data['topic_outro_model'];//topic_outro_model
        $inner_arr[] = $post_data['topic_images'];//topic_images
        $inner_arr[] = $post_data['sections_role'];//sections_role
        $inner_arr[] = $post_data['topic_videos'];//topic_videos
        $inner_arr[] = '';//rule_description
        $inner_arr[] = $post_data['custom_shortcodes'];//custom_shortcodes
        $inner_arr[] = $post_data['strip_by_regex'];//strip_by_regex
        $inner_arr[] = $post_data['replace_regex'];//replace_regex
        $inner_arr[] = $post_data['strip_by_regex_prompts'];//strip_by_regex_prompts
        $inner_arr[] = $post_data['replace_regex_prompts'];//replace_regex_prompts
        $inner_arr[] = $post_data['run_regex_on'];//run_regex_on
        $inner_arr[] = $post_data['max_links'];//max_links
        $inner_arr[] = $post_data['link_post_types'];//link_post_types
        $inner_arr[] = $post_data['enable_toc'];//enable_toc
        $inner_arr[] = $post_data['title_toc'];//title_toc
        $inner_arr[] = $post_data['qa_prompt'];//qa_prompt
        $inner_arr[] = $post_data['topic_qa_model'];//topic_qa_model
        $inner_arr[] = $post_data['enable_qa'];//enable_qa
        $inner_arr[] = $post_data['title_qa'];//title_qa
        $inner_arr[] = $post_data['title_outro'];//title_outro
        $inner_arr[] = $post_data['link_type'];//link_type
        $inner_arr[] = $post_data['link_list'];//link_list
        $inner_arr[] = '';//skip_inexist
        $inner_arr[] = $post_data['global_prepend'];//global_prepend
        $inner_arr[] = $post_data['global_append'];//global_append
        $inner_arr[] = $post_data['search_query_repetition'];//search_query_repetition
        $inner_arr[] = $post_data['img_all_headings'];//img_all_headings
        $inner_arr[] = $post_data['heading_img_location'];//heading_img_location
        $inner_arr[] = '';//days_no_run
        $inner_arr[] = '';//overwrite_existing
        $inner_arr[] = $post_data['link_nofollow'];//link_nofollow
        $inner_arr[] = $post_data['title_generator_method'];//title_generator_method
        $inner_arr[] = '';//parent_id
        $inner_arr[] = '';//rule_unique_id
        $inner_arr[] = $post_data['image_model'];//image_model
        $inner_arr[] = $post_data['assistant_id'];//assistant_id
        $type = 6;
    }
    $rules = array($inner_arr);
    $return_me = aiomatic_run_rule(0, $type, 0, 1, $rules, '', '', false, []);
    if(!is_array($return_me) || !isset($return_me[0]) || !isset($return_me[1]))
    {
        wp_send_json_error( array( 'message' => 'Rule running failed: ' . print_r($return_me, true)) );
    }
    else
    {
        wp_send_json_success( array( 'content' => $return_me[0], 'title' => $return_me[1] ) );
    }
    wp_send_json_error( array( 'message' => 'Incorrect query!' ) );
    die();
}

add_action( 'wp_ajax_aiomatic_poll_single_advanced_job', 'aiomatic_poll_single_advanced_job' );
function aiomatic_poll_single_advanced_job() 
{
    $nonce_verified = wp_verify_nonce($_POST['nonce'], 'openai-single-nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    if (!$nonce_verified) 
    {
        $status = array('status' => 'failed', 'data' => 'Security check failed!');
        echo json_encode($status);
        wp_die();
    }
    if(!isset($_POST['job_id']))
	{
        $status = array('status' => 'failed', 'data' => 'job_id is required!');
        echo json_encode($status);
        wp_die();
	}
	$job_id = $_POST['job_id'];
    $status = aiomatic_job_get_status(trim($job_id));
    if($status === false)
    {
        $status = array('status' => 'failed', 'data' => 'Job not found in database');
    }
    echo json_encode($status);
	wp_die();
}

add_action( 'wp_ajax_aiomatic_iframe', 'aiomatic_iframe_callback' );
function aiomatic_iframe_callback() 
{
    check_ajax_referer('openai-omni-nonce', 'nonce');
    if ( ! current_user_can('edit_posts') ) {
        wp_send_json_error( array( 'message' => 'Unauthorized' ), 403 );
    }
    require_once (dirname(__FILE__) . "/aiomatic-scraper.php"); 
    if(!current_user_can('access_aiomatic_menu')) die();
    $started = '%3Cs';
    $url = null;
    $cookie = isset($_GET['crawlCookie']) ? $_GET['crawlCookie'] : '' ;
    $clickelement = isset($_GET['clickelement']) ? $_GET['clickelement'] : '' ;
    $use_phantom = isset($_GET['usephantom']) ? $_GET['usephantom'] : '' ;
    $customUA = isset($_GET['customUA']) ? $_GET['customUA'] : '' ;
    $htuser = isset($_GET['htuser']) ? $_GET['htuser'] : '' ;
    $phantom_wait = isset($_GET['phantom_wait']) ? $_GET['phantom_wait'] : '' ;
    $request_delay = isset($_GET['request_delay']) ? $_GET['request_delay'] : '' ;
    $scripter = isset($_GET['scripter']) ? $_GET['scripter'] : '' ;
    $local_storage = isset($_GET['local_storage']) ? $_GET['local_storage'] : '' ;
    $auto_captcha = isset($_GET['auto_captcha']) ? $_GET['auto_captcha'] : '' ;
    $enable_adblock = isset($_GET['enable_adblock']) ? $_GET['enable_adblock'] : '' ;
    $url = $_GET['address'];
    if($customUA == 'random')
    {
        $customUA = aiomatic_get_random_user_agent();
    }
    if ( !$url ) {
        aiomatic_log_to_file('URL field empty when using Visual Selector.');
        exit();
    }
    $content = false;
    $got_phantom = false;
    if($use_phantom == '1')
    {
        $content = aiomatic_get_page_PhantomJS($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    elseif($use_phantom == '2')
    {
        $content = aiomatic_get_page_Puppeteer($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    elseif($use_phantom == '3')
    {
        $content = aiomatic_get_page_Tor($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    elseif($use_phantom == '4')
    {
        $content = aiomatic_get_page_PuppeteerAPI($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage, $auto_captcha, $enable_adblock, $clickelement);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    elseif($use_phantom == '5')
    {
        $content = aiomatic_get_page_TorAPI($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage, $auto_captcha, $enable_adblock, $clickelement);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    elseif($use_phantom == '6')
    {
        $content = aiomatic_get_page_PhantomJSAPI($url, $cookie, $customUA, '1', $htuser, $phantom_wait, $request_delay, $scripter, $local_storage);
        if($content !== false)
        {
            $got_phantom = true;
        }
    }
    if($got_phantom === false)
    {
        if (!aiomatic_check_if_phantom($use_phantom))
        { 
            $content = aiomatic_get_web_page($url, $cookie);
        }
    }
    if (  empty($content) ) 
    {
        if(empty($url))
        {
            $url = '';
        }
        aiomatic_log_to_file('Failed to get page when using Visual Selector: ' . esc_url_raw($url));
        echo 'Failed to get page when using Visual Selector: ' . esc_url_raw($url);
        header('404 Not Found');
        exit();
    }
    if ( !preg_match('/<base\s/i', $content) ) {
        $base = '<base href="' . $url . '">';
        $content = str_replace('</head>', $base . '</head>', $content);
    }
    $content = preg_replace('/src="\/\/(.*?)"/', 'src="https://$1"', $content);
    $content = preg_replace('/href="\/\/(.*?)"/', 'href="https://$1"', $content);
    if ( preg_match('!^https?://[^/]+!', $url, $matches) ) {
        $stem = $matches[0];
        $content1 = preg_replace('!(\s)(src|href)(=")\/!i', "\\1\\2\\3$stem/", $content);
        if($content1 !== null)
        {
            $content = $content1;
        }
        $content1 = preg_replace('!(\s)(url)(\s*\(\s*["\']?)\/!i', "\\1\\2\\3$stem/", $content);
        if($content1 !== null)
        {
            $content = $content1;
        }
    }
    $content = aiomatic_fix_links($content, $url);
    $content1 = preg_replace('{<script[\s\S]*?\/\s?script>}s', '', $content);
    if($content1 !== null)
    {
        $content = $content1;
    }
    echo $content . urldecode($started . "tyle%3E%5Bclass~%3Dhighlight%5D%7Bbox-shadow%3Ainset%200%200%200%201000px%20rgba%28255%2C0%2C0%2C.5%29%20%21important%3B%7D%5Bclass~%3Dhighlight%5D%7Boutline%3A.010416667in%20solid%20red%20%21important%3B%7D") . urldecode("%3C%2Fstyle%3E");
    die();
}
?>