<?php
/*
Plugin Name: Fun with Uninstallation
Plugin URI: http://www.wp-fun.co.uk/2008/01/09/fun-with-uninstallation/
Description: Adds an uninstall hook
Author: Andrew Rickmann
Version: 0.2
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(){

		register_activation_hook( __FILE__ , array( &$this , "install" ));
		add_action('after_plugin_row', array(&$this,'after_plugin_row_intercept'));
		add_action('load-plugins.php', array(&$this,'load_plugins_intercept'));
		
		}
		
		/**
		* Makes sure the option is an array on activation
		*/
		function install(){
			$uninstallable_plugins = get_option( 'uninstallable_plugins' );
			if ( !is_array( $uninstallable_plugins ) ) {
				update_option( 'uninstallable_plugins' , array() );
			}
		}
		
		/**
		* 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');
					$uninstallable_plugins = get_option( 'uninstallable_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 )) {
						foreach( $uninstallable_plugins as $uninstallable_plugin ) {
							if ( $plugin == $uninstallable_plugin[1]  ) {
								//include the plugin
								include_once( ABSPATH . PLUGINDIR . '/' . $plugin );
								
								//do the action
								add_action( 'uninstall_plugin_' . $plugin , $uninstallable_plugin[2] );
								do_action( 'uninstall_plugin_' . $plugin );
								
								//remove it from the uninstall list
								unregister_uninstall_hook(  $uninstallable_plugin[0] );
							}
						}

					}
					
					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
				} 
			}
		
		}
		
		/**
		* Called by the filter the_content
		*/
		function after_plugin_row_intercept($plugin_file){
			global $current_plugins;
			if ( !in_array($plugin_file, $current_plugins) && is_uninstallable( $plugin_file ) ) {
			$plugin_data = get_plugin_data( ABSPATH . PLUGINDIR . '/' . $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 ").$plugin_data['Name']."</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();
	
	function register_uninstall_hook( $name , $plugin_file , $function ){
	
		$uninstallable_plugins = get_option( 'uninstallable_plugins' );
		$plugin = trim( plugin_basename( $plugin_file ) );
		if ( file_exists( ABSPATH . PLUGINDIR . '/' . $plugin ) ){
			$uninstallable_plugins[$name] = array( $name , $plugin , $function );
		}
		
		update_option( 'uninstallable_plugins' , $uninstallable_plugins );
		
	}
	
	function unregister_uninstall_hook( $name ) {
		$uninstallable_plugins = get_option( 'uninstallable_plugins' );
		if ( isset( $uninstallable_plugins[$name] ) ) {
			unset( $uninstallable_plugins[$name] );
			update_option( 'uninstallable_plugins' , $uninstallable_plugins );
		}
	}
	
	function is_plugin_active( $plugin_file ){
		$plugin = trim( plugin_basename( $plugin_file ) );
		
		//check if being activated
		if ( isset($_GET['action']) ) {
			if ('activate' == $_GET['action']) {
				if ( isset( $_GET['plugin'] ) && $plugin == trim($_GET['plugin']) ) { return true; }
			}
		}
		
		//if not then we continue and check if active
		$current = get_option('active_plugins');
		if ( in_array( $plugin , $current ) ) {
			return true;	
		} else {
			return false;
		}
	
	}
	
	function is_uninstallable( $plugin_file ){
	
		$uninstallable_plugins = get_option( 'uninstallable_plugins' );
		$plugin = trim( $plugin_file );
		foreach($uninstallable_plugins as $uninstallable_plugin) {
			if ( $plugin == $uninstallable_plugin[1]  ) { 
				return true; 
			} 
		}
		return false;
	}
	
	
}




?>