22/**
33 * Plugin Name: CBX AI Bot Tracker
44 * Description: Tracks AI bot visits (OpenAI, Anthropic, Perplexity, etc.) and shows visit logs in admin dashboard.
5- * Version: 1.0.0
5+ * Plugin URI: https://github.com/codeboxrcodehub/cbxaibottracker
6+ * Version: 1.0.1
67 * Author: Codeboxr
8+ * Author URI: http://codeboxr.com
79 * Text Domain: cbxaibottracker
810 * Domain Path: /languages
11+ * License: GPL-2.0+
12+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
913 */
1014
1115if ( ! defined ( 'ABSPATH ' ) ) exit ;
@@ -35,11 +39,10 @@ public function __construct() {
3539 add_action ('init ' , [$ this , 'track_bot_visit ' ]);
3640 add_action ('admin_menu ' , [$ this , 'register_admin_menu ' ]);
3741 add_action ('admin_init ' , [$ this , 'handle_delete ' ]);
42+ add_action ('admin_init ' , [$ this , 'handle_delete_all ' ]); // NEW
3843 }
3944
40- /* ----------------------------
41- * Load Translation
42- * ---------------------------- */
45+ /* ---------------------------- */
4346 public function load_textdomain () {
4447 load_plugin_textdomain (
4548 'cbxaibottracker ' ,
@@ -48,9 +51,7 @@ public function load_textdomain() {
4851 );
4952 }
5053
51- /* ----------------------------
52- * Create DB Table
53- * ---------------------------- */
54+ /* ---------------------------- */
5455 public function create_table () {
5556 global $ wpdb ;
5657 $ charset_collate = $ wpdb ->get_charset_collate ();
@@ -70,13 +71,10 @@ public function create_table() {
7071 dbDelta ($ sql );
7172 }
7273
73- /* ----------------------------
74- * Track AI Bot Visit
75- * ---------------------------- */
74+ /* ---------------------------- */
7675 public function track_bot_visit () {
7776
7877 if ( is_admin () ) return ;
79-
8078 if ( empty ($ _SERVER ['HTTP_USER_AGENT ' ]) ) return ;
8179
8280 $ user_agent = $ _SERVER ['HTTP_USER_AGENT ' ];
@@ -130,9 +128,7 @@ public function track_bot_visit() {
130128 }
131129 }
132130
133- /* ----------------------------
134- * Admin Menu
135- * ---------------------------- */
131+ /* ---------------------------- */
136132 public function register_admin_menu () {
137133 add_menu_page (
138134 __ ('AI Bot Tracker ' , 'cbxaibottracker ' ),
@@ -145,9 +141,7 @@ public function register_admin_menu() {
145141 );
146142 }
147143
148- /* ----------------------------
149- * Handle Delete
150- * ---------------------------- */
144+ /* ---------------------------- */
151145 public function handle_delete () {
152146
153147 if ( ! isset ($ _GET ['cbx_delete ' ]) ) return ;
@@ -163,9 +157,22 @@ public function handle_delete() {
163157 exit ;
164158 }
165159
166- /* ----------------------------
167- * Admin Page
168- * ---------------------------- */
160+ /* ---------------------------- */
161+ public function handle_delete_all () {
162+
163+ if ( ! isset ($ _POST ['cbx_delete_all ' ]) ) return ;
164+ if ( ! current_user_can ('manage_options ' ) ) return ;
165+
166+ check_admin_referer ('cbx_delete_all_bots ' );
167+
168+ global $ wpdb ;
169+ $ wpdb ->query ("TRUNCATE TABLE {$ this ->table_name }" );
170+
171+ wp_redirect (admin_url ('admin.php?page=cbxaibottracker&deleted_all=1 ' ));
172+ exit ;
173+ }
174+
175+ /* ---------------------------- */
169176 public function admin_page () {
170177
171178 global $ wpdb ;
@@ -174,15 +181,48 @@ public function admin_page() {
174181 "SELECT * FROM {$ this ->table_name } ORDER BY last_visit DESC LIMIT 200 "
175182 );
176183
184+ $ total_entries = (int ) $ wpdb ->get_var (
185+ "SELECT COUNT(*) FROM {$ this ->table_name }"
186+ );
187+
188+ $ total_visits = (int ) $ wpdb ->get_var (
189+ "SELECT SUM(visit_count) FROM {$ this ->table_name }"
190+ );
191+
177192 echo '<div class="wrap"> ' ;
178193 echo '<h1> ' . esc_html__ ('AI Bot Visits ' , 'cbxaibottracker ' ) . '</h1> ' ;
179194
180195 if ( isset ($ _GET ['deleted ' ]) ) {
181- echo '<div class="notice notice-success is-dismissible"> ' ;
182- echo '<p> ' . esc_html__ ('Entry deleted successfully. ' , 'cbxaibottracker ' ) . '</p> ' ;
183- echo '</div> ' ;
196+ echo '<div class="notice notice-success is-dismissible"><p> ' ;
197+ echo esc_html__ ('Entry deleted successfully. ' , 'cbxaibottracker ' );
198+ echo '</p></div> ' ;
199+ }
200+
201+ if ( isset ($ _GET ['deleted_all ' ]) ) {
202+ echo '<div class="notice notice-success is-dismissible"><p> ' ;
203+ echo esc_html__ ('All entries deleted successfully. ' , 'cbxaibottracker ' );
204+ echo '</p></div> ' ;
184205 }
185206
207+ // Top Controls Row
208+ echo '<div style="display:flex; justify-content:space-between; align-items:center; margin-bottom:15px;"> ' ;
209+
210+ // Left - Delete All
211+ echo '<form method="post"> ' ;
212+ wp_nonce_field ('cbx_delete_all_bots ' );
213+ echo '<input type="hidden" name="cbx_delete_all" value="1" /> ' ;
214+ echo '<input type="submit" class="button button-primary" value=" ' . esc_attr__ ('Delete All Entries ' , 'cbxaibottracker ' ) . '" onclick="return confirm( \'Are you sure you want to delete all entries? \');" /> ' ;
215+ echo '</form> ' ;
216+
217+ // Right - Totals
218+ echo '<div style="font-weight:600;"> ' ;
219+ echo esc_html__ ('Total Entries: ' , 'cbxaibottracker ' ) . ' ' . esc_html ($ total_entries );
220+ echo ' | ' ;
221+ echo esc_html__ ('Total Visits: ' , 'cbxaibottracker ' ) . ' ' . esc_html ($ total_visits );
222+ echo '</div> ' ;
223+
224+ echo '</div> ' ;
225+
186226 echo '<table class="widefat striped"> ' ;
187227 echo '<thead><tr> ' ;
188228 echo '<th> ' . esc_html__ ('Bot Name ' , 'cbxaibottracker ' ) . '</th> ' ;
0 commit comments