get_charset_collate(); // Table for indexed products (for faster RAG retrieval) $table_products = $wpdb->prefix . 'aica_indexed_products'; $sql_products = "CREATE TABLE IF NOT EXISTS $table_products ( id bigint(20) NOT NULL AUTO_INCREMENT, product_id bigint(20) NOT NULL, content longtext NOT NULL, metadata longtext DEFAULT NULL, price decimal(10,2) DEFAULT NULL, stock_quantity int(11) DEFAULT NULL, categories varchar(255) DEFAULT NULL, embedding longtext DEFAULT NULL, last_updated datetime DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY product_id (product_id), KEY price (price), FULLTEXT KEY content (content) ) $charset_collate;"; // Table for chat conversations $table_conversations = $wpdb->prefix . 'aica_conversations'; $sql_conversations = "CREATE TABLE IF NOT EXISTS $table_conversations ( id bigint(20) NOT NULL AUTO_INCREMENT, session_id varchar(100) NOT NULL, user_id bigint(20) DEFAULT NULL, message longtext NOT NULL, role varchar(20) NOT NULL, metadata longtext DEFAULT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY session_id (session_id), KEY user_id (user_id), KEY created_at (created_at) ) $charset_collate;"; // Table for analytics $table_analytics = $wpdb->prefix . 'aica_analytics'; $sql_analytics = "CREATE TABLE IF NOT EXISTS $table_analytics ( id bigint(20) NOT NULL AUTO_INCREMENT, session_id varchar(100) NOT NULL, event_type varchar(50) NOT NULL, event_data longtext DEFAULT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP, PRIMARY KEY (id), KEY session_id (session_id), KEY event_type (event_type), KEY created_at (created_at) ) $charset_collate;"; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; // Suppress output from dbDelta ob_start(); dbDelta( $sql_products ); dbDelta( $sql_conversations ); dbDelta( $sql_analytics ); ob_end_clean(); } /** * Set default plugin options * * @return void */ private static function set_default_options() { $default_settings = array( // General settings 'enable_widget' => 'yes', 'widget_position' => 'bottom-right', // AI settings 'ai_provider' => 'openai', 'api_key' => '', 'model' => 'gpt-4o-mini', 'max_tokens' => 500, 'temperature' => 0.7, // Appearance 'primary_color' => '#0066FF', 'widget_title' => __( 'AI Assistant', 'ai-commerce-assistant' ), 'greeting_message' => __( 'Hi! How can I help you today?', 'ai-commerce-assistant' ), 'avatar_url' => '', // Behavior 'enable_product_recommendations' => 'yes', 'enable_cart_actions' => 'yes', 'enable_order_tracking' => 'no', 'max_products_per_response' => 5, // Rate limiting 'rate_limit_enabled' => 'yes', 'rate_limit_messages' => 50, 'rate_limit_period' => 'hour', // Privacy 'store_conversations' => 'yes', 'anonymize_data' => 'no', 'data_retention_days' => 30, ); add_option( 'aica_settings', $default_settings ); // Add magic prompts (suggestions) $default_prompts = array( array( 'text' => __( 'Show me new arrivals', 'ai-commerce-assistant' ), 'icon' => '✨', 'enabled' => true, ), array( 'text' => __( 'I need a gift idea', 'ai-commerce-assistant' ), 'icon' => '🎁', 'enabled' => true, ), array( 'text' => __( 'Find products under $50', 'ai-commerce-assistant' ), 'icon' => '💰', 'enabled' => true, ), array( 'text' => __( 'Track my order', 'ai-commerce-assistant' ), 'icon' => '📦', 'enabled' => true, ), ); add_option( 'aica_magic_prompts', $default_prompts ); } /** * Schedule cron events * * @return void */ private static function schedule_events() { // Schedule product reindexing (daily) if ( !wp_next_scheduled( 'aica_reindex_products' ) ) { wp_schedule_event( time(), 'daily', 'aica_reindex_products' ); } // Schedule cleanup of old conversations (weekly) if ( !wp_next_scheduled( 'aica_cleanup_old_data' ) ) { wp_schedule_event( time(), 'weekly', 'aica_cleanup_old_data' ); } } /** * Check if database tables exist * * @return bool */ public static function tables_exist() { global $wpdb; $table_products = $wpdb->prefix . 'aica_indexed_products'; $table_conversations = $wpdb->prefix . 'aica_conversations'; $table_analytics = $wpdb->prefix . 'aica_analytics'; $tables_exist = wp_cache_get( 'aica_tables_exist', 'aica_installer' ); if ( false === $tables_exist ) { // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $tables_exist = ( $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_products ) ) === $table_products && $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_conversations ) ) === $table_conversations && $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_analytics ) ) === $table_analytics ); // phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching wp_cache_set( 'aica_tables_exist', $tables_exist, 'aica_installer', HOUR_IN_SECONDS ); } return $tables_exist; } }