HEX
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.30
System: Linux iZj6c1151k3ad370bosnmsZ 3.10.0-1160.76.1.el7.x86_64 #1 SMP Wed Aug 10 16:21:17 UTC 2022 x86_64
User: root (0)
PHP: 7.4.30
Disabled: NONE
Upload Files
File: /var/www/html/www.winghung.com/wp-content/plugins/upkyk-assistant-ai/upkyk-assistant-ai.php
<?php
/*
Plugin Name: Upkyk Assistant AI
Description: Your intelligent AI custom chatbot for WordPress
Version: 1.2.3
Author: <a href="https://upkyk.com/assistant-ai">Upkyk</a>
Text Domain: upkyk-assistant-ai
Domain Path: /languages
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/

if (!defined('ABSPATH')) {
    exit; // Prevent direct access
}

// Define constants
define('UPKYK_ASSISTANT_AI_VERSION', '1.2.3');
define('UPKYK_ASSISTANT_AI_PATH', plugin_dir_path(__FILE__));
define('UPKYK_ASSISTANT_AI_URL', plugin_dir_url(__FILE__));
define('UPKYK_ITEM_REFERENCE', 'Upkyk Assistant AI');

/**
 * Main plugin class
 */
class Upkyk_Assistant_AI {
    /**
     * Constructor
     */
    public function __construct() {
        // Load required files
        $this->load_dependencies();
        
        // Define hooks
        $this->define_hooks();
    }
    
    /**
     * Load required files
     */
    private function load_dependencies() {
        // Core files
        require_once UPKYK_ASSISTANT_AI_PATH . 'includes/class-upkyk-ai-service.php';
        require_once UPKYK_ASSISTANT_AI_PATH . 'includes/class-upkyk-frontend.php';
        require_once UPKYK_ASSISTANT_AI_PATH . 'includes/class-upkyk-logger.php';
        
        // Admin
        if (is_admin()) {
            require_once UPKYK_ASSISTANT_AI_PATH . 'admin/class-upkyk-admin.php';
        }
    }
    
    /**
     * Define hooks
     */
    private function define_hooks() {
        // Admin hooks
        if (is_admin()) {
            $admin = new Upkyk_Assistant_AI_Admin();
            add_action('admin_menu', array($admin, 'add_admin_menu'));
            add_action('admin_init', array($admin, 'register_settings'));
            add_action('admin_enqueue_scripts', array($admin, 'enqueue_admin_assets'));
            
            // Add AJAX handlers if the method exists
            if (method_exists($admin, 'register_ajax_handlers')) {
                add_action('admin_init', array($admin, 'register_ajax_handlers'));
            }
        }
        
        // Frontend hooks
        $frontend = new Upkyk_Assistant_AI_Frontend();
        add_action('wp_enqueue_scripts', array($frontend, 'enqueue_assets'));
        add_shortcode('upkyk_assistant_ai', array($frontend, 'display_assistant_ai_shortcode'));
        
        // AJAX handlers
        add_action('wp_ajax_upkyk_chat_request', array($frontend, 'handle_chat_request'));
        add_action('wp_ajax_nopriv_upkyk_chat_request', array($frontend, 'handle_chat_request'));
        
        // Add emergency localization hook
        add_action('wp_footer', array($this, 'force_localize_assistant_ai_script'), 5);
        
        // Add emergency chatbot
        add_action('wp_footer', array($this, 'emergency_assistant_ai'), 999);
    }
    
    /**
     * Add compatibility functions for older code
     */
    public function add_compatibility_functions() {
        // AI compatibility functions
        if (!function_exists('chat_with_ai')) {
            function chat_with_ai($message, $client_id = null, $chat_history = []) {
                // Format message for AI in the correct structure
                $formatted_messages = [];
                
                // Add chat history if provided
                if (!empty($chat_history)) {
                    foreach ($chat_history as $entry) {
                        if (is_array($entry) && isset($entry['role'], $entry['content'])) {
                            $formatted_messages[] = $entry;
                        }
                    }
                }
                
                // Add current user message
                $formatted_messages[] = [
                    'role' => 'user',
                    'content' => $message
                ];
                
                // Call the AI class with properly formatted messages
                return Upkyk_AI_Service::chat_with_ai($formatted_messages, $client_id, [], false);
            }
        }
        
        if (!function_exists('get_welcome_message')) {
            function get_welcome_message() {
                return Upkyk_AI_Service::get_welcome_message();
            }
        }
    }

    public function run() {
        // Initialize the plugin
        add_action('plugins_loaded', array($this, 'add_compatibility_functions'));
        
        // Load plugin text domain for translations
        add_action('plugins_loaded', array($this, 'load_plugin_textdomain'));
        
        // Log plugin initialization
        do_action('upkyk_assistant_ai_debug_log', 'Plugin initialized');
    }
    
    /**
     * Load plugin textdomain for translations
     */
    public function load_plugin_textdomain() {
        load_plugin_textdomain(
            'upkyk-assistant-ai',
            false,
            dirname(plugin_basename(__FILE__)) . '/languages'
        );
    }

