// ===== AJAX: registrar visita =====
add_action('wp_ajax_nopriv_sra_track', 'sra_track'); add_action('wp_ajax_sra_track', 'sra_track');
function sra_track(){
check_ajax_referer('sra_track_nonce', 'nonce');
global $wpdb;
$visits_table = $wpdb->prefix . 'sra_visits';
$ip_cache_table = $wpdb->prefix . 'sra_ip_cache';
// --- IP REAL (Cloudflare / Proxy / Directo)
$ip = '';
if ( !empty($_SERVER['HTTP_CF_CONNECTING_IP']) && filter_var($_SERVER['HTTP_CF_CONNECTING_IP'], FILTER_VALIDATE_IP) ) {
$ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
} elseif ( !empty($_SERVER['HTTP_X_FORWARDED_FOR']) ) {
$parts = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
$candidate = trim($parts[0]);
if (filter_var($candidate, FILTER_VALIDATE_IP)) $ip = $candidate;
} elseif ( !empty($_SERVER['REMOTE_ADDR']) && filter_var($_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP) ) {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ip_hash = hash('sha256', (string)$ip);
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';
$url = isset($_POST['url']) ? esc_url_raw($_POST['url']) : '';
// Intentar leer caché de IP
$country = $city = null; $lat = $lon = null;
$geo = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$ip_cache_table} WHERE ip_hash=%s", $ip_hash));
if ($geo){
$country = $geo->country; $city = $geo->city; $lat = $geo->lat; $lon = $geo->lon;
} else {
// --- Geolocalización con ipwho.is ---
if (!empty($ip)) {
$response = wp_remote_get( 'https://ipwho.is/' . rawurlencode($ip), array('timeout' => 3) );
if ( !is_wp_error($response) && wp_remote_retrieve_response_code($response) == 200 ) {
$body = json_decode( wp_remote_retrieve_body($response), true );
if ( isset($body['success']) && $body['success'] === true ) {
$country = sanitize_text_field( $body['country'] ?? '' );
$city = sanitize_text_field( $body['city'] ?? '' );
$lat = isset($body['latitude']) ? floatval($body['latitude']) : null;
$lon = isset($body['longitude']) ? floatval($body['longitude']) : null;
}
}
}
// Guardar caché (aunque venga vacío, para no repetir llamadas)
$wpdb->replace($ip_cache_table, array(
'ip_hash' => $ip_hash,
'country' => $country,
'city' => $city,
'lat' => $lat,
'lon' => $lon,
'last_lookup' => current_time('mysql', 1)
));
}
// UA parsing y guardado de visita
$ua_info = sra_parse_user_agent($user_agent);
$wpdb->insert($visits_table, array(
'ip_hash' => $ip_hash,
'country' => $country,
'city' => $city,
'lat' => $lat,
'lon' => $lon,
'url' => $url,
'user_agent' => $user_agent,
'browser' => $ua_info['browser'],
'browser_version' => $ua_info['browser_version'],
'device_type' => $ua_info['device_type'],
'device_name' => $ua_info['device_name'],
'created_at' => current_time('mysql', 1)
));
wp_send_json_success(array('ok'=>true));
}
EVVO Cafetera Express Intensa
Saltar al contenido
¡¡ Menudo Precio!!
🔍
EVVO Cafetera Express Intensa 88,90 € El precio original era: 88,90 €. 75,90 € El precio actual es: 75,90 €. Ahorras: 13,00 € (14.6%)
Potente y versátil: Disfruta de espressos llenos de sabor y aroma con la potente bomba de 20 bares y 1350W. Esta cafetera express te permite usar café molido e infusiones…