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
)
));
// Enqueue Alerts & Reports assets if on that page
// if (strpos($hook, ABMSENSE_PREFIX . 'alerts_reports') !== false) {
// wp_enqueue_style(ABMSENSE_PREFIX . 'alerts-reports-style', plugin_dir_url(__FILE__) . 'css/alerts-reports.css', array(), ABMSENSE_VERSION);
// wp_enqueue_script(ABMSENSE_PREFIX . 'alerts-reports-script', plugin_dir_url(__FILE__) . 'js/alerts-reports.js', array('jquery'), ABMSENSE_VERSION, true);
// // Localize script with text strings
// wp_localize_script(ABMSENSE_PREFIX . 'alerts-reports-script', 'abmsenseAlertsData', array(
// 'noRecipientsText' => __('No recipients added yet.', 'abmsense'),
// 'selectLeadTypeText' => __('Please select at least one lead type when alerts are enabled.', 'abmsense'),
// 'selectChannelText' => __('Please select at least one notification channel when alerts are enabled.', 'abmsense'),
// 'selectRecipientText' => __('Please select at least one recipient for email alerts.', 'abmsense'),
// 'invalidEmailText' => __('Please enter valid email addresses.', 'abmsense')
// ));
// }
}
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 ONLY if the secret parameter is present
if (isset($_GET['showrealtime']) && $_GET['showrealtime'] == '1') {
add_submenu_page(
ABMSENSE_PREFIX . 'dashboard',
'ABMsense Data',
'Real-time Data',
'manage_options',
ABMSENSE_PREFIX . 'table',
'abmsense_display_admin_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'] ?? '')));
?>
Beta
-
🔥
-
-
0-00
1
80-100 🔥 -
50-79 -
0-49 -
Company Details
Lead Temperature
-
LinkedIn Profile
Employees
-
Headquarters
-
Industry
-
Annual Revenue
-
Domain
-
Email
-
Phone
-
Top Visited Pages
$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: ' . esc_html($response->get_error_message()));
}
$response_body = wp_remote_retrieve_body($response);
$result = json_decode($response_body, true);
if (!isset($result['success']) || !$result['success']) {
wp_send_json_error(isset($result['error']) ? esc_html($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'];
});
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.co (general contact)',
'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 - no randomization
$score = $new_scores[$index];
// 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
);
}
/** AJAX handler for exporting CSV data */
function abmsense_export_csv() {
check_ajax_referer('abmsense_export_nonce', 'abmsense_export_nonce');
if (!current_user_can('manage_options')) {
wp_die('Insufficient permissions');
}
$temperature = sanitize_text_field($_POST['temperature'] ?? 'all');
$period = sanitize_text_field($_POST['period'] ?? 'all');
$customer_name = sanitize_text_field($_POST['customer_name'] ?? '');
if (empty($customer_name)) {
wp_die('Missing customer name');
}
// Load API config
$config = include('db_config.php');
$api_url = $config['export_data_url'];
// Prepare data for API request
$data = array(
'customer_name' => $customer_name,
'report_email' => get_option(ABMSENSE_PREFIX . 'report_email', ''),
'export_period' => $period,
'export_all' => true // Request all data, not just what's displayed
);
// Encrypt data for the API request
$encrypted_data = abmsense_encrypt_data_for_api($data);
if (!$encrypted_data) {
wp_die('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_die('Export API request failed: ' . esc_html($response->get_error_message()));
}
$response_body = wp_remote_retrieve_body($response);
$result = json_decode($response_body, true);
if (!isset($result['success']) || !$result['success']) {
wp_die(isset($result['error']) ? esc_html($result['error']) : 'Unknown error occurred');
}
// Filter data based on temperature
$filtered_data = array();
foreach ($result['data'] as $item) {
$score = (int) $item['lead_score'];
// Apply temperature filter
$include_item = true;
switch ($temperature) {
case 'hot':
$include_item = $score >= 80;
break;
case 'warm':
$include_item = $score >= 50 && $score < 80;
break;
case 'cold':
$include_item = $score < 50;
break;
}
if ($include_item) {
$filtered_data[] = $item;
}
}
// Generate CSV
abmsense_generate_csv_output($filtered_data, $temperature, $period);
}
add_action('wp_ajax_abmsense_export_csv', 'abmsense_export_csv');
/** AJAX handler for exporting demo CSV data */
function abmsense_export_demo_csv() {
check_ajax_referer('abmsense_export_nonce', 'abmsense_export_nonce');
if (!current_user_can('manage_options')) {
wp_die('Insufficient permissions');
}
$temperature = sanitize_text_field($_POST['temperature'] ?? 'all');
$period = sanitize_text_field($_POST['period'] ?? 'All_time');
// Get sample data for the selected period
$sample_data = abmsense_get_sample_score_data($period);
$filtered_data = array();
// Filter demo data based on temperature
foreach ($sample_data['company_data'] as $item) {
$score = (int) $item['score'];
// Apply temperature filter
$include_item = true;
switch ($temperature) {
case 'hot':
$include_item = $score >= 80;
break;
case 'warm':
$include_item = $score >= 50 && $score < 80;
break;
case 'cold':
$include_item = $score < 50;
break;
}
if ($include_item) {
// Format demo data to match export structure
$filtered_data[] = array(
'company_name' => $item['company_name'],
'lead_score' => $item['score'],
'visit_duration' => wp_rand(60, 3600), // Random duration between 1-60 minutes
'number_of_visits' => wp_rand(1, 20),
'number_of_employees' => $item['employees'],
'hq' => $item['headquarters'],
'company_email' => $item['email'],
'company_phone' => $item['phone'],
'web_domain' => $item['domain'],
'industry' => $item['industry'],
'revenue' => $item['revenue'],
'linkedin' => $item['linkedin_url'],
'top_3_journey_pages' => implode('|', array_map(function($page) { return $page['title']; }, $item['top_pages'])),
'last_visited_date' => $item['last_updated']
);
}
}
// Generate CSV
abmsense_generate_csv_output($filtered_data, $temperature, $period);
}
add_action('wp_ajax_abmsense_export_demo_csv', 'abmsense_export_demo_csv');
/** Generate CSV output for download */
function abmsense_generate_csv_output($data, $temperature, $period) {
if (empty($data)) {
wp_die('No data available for export.');
}
// Set headers for CSV download
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=abmsense_export_' .
($temperature === 'all' ? 'All' : ucfirst($temperature)) . '_' .
($period === 'All_time' ? 'Last_90_Days' :
($period === 'Last_30_days' ? 'Last_30_Days' :
($period === 'Last_7_days' ? 'Last_7_Days' : 'Today'))) . '_' .
gmdate('Y-m-d') . '.csv');
// Initialize WP_Filesystem
global $wp_filesystem;
if (empty($wp_filesystem)) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
WP_Filesystem();
}
// Create output stream
$output = fopen('php://output', 'w');
// Add UTF-8 BOM for Excel compatibility
fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF));
// Add headers
fputcsv($output, array(
'Company name', 'Lead Score', 'Visit Duration', 'Number of Visits', 'Number of employees',
'HQ', 'Company Email', 'Company Phone', 'Web Domain', 'Industry',
'Revenue', 'LinkedIn', 'Top 3 Journey pages', 'Last visited date'
));
// Add data rows
foreach ($data as $row) {
fputcsv($output, array(
$row['company_name'],
$row['lead_score'],
$row['visit_duration'],
$row['number_of_visits'],
$row['number_of_employees'],
$row['hq'],
$row['company_email'],
$row['company_phone'],
$row['web_domain'],
$row['industry'],
$row['revenue'],
$row['linkedin'],
$row['top_3_journey_pages'],
$row['last_visited_date']
));
}
exit;
}