11<?php
22/**
33 * Plugin Name: Enterprise Audit Logger
4- * Description: High-performance activity tracker using custom SQL tables for compliance and security auditing.
4+ * Description: High-performance activity tracker using custom SQL for compliance and security auditing.
55 * Version: 1.1.0
66 * Author: Vamsi Bodapati
77 */
@@ -16,26 +16,19 @@ public function __construct() {
1616 global $ wpdb ;
1717 $ this ->table_name = $ wpdb ->prefix . 'rt_audit_logs ' ;
1818
19- // 1. Hook into plugin activation to create/update the DB Table
2019 register_activation_hook ( __FILE__ , array ( $ this , 'create_custom_table ' ) );
2120
22- // 2. Event Hooks: Logins and Content Updates
21+ // Hooks for tracking
2322 add_action ( 'wp_login ' , array ( $ this , 'log_user_login ' ), 10 , 2 );
2423 add_action ( 'save_post ' , array ( $ this , 'log_post_updates ' ), 10 , 3 );
2524
26- // 3. UI Hooks: Dashboard Widget and Sidebar Menu
25+ // UI Hooks
2726 add_action ( 'wp_dashboard_setup ' , array ( $ this , 'add_dashboard_widget ' ) );
28- add_action ( 'admin_menu ' , array ( $ this , 'add_security_logs_menu ' ) );
2927 }
3028
31- /**
32- * Creates a custom SQL table optimized for logging.
33- * Uses dbDelta() for safe database schema migrations.
34- */
3529 public function create_custom_table () {
3630 global $ wpdb ;
3731 $ charset_collate = $ wpdb ->get_charset_collate ();
38-
3932 $ sql = "CREATE TABLE $ this ->table_name (
4033 id mediumint(9) NOT NULL AUTO_INCREMENT,
4134 user_id mediumint(9) NOT NULL,
@@ -45,99 +38,44 @@ public function create_custom_table() {
4538 time datetime DEFAULT '0000-00-00 00:00:00' NOT NULL,
4639 PRIMARY KEY (id)
4740 ) $ charset_collate; " ;
48-
4941 require_once ( ABSPATH . 'wp-admin/includes/upgrade.php ' );
5042 dbDelta ( $ sql );
5143 }
5244
53- /**
54- * Captures Login Events.
55- */
5645 public function log_user_login ( $ user_login , $ user ) {
5746 global $ wpdb ;
58- $ wpdb ->insert (
59- $ this ->table_name ,
60- array (
61- 'user_id ' => $ user ->ID ,
62- 'user_login ' => $ user_login ,
63- 'activity_type ' => 'User Login ' ,
64- 'ip_address ' => $ _SERVER ['REMOTE_ADDR ' ],
65- 'time ' => current_time ( 'mysql ' )
66- )
67- );
47+ $ wpdb ->insert ( $ this ->table_name , array (
48+ 'user_id ' => $ user ->ID , 'user_login ' => $ user_login , 'activity_type ' => 'User Login ' ,
49+ 'ip_address ' => $ _SERVER ['REMOTE_ADDR ' ], 'time ' => current_time ( 'mysql ' )
50+ ) );
6851 }
6952
70- /**
71- * Captures Content Updates (Posts/Pages).
72- * Bypasses revisions and auto-saves to maintain database performance.
73- */
7453 public function log_post_updates ( $ post_ID , $ post , $ update ) {
75- if ( ! $ update || wp_is_post_revision ( $ post_ID ) ) {
76- return ;
77- }
54+ // Skip auto-saves and revisions to keep the DB clean (Performance Win)
55+ if ( ! $ update || wp_is_post_revision ( $ post_ID ) ) { return ; }
7856
7957 global $ wpdb ;
80- $ wpdb ->insert (
81- $ this ->table_name ,
82- array (
83- 'user_id ' => get_current_user_id (),
84- 'user_login ' => wp_get_current_user ()->user_login ,
85- 'activity_type ' => 'Post Updated: ' . get_the_title ( $ post_ID ),
86- 'ip_address ' => $ _SERVER ['REMOTE_ADDR ' ],
87- 'time ' => current_time ( 'mysql ' )
88- )
89- );
90- }
91-
92- /**
93- * Registers a dedicated sidebar menu for the logs.
94- */
95- public function add_security_logs_menu () {
96- add_menu_page (
97- 'Security Logs ' ,
98- 'Security Logs ' ,
99- 'manage_options ' ,
100- 'security-logs ' ,
101- array ( $ this , 'render_audit_page ' ),
102- 'dashicons-shield ' ,
103- 80
104- );
58+ $ wpdb ->insert ( $ this ->table_name , array (
59+ 'user_id ' => get_current_user_id (),
60+ 'user_login ' => wp_get_current_user ()->user_login ,
61+ 'activity_type ' => 'Updated Post: ' . get_the_title ( $ post_ID ),
62+ 'ip_address ' => $ _SERVER ['REMOTE_ADDR ' ], 'time ' => current_time ( 'mysql ' )
63+ ) );
10564 }
10665
10766 public function add_dashboard_widget () {
108- wp_add_dashboard_widget (
109- 'rt_audit_log_widget ' ,
110- '🔒 Enterprise Security Logs ' ,
111- array ( $ this , 'render_audit_page ' )
112- );
67+ wp_add_dashboard_widget ( 'rt_audit_log_widget ' , '🔒 Enterprise Security Logs ' , array ( $ this , 'render_widget ' ) );
11368 }
11469
115- /**
116- * Shared render function for both the Widget and the Menu Page.
117- */
118- public function render_audit_page () {
70+ public function render_widget () {
11971 global $ wpdb ;
120- $ logs = $ wpdb ->get_results ( "SELECT * FROM $ this ->table_name ORDER BY time DESC LIMIT 10 " );
121-
122- echo '<div class="wrap"><h2>Audit Logs</h2> ' ;
123- if ( empty ( $ logs ) ) {
124- echo '<p>No activity recorded yet.</p> ' ;
125- } else {
126- echo '<table class="wp-list-table widefat fixed striped"> ' ;
127- echo '<thead><tr><th>User</th><th>Action</th><th>IP Address</th><th>Time</th></tr></thead> ' ;
128- echo '<tbody> ' ;
129- foreach ( $ logs as $ log ) {
130- echo '<tr> ' ;
131- echo '<td><strong> ' . esc_html ( $ log ->user_login ) . '</strong></td> ' ;
132- echo '<td> ' . esc_html ( $ log ->activity_type ) . '</td> ' ;
133- echo '<td> ' . esc_html ( $ log ->ip_address ) . '</td> ' ;
134- echo '<td> ' . esc_html ( $ log ->time ) . '</td> ' ;
135- echo '</tr> ' ;
136- }
137- echo '</tbody></table> ' ;
72+ $ logs = $ wpdb ->get_results ( "SELECT * FROM $ this ->table_name ORDER BY time DESC LIMIT 5 " );
73+ if ( empty ( $ logs ) ) { echo '<p>No activity recorded yet.</p> ' ; return ; }
74+ echo '<table style="width:100%; text-align:left;"><thead><tr><th>User</th><th>Action</th><th>Time</th></tr></thead><tbody> ' ;
75+ foreach ( $ logs as $ log ) {
76+ echo '<tr><td> ' . esc_html ( $ log ->user_login ) . '</td><td> ' . esc_html ( $ log ->activity_type ) . '</td><td> ' . esc_html ( $ log ->time ) . '</td></tr> ' ;
13877 }
139- echo '</div > ' ;
78+ echo '</tbody></table > ' ;
14079 }
14180}
142-
14381new RT_Audit_Logger ();
0 commit comments