> */ private function get_allowed_icon_svg_tags() { return [ 'svg' => [ 'class' => true, 'viewbox' => true, 'fill' => true, 'xmlns' => true, 'aria-hidden' => true, 'focusable' => true, ], 'path' => [ 'd' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true, ], ]; } public function __construct() { $this->chat_handler = new ACOSMAIA_Chat_Handler(); add_action('init', [$this, 'init']); } public function init() { // Check new option name first, then fall back to old name for migration $enabled = get_option('acosmaia_enabled'); if ($enabled === false) { $enabled = get_option('wp_ai_chat_enabled', 'yes'); } if ($enabled !== 'yes') { return; } add_action('wp_enqueue_scripts', [$this, 'enqueue_frontend']); add_action('wp_footer', [$this, 'render_chat_widget']); add_action('rest_api_init', [$this, 'register_rest']); } public function enqueue_frontend() { $position = get_option('acosmaia_chat_position', 'bottom-right'); $primary_color = get_option('acosmaia_chat_color', '#0073aa'); $bubble_icon = get_option('acosmaia_bubble_icon', 'chat'); wp_enqueue_style( 'acosmaia-style', ACOSMAIA_URL . 'assets/css/chat-style.css', [], ACOSMAIA_VERSION ); wp_enqueue_script( 'acosmaia-script', ACOSMAIA_URL . 'assets/js/chat-frontend.js', [], ACOSMAIA_VERSION, true ); $provider = get_option('acosmaia_api_provider', 'openrouter'); if ('openai' === $provider) { $api_key = get_option('acosmaia_openai_key'); } elseif ('gemini' === $provider) { $api_key = get_option('acosmaia_gemini_key'); } else { $api_key = get_option('acosmaia_openrouter_key'); } $api_configured = ! empty($api_key); wp_localize_script('acosmaia-script', 'acosmaiaChat', [ 'assistant_name' => get_option('acosmaia_assistant_name', 'AI Assistant'), 'welcome_message' => get_option('acosmaia_welcome_message', 'Hi! How can I help you today? 🛒'), 'api_url' => rest_url('acosmaia/v1/chat'), 'nonce' => wp_create_nonce('acosmaia_nonce'), 'position' => $position, 'primary_color' => $primary_color, 'bubble_icon' => $bubble_icon, 'default_theme' => get_option('acosmaia_default_theme', 'light'), 'is_logged_in' => is_user_logged_in(), 'cart_url' => function_exists('wc_get_cart_url') ? wc_get_cart_url() : home_url('/cart/'), 'currency_symbol' => function_exists('get_woocommerce_currency_symbol') ? get_woocommerce_currency_symbol() : '$', 'api_configured' => $api_configured, ]); } private function get_bubble_icon_svg($icon_type) { if ($icon_type === 'spark') { return ''; } if ($icon_type === 'bolt') { return ''; } return ''; } public function render_chat_widget() { $position = get_option('acosmaia_chat_position', 'bottom-right'); $bubble_icon = get_option('acosmaia_bubble_icon', 'chat'); $assistant = get_option('acosmaia_assistant_name', 'AI Assistant'); $provider = get_option('acosmaia_api_provider', 'openrouter'); if ('openai' === $provider) { $api_key = get_option('acosmaia_openai_key'); } elseif ('gemini' === $provider) { $api_key = get_option('acosmaia_gemini_key'); } else { $api_key = get_option('acosmaia_openrouter_key'); } $api_configured = ! empty($api_key); $quick_replies = $api_configured ? $this->get_dynamic_quick_replies() : []; ?>
'🔍', 'text' => __('Find me a product', 'aco-smart-ai-assistant'), ], [ 'icon' => '❤️', 'text' => __('Track my order', 'aco-smart-ai-assistant'), ], [ 'icon' => '🛒', 'text' => __('View my cart', 'aco-smart-ai-assistant'), ], [ 'icon' => '🚚', 'text' => __("What's your shipping policy?", 'aco-smart-ai-assistant'), ], [ 'icon' => '✅', 'text' => __('How do I return an item?', 'aco-smart-ai-assistant'), ], ]; } private function get_average_product_price() { if (! function_exists('wc_get_products')) { return 0; } $products = wc_get_products([ 'limit' => 20, 'status' => 'publish', 'orderby' => 'date', 'order' => 'DESC', 'return' => 'objects', ]); if (empty($products)) { return 0; } $total = 0; $count = 0; foreach ($products as $product) { if ($product instanceof WC_Product && $product->get_price()) { $total += (float) $product->get_price(); $count++; } } return $count > 0 ? $total / $count : 0; } public function register_rest() { register_rest_route('acosmaia/v1', '/chat', [ 'methods' => 'POST', 'callback' => [$this->chat_handler, 'handle'], 'permission_callback' => [$this->chat_handler, 'permission_check'], ]); } public static function activate() { // Migrate old wp_ai_chat_* options to acosmaia_* names $old_options = [ 'wp_ai_chat_enabled' => 'acosmaia_enabled', 'wp_ai_chat_assistant_name' => 'acosmaia_assistant_name', 'wp_ai_chat_welcome_message' => 'acosmaia_welcome_message', 'wp_ai_chat_chat_position' => 'acosmaia_chat_position', 'wp_ai_chat_chat_color' => 'acosmaia_chat_color', 'wp_ai_chat_bubble_icon' => 'acosmaia_bubble_icon', 'wp_ai_chat_default_theme' => 'acosmaia_default_theme', 'wp_ai_chat_api_provider' => 'acosmaia_api_provider', 'wp_ai_chat_openrouter_key' => 'acosmaia_openrouter_key', 'wp_ai_chat_openai_key' => 'acosmaia_openai_key', 'wp_ai_chat_openai_model' => 'acosmaia_openai_model', 'wp_ai_chat_gemini_key' => 'acosmaia_gemini_key', 'wp_ai_chat_gemini_model' => 'acosmaia_gemini_model', 'wp_ai_chat_model' => 'acosmaia_model', 'wp_ai_chat_temperature' => 'acosmaia_temperature', 'wp_ai_chat_max_tokens' => 'acosmaia_max_tokens', 'wp_ai_chat_timeout' => 'acosmaia_timeout', 'wp_ai_chat_daily_limit' => 'acosmaia_daily_limit', 'wp_ai_chat_monthly_limit' => 'acosmaia_monthly_limit', 'wp_ai_chat_max_products' => 'acosmaia_max_products', 'wp_ai_chat_restrict_product_only' => 'acosmaia_restrict_product_only', 'wp_ai_chat_fallback_response' => 'acosmaia_fallback_response', 'wp_ai_chat_knowledge_text' => 'acosmaia_knowledge_text', 'wp_ai_chat_enabled_pages' => 'acosmaia_enabled_pages', ]; foreach ($old_options as $old_key => $new_key) { $old_value = get_option($old_key); if ($old_value !== false && get_option($new_key) === false) { update_option($new_key, $old_value); } } if (false !== get_option('acosmaia_enabled_pages', false)) { return; } $pages = get_pages([ 'post_status' => 'publish', 'sort_column' => 'menu_order,post_title', 'number' => 500, ]); $page_ids = []; if (is_array($pages)) { foreach ($pages as $page) { if ($page instanceof WP_Post && isset($page->ID)) { $page_ids[] = (int) $page->ID; } } } update_option('acosmaia_enabled_pages', array_values(array_unique($page_ids)), false); } public function permission_check(WP_REST_Request $request) { // Public endpoint - anyone can chat with the AI assistant. // Security is enforced inside handle() method (sanitization, rate limits, etc.). return __return_true(); } } new ACOSMAIA_Assistance(); register_activation_hook(__FILE__, ['ACOSMAIA_Assistance', 'activate']);