# mxchat-basic/2.0.0/mxchat-basic.php

MxChat – AI Chatbot &amp; Content Generation for WordPress, version 2.0.0. 177 lines.

- Page: https://pluginprobe.com/plugins/mxchat-basic/2.0.0/code/mxchat-basic.php
- Raw: https://pluginprobe.com/plugins/mxchat-basic/2.0.0/raw/mxchat-basic.php
- Modified: 2025-02-06T03:44:40+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/mxchat-basic/2.0.0/code/mxchat-basic.php#L10-L20`.

```php
<?php
/**
 * Plugin Name: MxChat
 * Description: AI chatbot for WordPress with OpenAI, Claude, xAI, DeepSeek, live agent, PDF uploads, WooCommerce, and training on website data.
 * Version: 2.0.0
 * Author: MxChat
 * Author URI: https://mxchat.ai
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 * Text Domain: mxchat
 * Domain Path: /languages
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}


function mxchat_load_textdomain() {
    load_plugin_textdomain('mxchat', false, dirname(plugin_basename(__FILE__)) . '/languages');
}
add_action('plugins_loaded', 'mxchat_load_textdomain');

// Include classes
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-integrator.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-admin.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-public.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-utils.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-user.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-woocommerce.php';
require_once plugin_dir_path(__FILE__) . 'includes/pdf-parser/alt_autoload.php';
require_once plugin_dir_path(__FILE__) . 'includes/class-mxchat-word-handler.php';

function mxchat_activate() {
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate();

    // Chat Transcripts Table
    $chat_transcripts_table = $wpdb->prefix . 'mxchat_chat_transcripts';
    $sql_chat_transcripts = "CREATE TABLE $chat_transcripts_table (
        id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
        user_id MEDIUMINT(9) DEFAULT 0,
        session_id VARCHAR(255) NOT NULL,
        role VARCHAR(255) NOT NULL,
        message TEXT NOT NULL,
        user_email VARCHAR(255) DEFAULT NULL,
        timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";

    // System Prompt Content Table
    $system_prompt_table = $wpdb->prefix . 'mxchat_system_prompt_content';
    $sql_system_prompt = "CREATE TABLE $system_prompt_table (
        id MEDIUMINT(9) NOT NULL AUTO_INCREMENT,
        url VARCHAR(255) NOT NULL,
        article_content LONGTEXT NOT NULL,
        embedding_vector LONGTEXT,
        source_url VARCHAR(255) DEFAULT NULL,
        timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY (id)
    ) $charset_collate;";

    // Intents Table
    $intents_table = $wpdb->prefix . 'mxchat_intents';
    $sql_intents_table = "CREATE TABLE $intents_table (
        id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
        intent_label VARCHAR(255) NOT NULL,
        phrases TEXT NOT NULL,
        embedding_vector LONGTEXT NOT NULL,
        callback_function VARCHAR(255) NOT NULL,
        similarity_threshold FLOAT DEFAULT 0.85,
        PRIMARY KEY (id)
    ) $charset_collate;";

    require_once ABSPATH . 'wp-admin/includes/upgrade.php';

    // Create or update tables
    dbDelta($sql_chat_transcripts);
    dbDelta($sql_system_prompt);
    dbDelta($sql_intents_table);

    // Ensure additional columns in `mxchat_chat_transcripts`
    mxchat_add_missing_columns($chat_transcripts_table, 'user_identifier', 'VARCHAR(255)');
    mxchat_add_missing_columns($chat_transcripts_table, 'user_email', 'VARCHAR(255) DEFAULT NULL');

    // Ensure additional columns in `mxchat_system_prompt_content`
    mxchat_add_missing_columns($system_prompt_table, 'embedding_vector', 'LONGTEXT');
    mxchat_add_missing_columns($system_prompt_table, 'source_url', 'VARCHAR(255) DEFAULT NULL');

    // Set default thresholds for existing intents
    $wpdb->query("UPDATE {$intents_table} SET similarity_threshold = 0.85 WHERE similarity_threshold IS NULL");

    // Update plugin version in the database
    update_option('mxchat_plugin_version', '2.0.0');
}


function mxchat_add_missing_columns($table, $column_name, $column_type) {
    global $wpdb;
    $column_exists = $wpdb->get_results($wpdb->prepare("SHOW COLUMNS FROM $table LIKE %s", $column_name));
    if (empty($column_exists)) {
        $wpdb->query("ALTER TABLE $table ADD COLUMN $column_name $column_type");
    }
}

function mxchat_check_for_update() {
    $current_version = get_option('mxchat_plugin_version');
    $plugin_version = '2.0.0'; // Update with your latest version

    if ($current_version !== $plugin_version) {
        mxchat_activate(); // Run the activation script to apply schema changes
        mxchat_migrate_live_agent_status(); // Add migration for live agent status
        update_option('mxchat_plugin_version', $plugin_version); // Update the version
    }
}

function mxchat_migrate_live_agent_status() {
    $options = get_option('mxchat_options', []);

    // Check if live_agent_status exists
    if (isset($options['live_agent_status'])) {
        $current_status = $options['live_agent_status'];
        $needs_update = false;

        // Convert to new format if needed
        if ($current_status === 'online') {
            $options['live_agent_status'] = 'on';
            $needs_update = true;
        } else if ($current_status === 'offline') {
            $options['live_agent_status'] = 'off';
            $needs_update = true;
        } else if (!in_array($current_status, ['on', 'off'])) {
            // Default to off for any unexpected values
            $options['live_agent_status'] = 'off';
            $needs_update = true;
        }

        // Only update if needed
        if ($needs_update) {
            update_option('mxchat_options', $options);
        }
    } else {
        // If status doesn't exist, set default to off
        $options['live_agent_status'] = 'off';
        update_option('mxchat_options', $options);
    }
}


// Run the update check function on every request
add_action('plugins_loaded', 'mxchat_check_for_update');

// Register activation hook
register_activation_hook(__FILE__, 'mxchat_activate');


add_filter('cron_schedules', function($schedules) {
    $schedules['one_minute'] = array(
        'interval' => 60,
        'display' => 'Every Minute'
    );
    return $schedules;
});

add_action('init', function() {
    add_action('mxchat_process_pdf_pages', array('MxChat_Admin', 'process_pdf_pages_cron'), 10, 5);
    add_action('mxchat_process_sitemap_urls', array('MxChat_Admin', 'process_sitemap_urls_cron'), 10, 5);
});


// Instantiate classes
if (is_admin()) {
    $mxchat_admin = new MxChat_Admin();
}
$mxchat_public = new MxChat_Public();
$mxchat_integrator = new MxChat_Integrator();

```
