admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('abmsense_dashboard_nonce'), 'colors' => array( 'hot' => '#E53E3E', // Red for hot leads 'medium' => '#F59E0B', // Orange for medium leads 'low' => '#10B981', // Green for low leads ) )); } add_action('admin_enqueue_scripts', 'abmsense_enqueue_dashboard_assets'); /** Create Dashboard main admin menu entry */ function abmsense_create_dashboard_menu() { // Update the main menu to point to the dashboard add_menu_page( 'ABMsense', 'ABMsense', 'manage_options', ABMSENSE_PREFIX . 'dashboard', 'abmsense_dashboard_page', 'dashicons-visibility', 6 ); // Add dashboard as first submenu add_submenu_page( ABMSENSE_PREFIX . 'dashboard', 'ABMsense Dashboard', 'Dashboard', 'manage_options', ABMSENSE_PREFIX . 'dashboard', 'abmsense_dashboard_page' ); // Add settings submenu add_submenu_page( ABMSENSE_PREFIX . 'dashboard', 'ABMsense Settings', 'Settings', 'manage_options', ABMSENSE_PREFIX . 'settings', 'abmsense_settings_page' ); // Add the "Real-time Data" tab add_submenu_page( ABMSENSE_PREFIX . 'dashboard', 'ABMsense Data', 'Real-time Data', 'manage_options', ABMSENSE_PREFIX . 'table', 'abmsense_display_admin_page' ); // Add the "Reports" tab add_submenu_page( ABMSENSE_PREFIX . 'dashboard', 'Generate Your Report', 'Reports', 'manage_options', ABMSENSE_PREFIX . 'on_demand', 'abmsense_on_demand_page' ); } add_action('admin_menu', 'abmsense_create_dashboard_menu'); /** Helper function to encrypt data for API requests */ function abmsense_encrypt_data_for_api($data) { // Include the config file to get the public key $config = include('db_config.php'); $public_key = $config['public_key']; // Return result from existing encryption function return abmsense_encrypt_data($data, $public_key); } /** Display the main dashboard page focused on visitor scores */ function abmsense_dashboard_page() { // Get customer name (website domain) $customer_name = get_option(ABMSENSE_PREFIX . 'customer_name', sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'] ?? ''))); ?>
$customer_name, 'report_email' => get_option(ABMSENSE_PREFIX . 'report_email', ''), 'export_period' => $time_period // Use the selected time period ); // Encrypt data for the API request $encrypted_data = abmsense_encrypt_data_for_api($data); if (!$encrypted_data) { wp_send_json_error('Failed to encrypt data'); } // Make API request $response = wp_remote_post($api_url, array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => wp_json_encode($encrypted_data), 'timeout' => 60, 'sslverify' => true )); if (is_wp_error($response)) { wp_send_json_error('Dashboard API request failed: ' . $response->get_error_message()); } else { abmsense_log('SSL request success: ' . wp_remote_retrieve_response_code($response), 'success'); } $response_body = wp_remote_retrieve_body($response); $result = json_decode($response_body, true); if (!isset($result['success']) || !$result['success']) { wp_send_json_error($result['error'] ?? 'Unknown error occurred'); } // If no data, return sample data if (empty($result['data'])) { $sample_data = abmsense_get_sample_score_data($time_period); wp_send_json_success($sample_data); return; } // Count total unique companies before filtering $unique_companies = array(); foreach ($result['data'] as $item) { if (!empty($item['company_name'])) { $unique_companies[$item['company_name']] = 1; // Using array keys for uniqueness } } $total_companies_count = count($unique_companies); // Extract only company name and score data, and sort by score $scores_data = array(); foreach ($result['data'] as $item) { if (!empty($item['company_name']) && isset($item['lead_score'])) { $scores_data[] = array( 'company_name' => $item['company_name'], 'score' => (int) $item['lead_score'], 'last_updated' => !empty($item['last_visited_date']) ? $item['last_visited_date'] : gmdate('Y-m-d') ); } } // Sort by score in descending order usort($scores_data, function($a, $b) { return $b['score'] - $a['score']; }); // Limit to 7 rows for display, but keep the total count $scores_data = array_slice($scores_data, 0, 7); wp_send_json_success(array( 'company_data' => $scores_data, 'is_demo_data' => false, 'total_count' => $total_companies_count, 'time_period' => $time_period // Include time period in response )); } add_action('wp_ajax_abmsense_get_visitor_scores', 'abmsense_ajax_get_visitor_scores'); /** Generate sample score data when real data is not available */ function abmsense_get_sample_score_data($time_period = 'All_time') { $companies = array( 'Acme Corporation', 'Globex Industries', 'Stark Enterprises', 'Wayne Industries', 'Umbrella Corporation', 'Cyberdyne Systems', 'Soylent Corporation' ); $scores_data = array(); // Create a spread of scores across all categories (hot, medium, low) $scores = array(95, 87, 82, 63, 57, 38, 26); // Adjust the number of companies based on time period $company_count = 7; $total_companies = 15; // Adjust based on time period to simulate different data volumes if ($time_period === 'Today') { $company_count = min(3, count($companies)); $total_companies = 5; } else if ($time_period === 'Last_7_days') { $company_count = min(5, count($companies)); $total_companies = 8; } else if ($time_period === 'Last_30_days') { $company_count = min(6, count($companies)); $total_companies = 12; } // Slice the arrays to adjust for time period $companies = array_slice($companies, 0, $company_count); $scores = array_slice($scores, 0, $company_count); foreach ($companies as $index => $company) { // Vary the scores slightly for more realistic data $base_score = $scores[$index]; $score = max(min($base_score + wp_rand(-3, 3), 100), 1); // Keep between 1-100 $scores_data[] = array( 'company_name' => $company, 'score' => $score, 'last_updated' => gmdate('D, d M Y', strtotime('-' . wp_rand(1, 14) . ' days')) ); } return array( 'company_data' => $scores_data, 'is_demo_data' => true, 'total_count' => $total_companies, 'time_period' => $time_period ); }