Skip to content

Instantly share code, notes, and snippets.

@thefuxia
Created May 27, 2012 03:41
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thefuxia/2802082 to your computer and use it in GitHub Desktop.
Save thefuxia/2802082 to your computer and use it in GitHub Desktop.
Plugin Log: Log all plugin (de)activations.
<?php # -*- coding: utf-8 -*-
/**
* Plugin Name: T5 Plugin Log
* Description: Log all plugin (de)activations.
* Version: 2012.05.27
* Author: Thomas Scholz <info@toscho.de>
* Author URI: http://toscho.de
* License: MIT
* License URI: http://www.opensource.org/licenses/mit-license.php
*
* Copyright (c) 2012 Thomas Scholz
*/
add_action( 'activated_plugin', 't5_plugin_logger', 10, 2 );
add_action( 'deactivated_plugin', 't5_plugin_logger', 10, 2 );
add_action( 'wp_dashboard_setup', 't5_register_plugin_log_dashboard_widget' );
register_deactivation_hook( __FILE__, 't5_deactivate_plugin_log' );
/**
* Log plugin activations and deactivations.
*
* @param string $plugin
* @param bool $network_wide
* @return void
*/
function t5_plugin_logger( $plugin, $network_wide )
{
$log_size = 20;
$log = get_option( 't5_plugin_log' );
! $log && $log = array ();
// Remove the oldest entry.
sizeof( $log ) > $log_size and array_shift( $log );
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . '/' . $plugin );
$date_format = get_option( 'date_format' ) . ' · ' . get_option( 'time_format' );
$log[] = array (
'user' => esc_html( wp_get_current_user()->display_name ),
'plugin' => $plugin_data['Name'],
'network' => $network_wide ? '✔' : '',
'time' => date( $date_format, time() ),
'action' => 'deactivated_plugin' === current_filter() ? 'deactivated' : 'activated'
);
update_option( 't5_plugin_log', $log );
}
/**
* Register dashboard widget.
*
* @return void
*/
function t5_register_plugin_log_dashboard_widget()
{
if ( current_user_can( 'activate_plugins' ) )
{
wp_add_dashboard_widget(
't5_plugin_log_dbw',
'Plugin Log',
't5_display_plugin_log_dbw'
);
}
}
/**
* Print the dashboard widget.
*
* @return void
*/
function t5_display_plugin_log_dbw()
{
$log = get_option( 't5_plugin_log' );
if ( ! $log )
{
print 'The plugin log is empty';
return;
}
print '<table class="widefat">
<thead>
<tr>
<th>Time</th>
<th>User</th>
<th>Action</th>
<th>Plugin</th>
<th>Network</th>
</tr>
</thead>
<tbody>';
foreach ( $log as $entry )
{
printf(
'<tr><td>%1$s</td><td>%2$s</td><td>%3$s</td><td>%4$s</td><td>%5$s</td></tr>',
$entry['time'],
$entry['user'],
$entry['action'],
$entry['plugin'],
$entry['network']
);
}
print '</tbody></table>';
}
/**
* Remove the option for our log.
*
* @return void
*/
function t5_deactivate_plugin_log()
{
// Otherwise the next line wouldn't work.
remove_action( 'deactivated_plugin', 't5_plugin_logger' );
// Clean up the options table.
delete_option( 't5_plugin_log' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment