identify_bot( $ua ); if ( ! $bot ) { return; } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '/'; $entry = array( 'bot' => $bot, 'url' => $uri, 'time' => time(), ); $log = (array) get_option( self::OPTION_KEY, array() ); $log[] = $entry; if ( count( $log ) > self::MAX ) { $log = array_slice( $log, -self::MAX ); } // autoload = false — log data does not need to load on every request. update_option( self::OPTION_KEY, $log, false ); } private function identify_bot( string $ua ): string { foreach ( self::BOTS as $bot ) { if ( false !== stripos( $ua, $bot ) ) { return $bot; } } return ''; } /** * Return the most recent log entries, newest first. */ public static function get_recent( int $limit = 50 ): array { $log = (array) get_option( self::OPTION_KEY, array() ); return array_slice( array_reverse( $log ), 0, $limit ); } /** * Return a bot_name => visit_count summary, sorted by count descending. */ public static function get_summary(): array { $log = (array) get_option( self::OPTION_KEY, array() ); $counts = array(); foreach ( $log as $entry ) { $bot = $entry['bot'] ?? 'unknown'; $counts[ $bot ] = ( $counts[ $bot ] ?? 0 ) + 1; } arsort( $counts ); return $counts; } /** * Erase all log entries (used by uninstall and admin reset). */ public static function clear(): void { delete_option( self::OPTION_KEY ); } }