| 1 | <?php
|
|---|
| 2 | /*
|
|---|
| 3 | Plugin Name: Fun with Uninstallation
|
|---|
| 4 | Plugin URI: http://www.wp-fun.co.uk/fun-with-uninstallation/
|
|---|
| 5 | Description: Adds an uninstall hook
|
|---|
| 6 | Author: Andrew Rickmann
|
|---|
| 7 | Version: 0.1
|
|---|
| 8 | Author URI: http://www.wp-fun.co.uk
|
|---|
| 9 | Generated At: www.wp-fun.co.uk;
|
|---|
| 10 | */
|
|---|
| 11 |
|
|---|
| 12 | if (!class_exists('fw_uninstallation')) {
|
|---|
| 13 | class fw_uninstallation {
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 | /**
|
|---|
| 17 | * PHP 4 Compatible Constructor
|
|---|
| 18 | */
|
|---|
| 19 | function fw_uninstallation(){$this->__construct();}
|
|---|
| 20 |
|
|---|
| 21 | /**
|
|---|
| 22 | * PHP 5 Constructor
|
|---|
| 23 | */
|
|---|
| 24 | function __construct(){
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 | add_action('after_plugin_row', array(&$this,'after_plugin_row_intercept'));
|
|---|
| 28 | add_action('load-plugins.php', array(&$this,'load_plugins_intercept'));
|
|---|
| 29 |
|
|---|
| 30 |
|
|---|
| 31 | }
|
|---|
| 32 |
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Checks that the plugin is in a state to be uninstalled, and calls the action to do so
|
|---|
| 36 | */
|
|---|
| 37 | function load_plugins_intercept(){
|
|---|
| 38 |
|
|---|
| 39 | if ( isset($_GET['action']) ) {
|
|---|
| 40 | if ('uninstall' == $_GET['action']) {
|
|---|
| 41 | check_admin_referer('uninstall-plugin_' . $_GET['plugin']);
|
|---|
| 42 | $current = get_option('active_plugins');
|
|---|
| 43 | $plugin = trim($_GET['plugin']);
|
|---|
| 44 | if ( validate_file($plugin) )
|
|---|
| 45 | wp_die(__('Invalid plugin.'));
|
|---|
| 46 | if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
|
|---|
| 47 | wp_die(__('Plugin file does not exist.'));
|
|---|
| 48 | if ( !in_array($plugin, $current )) {
|
|---|
| 49 | if ( $uninstaller = $this->check_uninstall_file( $plugin ) ) {
|
|---|
| 50 | @include( $uninstaller );
|
|---|
| 51 | }
|
|---|
| 52 | }
|
|---|
| 53 |
|
|---|
| 54 | wp_redirect('plugins.php?uninstalled=true');
|
|---|
| 55 | }
|
|---|
| 56 | }
|
|---|
| 57 |
|
|---|
| 58 | //uninstalled message
|
|---|
| 59 | if ( isset($_GET['uninstalled']) ) {
|
|---|
| 60 | //start the page earlier than anticpated
|
|---|
| 61 | require_once('admin.php');
|
|---|
| 62 | $title = __('Manage Plugins');
|
|---|
| 63 | //includes required for the menu to work properly
|
|---|
| 64 | global $parent_file;
|
|---|
| 65 | global $menu;
|
|---|
| 66 | global $submenu;
|
|---|
| 67 | global $pagenow;
|
|---|
| 68 | global $plugin_page;
|
|---|
| 69 | global $_wp_real_parent_file;
|
|---|
| 70 | global $_wp_menu_nopriv;
|
|---|
| 71 | global $_wp_submenu_nopriv;
|
|---|
| 72 | require_once('admin-header.php');
|
|---|
| 73 | if ('true' == $_GET['uninstalled']) {
|
|---|
| 74 | ?><div id="message" class="updated fade"><p><?php _e('Plugin <strong>Uninstalled</strong>.') ?></p></div><?php } else {
|
|---|
| 75 | ?><div id="message" class="updated fade"><p><?php _e('<strong>Error</strong> The plugin could not be uninstalled.') ?></p></div><?php
|
|---|
| 76 | }
|
|---|
| 77 | }
|
|---|
| 78 |
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | /**
|
|---|
| 82 | * checks for the existence of an uninstall file and returns it's name
|
|---|
| 83 | */
|
|---|
| 84 | function check_uninstall_file( $plugin ){
|
|---|
| 85 |
|
|---|
| 86 | $plugin_file = ABSPATH . PLUGINDIR . '/' .$plugin;
|
|---|
| 87 | $file_name = str_replace( '.php' , '_uninstaller.php' , $plugin_file );
|
|---|
| 88 | return ( file_exists( $file_name ) ) ? $file_name : false ;
|
|---|
| 89 |
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 | /**
|
|---|
| 94 | * Called by the filter the_content
|
|---|
| 95 | */
|
|---|
| 96 | function after_plugin_row_intercept($plugin_file){
|
|---|
| 97 | global $current_plugins;
|
|---|
| 98 | if ( !in_array($plugin_file, $current_plugins) && $this->check_uninstall_file( $plugin_file ) ) {
|
|---|
| 99 | $toggle = "<a href='" . wp_nonce_url("plugins.php?action=uninstall&plugin=$plugin_file", 'uninstall-plugin_' . $plugin_file) . "' title='".__('Uninstall this plugin')."' class='delete'><strong>".__("Uninstall all the options and settings (including database tables ) relating to this plugin")."</strong></a>";
|
|---|
| 100 | ?>
|
|---|
| 101 | <tr>
|
|---|
| 102 | <td colspan="5" style="border-top:1px solid #ccc; border-bottom:1px solid #ccc; background-color:#F9B7E0"><?php echo $toggle; ?></td>
|
|---|
| 103 | </tr>
|
|---|
| 104 | <?php
|
|---|
| 105 | }
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
|
|---|
| 110 | }
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | //instantiate the class
|
|---|
| 114 | if (class_exists('fw_uninstallation')) {
|
|---|
| 115 | $fw_uninstallation = new fw_uninstallation();
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 |
|
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 | ?>
|
|---|