Index: plugin.php
===================================================================
--- plugin.php	(revision 6603)
+++ plugin.php	(working copy)
@@ -190,6 +190,109 @@
 	return 0;
 }
 
+/**
+* get_plugin_assets() retrieves the plugin's registered assets from the option
+*
+* Part of the uninstall process. 
+* This function checks whether the plugin author has registered any assets
+* that can be uninstalled and if so returns a named array with those options.
+*
+* @link http://trac.wordpress.org/ticket/5625
+*
+* @param string $plugin_file the path to the plugin file, usually given by __FILE__
+* @return array|bool the plugin's uninstallable assets, or false if there are none
+*
+*/
+function get_plugin_assets( $plugin_file ) {
+	$plugin_assets = get_option( 'plugin_assets' );
+	$plugin = plugin_basename( $plugin_file );
+	if ( !is_array( $plugin_assets ) )
+		return false;
+	if ( isset( $plugin_assets[$plugin] ) ) {
+		return $plugin_assets[$plugin];
+	} else {
+		return false;
+	}
+}
+
+/**
+* is_plugin_uninstallable() checks if a plugin has registered any uninstallable assets
+*
+* Part of the uninstall process. 
+* This function checks whether the plugin author has registered any assets
+* that can be uninstalled.
+*
+* @link http://trac.wordpress.org/ticket/5625
+*
+* @param string $plugin_file the path to the plugin file, usually given by __FILE__
+* @return bool true if the plugin has assets that can be uninstalled
+*
+*/	
+function is_plugin_uninstallable( $plugin_file ){
+	$uninstallable_plugins = get_option( 'plugin_assets' );
+	$plugin = plugin_basename( $plugin_file );
+	if ( isset( $uninstallable_plugins[$plugin] ) ) {
+		return true;
+	} else {
+		return false;
+	}
+}
+
+
+/**
+ * delete_plugin_assets() - Delete the database tables and options created by a plugin
+ *
+ * Part of the uninstall process. 
+ * This function retrieves the list of assets the plugin author registered, using
+ * register_plugin_assets and removes the database tables and options specified.
+ *
+ * @link http://trac.wordpress.org/ticket/5625
+ *
+ * @param string $plugin_file the path to the plugin
+ * @return bool returns true if the tables and options are no longer present
+ */
+function delete_plugin_assets( $plugin_file ){
+	global $wpdb;
+	$deletion_complete = true;
+	$uninstallable_plugins = get_option( 'plugin_assets' );
+	$plugin = plugin_basename( $plugin_file );
+	if ( isset( $uninstallable_plugins[$plugin] ) ) {
+		$plugin_assets = $uninstallable_plugins[$plugin];
+		//remove tables
+		$remove_table_sql = 'DROP TABLE IF EXISTS `%s`';
+		$check_table_sql = 'SELECT 1 FROM `%s` LIMIT 0';
+		foreach( $plugin_assets['tables'] as $table ){
+			//filter
+			$table = str_replace( '`' , '' , $table);
+			$table = str_replace( ';' , '' , $table);
+			$table = str_replace( '*' , '' , $table);
+			
+			//check against core tables
+			if ( !in_array( str_replace( $wpdb->prefix , '' , $table ) , $wpdb->tables ) ) {
+				$sql = sprintf( $remove_table_sql , $table );
+				$wpdb->query( $sql );
+				//check if table is now gone
+				if ( $wpdb->query( sprintf( $check_table_sql , $table ) ) ) {
+					$deletion_complete = false;
+				}
+			}
+		}
+		//remove options
+		foreach( $plugin_assets['options'] as $option ){
+			delete_option( $option );
+			if ( get_option( $option ) ) {
+				$deletion_complete = false;
+			};
+		}
+		//remove assets from asset list
+		if ( $deletion_complete ) {
+			 unregister_plugin_assets( $plugin_file );
+		}
+	} 
+	return $deletion_complete;
+}
+
+
 //
 // Menu
 //

