prefix . 'realbots_daily'; } /** Bot list download only after the admin opts in. */ function realbots_downloads_allowed() { $s = get_option('realbots_settings', array()); return !empty($s['downloads']); } /** Fetching key directories named by signed requests: separate opt-in (off by default). */ function realbots_agent_keys_allowed() { $s = get_option('realbots_settings', array()); return !empty($s['agent_keys']); } /** User-agent for our own downloads: plugin name and version only (no site address, no WordPress version). */ function realbots_http_user_agent() { return 'BotLedger/' . REALBOTS_VERSION; } function realbots_activate() { realbots_install_table(); add_option('realbots_settings', array('block_fakes' => 0, 'keep_days' => 90, 'downloads' => 0, 'agent_keys' => 0), '', 'no'); if (!wp_next_scheduled('realbots_daily')) { wp_schedule_event(time() + HOUR_IN_SECONDS, 'daily', 'realbots_daily'); } } /** Creates the stats table, or upgrades a 0.3.0 table (long 5-column primary key) in place. */ function realbots_install_table() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $t = realbots_table(); // phpcs:disable WordPress.DB.DirectDatabaseQuery -- one-time schema upgrade of our own table $exists = $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->esc_like($t))) === $t; if ($exists && !$wpdb->get_var($wpdb->prepare('SHOW COLUMNS FROM %i LIKE %s', $t, 'rowhash'))) { $wpdb->query($wpdb->prepare("ALTER TABLE %i ADD rowhash char(32) NOT NULL DEFAULT '' AFTER day", $t)); $wpdb->query($wpdb->prepare("UPDATE %i SET rowhash = MD5(CONCAT_WS('|', kind, botkey, verdict, path))", $t)); $wpdb->query($wpdb->prepare('ALTER TABLE %i DROP PRIMARY KEY, ADD PRIMARY KEY (day, rowhash)', $t)); } // phpcs:enable WordPress.DB.DirectDatabaseQuery $charset = $wpdb->get_charset_collate(); // One row per day × kind × key × verdict × path; hits incremented in place. // kind: 'bot' (key = bot slug), 'ref' (key = AI assistant name) or 'agent'. // rowhash = md5(kind|botkey|verdict|path): keeps the primary key short // (3 + 32 chars) so it fits the 767-byte index limit of older MySQL/InnoDB. dbDelta("CREATE TABLE $t ( day date NOT NULL, rowhash char(32) NOT NULL, kind varchar(8) NOT NULL, botkey varchar(64) NOT NULL, verdict varchar(16) NOT NULL, path varchar(191) NOT NULL, hits int unsigned NOT NULL DEFAULT 0, PRIMARY KEY (day,rowhash), KEY kind_day (kind,day) ) $charset;"); update_option('realbots_db_version', '2', false); } // Files replaced without re-activation (manual update from 0.3.0): upgrade the table once. add_action('plugins_loaded', function () { if (get_option('realbots_db_version') !== '2' && get_option('realbots_settings') !== false) { realbots_install_table(); } }); function realbots_deactivate() { wp_clear_scheduled_hook('realbots_daily'); } add_action('realbots_daily', function () { if (realbots_downloads_allowed()) { realbots_refresh_feed(); } realbots_prune(); }); function realbots_prune() { global $wpdb; $s = get_option('realbots_settings', array()); $days = max(7, (int) ($s['keep_days'] ?? 90)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- own table, maintenance delete $wpdb->query($wpdb->prepare('DELETE FROM %i WHERE day < %s', realbots_table(), gmdate('Y-m-d', time() - $days * DAY_IN_SECONDS))); } // Track as early as possible on front-end requests. add_action('init', 'realbots_track', 0);