Skip to content

Commit a8a7fc4

Browse files
authored
Update enterprise-audit-log.php
1 parent cae96c5 commit a8a7fc4

1 file changed

Lines changed: 66 additions & 26 deletions

File tree

enterprise-audit-log.php

Lines changed: 66 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22
/**
33
* Plugin Name: Enterprise Audit Logger
4-
* Description: Creates a custom database table to track user activity (Logins, Post Updates) for compliance and security auditing.
5-
* Version: 1.0.0
4+
* Description: High-performance activity tracker using custom SQL tables for compliance and security auditing.
5+
* Version: 1.1.0
66
* Author: Vamsi Bodapati
77
*/
88

@@ -16,19 +16,21 @@ public function __construct() {
1616
global $wpdb;
1717
$this->table_name = $wpdb->prefix . 'rt_audit_logs';
1818

19-
// 1. Hook into plugin activation to create the DB Table
19+
// 1. Hook into plugin activation to create/update the DB Table
2020
register_activation_hook( __FILE__, array( $this, 'create_custom_table' ) );
2121

22-
// 2. Log when a user logs in
22+
// 2. Event Hooks: Logins and Content Updates
2323
add_action( 'wp_login', array( $this, 'log_user_login' ), 10, 2 );
24+
add_action( 'save_post', array( $this, 'log_post_updates' ), 10, 3 );
2425

25-
// 3. Add a Dashboard Widget to view logs
26+
// 3. UI Hooks: Dashboard Widget and Sidebar Menu
2627
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widget' ) );
28+
add_action( 'admin_menu', array( $this, 'add_security_logs_menu' ) );
2729
}
2830

2931
/**
3032
* Creates a custom SQL table optimized for logging.
31-
* Uses dbDelta() which is the WordPress standard for database migrations.
33+
* Uses dbDelta() for safe database schema migrations.
3234
*/
3335
public function create_custom_table() {
3436
global $wpdb;
@@ -49,54 +51,92 @@ public function create_custom_table() {
4951
}
5052

5153
/**
52-
* Captures the Login Event and inserts into our custom table.
54+
* Captures Login Events.
5355
*/
5456
public function log_user_login( $user_login, $user ) {
5557
global $wpdb;
56-
5758
$wpdb->insert(
5859
$this->table_name,
5960
array(
6061
'user_id' => $user->ID,
6162
'user_login' => $user_login,
6263
'activity_type' => 'User Login',
63-
'ip_address' => $_SERVER['REMOTE_ADDR'], // Basic IP capture
64+
'ip_address' => $_SERVER['REMOTE_ADDR'],
6465
'time' => current_time( 'mysql' )
6566
)
6667
);
6768
}
6869

6970
/**
70-
* Adds a widget to the WP Admin Dashboard to visualize the data.
71+
* Captures Content Updates (Posts/Pages).
72+
* Bypasses revisions and auto-saves to maintain database performance.
7173
*/
74+
public function log_post_updates( $post_ID, $post, $update ) {
75+
if ( ! $update || wp_is_post_revision( $post_ID ) ) {
76+
return;
77+
}
78+
79+
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+
);
105+
}
106+
72107
public function add_dashboard_widget() {
73108
wp_add_dashboard_widget(
74109
'rt_audit_log_widget',
75110
'🔒 Enterprise Security Logs',
76-
array( $this, 'render_dashboard_widget' )
111+
array( $this, 'render_audit_page' )
77112
);
78113
}
79114

80-
public function render_dashboard_widget() {
115+
/**
116+
* Shared render function for both the Widget and the Menu Page.
117+
*/
118+
public function render_audit_page() {
81119
global $wpdb;
82-
$logs = $wpdb->get_results( "SELECT * FROM $this->table_name ORDER BY time DESC LIMIT 5" );
120+
$logs = $wpdb->get_results( "SELECT * FROM $this->table_name ORDER BY time DESC LIMIT 10" );
83121

122+
echo '<div class="wrap"><h2>Audit Logs</h2>';
84123
if ( empty( $logs ) ) {
85124
echo '<p>No activity recorded yet.</p>';
86-
return;
87-
}
88-
89-
echo '<table style="width:100%; text-align:left;">';
90-
echo '<thead><tr><th>User</th><th>Action</th><th>Time</th></tr></thead>';
91-
echo '<tbody>';
92-
foreach ( $logs as $log ) {
93-
echo '<tr>';
94-
echo '<td>' . esc_html( $log->user_login ) . '</td>';
95-
echo '<td>' . esc_html( $log->activity_type ) . '</td>';
96-
echo '<td>' . esc_html( $log->time ) . '</td>';
97-
echo '</tr>';
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>';
98138
}
99-
echo '</tbody></table>';
139+
echo '</div>';
100140
}
101141
}
102142

0 commit comments

Comments
 (0)