* @license GPL-2.0+ https://www.gnu.org/licenses/gpl-2.0.html * @link https://aisk.chat */ /** * AISK_Scripts Class * * Handles loading and localization of scripts and styles for the chat widget * * @category Class * @package AISK * @author Aisk Team * @license GPL-2.0+ https://www.gnu.org/licenses/gpl-2.0.html * @link https://aisk.chat */ class AISK_Scripts { /** * Translate a setting when empty or still equal to the English default. * * @param string $value Saved setting value. * @param string $default English msgid / default. * @return string */ private static function localized_setting( $value, $default ) { if ( is_string( $value ) && '' !== trim( $value ) && $value !== $default ) { return $value; } switch ( $default ) { case 'Hi there! How can I help you?': return __( 'Hi there! How can I help you?', 'aisk-ai-chat' ); case 'Type your message...': return __( 'Type your message...', 'aisk-ai-chat' ); case 'Hey, need help? 👋': return __( 'Hey, need help? 👋', 'aisk-ai-chat' ); default: return $default; } } /** * Inject Jed locale data before the chat widget script. * * Fallback when wp_set_script_translations does not print (missing deps / strategy edge cases). * * @return void */ private static function inject_script_translations() { $json = load_script_textdomain( 'aisk-chat-widget', 'aisk-ai-chat', AISK_PLUGIN_DIR . 'languages' ); if ( ! $json ) { // Try handle-named catalog directly. $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); $handle_f = AISK_PLUGIN_DIR . 'languages/aisk-ai-chat-' . $locale . '-aisk-chat-widget.json'; if ( is_readable( $handle_f ) ) { $json = file_get_contents( $handle_f ); } } if ( ! $json ) { return; } $inline = sprintf( '( function( domain, translations ) { if ( ! window.wp || ! wp.i18n || ! wp.i18n.setLocaleData ) { return; } var localeData = translations.locale_data[ domain ] || translations.locale_data.messages; if ( ! localeData ) { return; } localeData[""] = localeData[""] || {}; localeData[""].domain = domain; wp.i18n.setLocaleData( localeData, domain ); } )( "aisk-ai-chat", %s );', $json ); wp_add_inline_script( 'aisk-chat-widget', $inline, 'before' ); } /** * Loads chat widget assets and localizes settings * * Enqueues required CSS/JS files and prepares frontend settings * for the chat widget functionality. * * @return array Processed frontend settings */ public static function load_chat_widget_assets() { // Register and enqueue chat widget styles wp_register_style( 'aisk-chat-widget-styles', AISK_PLUGIN_URL . 'build/chat-widget.css', [], AISK_VERSION ); wp_enqueue_style('aisk-chat-widget-styles'); // Register and enqueue chat formatter styles wp_register_style( 'aisk-chat-formatter-styles', AISK_PLUGIN_URL . 'assets/css/chat-formatter.css', ['aisk-chat-widget-styles'], AISK_VERSION ); wp_enqueue_style('aisk-chat-formatter-styles'); // Ensure i18n stack is present on the frontend (required for __() in the widget). wp_enqueue_script( 'wp-hooks' ); wp_enqueue_script( 'wp-i18n' ); // Register and enqueue chat widget scripts. // Avoid "defer" so wp-i18n + locale data always run before the widget IIFE. wp_register_script( 'aisk-chat-widget', AISK_PLUGIN_URL . 'build/chat-widget.js', ['wp-hooks', 'wp-i18n', 'wp-element', 'react', 'react-dom'], AISK_VERSION, true ); wp_enqueue_script('aisk-chat-widget'); wp_set_script_translations( 'aisk-chat-widget', 'aisk-ai-chat', AISK_PLUGIN_DIR . 'languages' ); self::inject_script_translations(); // Get settings $settings = get_option('aisk_settings', []); $default_greeting = 'Hi there! How can I help you?'; $default_placeholder = 'Type your message...'; $default_bubble = 'Hey, need help? 👋'; $default_rolling = [ '👋 Need help?', '💬 Chat with us!', '🛍️ Find products', ]; $saved_rolling = isset( $settings['chatwidget']['rolling_messages'] ) ? $settings['chatwidget']['rolling_messages'] : null; if ( ! is_array( $saved_rolling ) || empty( $saved_rolling ) || $saved_rolling === $default_rolling ) { $rolling_messages = [ __( '👋 Need help?', 'aisk-ai-chat' ), __( '💬 Chat with us!', 'aisk-ai-chat' ), __( '🛍️ Find products', 'aisk-ai-chat' ), ]; } else { $rolling_messages = $saved_rolling; } // Create frontend-only settings object $frontend_settings = [ 'chatwidget' => [ 'chat_icon' => isset($settings['chatwidget']['chat_icon']) ? $settings['chatwidget']['chat_icon'] : '', 'widget_logo' => isset($settings['chatwidget']['widget_logo']) ? $settings['chatwidget']['widget_logo'] : '', 'widget_text' => isset($settings['chatwidget']['widget_text']) && "" !== $settings['chatwidget']['widget_text'] ? $settings['chatwidget']['widget_text'] : get_bloginfo('name'), 'widget_color' => isset($settings['chatwidget']['widget_color']) ? $settings['chatwidget']['widget_color'] : '#4F46E5', 'suggested_questions' => isset($settings['chatwidget']['suggested_questions']) ? $settings['chatwidget']['suggested_questions'] : [], 'widget_position' => isset($settings['chatwidget']['widget_position']) ? $settings['chatwidget']['widget_position'] : 'bottom-right', 'widget_greeting' => self::localized_setting( isset( $settings['chatwidget']['widget_greeting'] ) ? $settings['chatwidget']['widget_greeting'] : '', $default_greeting ), 'widget_placeholder' => self::localized_setting( isset( $settings['chatwidget']['widget_placeholder'] ) ? $settings['chatwidget']['widget_placeholder'] : '', $default_placeholder ), 'widget_title' => isset($settings['chatwidget']['widget_title']) ? $settings['chatwidget']['widget_title'] : '', 'widget_subtitle' => isset($settings['chatwidget']['widget_subtitle']) ? $settings['chatwidget']['widget_subtitle'] : '', 'bubble_type' => isset($settings['chatwidget']['bubble_type']) ? $settings['chatwidget']['bubble_type'] : 'default', 'default_message' => self::localized_setting( isset( $settings['chatwidget']['default_message'] ) ? $settings['chatwidget']['default_message'] : '', $default_bubble ), 'rolling_messages' => $rolling_messages, // Orders tab in the bottom menu (only shown when WooCommerce is enabled); on by default 'show_orders_tab' => isset($settings['chatwidget']['show_orders_tab']) ? (bool) $settings['chatwidget']['show_orders_tab'] : true, ], 'integrations' => [ 'whatsapp' => [ 'enabled' => isset($settings['integrations']['whatsapp']['enabled']) ? $settings['integrations']['whatsapp']['enabled'] : false, 'phone_number' => isset($settings['integrations']['whatsapp']['phone_number']) ? $settings['integrations']['whatsapp']['phone_number'] : '', ], 'telegram' => [ 'enabled' => isset($settings['integrations']['telegram']['enabled']) ? $settings['integrations']['telegram']['enabled'] : false, 'bot_username' => isset($settings['integrations']['telegram']['bot_username']) ? $settings['integrations']['telegram']['bot_username'] : '', ], 'contact_form' => [ 'enabled' => isset($settings['integrations']['contact_form']['enabled']) ? $settings['integrations']['contact_form']['enabled'] : false, 'shortcode' => isset($settings['integrations']['contact_form']['shortcode']) ? $settings['integrations']['contact_form']['shortcode'] : '', ], ], 'colors' => [ 'primary' => isset($settings['colors']['primary']) ? $settings['colors']['primary'] : '#4F46E5', 'secondary' => isset($settings['colors']['secondary']) ? $settings['colors']['secondary'] : '#E0E7FF', 'text' => isset($settings['colors']['text']) ? $settings['colors']['text'] : '#FFFFFF', ], // Generated questions (admin can hide/edit them) 'help_questions' => class_exists( 'AISK_Help_Questions' ) ? AISK_Help_Questions::get_instance()->get_active_questions() : [], // Only true when the WooCommerce integration is switched on and WooCommerce is active 'woocommerce_enabled' => ! empty( $settings['ai_config']['woocommerce_enabled'] ) && class_exists( 'WooCommerce' ), // Footer credit link is opt-in only 'misc' => [ 'show_credit' => ! empty( $settings['misc']['show_credit'] ), ], // Essential system paths/urls 'pluginUrl' => AISK_PLUGIN_URL, 'apiUrl' => rest_url('aisk/v1'), 'adminUrl' => admin_url('admin.php'), 'nonce' => wp_create_nonce('wp_rest'), 'locale' => function_exists( 'determine_locale' ) ? determine_locale() : get_locale(), ]; // Add custom CSS if exists if ( ! empty($settings['misc']['custom_css']) ) { wp_add_inline_style( 'aisk-chat-widget-styles', wp_strip_all_tags( $settings['misc']['custom_css'] ) ); } // Allow features to modify settings $settings = apply_filters('aisk_script_data', $frontend_settings); // Localize script wp_localize_script('aisk-chat-widget', 'AiskData', $settings); return $settings; } }