<?php
/*
Plugin Name: Fun with Uninstallation
Plugin URI: http://www.wp-fun.co.uk/fun-with-uninstallation/
Description: Adds an uninstall hook
Author: Andrew Rickmann
Version: 0.1
Author URI: http://www.wp-fun.co.uk
Generated At: www.wp-fun.co.uk;
*/ 

if (!class_exists('fw_uninstallation')) {
    class fw_uninstallation	{

		
		/**
		* PHP 4 Compatible Constructor
		*/
		function fw_uninstallation(){$this->__construct();}
		
		/**
		* PHP 5 Constructor
		*/		
		function __construct(){


		add_action('after_plugin_row', array(&$this,'after_plugin_row_intercept'));
		add_action('load-plugins.php', array(&$this,'load_plugins_intercept'));


		}
		
		
		/**
		* Checks that the plugin is in a state to be uninstalled, and calls the action to do so
		*/
		function load_plugins_intercept(){
		
			if ( isset($_GET['action']) ) {
				if ('uninstall' == $_GET['action']) {
					check_admin_referer('uninstall-plugin_' . $_GET['plugin']);
					$current = get_option('active_plugins');
					$plugin = trim($_GET['plugin']);
					if ( validate_file($plugin) )
						wp_die(__('Invalid plugin.'));
					if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
						wp_die(__('Plugin file does not exist.'));
					if ( !in_array($plugin, $current )) {
						if ( $uninstaller = $this->check_uninstall_file( $plugin ) ) {
							@include( $uninstaller );
						}
					}
					
					wp_redirect('plugins.php?uninstalled=true');
				}
			}
			
			//uninstalled message
			if ( isset($_GET['uninstalled']) ) {
				//start the page earlier than anticpated
					require_once('admin.php');
					$title = __('Manage Plugins');
					//includes required for the menu to work properly
						global $parent_file;
						global $menu;
						global $submenu;
						global $pagenow;
						global $plugin_page;
						global $_wp_real_parent_file;
						global $_wp_menu_nopriv;
						global $_wp_submenu_nopriv;
					require_once('admin-header.php');
				if ('true' == $_GET['uninstalled']) {
					?><div id="message" class="updated fade"><p><?php _e('Plugin <strong>Uninstalled</strong>.') ?></p></div><?php			} else {
					?><div id="message" class="updated fade"><p><?php _e('<strong>Error</strong> The plugin could not be uninstalled.') ?></p></div><?php
				} 
			}
		
		}
		
		/**
		* checks for the existence of an uninstall file and returns it's name
		*/
		function check_uninstall_file( $plugin ){
		
			$plugin_file = ABSPATH . PLUGINDIR . '/' .$plugin;
			$file_name = str_replace( '.php' , '_uninstaller.php' , $plugin_file );
			return ( file_exists( $file_name ) ) ? $file_name  : false ;
		
		}
		
	
		/**
		* Called by the filter the_content
		*/
		function after_plugin_row_intercept($plugin_file){
			global $current_plugins;
			if ( !in_array($plugin_file, $current_plugins) && $this->check_uninstall_file( $plugin_file ) ) {
			$toggle = "<a href='" . wp_nonce_url("plugins.php?action=uninstall&amp;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>";
			?>
			<tr>
			<td colspan="5" style="border-top:1px solid #ccc; border-bottom:1px solid #ccc; background-color:#F9B7E0"><?php echo $toggle; ?></td>
			</tr>
			<?php
			}		
		}
		
		

    }
}

//instantiate the class
if (class_exists('fw_uninstallation')) {
	$fw_uninstallation = new fw_uninstallation();
}




?>