Make WordPress Core

Ticket #5625: fun_with_uninstallation0.2.php

File fun_with_uninstallation0.2.php, 5.6 KB (added by arickmann, 17 years ago)

Version 0.2 that uses a hook.

Line 
1<?php
2/*
3Plugin Name: Fun with Uninstallation
4Plugin URI: http://www.wp-fun.co.uk/2008/01/09/fun-with-uninstallation/
5Description: Adds an uninstall hook
6Author: Andrew Rickmann
7Version: 0.2
8Author URI: http://www.wp-fun.co.uk
9Generated At: www.wp-fun.co.uk;
10*/ 
11
12if (!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                register_activation_hook( __FILE__ , array( &$this , "install" ));
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                * Makes sure the option is an array on activation
34                */
35                function install(){
36                        $uninstallable_plugins = get_option( 'uninstallable_plugins' );
37                        if ( !is_array( $uninstallable_plugins ) ) {
38                                update_option( 'uninstallable_plugins' , array() );
39                        }
40                }
41               
42                /**
43                * Checks that the plugin is in a state to be uninstalled, and calls the action to do so
44                */
45                function load_plugins_intercept(){
46               
47                        if ( isset($_GET['action']) ) {
48                                if ('uninstall' == $_GET['action']) {
49                                        check_admin_referer('uninstall-plugin_' . $_GET['plugin']);
50                                        $current = get_option('active_plugins');
51                                        $uninstallable_plugins = get_option( 'uninstallable_plugins' );
52                                        $plugin = trim($_GET['plugin']);
53                                        if ( validate_file($plugin) )
54                                                wp_die(__('Invalid plugin.'));
55                                        if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
56                                                wp_die(__('Plugin file does not exist.'));
57                                        if ( !in_array($plugin, $current )) {
58                                                foreach( $uninstallable_plugins as $uninstallable_plugin ) {
59                                                        if ( $plugin == $uninstallable_plugin[1]  ) {
60                                                                //include the plugin
61                                                                include_once( ABSPATH . PLUGINDIR . '/' . $plugin );
62                                                               
63                                                                //do the action
64                                                                add_action( 'uninstall_plugin_' . $plugin , $uninstallable_plugin[2] );
65                                                                do_action( 'uninstall_plugin_' . $plugin );
66                                                               
67                                                                //remove it from the uninstall list
68                                                                unregister_uninstall_hook(  $uninstallable_plugin[0] );
69                                                        }
70                                                }
71
72                                        }
73                                       
74                                        wp_redirect('plugins.php?uninstalled=true');
75                                }
76                        }
77                       
78                        //uninstalled message
79                        if ( isset($_GET['uninstalled']) ) {
80                                //start the page earlier than anticpated
81                                        require_once('admin.php');
82                                        $title = __('Manage Plugins');
83                                        //includes required for the menu to work properly
84                                                global $parent_file;
85                                                global $menu;
86                                                global $submenu;
87                                                global $pagenow;
88                                                global $plugin_page;
89                                                global $_wp_real_parent_file;
90                                                global $_wp_menu_nopriv;
91                                                global $_wp_submenu_nopriv;
92                                        require_once('admin-header.php');
93                                if ('true' == $_GET['uninstalled']) {
94                                        ?><div id="message" class="updated fade"><p><?php _e('Plugin <strong>Uninstalled</strong>.') ?></p></div><?php                  } else {
95                                        ?><div id="message" class="updated fade"><p><?php _e('<strong>Error</strong> The plugin could not be uninstalled.') ?></p></div><?php
96                                } 
97                        }
98               
99                }
100               
101                /**
102                * Called by the filter the_content
103                */
104                function after_plugin_row_intercept($plugin_file){
105                        global $current_plugins;
106                        if ( !in_array($plugin_file, $current_plugins) && is_uninstallable( $plugin_file ) ) {
107                        $plugin_data = get_plugin_data( ABSPATH . PLUGINDIR . '/' . $plugin_file );
108                        $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>";
109                        ?>
110                        <tr>
111                        <td colspan="5" style="border-top:1px solid #ccc; border-bottom:1px solid #ccc; background-color:#F9B7E0"><?php echo $toggle; ?></td>
112                        </tr>
113                        <?php
114                        }               
115                }
116               
117               
118
119    }
120}
121
122//instantiate the class
123if (class_exists('fw_uninstallation')) {
124        $fw_uninstallation = new fw_uninstallation();
125       
126        function register_uninstall_hook( $name , $plugin_file , $function ){
127       
128                $uninstallable_plugins = get_option( 'uninstallable_plugins' );
129                $plugin = trim( plugin_basename( $plugin_file ) );
130                if ( file_exists( ABSPATH . PLUGINDIR . '/' . $plugin ) ){
131                        $uninstallable_plugins[$name] = array( $name , $plugin , $function );
132                }
133               
134                update_option( 'uninstallable_plugins' , $uninstallable_plugins );
135               
136        }
137       
138        function unregister_uninstall_hook( $name ) {
139                $uninstallable_plugins = get_option( 'uninstallable_plugins' );
140                if ( isset( $uninstallable_plugins[$name] ) ) {
141                        unset( $uninstallable_plugins[$name] );
142                        update_option( 'uninstallable_plugins' , $uninstallable_plugins );
143                }
144        }
145       
146        function is_plugin_active( $plugin_file ){
147                $plugin = trim( plugin_basename( $plugin_file ) );
148               
149                //check if being activated
150                if ( isset($_GET['action']) ) {
151                        if ('activate' == $_GET['action']) {
152                                if ( isset( $_GET['plugin'] ) && $plugin == trim($_GET['plugin']) ) { return true; }
153                        }
154                }
155               
156                //if not then we continue and check if active
157                $current = get_option('active_plugins');
158                if ( in_array( $plugin , $current ) ) {
159                        return true;   
160                } else {
161                        return false;
162                }
163       
164        }
165       
166        function is_uninstallable( $plugin_file ){
167       
168                $uninstallable_plugins = get_option( 'uninstallable_plugins' );
169                $plugin = trim( $plugin_file );
170                foreach($uninstallable_plugins as $uninstallable_plugin) {
171                        if ( $plugin == $uninstallable_plugin[1]  ) { 
172                                return true; 
173                        } 
174                }
175                return false;
176        }
177       
178       
179}
180
181
182
183
184?>