    public function force_localize_assistant_ai_script() {
        // Only run if chatbot is enabled
        if (get_option('upkyk_assistant_ai_enabled', '1') !== '1') {
            return;
        }
        
        // Check if script is enqueued
        if (!wp_script_is('upkyk-assistant-ai-script', 'enqueued')) {
            // Log debug info using WordPress debug functions
            do_action('upkyk_assistant_ai_debug_log', 'Script not enqueued, cannot localize');
            return;
        }
        
        // Get ALL settings
        $primary_color = get_option('upkyk_assistant_ai_color', '#0082C8');
        $user_color = get_option('upkyk_assistant_ai_user_color', '#e9ecef');
        $button_shape = get_option('upkyk_assistant_ai_button_shape', 'circle');
        $button_size = get_option('upkyk_assistant_ai_button_size', 'medium');
        $assistant_ai_name = get_option('upkyk_assistant_ai_name', 'Chat Support');
        $welcome_message = html_entity_decode(get_option('upkyk_assistant_ai_welcome_message', 'Hi there! How can I help you today?'));
        $input_placeholder = html_entity_decode(get_option('upkyk_assistant_ai_input_placeholder', 'Type your message...'));
        $font_family = get_option('upkyk_assistant_ai_font', 'Roboto, sans-serif');
        $font_size = get_option('upkyk_assistant_ai_font_size', 'medium');
        $message_padding = get_option('upkyk_assistant_ai_message_padding', 'normal');
        $avatar = get_option('upkyk_assistant_ai_avatar', UPKYK_ASSISTANT_AI_URL . 'assets/images/default-avatar.svg');
        
        // Force localize again with ALL settings
        wp_localize_script(
            'upkyk-assistant-ai-script',
            'upkyk_assistant_ai_ajax',
            array(
                'ajax_url' => admin_url('admin-ajax.php'),
                'nonce' => wp_create_nonce('upkyk_assistant_ai_nonce'),
                'plugin_url' => plugin_dir_url(__FILE__),
                'settings' => array(
                    'enabled' => true,
                    'color' => $primary_color,
                    'user_color' => $user_color,
                    'button_icon' => get_option('upkyk_assistant_ai_button_icon', 'chat'),
                    'button_shape' => $button_shape,
                    'button_size' => $button_size,
                    'assistant_ai_name' => $assistant_ai_name,
                    'welcome_message' => $welcome_message,
                    'input_placeholder' => $input_placeholder,
                    'font' => $font_family,
                    'font_size' => $font_size,
                    'message_padding' => $message_padding,
                    'avatar' => $avatar,
                    'auto_language' => get_option('upkyk_assistant_ai_auto_language', '1') === '1',
                    'default_language' => get_option('upkyk_assistant_ai_default_language', 'en'),
                    'timestamp' => time()
                )
            )
        );
        
        // Log debug info using WordPress debug functions
        do_action('upkyk_assistant_ai_debug_log', 'Force localized script with all style settings');
    }

    public function emergency_assistant_ai() {
        // Only show if enabled
        if (get_option('upkyk_assistant_ai_enabled', '1') !== '1') {
            return;
        }
        
        $primary_color = get_option('upkyk_assistant_ai_color', '#0082C8');
        
        // Output HTML
        ?>
        <div id="upkyk-fallback" style="position: fixed; bottom: 20px; right: 20px; z-index: 99999; opacity: 0; transition: opacity 0.5s ease; pointer-events: none;">
            <button id="upkyk-fallback-button" style="width: 60px; height: 60px; background-color: <?php echo esc_attr($primary_color); ?>; border-radius: 50%; border: none; color: white; cursor: pointer; box-shadow: 0 2px 10px rgba(0,0,0,0.2); display: flex; align-items: center; justify-content: center;">
                <svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
                    <path d="M12 22C17.5228 22 22 17.5228 22 12C22 6.47715 17.5228 2 12 2C6.47715 2 2 6.47715 2 12C2 17.5228 6.47715 22 12 22Z" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M12 16V12" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                    <path d="M12 8H12.01" stroke="white" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
                </svg>
            </button>
        </div>
        
        <?php
        // Enqueue the fallback script
        wp_enqueue_script(
            'upkyk-assistant-ai-fallback',
            UPKYK_ASSISTANT_AI_URL . 'assets/js/assistant-ai-fallback.js',
            array(),
            UPKYK_ASSISTANT_AI_VERSION,
            true
        );
        
        // Add the configuration as inline script
        wp_add_inline_script(
            'upkyk-assistant-ai-fallback',
            'var upkykFallbackConfig = {
                checkInterval: 500,
                maxChecks: 5
            };',
            'before'
        );
        ?>
        <?php
    }
}

// Activation hook
register_activation_hook(__FILE__, 'upkyk_assistant_ai_activate');

/**
 * Activation function
 */
function upkyk_assistant_ai_activate() {
    // Create tables and other activation tasks
    upkyk_assistant_ai_create_tables();
    
    // Initialize API settings as needed
    if (!get_option('upkyk_assistant_ai_tier')) {
        update_option('upkyk_assistant_ai_tier', 'free');
    }
}

/**
 * Create database tables
 */
function upkyk_assistant_ai_create_tables() {
    global $wpdb;
    $charset_collate = $wpdb->get_charset_collate();
    
    // Custom phrases table
    $table_phrases = $wpdb->prefix . 'upkyk_custom_phrases';
    
    $sql_phrases = "CREATE TABLE $table_phrases (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        question text NOT NULL,
        answer text NOT NULL,
        category varchar(100) DEFAULT NULL,
        date_added datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
        PRIMARY KEY  (id)
    ) $charset_collate;";
    
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql_phrases);
}

// Initialize the plugin
$plugin = new Upkyk_Assistant_AI();
$plugin->run();