core = $mwai_core; if ( is_admin() ) { return; } add_shortcode( 'mwai_chat', array( $this, 'chat' ) ); add_shortcode( 'mwai_chatbot', array( $this, 'chat' ) ); add_shortcode( 'mwai_imagesbot', array( $this, 'imageschat' ) ); add_action( 'rest_api_init', array( $this, 'rest_api_init' ) ); add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); new Meow_MWAI_Modules_Chatbot_Chats(); if ( $this->core->get_option( 'shortcode_chat_inject' ) ) { add_action( 'wp_footer', array( $this, 'inject_chat' ) ); } $logs = $this->core->get_option( 'shortcode_chat_logs' ); if ( $logs && strpos( $logs, 'file' ) !== false ) { new Meow_MWAI_Modules_Chatbot_Logs(); } if ( $this->core->get_option( 'shortcode_chat_styles' ) ) { add_filter( 'mwai_chatbot_style', [ $this, 'apply_chat_styles' ], 10, 2 ); } } public function enqueue_scripts() { if ( $this->core->get_option( 'shortcode_chat_syntax_highlighting' ) ) { wp_enqueue_script( 'mwai_chatbot', MWAI_URL . 'vendor/highlightjs/highlight.min.js', [], '11.7', false ); wp_enqueue_style( 'mwai_chatbot', MWAI_URL . '/vendor/highlightjs/stackoverflow-dark.min.css', [], '11.7' ); } if ( $this->core->get_option( 'shortcode_chat_typewriter' ) ) { wp_enqueue_script( 'mwai_chatbot_typewriter', MWAI_URL . 'vendor/typewriterjs/typewriter.min.js', [], '2.0', true ); } } public function rest_api_init() { register_rest_route( $this->namespace, '/chat', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_chat' ), 'permission_callback' => '__return_true' ) ); register_rest_route( $this->namespace, '/imagesbot', array( 'methods' => 'POST', 'callback' => array( $this, 'rest_imagesbot' ), 'permission_callback' => '__return_true' ) ); } public function chatgpt_style( $id ) { $css = file_get_contents( MWAI_PATH . '/classes/modules/chatbot-chatgpt.css' ); $css = str_replace( '#mwai-chat-id', "#mwai-chat-{$id}", $css ); return ''; } public function rest_chat( $request ) { try { $params = $request->get_json_params(); $query = new Meow_MWAI_QueryText( $params['prompt'], 1024 ); $query->injectParams( $params ); $takeoverAnswer = apply_filters( 'mwai_chatbot_takeover', null, $query, $params ); if ( !empty( $takeoverAnswer ) ) { return new WP_REST_Response( [ 'success' => true, 'answer' => $takeoverAnswer, 'html' => $takeoverAnswer, 'usage' => null ], 200 ); } // Moderation if ( $this->core->get_option( 'shortcode_chat_moderation' ) ) { global $mwai; $isFlagged = $mwai->moderationCheck( $query->prompt ); if ( $isFlagged ) { return new WP_REST_Response( [ 'success' => false, 'message' => 'Sorry, your message has been rejected by moderation.' ], 403 ); } } // Awareness & Embeddings $embedding = null; $embeddingsIndex = $params['embeddingsIndex']; if ( $query->mode === 'chat' && !empty( $embeddingsIndex ) ) { $embedding = apply_filters( 'mwai_context_search', $query, $embeddingsIndex ); if ( !empty( $embedding ) ) { $query->injectContext( $embedding['content'] ); } } $answer = $this->core->ai->run( $query ); $rawText = $answer->result; $extra = [ 'embedding' => $embedding ]; $html = apply_filters( 'mwai_chatbot_reply', $rawText, $query, $params, $extra ); if ( $this->core->get_option( 'shortcode_chat_formatting' ) ) { $html = $this->core->markdown_to_html( $html ); } return new WP_REST_Response( [ 'success' => true, 'answer' => $rawText, 'html' => $html, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } public function rest_imagesbot( $request ) { try { $params = $request->get_json_params(); $query = new Meow_MWAI_QueryImage( $params['prompt'] ); $query->injectParams( $params ); $answer = $this->core->ai->run( $query ); return new WP_REST_Response( [ 'success' => true, 'images' => $answer->results, 'usage' => $answer->usage ], 200 ); } catch ( Exception $e ) { return new WP_REST_Response( [ 'success' => false, 'message' => $e->getMessage() ], 500 ); } } public function apply_chat_styles( $css, $chatbotId ) { $chatStyles = $this->core->get_option( 'shortcode_chat_styles' ); return preg_replace_callback( '/--mwai-(\w+):\s*([^;]+);/', function ( $matches ) use ( $chatStyles ) { if ( isset( $chatStyles[$matches[1]] ) ) { return '--mwai-' . $matches[1] . ': ' . $chatStyles[$matches[1]] . ';'; } return $matches[0]; }, $css ); } public function inject_chat() { $params = $this->core->get_option( 'shortcode_chat_params' ); echo $this->chat( $params ); } public function imageschat( $atts ) { $atts['mode'] = 'images'; return $this->chat( $atts ); } public function getCurrentUser() { if ( is_user_logged_in() ) { return wp_get_current_user(); } return null; } public function handlePlaceholders( $data, $guestName = 'Guest: ' ) { if ( strpos( $data, '{' ) === false ) { return $data; } $placeholders_meta = [ '{FIRST_NAME}', '{LAST_NAME}' ]; $placeholders_data = [ '{USER_LOGIN}', '{DISPLAY_NAME}' ]; $user = $this->getCurrentUser(); if ( $user ) { foreach ( $placeholders_meta as $placeholder ) { if ( strpos( $data, $placeholder ) === false ) { continue; } $lcPlaceholder = substr( strtolower( $placeholder ), 1, -1 ); $value = get_user_meta( $user->ID, $lcPlaceholder, true ); $data = str_replace( $placeholder, $value, $data ); } foreach ( $placeholders_data as $placeholder ) { if ( strpos( $data, $placeholder ) === false ) { continue; } $lcPlaceholder = substr( strtolower( $placeholder ), 1, -1 ); $value = $user->data->$lcPlaceholder; $data = str_replace( $placeholder, $value, $data ); } if ( !empty( $data ) ) { return $data; } } return $guestName; } public function formatUserName( $userName, $guestName = 'Guest: ' ) { // Default avatar if ( empty( $userName ) ) { $user = $this->getCurrentUser(); if ( $user ) { // Gravatar $userName = '