array( $this, 'render_shortcode' ), ) ); } } /** * Render chat widget markup. * * @param array|string $atts Attributes. */ public function render_shortcode( $atts = array() ): string { if ( is_admin() || ! Aivexa_Settings::is_agent_ready() ) { return ''; } $this->should_load_assets = true; $settings = Aivexa_Settings::get_all(); $atts = shortcode_atts( array( 'mode' => 'inline', ), is_array( $atts ) ? $atts : array(), 'aivexa' ); ob_start(); $this->render_chat_markup( (string) $atts['mode'], $settings ); return (string) ob_get_clean(); } /** * Maybe enqueue frontend assets. */ public function maybe_enqueue_assets(): void { global $post; if ( is_admin() ) { return; } if ( ! Aivexa_Settings::is_agent_ready() ) { return; } $settings = Aivexa_Settings::get_all(); if ( ! $this->should_load_assets ) { if ( ! empty( $settings['floating_widget_enabled'] ) ) { $this->should_load_assets = true; } elseif ( $post instanceof WP_Post && ( has_shortcode( $post->post_content, 'aivexa' ) ) ) { $this->should_load_assets = true; } elseif ( $post instanceof WP_Post && function_exists( 'has_block' ) && has_block( 'aivexa/chat', $post ) ) { $this->should_load_assets = true; } } if ( ! $this->should_load_assets ) { return; } wp_enqueue_style( 'aivexa-chat', AIVEXA_PLUGIN_URL . 'public/css/chat.css', array(), AIVEXA_VERSION ); wp_enqueue_script( 'aivexa-chat', AIVEXA_PLUGIN_URL . 'public/js/chat.js', array(), AIVEXA_VERSION, true ); wp_localize_script( 'aivexa-chat', 'aivexaChat', array( 'restUrl' => esc_url_raw( rest_url( AIVEXA_REST_NAMESPACE ) ), 'nonce' => wp_create_nonce( 'wp_rest' ), 'agentName' => (string) $settings['agent_name'], 'welcomeMessage' => (string) $settings['welcome_message'], 'buttonText' => (string) $settings['button_text'], 'primaryColor' => (string) $settings['primary_color'], 'widgetPosition' => (string) $settings['widget_position'], 'i18n' => array( 'send' => __( 'Send', 'aivexa' ), 'placeholder' => __( 'Type your message…', 'aivexa' ), 'reset' => __( 'Reset conversation', 'aivexa' ), 'close' => __( 'Close chat', 'aivexa' ), 'open' => (string) $settings['button_text'], 'loading' => __( 'Thinking…', 'aivexa' ), 'error' => __( 'Sorry, the AI assistant is temporarily unavailable. Please try again later.', 'aivexa' ), 'assistantLabel' => __( 'Assistant message', 'aivexa' ), 'userLabel' => __( 'Your message', 'aivexa' ), ), ) ); } /** * Render floating widget in footer when enabled. */ public function maybe_render_floating_widget(): void { if ( is_admin() || ! Aivexa_Settings::is_agent_ready() ) { return; } $settings = Aivexa_Settings::get_all(); if ( empty( $settings['floating_widget_enabled'] ) ) { return; } $this->should_load_assets = true; $this->render_chat_markup( 'floating', $settings ); } /** * Output chat HTML. * * @param string $mode Render mode. * @param array $settings Settings. */ private function render_chat_markup( string $mode, array $settings ): void { ++$this->instance_count; $position = in_array( (string) $settings['widget_position'], array( 'bottom-right', 'bottom-left' ), true ) ? (string) $settings['widget_position'] : 'bottom-right'; $panel_id = 'aivexa-chat-panel-' . $this->instance_count; $input_id = 'aivexa-chat-input-' . $this->instance_count; $classes = array( 'aivexa-chat-root', 'aivexa-chat-mode-' . sanitize_html_class( $mode ), 'aivexa-chat-position-' . sanitize_html_class( $position ), ); ?>
>