<?php
if (!defined('ABSPATH')) {
    exit;
}

/**
 * Helper function to retrieve the client's IP address.
 * It checks various server variables (HTTP_CLIENT_IP, HTTP_X_FORWARDED_FOR)
 * to handle proxies and returns the most likely IP address.
 *
 * @return string The client IP address.
 */
function admilogu_get_ip()
{
    $ip = '';
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
        $ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_CLIENT_IP']));
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ip = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_FORWARDED_FOR']));
    } else {
        $ip = isset($_SERVER['REMOTE_ADDR']) ? sanitize_text_field(wp_unslash($_SERVER['REMOTE_ADDR'])) : 'UNKNOWN';
    }

    // Handle comma-separated IP lists from proxies
    if (strpos($ip, ',') !== false) {
        $ip_parts = explode(',', $ip);
        $ip = trim($ip_parts[0]);
    }

    return $ip;
}
