'sanitize_text_field', ] ); // Register report_email setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'report_email', [ 'sanitize_callback' => 'abmsense_sanitize_email', ] ); // Register report_frequency setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'report_frequency', [ 'sanitize_callback' => 'abmsense_sanitize_frequency', ] ); // Register display_consent_message setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'display_consent_message', [ 'sanitize_callback' => 'abmsense_sanitize_checkbox', ] ); // Register show_table_menu setting register_setting( ABMSENSE_PREFIX . 'options', ABMSENSE_PREFIX . 'show_table_menu', [ 'sanitize_callback' => 'abmsense_sanitize_checkbox', ] ); } // Sanitize email addresses function abmsense_sanitize_email($input) { // For multiple emails $emails = explode(',', $input); $sanitized_emails = array_map('sanitize_email', $emails); return implode(',', array_filter($sanitized_emails)); } // Sanitize report frequency function abmsense_sanitize_frequency($input) { $allowed_values = ['daily', 'weekly', 'monthly']; return in_array($input, $allowed_values) ? $input : 'weekly'; } // Sanitize checkbox values function abmsense_sanitize_checkbox($input) { return (isset($input) && true == $input) ? '1' : '0'; } // Add this function to utility.php function abmsense_get_api_request_args($body) { // Try to locate a valid CA certificate bundle $ca_bundle_paths = array( plugin_dir_path(__FILE__) . 'cacert.pem', // Plugin's own CA bundle ABSPATH . WPINC . '/certificates/ca-bundle.crt', // WordPress bundle '/etc/ssl/certs/ca-certificates.crt', // Debian/Ubuntu '/etc/pki/tls/certs/ca-bundle.crt', // RHEL/CentOS '/etc/ssl/ca-bundle.pem', // OpenSUSE '/usr/local/share/certs/ca-root-nss.crt' // FreeBSD ); $ca_bundle = null; foreach ($ca_bundle_paths as $path) { if (file_exists($path)) { $ca_bundle = $path; break; } } return array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => $body, 'timeout' => 30, 'sslverify' => true, 'sslcertificates' => $ca_bundle, // Use the best CA bundle available ); } // Update the save_data function in settings.php function abmsense_save_data($customer_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed) { $config = include('db_config.php'); $api_url = $config['upsert_abmsense_our_customers_details']; $public_key = $config['public_key']; $data = [ 'customer_name' => $customer_name, 'report_email' => $report_email, 'report_frequency' => $report_frequency, 'is_consent' => $is_consent, 'date_registered' => $date_registered, 'version_installed' => $version_installed, 'sku' => 'ABM-1000-ID-MONTHLY-FREE-TIER' ]; $encrypted_data = abmsense_encrypt_data($data, $public_key); if (!$encrypted_data) { abmsense_log("save_data Error: Encryption failed", 'error'); return false; } $json_data = wp_json_encode($encrypted_data); if (false === $json_data) { abmsense_log("Failed to encode data to JSON", 'error'); return false; } $args = abmsense_get_api_request_args($json_data); $response = abmsense_safe_api_request($api_url, $args); if (is_wp_error($response)) { abmsense_log("save_data Error: API request failed - " . $response->get_error_message(), 'error'); return false; } $response_code = wp_remote_retrieve_response_code($response); $response_body = wp_remote_retrieve_body($response); $result = json_decode($response_body, true); if ($response_code !== 200 || !isset($result['success']) || $result['success'] !== true) { $error_message = isset($result['error']) ? $result['error'] : 'Unknown error'; abmsense_log("save_data Error: API response indicates failure - " . $error_message, 'error'); return false; } // Update local WordPress options update_option(ABMSENSE_PREFIX . 'customer_name', $customer_name); update_option(ABMSENSE_PREFIX . 'report_email', $report_email); update_option(ABMSENSE_PREFIX . 'report_frequency', $report_frequency); update_option(ABMSENSE_PREFIX . 'display_consent_message', $is_consent); update_option(ABMSENSE_PREFIX . 'date_registered', $date_registered); update_option(ABMSENSE_PREFIX . 'version_installed', $version_installed); // Clear the consent cache $cache_key = 'abmsense_consent_' . md5($customer_name); delete_transient($cache_key); return true; } // Validate email addresses function abmsense_validate_emails($emails) { foreach (explode(',', $emails) as $email) { if (!filter_var(trim($email), FILTER_VALIDATE_EMAIL)) { return false; } } return true; } // Get the plugin version from the main file function abmsense_get_version() { global $wp_filesystem; if (empty($wp_filesystem)) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } $file = plugin_dir_path(__FILE__) . 'abmsense.php'; $content = $wp_filesystem->get_contents($file); if ($content && preg_match('/Version:\s*([^\s]+)/', $content, $matches)) { return $matches[1]; } return 'Unknown'; } // Display the plugin wp-admin settings page function abmsense_settings_page() { $current_user = wp_get_current_user(); $admin_email = $current_user->user_email; $report_email = get_option(ABMSENSE_PREFIX . 'report_email', $admin_email); $report_frequency = get_option(ABMSENSE_PREFIX . 'report_frequency', 'weekly'); $customer_name = get_option(ABMSENSE_PREFIX . 'customer_name'); $is_consent = get_option(ABMSENSE_PREFIX . 'display_consent_message'); $version_installed = abmsense_get_version(); $date_registered = gmdate('Y-m-d'); if (isset($_POST['submit'])) { if (!isset($_POST[ABMSENSE_PREFIX . 'nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST[ABMSENSE_PREFIX . 'nonce'])), ABMSENSE_PREFIX . 'save_settings')) { echo "Nonce verification failed"; return; } $customer_name = isset($_POST['customer_name']) ? sanitize_text_field(wp_unslash($_POST['customer_name'])) : ''; $report_email = isset($_POST['report_email']) ? sanitize_text_field(wp_unslash($_POST['report_email'])) : ''; $report_frequency = isset($_POST['report_frequency']) ? sanitize_text_field(wp_unslash($_POST['report_frequency'])) : ''; $is_consent = isset($_POST[ABMSENSE_PREFIX . 'display_consent_message']) ? 1 : 0; if (!$customer_name || !$report_email || !$report_frequency) { echo "Please fill in all fields"; } elseif (!abmsense_validate_emails($report_email)) { echo "Please enter valid email addresses"; } else { abmsense_save_data($customer_name, $report_email, $report_frequency, $is_consent, $date_registered, $version_installed); $frequency_messages = [ 'daily' => "The visitors identification report will be sent daily at 4am London time to $report_email.", 'weekly' => "The visitors identification report will be sent every Monday at 4am London time to $report_email.", 'monthly' => "The visitors identification report will be sent every first day of the month at 4am London time to $report_email." ]; echo "

