'defer', 'in_footer' => true ) ); wp_add_inline_script( self::HANDLE, 'window.AgentixChatConfig=' . wp_json_encode( self::client_config( $preview ), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' ); } /** Everything the browser needs, including the workflow URL (a public launch link). */ public static function client_config( bool $preview = false ): array { $o = Agentix_Chat_Options::all(); if ( ! is_admin() ) { foreach ( Agentix_Chat_I18n::KEYS as $key ) { $o[ $key ] = Agentix_Chat_I18n::t( $key, (string) $o[ $key ] ); } } $site = get_bloginfo( 'name' ); $cfg = array( 'cssUrl' => AGENTIX_CHAT_URL . 'assets/widget.css?ver=' . self::asset_version( 'assets/widget.css' ), 'logo' => array( 'light' => AGENTIX_CHAT_URL . 'assets/img/agentix-logo-light.png', 'dark' => AGENTIX_CHAT_URL . 'assets/img/agentix-logo-dark.png', ), 'preview' => $preview, 'launcher' => $preview || Agentix_Chat_Display_Rules::should_show_launcher(), 'rtl' => is_rtl(), 'test' => 'test' === $o['mode'], 'iframe' => array( 'url' => self::iframe_url( $o ), 'allow' => implode( '; ', (array) $o['iframe_permissions'] ), 'preload' => $o['iframe_preload'], 'newTab' => (bool) $o['iframe_new_tab'], 'loading' => $o['loading_text'], ), 'bubble' => array( 'style' => $o['launcher_style'], 'label' => $o['launcher_label'], 'shape' => $o['launcher_shape'], 'size' => (int) $o['launcher_size'], 'icon' => $o['launcher_icon'], 'image' => $o['launcher_icon_url'], 'iconScale' => (int) $o['launcher_icon_scale'], 'bg' => $o['launcher_bg'] ?: $o['primary_color'], 'bg2' => $o['launcher_gradient'] ? $o['launcher_bg2'] : '', 'fg' => $o['launcher_fg'], 'borderWidth' => (int) $o['launcher_border_width'], 'borderColor' => $o['launcher_border_color'], 'shadow' => $o['launcher_shadow'], 'hover' => $o['launcher_hover'], 'attention' => $o['launcher_attention'], 'every' => (int) $o['launcher_attention_every'], 'dot' => (bool) $o['launcher_dot'], 'tooltip' => $o['launcher_tooltip'], 'whenOpen' => $o['launcher_when_open'], 'mobileY' => (int) $o['offset_mobile_y'], ), 'ui' => array( 'theme' => $o['theme'], 'color' => $o['primary_color'], 'font' => $o['font'], 'radius' => (int) $o['radius'], 'position' => $o['position'], 'offsetX' => (int) $o['offset_x'], 'offsetY' => (int) $o['offset_y'], 'zIndex' => (int) $o['z_index'], 'width' => (int) $o['panel_width'], 'height' => (int) $o['panel_height'], 'launcherStyle' => $o['launcher_style'], 'launcherLabel' => $o['launcher_label'], 'launcherIcon' => $o['launcher_icon'], 'launcherImage' => $o['launcher_icon_url'], 'avatar' => $o['agent_avatar'] ?: (string) get_site_icon_url( 96 ), 'title' => $o['header_title'] ?: $site, 'subtitle' => $o['header_subtitle'], 'branding' => (bool) $o['show_branding'], 'mobileFull' => (bool) $o['mobile_fullscreen'], 'hideOverlays' => (bool) $o['hide_overlays'], 'fit' => 'fit' === $o['panel_size'], 'showHeader' => (bool) $o['show_header'], 'showStatus' => (bool) $o['show_status'], 'openAnimation' => $o['open_animation'], 'backdrop' => (bool) $o['backdrop'], ), 'behaviour' => array( 'autoOpen' => $preview ? 'never' : $o['auto_open'], 'autoOpenDelay' => (int) $o['auto_open_delay'], 'autoOpenScroll' => (int) $o['auto_open_scroll'], 'autoOpenOnce' => (bool) $o['auto_open_once'], 'teaser' => $o['teaser_enabled'] ? $o['teaser_text'] : '', 'teaserDelay' => (int) $o['teaser_delay'], 'devices' => $o['display_devices'], 'analytics' => (bool) $o['analytics'], 'closeOnEsc' => (bool) $o['close_on_esc'], ), 'hours' => $o['hours_enabled'] ? array( 'schedule' => $o['hours'], 'timezone' => wp_timezone_string(), 'offsetMin' => (int) round( (float) get_option( 'gmt_offset' ) * 60 ), 'offline' => $o['offline_behaviour'], 'message' => $o['offline_message'], ) : null, 'consent' => $o['consent_required'] ? array( 'text' => $o['consent_text'], 'policyUrl' => (string) get_privacy_policy_url(), 'policy' => __( 'Privacy Policy', 'agentixhub-chat' ), 'button' => $o['consent_button'], ) : null, 'i18n' => self::strings(), ); return apply_filters( 'agentix_chat_client_config', $cfg ); } private static function strings(): array { return array( 'open' => __( 'Open chat', 'agentixhub-chat' ), 'close' => __( 'Close chat', 'agentixhub-chat' ), 'powered' => __( 'Powered by', 'agentixhub-chat' ), 'start' => __( 'Start chat', 'agentixhub-chat' ), 'offline' => __( 'Offline', 'agentixhub-chat' ), 'online' => __( 'Online', 'agentixhub-chat' ), 'test' => __( 'Test mode', 'agentixhub-chat' ), 'newTab' => __( 'Open in new window', 'agentixhub-chat' ), 'noUrl' => __( 'Add your workflow URL in AgentixHub Chat → Connection to see it here.', 'agentixhub-chat' ), ); } /** * Shared renderer for shortcode, block and classic widget. * * @param array $args mode (inline|button), height, title, label. */ public static function render_embed( array $args ): string { $o = Agentix_Chat_Options::all(); if ( ! Agentix_Chat_Display_Rules::ready( $o ) || ! Agentix_Chat_Display_Rules::audience_ok( $o ) ) { return ''; } self::register_assets(); wp_enqueue_script( self::HANDLE ); $mode = ( $args['mode'] ?? 'inline' ) === 'button' ? 'button' : 'inline'; $data = array( 'mode' => $mode, 'height' => max( 300, min( 1200, (int) ( $args['height'] ?? 560 ) ) ), 'title' => sanitize_text_field( (string) ( $args['title'] ?? '' ) ), ); if ( 'button' === $mode ) { $label = $args['label'] ?? '' ?: __( 'Chat with us', 'agentixhub-chat' ); return sprintf( '', esc_html( $label ) ); } return sprintf( '
', esc_attr( wp_json_encode( $data ) ), $data['height'] ); } public static function shortcode( $atts ): string { $atts = shortcode_atts( array( 'mode' => 'inline', 'height' => 560, 'title' => '', 'label' => '' ), $atts, 'agentix_chat' ); return self::render_embed( $atts ); } /** AgentixHub colour scheme for each "Dark mode support" choice (launchpad `theme` parameter). */ const THEME_PARAM = array( 'auto' => 'system', 'light' => 'light', 'dark' => 'dark' ); /** * Workflow URL with the window's colour scheme and the page language (launchpad `theme` / `lang`), * plus admin-defined parameters with page/user placeholders resolved; admin values win. */ private static function iframe_url( array $o ): string { $url = (string) $o['workflow_url']; if ( '' === $url ) { return ''; } $user = wp_get_current_user(); $vars = array( '{lang}' => self::language(), '{page_url}' => is_singular() ? (string) get_permalink() : home_url( '/' ), '{page_title}' => is_singular() ? html_entity_decode( get_the_title(), ENT_QUOTES, 'UTF-8' ) : get_bloginfo( 'name' ), '{user_name}' => $user->exists() ? $user->display_name : '', '{user_email}' => $user->exists() ? $user->user_email : '', ); $args = array( 'theme' => self::THEME_PARAM[ $o['theme'] ] ?? 'system', // Only languages the launchpad has UI texts for; others would show untranslated keys. 'lang' => 'ar' === self::language() ? 'ar' : 'en', ); foreach ( self::parse_kv( (string) $o['iframe_params'] ) as $k => $v ) { $args[ $k ] = strtr( $v, $vars ); } return add_query_arg( array_map( 'rawurlencode', $args ), $url ); } /** Active language, aware of Polylang and WPML. */ private static function language(): string { if ( function_exists( 'pll_current_language' ) && pll_current_language() ) { return (string) pll_current_language(); } if ( defined( 'ICL_LANGUAGE_CODE' ) && ICL_LANGUAGE_CODE ) { return (string) ICL_LANGUAGE_CODE; } return substr( determine_locale(), 0, 2 ); } /** key=value lines → array. */ private static function parse_kv( string $lines ): array { $out = array(); foreach ( preg_split( '/\R/u', $lines ) as $line ) { if ( str_contains( $line, '=' ) ) { list( $k, $v ) = array_map( 'trim', explode( '=', $line, 2 ) ); if ( '' !== $k ) { $out[ sanitize_key( $k ) ] = sanitize_text_field( $v ); } } } return $out; } public static function asset_version( string $rel ): string { $file = AGENTIX_CHAT_DIR . $rel; return ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG && file_exists( $file ) ) ? (string) filemtime( $file ) : AGENTIX_CHAT_VERSION; } }