modules = $modules; add_action( 'wp_dashboard_setup', array( $this, 'register' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) ); add_action( 'wp_ajax_aime_dashboard_widget_refresh', array( $this, 'ajax_refresh' ) ); add_action( 'wp_ajax_aime_chatbot_realtime', array( $this, 'ajax_chatbot_realtime' ) ); // Force widget to top of dashboard regardless of saved user order. add_filter( 'get_user_option_meta-box-order_dashboard', array( $this, 'force_widget_order' ) ); // Reorder registration order for users with no saved widget order. add_action( 'wp_dashboard_setup', array( $this, 'reorder_registered_widget' ), 100 ); } /** * Enqueue widget assets on the main WP dashboard only. * * @param string $hook_suffix Current admin page hook. */ public function enqueue_assets( string $hook_suffix ): void { // Only load on the main WP dashboard. if ( 'index.php' !== $hook_suffix ) { return; } wp_enqueue_style( 'aime-dashboard-widget', AIME_PLUGIN_URL . 'assets/css/dashboard-widget.css', array(), AIME_VERSION ); wp_enqueue_script( 'aime-dashboard-widget', AIME_PLUGIN_URL . 'assets/js/dashboard-widget.js', array(), AIME_VERSION, true ); } /** * Register the dashboard widget. */ public function register(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } wp_add_dashboard_widget( 'aime_dashboard_widget', 'AI Marketing Expert', array( $this, 'render' ) ); } /** * Get stats for a specific module via its registered filter. * * @param string $module_id Module identifier. * @return array Module stats array. */ private function get_module_stats( string $module_id ): array { if ( ! $this->modules->is_active( $module_id ) ) { return array(); } return apply_filters( "aime_{$module_id}_dashboard_stats", array() ); } /** * Get chatbot real-time data for the active conversations feed. * * @return array Associative array with active_count, messages_today, * leads_today, and recent conversations. */ private function get_chatbot_realtime(): array { global $wpdb; $p = $wpdb->prefix; $conversations = "{$p}aime_chatbot_conversations"; $messages = "{$p}aime_chatbot_messages"; $bots = "{$p}aime_chatbot_bots"; $today = gmdate( 'Y-m-d 00:00:00' ); // Check table existence. if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $conversations ) ) ) { return array( 'active_count' => 0, 'messages_today' => 0, 'leads_today' => 0, 'total_bots' => 0, 'recent' => array(), ); } // Only count conversations where the visitor has sent at least one message. // A session with only a bot greeting (system/ai sender) is not truly "active". $active_count = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i c WHERE c.status IN (%s, %s) AND EXISTS ( SELECT 1 FROM %i m WHERE m.conversation_id = c.id AND m.sender_type = %s )', $conversations, 'active', 'human_takeover', $messages, 'visitor' ) ); // Only count visitor messages so welcome/system/AI messages don't inflate the total. $messages_today = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at >= %s AND sender_type = %s', $messages, $today, 'visitor' ) ); $leads_today = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE lead_captured = %d AND created_at >= %s', $conversations, 1, $today ) ); $total_bots = (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i', $bots ) ); // Fetch recent conversations (last 5) with last message preview. $recent = array(); if ( $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $messages ) ) ) { // Only show conversations where the visitor has actually written something; // msg_count reflects visitor messages only so "1 msg" always means real engagement. $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT c.id, c.visitor_name, c.page_url, c.status, c.created_at, ( SELECT SUBSTRING(m.content, 1, 60) FROM %i m WHERE m.conversation_id = c.id ORDER BY m.id DESC LIMIT 1 ) AS last_message, ( SELECT COUNT(*) FROM %i m2 WHERE m2.conversation_id = c.id AND m2.sender_type = %s ) AS msg_count FROM %i c WHERE c.created_at >= DATE_SUB(%s, INTERVAL 24 HOUR) AND EXISTS ( SELECT 1 FROM %i mv WHERE mv.conversation_id = c.id AND mv.sender_type = %s ) ORDER BY c.created_at DESC LIMIT 5', $messages, $messages, 'visitor', $conversations, $today, $messages, 'visitor' ) ); } if ( $rows ) { foreach ( $rows as $row ) { $recent[] = array( 'id' => (int) $row->id, 'visitor_name' => $row->visitor_name ? sanitize_text_field( $row->visitor_name ) : __( 'Visitor', 'ai-marketing-expert' ), 'page_url' => $row->page_url ? esc_url_raw( $row->page_url ) : '', 'status' => sanitize_key( $row->status ), 'last_message' => $row->last_message ? sanitize_text_field( $row->last_message ) : '', 'msg_count' => (int) $row->msg_count, 'created_at' => $row->created_at, 'time_ago' => sprintf( /* translators: %s: human-readable time difference, e.g. "15 hours". */ __( '%s ago', 'ai-marketing-expert' ), human_time_diff( strtotime( $row->created_at . ' UTC' ), time() ) ), ); } } return array( 'active_count' => $active_count, 'messages_today' => $messages_today, 'leads_today' => $leads_today, 'total_bots' => $total_bots, 'recent' => $recent, ); } /** * Render the widget contents. * * @param string|array $args Widget arguments (WP may pass empty string). * @param mixed|null $callback_args Callback arguments. */ public function render( $args = '', $callback_args = null ): void { $chatbot = $this->get_chatbot_realtime(); $email = $this->get_module_stats( 'email-marketing' ); $seo = $this->get_module_stats( 'seo' ); $content = $this->get_module_stats( 'content-generator' ); $social = $this->get_module_stats( 'social-media' ); $auto = $this->get_module_stats( 'workflow-automation' ); wp_localize_script( 'aime-dashboard-widget', 'aimeWidgetData', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'chatbotUrl' => admin_url( 'admin.php?page=ai-marketing-expert-chatbot' ), 'nonce' => wp_create_nonce( 'aime_dashboard_widget' ), 'hasActive' => $chatbot['active_count'] > 0, ) ); ?>