admin_url('admin-ajax.php'),
'nonce' => wp_create_nonce('abmsense_dashboard_nonce'),
'colors' => array(
'hot' => '#E53E3E', // Red for hot leads
'warm' => '#F59E0B', // Orange for warm leads
'cold' => '#B3D4FC', // Light blue for cold 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'] ?? '')));
?>
| Rank |
Company |
Score |
Last Updated |
LinkedIn |
|
- 80-100 🔥 -
- 50-79 -
- 0-49 -
$customer_name,
'report_email' => get_option(ABMSENSE_PREFIX . 'report_email', ''),
'export_period' => $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);
// Add debug logging
// abmsense_log('API Response Data: ' . print_r($result, true), 'debug');
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'])) {
// Check if the user has ever had data by making a separate API call for all time
if ($time_period !== 'All_time') {
// Make a separate API call for all time to check if the user has ever had data
$all_time_data = array(
'customer_name' => $customer_name,
'report_email' => get_option(ABMSENSE_PREFIX . 'report_email', ''),
'export_period' => 'All_time'
);
// Encrypt data for the API request
$encrypted_all_time_data = abmsense_encrypt_data_for_api($all_time_data);
if (!$encrypted_all_time_data) {
wp_send_json_error('Failed to encrypt data');
}
// Make API request for all time data
$all_time_response = wp_remote_post($api_url, array(
'method' => 'POST',
'headers' => array('Content-Type' => 'application/json'),
'body' => wp_json_encode($encrypted_all_time_data),
'timeout' => 60,
'sslverify' => true
));
if (!is_wp_error($all_time_response)) {
$all_time_response_body = wp_remote_retrieve_body($all_time_response);
$all_time_result = json_decode($all_time_response_body, true);
// If the user has data for all time, return empty data instead of demo data
if (isset($all_time_result['success']) && $all_time_result['success'] && !empty($all_time_result['data'])) {
wp_send_json_success(array(
'company_data' => array(),
'is_demo_data' => false,
'total_count' => 0,
'time_period' => $time_period
));
return;
}
}
}
// If we get here, either we're already checking all time data or the user has never had data
$sample_data = abmsense_get_sample_score_data($time_period);
wp_send_json_success($sample_data);
return;
}
// Extract company data from API response
$scores_data = array();
foreach ($result['data'] as $item) {
if (!empty($item['company_name']) && isset($item['lead_score'])) {
// Ensure we have a valid company name and score
$company_name = sanitize_text_field($item['company_name']);
$score = (int) $item['lead_score'];
// Extract LinkedIn URL from the linkedin field
$linkedin_url = null;
if (!empty($item['linkedin'])) {
// Check if it's in markdown format [text](url)
if (preg_match('/\[(.*?)\]\((.*?)\)/', $item['linkedin'], $matches)) {
$linkedin_url = esc_url($matches[2]);
} else {
// If it's just a URL, use it directly
$linkedin_url = esc_url($item['linkedin']);
}
}
// Extract top pages from the journey pages field
$top_pages = array();
if (!empty($item['top_3_journey_pages'])) {
$pages = explode('|', $item['top_3_journey_pages']);
foreach ($pages as $page) {
if (!empty($page)) {
$top_pages[] = array(
'title' => sanitize_text_field($page)
);
}
}
}
// Build the company data array with all available fields
$company_data = array(
'company_name' => $company_name,
'score' => $score,
'last_updated' => !empty($item['last_visited_date']) ? sanitize_text_field($item['last_visited_date']) : gmdate('Y-m-d'),
'linkedin_url' => $linkedin_url,
'employees' => !empty($item['number_of_employees']) ? sanitize_text_field($item['number_of_employees']) : null,
'headquarters' => !empty($item['hq']) ? sanitize_text_field($item['hq']) : null,
'email' => !empty($item['company_email']) ? sanitize_email($item['company_email']) : null,
'phone' => !empty($item['company_phone']) ? sanitize_text_field($item['company_phone']) : null,
'industry' => !empty($item['industry']) ? sanitize_text_field($item['industry']) : null,
'domain' => !empty($item['web_domain']) ? sanitize_text_field($item['web_domain']) : null,
'revenue' => !empty($item['revenue']) ? sanitize_text_field($item['revenue']) : null,
'top_pages' => $top_pages,
'visit_duration' => !empty($item['visit_duration']) ? (int) $item['visit_duration'] : 0,
'number_of_visits' => !empty($item['number_of_visits']) ? (int) $item['number_of_visits'] : 0
);
$scores_data[] = $company_data;
}
}
// Count total unique companies
$total_companies_count = count($scores_data);
// Sort by score in descending order
usort($scores_data, function($a, $b) {
return $b['score'] - $a['score'];
});
// Add debug logging for final data
// abmsense_log('Final scores_data: ' . print_r($scores_data, true), 'debug');
wp_send_json_success(array(
'company_data' => $scores_data,
'is_demo_data' => false,
'total_count' => $total_companies_count,
'time_period' => $time_period
));
}
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') {
// Base company data
$companies = array(
'Acme Corporation',
'Globex Industries',
'Stark Enterprises',
'Wayne Industries',
'Umbrella Corporation',
'Cyberdyne Systems',
'Soylent Corporation',
'Initech Solutions',
'Hooli Technologies',
'Pied Piper Inc',
'Dunder Mifflin',
'Los Pollos Hermanos',
'Vandelay Industries',
'Bluth Company',
'Sterling Cooper'
);
// Base scores array (15 companies)
$scores = array(
95, // Hot
92,
88,
85,
82, // Hot leads (5 total)
75,
70,
65,
62,
58, // Medium leads (5 total)
45,
38,
32,
25,
20 // Low leads (5 total)
);
// Sample company details
$company_details = array(
'Acme Corporation' => array(
'employees' => 2500,
'headquarters' => 'New York, NY',
'email' => 'contact@acmecorp.com',
'phone' => '+1 (212) 555-0123',
'industry' => 'Technology',
'domain' => 'acmecorp.com',
'revenue' => 250000000
),
'Globex Industries' => array(
'employees' => 1800,
'headquarters' => 'San Francisco, CA',
'email' => 'info@globex.com',
'phone' => '+1 (415) 555-0456',
'industry' => 'Manufacturing',
'domain' => 'globex.com',
'revenue' => 180000000
),
'Stark Enterprises' => array(
'employees' => 5000,
'headquarters' => 'Los Angeles, CA',
'email' => 'contact@stark.com',
'phone' => '+1 (310) 555-0789',
'industry' => 'Aerospace',
'domain' => 'stark.com',
'revenue' => 750000000
),
'Wayne Industries' => array(
'employees' => 3200,
'headquarters' => 'Gotham City, NJ',
'email' => 'info@wayne.com',
'phone' => '+1 (201) 555-0123',
'industry' => 'Defense',
'domain' => 'wayne.com',
'revenue' => 420000000
),
'Umbrella Corporation' => array(
'employees' => 15000,
'headquarters' => 'Raccoon City, IL',
'email' => 'contact@umbrella.com',
'phone' => '+1 (312) 555-0456',
'industry' => 'Pharmaceutical',
'domain' => 'umbrella.com',
'revenue' => 2500000000
),
'Cyberdyne Systems' => array(
'employees' => 2800,
'headquarters' => 'San Diego, CA',
'email' => 'info@cyberdyne.com',
'phone' => '+1 (619) 555-0789',
'industry' => 'Robotics',
'domain' => 'cyberdyne.com',
'revenue' => 320000000
),
'Soylent Corporation' => array(
'employees' => 1200,
'headquarters' => 'Chicago, IL',
'email' => 'contact@soylent.com',
'phone' => '+1 (773) 555-0123',
'industry' => 'Food Technology',
'domain' => 'soylent.com',
'revenue' => 150000000
),
'Initech Solutions' => array(
'employees' => 800,
'headquarters' => 'Austin, TX',
'email' => 'info@initech.com',
'phone' => '+1 (512) 555-0456',
'industry' => 'Software',
'domain' => 'initech.com',
'revenue' => 95000000
),
'Hooli Technologies' => array(
'employees' => 4200,
'headquarters' => 'Palo Alto, CA',
'email' => 'contact@hooli.com',
'phone' => '+1 (650) 555-0789',
'industry' => 'Internet',
'domain' => 'hooli.com',
'revenue' => 580000000
),
'Pied Piper Inc' => array(
'employees' => 150,
'headquarters' => 'San Francisco, CA',
'email' => 'info@piedpiper.com',
'phone' => '+1 (415) 555-0123',
'industry' => 'Software',
'domain' => 'piedpiper.com',
'revenue' => 25000000
),
'Dunder Mifflin' => array(
'employees' => 450,
'headquarters' => 'Scranton, PA',
'email' => 'contact@dundermifflin.com',
'phone' => '+1 (570) 555-0456',
'industry' => 'Paper Products',
'domain' => 'dundermifflin.com',
'revenue' => 35000000
),
'Los Pollos Hermanos' => array(
'employees' => 1200,
'headquarters' => 'Albuquerque, NM',
'email' => 'info@lospollos.com',
'phone' => '+1 (505) 555-0789',
'industry' => 'Restaurant',
'domain' => 'lospollos.com',
'revenue' => 85000000
),
'Vandelay Industries' => array(
'employees' => 300,
'headquarters' => 'New York, NY',
'email' => 'contact@vandelay.com',
'phone' => '+1 (212) 555-0123',
'industry' => 'Import/Export',
'domain' => 'vandelay.com',
'revenue' => 15000000
),
'Bluth Company' => array(
'employees' => 200,
'headquarters' => 'Newport Beach, CA',
'email' => 'info@bluth.com',
'phone' => '+1 (949) 555-0456',
'industry' => 'Real Estate',
'domain' => 'bluth.com',
'revenue' => 12000000
),
'Sterling Cooper' => array(
'employees' => 600,
'headquarters' => 'New York, NY',
'email' => 'contact@sterlingcooper.com',
'phone' => '+1 (212) 555-0789',
'industry' => 'Advertising',
'domain' => 'sterlingcooper.com',
'revenue' => 45000000
)
);
// Adjust the number of companies based on time period
$company_count = 15; // Default for All_time
$total_companies = 15;
$hot_companies = 5; // Default hot companies for All_time
switch ($time_period) {
case 'Today':
$company_count = 5; // Show 5 companies for today
$total_companies = 5;
$hot_companies = 1; // Only 1 hot company for today
// Create scores for today's 5 companies
$new_scores = array(
85, // One hot company (>80)
75, // Warm
65, // Warm
45, // Cold
35 // Cold
);
break;
case 'Last_7_days':
$company_count = 8; // Show 8 companies for last 7 days
$total_companies = 8;
$hot_companies = 3; // 3 hot companies for last 7 days
// Create scores for 7-day companies
$new_scores = array(
92, // Hot
88, // Hot
82, // Hot
75, // Warm
70, // Warm
65, // Warm
45, // Cold
35 // Cold
);
break;
case 'Last_30_days':
$company_count = 10; // Show 10 companies for last 30 days
$total_companies = 10;
$hot_companies = 4; // 4 hot companies for last 30 days
// Create scores for 30-day companies
$new_scores = array(
95, // Hot
90, // Hot
85, // Hot
82, // Hot
75, // Warm
70, // Warm
65, // Warm
45, // Cold
40, // Cold
35 // Cold
);
break;
case 'All_time':
$company_count = 15; // Show all 15 companies
$total_companies = 15;
$hot_companies = 5; // 5 hot companies for all time
break;
}
// Slice the arrays to adjust for time period
$companies = array_slice($companies, 0, $company_count);
// If we're in All_time, create the scores array (other periods already have their scores)
if ($time_period === 'All_time') {
// Create a new scores array with the right number of hot companies
$new_scores = array();
// First add hot scores (80-100)
for ($i = 0; $i < $hot_companies; $i++) {
$new_scores[] = 95 - ($i * 3); // 95, 92, 89, 86, 83
}
// Then add medium scores (50-79)
$medium_count = min(5, $company_count - $hot_companies);
for ($i = 0; $i < $medium_count; $i++) {
$new_scores[] = 75 - ($i * 5); // 75, 70, 65, 60, 55
}
// Then add cold scores (1-49)
$cold_count = $company_count - $hot_companies - $medium_count;
for ($i = 0; $i < $cold_count; $i++) {
$new_scores[] = 45 - ($i * 5); // 45, 40, 35, 30, 25
}
}
$scores_data = array();
foreach ($companies as $index => $company) {
// Get the score for this company
$base_score = $new_scores[$index];
$score = max(min($base_score + wp_rand(-1, 1), 100), 1); // Keep between 1-100, smaller random variation
// Generate a random date within the appropriate time period
$days_ago = 0;
switch ($time_period) {
case 'Today':
$days_ago = 0; // All from today
break;
case 'Last_7_days':
$days_ago = wp_rand(0, 7); // Last 7 days
break;
case 'Last_30_days':
$days_ago = wp_rand(0, 30); // Last 30 days
break;
case 'All_time':
$days_ago = wp_rand(0, 90); // Up to 90 days ago
break;
}
// Get company details
$details = $company_details[$company] ?? array();
$scores_data[] = array(
'company_name' => $company,
'score' => $score,
'last_updated' => gmdate('Y-m-d', strtotime('-' . $days_ago . ' days')),
'linkedin_url' => 'https://www.linkedin.com/company/abmsense/',
'employees' => $details['employees'] ?? null,
'headquarters' => $details['headquarters'] ?? null,
'email' => $details['email'] ?? null,
'phone' => $details['phone'] ?? null,
'industry' => $details['industry'] ?? null,
'domain' => $details['domain'] ?? null,
'revenue' => $details['revenue'] ?? null,
'top_pages' => array(
array(
'title' => 'Products'
),
array(
'title' => 'About Us'
),
array(
'title' => 'Contact'
)
)
);
}
return array(
'company_data' => $scores_data,
'is_demo_data' => true,
'total_count' => $total_companies,
'time_period' => $time_period
);
}