" . wp_kses_post($frequency_messages[$report_frequency]) . "

"; } } ?>

ABMsense Settings (Beta)

>
Suggest a feature: info@abmsense.com
prefix}" . ABMSENSE_PREFIX . 'temp_data'; // Check if the table exists // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $table_exists = $wpdb->get_var( $wpdb->prepare( "SHOW TABLES LIKE %s", $table_name ) ); if (empty($table_exists)) { echo '

' . esc_html__('Required database table does not exist. Please deactivate and reactivate the plugin.', 'abmsense') . '

'; return; } // Fetch results from the custom table $esc_table_name = esc_sql($table_name); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $results = $wpdb->get_results( $wpdb->prepare( // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared "SELECT * FROM `$esc_table_name` LIMIT %d", 1000 ), ARRAY_A ); if ($wpdb->last_error) { abmsense_log('Database error: ' . $wpdb->last_error, 'error'); echo '

' . esc_html__('Error fetching data from the database. Please try again later.', 'abmsense') . '

'; return; } // Cache the results wp_cache_set($cache_key, $results, '', HOUR_IN_SECONDS); } // Ensure proper UTF-8 output if (!headers_sent()) { header('Content-Type: text/html; charset=utf-8'); } echo '

ABMsense Real-time Data (Beta)

'; if (empty($results)) { echo '

No data available.

'; return; } echo ''; echo ''; echo ''; foreach ($results as $row) { echo ''; foreach ($row as $key => $value) { if ($key === 'last_update') $value = gmdate('Y-m-d', strtotime($value)); echo ''; } echo ''; } echo '
IDPage TitleTime Spent (s)Page ViewsVisitor IPHeader IPVisitor CityVisitor CountryVisitor CompanyAccount IDLast Update
' . esc_html($value) . '
'; } // Enqueue the plugin admin styles function abmsense_enqueue_admin_styles($hook) { // Only enqueue on ABMsense admin pages if ('toplevel_page_' . ABMSENSE_PREFIX . 'settings' !== $hook && ABMSENSE_PREFIX . 'page_' . ABMSENSE_PREFIX . 'table' !== $hook) { return; } wp_enqueue_style(ABMSENSE_PREFIX . 'admin-style', plugin_dir_url(__FILE__) . 'css/abmsense-admin-style.css', array(), '1.0.0'); } // Toggle the display of the table menu function abmsense_toggle_table_menu($visible) { update_option(ABMSENSE_PREFIX . 'show_table_menu', $visible); } // Display the "On Demand" page function abmsense_on_demand_page() { if (!current_user_can('manage_options')) { return; } ?>

Export Visitors Data

Choose the time range for your report:

$customer_name, 'report_email' => $report_email, 'export_period' => $export_period ]; $encrypted_data = abmsense_encrypt_data($data, $public_key); if (!$encrypted_data) { wp_send_json_error('Encryption failed'); } $response = wp_remote_post($api_url, array( 'method' => 'POST', 'headers' => array('Content-Type' => 'application/json'), 'body' => wp_json_encode($encrypted_data), // Using wp_json_encode 'timeout' => 60, 'sslverify' => true )); if (is_wp_error($response)) { wp_send_json_error('Settings 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'); } wp_send_json_success($result['data']); } add_action('wp_ajax_abmsense_export_data', 'abmsense_handle_export'); add_action('admin_enqueue_scripts', 'abmsense_enqueue_admin_styles'); add_action('admin_init', 'abmsense_register_settings'); abmsense_toggle_table_menu(true); ?>