prefix}adv_click_visits (non input utente). * I placeholder di $wpdb->prepare() non possono sostituire i nomi tabella. */ if (!defined('ABSPATH')) { exit; } function adv_click_ingest_visits_table(): string { global $wpdb; return $wpdb->prefix . 'adv_click_visits'; } /** * @return array */ function adv_click_ingest_table_status(bool $repair = false): array { global $wpdb; $table = adv_click_ingest_visits_table(); $status = [ $table => [ 'exists' => $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) === $table, 'description' => __('Pagine visitate (funnel per IP / chatbot).', 'adv-click-lead-ingest'), 'rows' => 0, ], ]; if ($repair || !$status[$table]['exists']) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); dbDelta("CREATE TABLE {$table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, guid varchar(64) NOT NULL DEFAULT '', ip varchar(64) NOT NULL DEFAULT '', url_pagina text NOT NULL, sorgente varchar(100) NOT NULL DEFAULT '', created_at datetime NOT NULL, PRIMARY KEY (id), KEY ip_created (ip, created_at), KEY guid_created (guid, created_at) ) {$charset};"); $status[$table]['exists'] = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) === $table; } if ($status[$table]['exists']) { $status[$table]['rows'] = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}"); } return $status; } function adv_click_ingest_record_visit_row(string $guid, ?string $ip, string $url, ?string $sorgente): void { global $wpdb; $table = adv_click_ingest_visits_table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) !== $table) { adv_click_ingest_table_status(true); } $wpdb->insert( $table, [ 'guid' => substr($guid, 0, 64), 'ip' => $ip !== null ? substr($ip, 0, 64) : '', 'url_pagina' => $url, 'sorgente' => $sorgente !== null ? substr($sorgente, 0, 100) : '', 'created_at' => current_time('mysql'), ], ['%s', '%s', '%s', '%s', '%s'], ); if (wp_rand(1, 40) === 1) { $wpdb->query("DELETE FROM {$table} WHERE created_at < (NOW() - INTERVAL 30 DAY)"); } } /** * @return list */ function adv_click_ingest_visits_for_ip(string $ip): array { global $wpdb; $table = adv_click_ingest_visits_table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) !== $table) { return []; } $rows = $wpdb->get_results( $wpdb->prepare( "SELECT url_pagina, sorgente, created_at FROM {$table} WHERE ip = %s AND created_at > (NOW() - INTERVAL 30 DAY) ORDER BY created_at ASC LIMIT 200", $ip, ), ARRAY_A, ); return is_array($rows) ? $rows : []; } function adv_click_ingest_first_visit_ts(?string $ip): ?int { if ($ip === null || $ip === '') { return null; } global $wpdb; $table = adv_click_ingest_visits_table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) !== $table) { return null; } $raw = $wpdb->get_var( $wpdb->prepare( "SELECT created_at FROM {$table} WHERE ip = %s ORDER BY created_at ASC LIMIT 1", $ip, ), ); return is_string($raw) && $raw !== '' ? (int) strtotime($raw) : null; } /** * @return array{total: int, unique_ips: int, today: int, visits_7d: int, ips_7d: int} */ function adv_click_ingest_visit_stats(): array { global $wpdb; $empty = ['total' => 0, 'unique_ips' => 0, 'today' => 0, 'visits_7d' => 0, 'ips_7d' => 0]; $table = adv_click_ingest_visits_table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) !== $table) { return $empty; } $today = current_time('Y-m-d'); return [ 'total' => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}"), 'unique_ips' => (int) $wpdb->get_var("SELECT COUNT(DISTINCT ip) FROM {$table} WHERE ip <> ''"), 'today' => (int) $wpdb->get_var($wpdb->prepare("SELECT COUNT(*) FROM {$table} WHERE DATE(created_at) = %s", $today)), 'visits_7d' => (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table} WHERE created_at > (NOW() - INTERVAL 7 DAY)"), 'ips_7d' => (int) $wpdb->get_var("SELECT COUNT(DISTINCT ip) FROM {$table} WHERE created_at > (NOW() - INTERVAL 7 DAY) AND ip <> ''"), ]; } function adv_click_ingest_clear_visits(): int { global $wpdb; $table = adv_click_ingest_visits_table(); if ($wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $table)) !== $table) { return 0; } $count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}"); $wpdb->query("TRUNCATE TABLE {$table}"); return $count; }