Skip to content

Instantly share code, notes, and snippets.

@lordspace
Created January 6, 2014 00:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lordspace/8276394 to your computer and use it in GitHub Desktop.
Save lordspace/8276394 to your computer and use it in GitHub Desktop.
How to Get Notified when Your Client Installs/Uninstalls a Plugin More: http://club.orbisius.com/howto/wordpress-tips/get-notified-client-installs-uninstalls-wordpress-plugin/
<?php
/**
* This file will send you alerts when your clients install/uninstall plugins
* Usage: Save this as wp-content/mu-plugins/my-spy.php
*
* License: GPL
* @author orbisius.com
* Use it at your own risk. You may have to disclose this in your terms of service.
* Check with a laywer first.
*/
define('SPY_MANAGE_PLUGINS_TO', 'you@yoursite.com');
define('SPY_MANAGE_PLUGINS_FROM', 'Manage Plugins <spy@%host%>'); // use %host% to let the plugin determine the host
add_action('activated_plugin', 'orbisius_my_spy_manage_plugins', 10, 2);
add_action('deactivated_plugin', 'orbisius_my_spy_manage_plugins', 10, 2);
/**
* This functon is triggered when a plugin is activated or deactivated.
* It saves the event: activate_plugin, deactivate_plugins.
* The information is sent to an email defined by SPY_MANAGE_PLUGINS_TO const.
*
* @param string $plugin e.g. plugin-dir/plugin.php
* @param bool $network_deactivating
* @return void nothing
*/
function orbisius_my_spy_manage_plugins($plugin, $network_act_deactivating) {
$site = site_url();
// the last item from the array contains the event: activate_plugins/deactivate_plugins
$callers = debug_backtrace();
$caller = array_pop($callers);
$ip = empty($_SERVER['REMOTE_ADDR']) ? '0.0.0.0' : $_SERVER['REMOTE_ADDR']; // command line scripts won't have IP set.
$network_act_deactivating = $network_act_deactivating ? 'Y' : 'N';
$buff = '';
$buff .= "Site: $site | ";
$buff .= "Plugin: $plugin | ";
$buff .= "Method: {$caller['function']} | ";
$buff .= "network act/deactivating: $network_act_deactivating | ";
$buff .= "IP: $ip | ";
$current_user = wp_get_current_user();
if (!empty($current_user->ID)) {
$buff .= 'Username: ' . $current_user->user_login . ' | ';
$buff .= 'User email: ' . $current_user->user_email . ' | ';
$buff .= 'User first name: ' . $current_user->user_firstname . ' | ';
$buff .= 'User last name: ' . $current_user->user_lastname . ' | ';
$buff .= 'User display name: ' . $current_user->display_name . ' | ';
$buff .= 'User ID: ' . $current_user->ID . ' | ';
}
$buff .= "Date: " . date('r');
$buff .= "\n";
$headers = 'From: ' . SPY_MANAGE_PLUGINS_FROM . "\r\n";
$host = $_SERVER['HTTP_HOST'];
$headers = preg_replace('#[\%\{]+host[\%}]+#si', $host, $headers);
// Bon Voyage Email!
wp_mail(SPY_MANAGE_PLUGINS_TO, 'Manage Plugin: ' . $host, $buff, $headers);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment