# mxchat-basic/1.0.6/mxchat-basic.php

MxChat – AI Chatbot &amp; Content Generation for WordPress, version 1.0.6. 109 lines.

- Page: https://pluginprobe.com/plugins/mxchat-basic/1.0.6/code/mxchat-basic.php
- Raw: https://pluginprobe.com/plugins/mxchat-basic/1.0.6/raw/mxchat-basic.php
- Modified: 2024-09-11T13:11:48+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/1.0.6/code/mxchat-basic.php#L10-L20`.

```php
<?php
/**
 * Plugin Name: MxChat
 * Description: AI Chatbot for WordPress. Features like custom knowledge submission, chat transcripts, and OpenAI integration for real-time user interaction. Perfect for blogs, business sites, e-commerce platforms, and more.
 * Version: 1.0.6
 * Author: MxChat
 * Author URI: https://mxchat.ai/
 * License: GPLv2 or later
 * License URI: https://www.gnu.org/licenses/gpl-2.0.html
 */

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly.
}

// 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';

function mxchat_activate() {
    global $wpdb;

    // 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,
        timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY  (id)
    );";

    // 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,
        timestamp timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        PRIMARY KEY  (id)
    );";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    // Create or update the chat transcripts table
    dbDelta($sql_chat_transcripts);

    // Create or update the system prompt content table
    dbDelta($sql_system_prompt);

    // Ensure 'embedding_vector' column exists in the 'system_prompt_content' table
    $column_exists = $wpdb->get_results($wpdb->prepare(
        "SHOW COLUMNS FROM %s LIKE %s",
        $system_prompt_table,
        'embedding_vector'
    ));
    if (empty($column_exists)) {
        $wpdb->query($wpdb->prepare(
            "ALTER TABLE %s ADD COLUMN embedding_vector longtext",
            $system_prompt_table
        ));
    }

    // Ensure 'source_url' column exists in the 'system_prompt_content' table
    $source_url_exists = $wpdb->get_results($wpdb->prepare(
        "SHOW COLUMNS FROM %s LIKE %s",
        $system_prompt_table,
        'source_url'
    ));
    if (empty($source_url_exists)) {
        $wpdb->query($wpdb->prepare(
            "ALTER TABLE %s ADD COLUMN source_url VARCHAR(255) DEFAULT NULL",
            $system_prompt_table
        ));
    }

    // Update the version number in the database
    update_option('mxchat_plugin_version', '1.0.6');
}

function mxchat_check_for_update() {
    $current_version = get_option('mxchat_plugin_version');
    $plugin_version = '1.0.6';

    if ($current_version !== $plugin_version) {
        mxchat_activate(); // Run the activation script which includes database migrations
        update_option('mxchat_plugin_version', $plugin_version); // Update to the latest version
    }
}

// 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');

// Instantiate classes
if (is_admin()) {
    $mxchat_admin = new MxChat_Admin();
}
$mxchat_public = new MxChat_Public();
$mxchat_integrator = new MxChat_Integrator();


```
