prefix; $page = max( 1, $request->get_param( 'page' ) ); $per_page = min( 100, max( 1, $request->get_param( 'per_page' ) ?: 20 ) ); $offset = ( $page - 1 ) * $per_page; $status = $request->get_param( 'status' ); $bot_id = absint( $request->get_param( 'bot_id' ) ); $search = $request->get_param( 'search' ); $visitor = sanitize_key( $request->get_param( 'visitor_type' ) ?? '' ); $where = 'WHERE 1=1'; $args = array(); if ( $status ) { $where .= ' AND c.status = %s'; $args[] = $status; } // Visitor type filter: leads (captured / has email) vs anonymous. if ( 'lead' === $visitor ) { $where .= " AND (c.lead_captured = 1 OR (c.visitor_email IS NOT NULL AND c.visitor_email <> ''))"; } elseif ( 'anonymous' === $visitor ) { $where .= " AND c.lead_captured = 0 AND (c.visitor_email IS NULL OR c.visitor_email = '')"; } if ( $bot_id ) { $where .= ' AND c.bot_id = %d'; $args[] = $bot_id; } if ( $search ) { $where .= ' AND (c.visitor_name LIKE %s OR c.visitor_email LIKE %s)'; $like = '%' . $wpdb->esc_like( $search ) . '%'; $args[] = $like; $args[] = $like; } $conv_table = $p . 'aime_chatbot_conversations'; $msg_table = $p . 'aime_chatbot_messages'; $bot_table = $p . 'aime_chatbot_bots'; // Only real chats: the visitor must have written at least one message. $where .= " AND EXISTS ( SELECT 1 FROM {$msg_table} mv WHERE mv.conversation_id = c.id AND mv.sender_type = 'visitor' )"; $count_sql = "SELECT COUNT(*) FROM {$conv_table} c {$where}"; $total = (int) ( $args ? $wpdb->get_var( $wpdb->prepare( $count_sql, ...$args ) ) : $wpdb->get_var( $count_sql ) ); $query = "SELECT c.id, c.bot_id, c.visitor_id, c.visitor_name, c.visitor_email, c.status, c.agent_id, c.lead_captured, c.satisfaction_rating, c.source, c.page_url, c.is_public, c.started_at, c.ended_at, c.created_at, b.name AS bot_name, (SELECT COUNT(*) FROM {$msg_table} m WHERE m.conversation_id = c.id AND m.sender_type != 'system') AS message_count, (SELECT m2.content FROM {$msg_table} m2 WHERE m2.conversation_id = c.id AND m2.sender_type != 'system' ORDER BY m2.id DESC LIMIT 1) AS last_message FROM {$conv_table} c LEFT JOIN {$bot_table} b ON b.id = c.bot_id {$where} ORDER BY c.updated_at DESC LIMIT %d OFFSET %d"; $args[] = $per_page; $args[] = $offset; $items = $wpdb->get_results( $wpdb->prepare( $query, ...$args ) ); return new \WP_REST_Response( array( 'items' => $items ?: array(), 'total' => $total, 'page' => $page, 'per_page' => $per_page, 'pages' => (int) ceil( $total / $per_page ), ) ); } /* ── ACTIVE conversations (for live chat) ───────── */ public function active( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $p = $wpdb->prefix; $conv_table = $p . 'aime_chatbot_conversations'; $bot_table = $p . 'aime_chatbot_bots'; $msg_table = $p . 'aime_chatbot_messages'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $items = $wpdb->get_results( $wpdb->prepare( 'SELECT c.id, c.bot_id, c.visitor_id, c.visitor_name, c.visitor_email, c.status, c.agent_id, c.lead_captured, c.page_url, c.updated_at, b.name AS bot_name, (SELECT COUNT(*) FROM %i m WHERE m.conversation_id = c.id AND m.is_read = 0 AND m.sender_type = %s) AS unread_count, (SELECT m2.content FROM %i m2 WHERE m2.conversation_id = c.id AND m2.sender_type != %s ORDER BY m2.id DESC LIMIT 1) AS last_message FROM %i c LEFT JOIN %i b ON b.id = c.bot_id WHERE c.status IN (%s,%s) AND EXISTS ( SELECT 1 FROM %i mv WHERE mv.conversation_id = c.id AND mv.sender_type = %s ) ORDER BY c.updated_at DESC LIMIT 100', $msg_table, 'visitor', $msg_table, 'system', $conv_table, $bot_table, 'active', 'human_takeover', $msg_table, 'visitor' ) ); return new \WP_REST_Response( $items ?: array() ); } /* ── SHOW single conversation with messages ──────── */ public function show( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $bot_table = $wpdb->prefix . 'aime_chatbot_bots'; $msg_table = $wpdb->prefix . 'aime_chatbot_messages'; $id = absint( $request->get_param( 'id' ) ); $conversation = $wpdb->get_row( $wpdb->prepare( "SELECT c.*, b.name AS bot_name FROM %i c LEFT JOIN %i b ON b.id = c.bot_id WHERE c.id = %d", $conv_table, $bot_table, $id ) ); if ( ! $conversation ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } $conversation->metadata = json_decode( $conversation->metadata ?: '{}', true ); // Get all messages except system messages. $conversation->messages = $wpdb->get_results( $wpdb->prepare( "SELECT id, sender_type, sender_id, content, content_type, metadata, is_read, created_at FROM %i WHERE conversation_id = %d AND sender_type != 'system' ORDER BY created_at ASC", $msg_table, $id ) ); foreach ( $conversation->messages as &$msg ) { $msg->metadata = json_decode( $msg->metadata ?: '{}', true ); } unset( $msg ); // Mark visitor messages as read. $wpdb->update( $msg_table, array( 'is_read' => 1 ), array( 'conversation_id' => $id, 'sender_type' => 'visitor', 'is_read' => 0, ) ); return new \WP_REST_Response( $conversation ); } /* ── HUMAN TAKEOVER ─────────────────────────────── */ public function takeover( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $id = absint( $request->get_param( 'id' ) ); $conversation = $wpdb->get_row( $wpdb->prepare( "SELECT id, status FROM %i WHERE id = %d", $conv_table, $id ) ); if ( ! $conversation ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } if ( 'closed' === $conversation->status ) { return new \WP_REST_Response( array( 'message' => __( 'Cannot take over a closed conversation.', 'ai-marketing-expert' ) ), 400 ); } $now = current_time( 'mysql', true ); $agent_id = get_current_user_id(); $agent = get_userdata( $agent_id ); $wpdb->update( "{$p}aime_chatbot_conversations", array( 'status' => 'human_takeover', 'agent_id' => $agent_id, 'updated_at' => $now, ), array( 'id' => $id ) ); // Insert system message. $wpdb->insert( "{$p}aime_chatbot_messages", array( 'conversation_id' => $id, 'sender_type' => 'system', 'sender_id' => (string) $agent_id, 'content' => sprintf( /* translators: %s: agent display name */ __( '%s has joined the conversation.', 'ai-marketing-expert' ), $agent ? $agent->display_name : __( 'An agent', 'ai-marketing-expert' ) ), 'content_type' => 'text', 'created_at' => $now, ) ); return new \WP_REST_Response( array( 'message' => __( 'Takeover successful.', 'ai-marketing-expert' ) ) ); } /* ── CLOSE conversation ──────────────────────────── */ public function close( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $msg_table = $wpdb->prefix . 'aime_chatbot_messages'; $id = absint( $request->get_param( 'id' ) ); $conversation = $wpdb->get_row( $wpdb->prepare( "SELECT id, status FROM %i WHERE id = %d", $conv_table, $id ) ); if ( ! $conversation ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } $now = current_time( 'mysql', true ); $wpdb->update( $conv_table, array( 'status' => 'closed', 'ended_at' => $now, 'updated_at' => $now, ), array( 'id' => $id ) ); // Insert system message. $wpdb->insert( $msg_table, array( 'conversation_id' => $id, 'sender_type' => 'system', 'content' => __( 'Conversation has been closed.', 'ai-marketing-expert' ), 'content_type' => 'text', 'created_at' => $now, ) ); return new \WP_REST_Response( array( 'message' => __( 'Conversation closed.', 'ai-marketing-expert' ) ) ); } /* ── DELETE conversation ─────────────────────────── */ public function destroy( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $msg_table = $wpdb->prefix . 'aime_chatbot_messages'; $id = absint( $request->get_param( 'id' ) ); $existing = $wpdb->get_row( $wpdb->prepare( "SELECT id FROM %i WHERE id = %d", $conv_table, $id ) ); if ( ! $existing ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } // Delete messages. $wpdb->delete( $msg_table, array( 'conversation_id' => $id ) ); // Delete conversation. $wpdb->delete( $conv_table, array( 'id' => $id ) ); return new \WP_REST_Response( array( 'message' => __( 'Conversation deleted.', 'ai-marketing-expert' ) ) ); } /* ── EXPORT conversations ────────────────────────── */ public function export( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $p = $wpdb->prefix; $rows = array(); $conv_table = $p . 'aime_chatbot_conversations'; $bot_table = $p . 'aime_chatbot_bots'; $msg_table = $p . 'aime_chatbot_messages'; $conversations = $wpdb->get_results( $wpdb->prepare( 'SELECT c.id, c.visitor_name, c.visitor_email, c.status, c.source, c.lead_captured, c.satisfaction_rating, c.created_at, c.ended_at, b.name AS bot_name FROM %i c LEFT JOIN %i b ON b.id = c.bot_id ORDER BY c.created_at DESC', $conv_table, $bot_table ) ); foreach ( $conversations as $conv ) { $messages = $wpdb->get_results( $wpdb->prepare( "SELECT sender_type, content, created_at FROM %i WHERE conversation_id = %d ORDER BY created_at ASC", $msg_table, $conv->id ) ); $transcript = ''; foreach ( $messages as $m ) { $transcript .= sprintf( "[%s] %s: %s\n", $m->created_at, $m->sender_type, $m->content ); } $rows[] = array( 'id' => $conv->id, 'bot' => $conv->bot_name, 'visitor_name' => $conv->visitor_name, 'visitor_email' => $conv->visitor_email, 'status' => $conv->status, 'source' => $conv->source, 'lead' => $conv->lead_captured ? 'Yes' : 'No', 'rating' => $conv->satisfaction_rating ?: '', 'started' => $conv->created_at, 'ended' => $conv->ended_at ?: '', 'transcript' => $transcript, ); } return new \WP_REST_Response( array( 'columns' => array( 'id', 'bot', 'visitor_name', 'visitor_email', 'status', 'source', 'lead', 'rating', 'started', 'ended', 'transcript' ), 'rows' => $rows, ) ); } /* ── EXPORT SINGLE — single conversation CSV ─────── */ public function export_single( \WP_REST_Request $request ): \WP_REST_Response { $id = absint( $request->get_param( 'id' ) ); global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $bot_table = $wpdb->prefix . 'aime_chatbot_bots'; $msg_table = $wpdb->prefix . 'aime_chatbot_messages'; $conversation = $wpdb->get_row( $wpdb->prepare( "SELECT c.*, b.name AS bot_name FROM %i c LEFT JOIN %i b ON b.id = c.bot_id WHERE c.id = %d", $conv_table, $bot_table, $id ) ); if ( ! $conversation ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } $messages = $wpdb->get_results( $wpdb->prepare( "SELECT sender_type, content, created_at FROM %i WHERE conversation_id = %d ORDER BY created_at ASC", $msg_table, $id ) ); // Build CSV string. $lines = array(); $lines[] = 'Timestamp,Sender,Message'; foreach ( $messages as $m ) { $escaped = '"' . str_replace( '"', '""', $m->content ) . '"'; $lines[] = sprintf( '%s,%s,%s', $m->created_at, $m->sender_type, $escaped ); } return new \WP_REST_Response( array( 'csv' => implode( "\n", $lines ), ) ); } /* ── BULK actions ────────────────────────────────── */ public function bulk( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $p = $wpdb->prefix; $ids = array_map( 'absint', $request->get_param( 'ids' ) ?? array() ); if ( empty( $ids ) ) { return new \WP_REST_Response( array( 'message' => __( 'No conversations selected.', 'ai-marketing-expert' ) ), 400 ); } $action = sanitize_text_field( $request->get_param( 'action' ) ); if ( 'export_leads' === $action ) { return $this->export_leads_csv( $ids ); } $now = current_time( 'mysql', true ); $count = count( $ids ); $ph = implode( ',', array_fill( 0, $count, '%d' ) ); $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $msg_table = $wpdb->prefix . 'aime_chatbot_messages'; switch ( $action ) { case 'delete': $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE conversation_id IN ({$ph})", $msg_table, ...$ids ) ); $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE id IN ({$ph})", $conv_table, ...$ids ) ); break; case 'close': $wpdb->query( $wpdb->prepare( "UPDATE %i SET status = 'closed', ended_at = %s, updated_at = %s WHERE id IN ({$ph})", $conv_table, $now, $now, ...$ids ) ); // Insert system message for each. foreach ( $ids as $cid ) { $wpdb->insert( $msg_table, array( 'conversation_id' => $cid, 'sender_type' => 'system', 'content' => __( 'Conversation has been closed.', 'ai-marketing-expert' ), 'content_type' => 'text', 'created_at' => $now, ) ); } break; case 'make_public': $wpdb->query( $wpdb->prepare( "UPDATE %i SET is_public = 1 WHERE id IN ({$ph})", $conv_table, ...$ids ) ); break; case 'make_private': $wpdb->query( $wpdb->prepare( "UPDATE %i SET is_public = 0 WHERE id IN ({$ph})", $conv_table, ...$ids ) ); break; default: return new \WP_REST_Response( array( 'message' => __( 'Unknown action.', 'ai-marketing-expert' ) ), 400 ); } return new \WP_REST_Response( array( /* translators: %d: number of conversations affected */ 'message' => sprintf( __( '%d conversation(s) updated.', 'ai-marketing-expert' ), $count ), ) ); } /* ── EXPORT LEADS from selected conversations ────────── */ /** * Build a leads CSV from the selected conversation IDs. * * Only conversations with a captured lead (lead_captured flag or a * visitor email) are included. Phone/company come from the * conversation metadata JSON written by the public lead endpoint. * * @param int[] $ids Conversation IDs. */ private function export_leads_csv( array $ids ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $bot_table = $wpdb->prefix . 'aime_chatbot_bots'; $ph = implode( ',', array_fill( 0, count( $ids ), '%d' ) ); $conversations = $wpdb->get_results( $wpdb->prepare( "SELECT c.id, c.visitor_name, c.visitor_email, c.metadata, c.page_url, c.status, c.created_at, b.name AS bot_name FROM %i c LEFT JOIN %i b ON b.id = c.bot_id WHERE c.id IN ({$ph}) AND (c.lead_captured = 1 OR (c.visitor_email IS NOT NULL AND c.visitor_email <> '')) ORDER BY c.created_at DESC", $conv_table, $bot_table, ...$ids ) ); if ( empty( $conversations ) ) { return new \WP_REST_Response( array( 'message' => __( 'No leads found in the selected conversations.', 'ai-marketing-expert' ), ), 400 ); } $esc = static function ( $value ): string { return '"' . str_replace( '"', '""', (string) $value ) . '"'; }; $lines = array(); $lines[] = 'Conversation ID,Bot,Name,Email,Phone,Company,Page URL,Status,Captured At'; foreach ( $conversations as $conv ) { $meta = json_decode( $conv->metadata ?: '{}', true ) ?: array(); $lines[] = implode( ',', array( (int) $conv->id, $esc( $conv->bot_name ), $esc( $conv->visitor_name ), $esc( $conv->visitor_email ), $esc( $meta['phone'] ?? '' ), $esc( $meta['company'] ?? '' ), $esc( $conv->page_url ), $esc( $conv->status ), $esc( $conv->created_at ), ) ); } return new \WP_REST_Response( array( 'csv' => implode( "\n", $lines ), 'filename' => 'chatbot-leads-' . gmdate( 'Y-m-d' ) . '.csv', 'count' => count( $conversations ), ) ); } /* ── TOGGLE PUBLIC visibility ───────────────────── */ public function toggle_public( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $id = absint( $request->get_param( 'id' ) ); $conversation = $wpdb->get_row( $wpdb->prepare( "SELECT id, is_public FROM %i WHERE id = %d", $conv_table, $id ) ); if ( ! $conversation ) { return new \WP_REST_Response( array( 'message' => __( 'Conversation not found.', 'ai-marketing-expert' ) ), 404 ); } $new_value = $conversation->is_public ? 0 : 1; $wpdb->update( $conv_table, array( 'is_public' => $new_value ), array( 'id' => $id ) ); return new \WP_REST_Response( array( 'is_public' => (bool) $new_value, 'message' => $new_value ? __( 'Conversation is now public.', 'ai-marketing-expert' ) : __( 'Conversation is now hidden.', 'ai-marketing-expert' ), ) ); } /** * Sync captured chatbot leads to Email Marketing subscribers. */ public function sync_leads_to_subscribers( \WP_REST_Request $request ): \WP_REST_Response { global $wpdb; $conv_table = $wpdb->prefix . 'aime_chatbot_conversations'; $subs_table = $wpdb->prefix . 'aime_subscribers'; // Verify that email subscribers table exists if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $subs_table ) ) !== $subs_table ) { return new \WP_REST_Response( array( 'success' => false, 'message' => __( 'Email Marketing module is not yet initialized or table missing.', 'ai-marketing-expert' ), ), 400 ); } $leads = $wpdb->get_results( $wpdb->prepare( 'SELECT DISTINCT visitor_name, visitor_email, started_at FROM %i WHERE (lead_captured = 1 OR (visitor_email IS NOT NULL AND visitor_email != "")) AND visitor_email != ""', $conv_table ) ); if ( empty( $leads ) ) { return new \WP_REST_Response( array( 'success' => true, 'synced' => 0, 'message' => __( 'No chatbot leads found to sync.', 'ai-marketing-expert' ), ), 200 ); } $synced = 0; $now = current_time( 'mysql' ); foreach ( $leads as $lead ) { $email = sanitize_email( $lead->visitor_email ); if ( ! is_email( $email ) ) { continue; } $exists = (int) $wpdb->get_var( $wpdb->prepare( "SELECT id FROM {$subs_table} WHERE email = %s LIMIT 1", $email ) ); if ( ! $exists ) { $name_parts = explode( ' ', trim( (string) $lead->visitor_name ), 2 ); $first_name = sanitize_text_field( $name_parts[0] ?? '' ); $last_name = sanitize_text_field( $name_parts[1] ?? '' ); $wpdb->insert( $subs_table, array( 'hash' => md5( $email . time() ), 'first_name' => $first_name, 'last_name' => $last_name, 'email' => $email, 'status' => 'subscribed', 'contact_type' => 'lead', 'source' => 'chatbot', 'created_at' => $lead->started_at ?: $now, 'updated_at' => $now, ) ); $new_id = (int) $wpdb->insert_id; if ( $new_id > 0 ) { $synced++; do_action( 'aime_subscriber_created', $new_id, array( 'email' => $email, 'first_name' => $first_name, 'source' => 'chatbot', ) ); } } } return new \WP_REST_Response( array( 'success' => true, 'synced' => $synced, 'total' => count( $leads ), 'message' => sprintf( /* translators: %d: count of synced subscribers */ __( 'Successfully synced %d leads to Email Marketing subscribers.', 'ai-marketing-expert' ), $synced ), ), 200 ); } }