File: /var/www/html/wp-content/mu-plugins/plugin-install-audit-log.php
<?php
/**
* Plugin install/update audit logger.
*/
if (!defined('ABSPATH')) {
exit;
}
add_action('upgrader_process_complete', function ($upgrader, $options) {
if (!is_array($options) || (($options['type'] ?? '') !== 'plugin')) {
return;
}
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
$forwarded_for = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? '';
$user_id = function_exists('get_current_user_id') ? get_current_user_id() : 0;
$user = $user_id ? get_userdata($user_id) : null;
error_log('PLUGIN INSTALL/UPDATE AUDIT: ' . wp_json_encode([
'options' => $options,
'remote_addr' => $ip,
'x_forwarded_for' => $forwarded_for,
'user_id' => $user_id,
'user_login' => $user ? $user->user_login : null,
'request_uri' => $_SERVER['REQUEST_URI'] ?? '',
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? '',
'time' => gmdate('c'),
]));
}, 10, 2);