WordPress.org

Make WordPress Core

Ticket #5625: 7372.r8529.patch

File 7372.r8529.patch, 13.2 KB (added by jacobsantos, 10 years ago)

Initial uninstall hook support

  • wp-admin/includes/plugin.php

     
    157157        return $wp_plugins;
    158158}
    159159
     160/**
     161 * Check whether the plugin is active by checking the active_plugins list.
     162 *
     163 * @since 2.5.0
     164 *
     165 * @param string $plugin Base plugin path from plugins directory.
     166 * @return bool True, if in the active plugins list. False, not in the list.
     167 */
    160168function is_plugin_active($plugin){
    161169        return in_array($plugin, get_option('active_plugins'));
    162170}
     
    328336        return 0;
    329337}
    330338
     339/**
     340 * Whether the plugin can be uninstalled.
     341 *
     342 * @since {@internal Version Unknown}}
     343 *
     344 * @param string $plugin Plugin path to check.
     345 * @return bool Whether plugin can be uninstalled.
     346 */
     347function is_uninstallable_plugin($plugin) {
     348        $file = plugin_basename($plugin);
     349
     350        if( false === has_action('uninstall_' . $file) )
     351                return false;
     352
     353        return true;
     354}
     355
     356/**
     357 * Uninstall a single plugin.
     358 *
     359 * Calls the uninstall hook, if it is available.
     360 *
     361 * @param unknown_type $plugin
     362 */
     363function uninstall_plugin($plugin) {
     364        $plugin = plugin_basename($plugin);
     365        do_action('uninstall_' . $plugin);
     366        deactivate_plugins($plugin, true);
     367}
     368
     369/**
     370 * Uninstall multiple plugins.
     371 *
     372 * @since {@internal Version Unknown}}
     373 *
     374 * @param array $plugins
     375 * @return bool Whether uninstall process is completed.
     376 */
     377function uninstall_plugins($plugins) {
     378        if ( !is_array($plugins) )
     379                $plugins = array($plugins);
     380
     381        foreach ( (array) $plugins as $plugin )
     382                uninstall_plugin($plugin);
     383
     384        return true;
     385}
     386
    331387//
    332388// Menu
    333389//
  • wp-admin/plugins.php

     
    22require_once('admin.php');
    33
    44$action = '';
    5 foreach( array('activate-selected', 'deactivate-selected', 'delete-selected', 'clear-recent-list') as $action_key ) {
     5foreach( array('uninstall-selected', 'activate-selected', 'deactivate-selected', 'delete-selected', 'clear-recent-list') as $action_key ) {
    66        if( isset($_POST[$action_key]) ) {
    77                $action = $action_key;
    88                break;
     
    4444                        wp_redirect('plugins.php?activate-multi=true');
    4545                        exit;
    4646                        break;
     47                case 'uninstall':
     48                        check_admin_referer('uninstall-plugin_' . $plugin);
     49                        uninstall_plugin($plugin);
     50                        wp_redirect('plugins.php?uninstall=true');
     51                        exit;
     52                        break;
     53                case 'uninstall-selected':
     54                        check_admin_referer('bulk-manage-plugins');
     55                        uninstall_plugins($_POST['checked']);
     56                        wp_redirect('plugins.php?uninstall-multi=true');
     57                        exit;
     58                        break;
    4759                case 'error_scrape':
    4860                        check_admin_referer('plugin-activation-error_' . $plugin);
    4961                        $valid = validate_plugin($plugin);
     
    194206        <div id="message" class="updated fade"><p><?php _e('Plugin <strong>deactivated</strong>.') ?></p></div>
    195207<?php elseif (isset($_GET['deactivate-multi'])) : ?>
    196208        <div id="message" class="updated fade"><p><?php _e('Selected plugins <strong>deactivated</strong>.'); ?></p></div>
     209<?php elseif ( isset($_GET['uninstall']) ) : ?>
     210        <div id="message" class="updated fade"><p><?php _e('Plugin <strong>uninstalled</strong>.') ?></p></div>
     211<?php elseif ( isset($_GET['uninstall-multi']) ) : ?>
     212        <div id="message" class="updated fade"><p><?php _e('Selected plugins <strong>uninstalled</strong>.') ?></p></div>
    197213<?php endif; ?>
    198214
    199215<div class="wrap">
     
    292308                if ( current_user_can('edit_plugins') && is_writable(WP_PLUGIN_DIR . '/' . $plugin_file) )
    293309                        $action_links[] = '<a href="plugin-editor.php?file=' . $plugin_file . '" title="' . __('Open this file in the Plugin Editor') . '" class="edit">' . __('Edit') . '</a>';
    294310
     311                if( is_uninstallable_plugin($plugin_file) )
     312                        $action_links[] = '<a href="' . wp_nonce_url('plugins.php?action=uninstall&amp;plugin=' . $plugin_file, 'uninstall-plugin_' . $plugin_file) . '" title="' . __('Uninstall this plugin') . '" class="uninstall">' . __('Uninstall') . '</a>';
     313
    295314                $action_links = apply_filters('plugin_action_links', $action_links, $plugin_file, $plugin_data, $context);
    296315
    297316                echo "
     
    321340
    322341<div class="tablenav">
    323342        <div class="alignleft">
     343                <input type="submit" name="uninstall-selected" value="<?php _e('Uninstall') ?>" class="button-secondary" />
    324344                <input type="submit" name="deactivate-selected" value="<?php _e('Deactivate') ?>" class="button-secondary" />
    325345        </div>
    326346</div>
  • wp-includes/plugin.php

     
    2020 */
    2121
    2222/**
    23  * add_filter() - Hooks a function or method to a specific filter action.
     23 * Hooks a function or method to a specific filter action.
    2424 *
    2525 * Filters are the hooks that WordPress launches to modify text of various types
    2626 * before adding it to the database or sending it to the browser screen. Plugins
     
    6969}
    7070
    7171/**
    72  * has_filter() - Check if any filter has been registered for a hook.
     72 * Check if any filter has been registered for a hook.
    7373 *
    7474 * @package WordPress
    7575 * @subpackage Plugin
     
    9999}
    100100
    101101/**
    102  * apply_filters() - Call the functions added to a filter hook.
     102 * Call the functions added to a filter hook.
    103103 *
    104104 * The callback functions attached to filter hook <tt>$tag</tt> are invoked by
    105105 * calling this function. This function can be used to create a new filter hook
     
    171171}
    172172
    173173/**
    174  * remove_filter() - Removes a function from a specified filter hook.
     174 * Removes a function from a specified filter hook.
    175175 *
    176176 * This function removes a function attached to a specified filter hook. This
    177177 * method can be used to remove default functions attached to a specific filter
    178178 * hook and possibly replace them with a substitute.
    179179 *
    180  * To remove a hook, the <tt>$function_to_remove</tt> and <tt>$priority</tt> arguments
    181  * must match when the hook was added. This goes for both filters and actions. No warning
     180 * To remove a hook, the $function_to_remove and $priority arguments must match
     181 * when the hook was added. This goes for both filters and actions. No warning
    182182 * will be given on removal failure.
    183183 *
    184184 * @package WordPress
     
    208208
    209209
    210210/**
    211  * current_filter() - Return the name of the current filter or action.
     211 * Return the name of the current filter or action.
    212212 *
    213213 * @package WordPress
    214214 * @subpackage Plugin
     
    223223
    224224
    225225/**
    226  * add_action() - Hooks a function on to a specific action.
     226 * Hooks a function on to a specific action.
    227227 *
    228228 * Actions are the hooks that the WordPress core launches at specific points
    229229 * during execution, or when specific events occur. Plugins can specify that
     
    247247
    248248
    249249/**
    250  * do_action() - Execute functions hooked on a specific action hook.
     250 * Execute functions hooked on a specific action hook.
    251251 *
    252252 * This function invokes all functions attached to action hook <tt>$tag</tt>.
    253253 * It is possible to create new action hooks by simply calling this function,
     
    316316}
    317317
    318318/**
    319  * did_action() - Return the number times an action is fired.
     319 * Return the number times an action is fired.
    320320 *
    321321 * @package WordPress
    322322 * @subpackage Plugin
     
    336336}
    337337
    338338/**
    339  * do_action_ref_array() - Execute functions hooked on a specific action hook, specifying arguments in an array.
     339 * Execute functions hooked on a specific action hook, specifying arguments in an array.
    340340 *
    341341 * @see do_action() This function is identical, but the arguments passed to
    342342 * the functions hooked to <tt>$tag</tt> are supplied using an array.
     
    391391}
    392392
    393393/**
    394  * has_action() - Check if any action has been registered for a hook.
     394 * Check if any action has been registered for a hook.
    395395 *
    396396 * @package WordPress
    397397 * @subpackage Plugin
     
    407407}
    408408
    409409/**
    410  * remove_action() - Removes a function from a specified action hook.
     410 * Removes a function from a specified action hook.
    411411 *
    412412 * This function removes a function attached to a specified action hook. This
    413413 * method can be used to remove default functions attached to a specific filter
     
    432432//
    433433
    434434/**
    435  * plugin_basename() - Gets the basename of a plugin.
     435 * Gets the basename of a plugin.
    436436 *
    437437 * This method extracts the name of a plugin from its filename.
    438438 *
     
    456456}
    457457
    458458/**
    459  * register_activation_hook() - Hook a function on a plugin activation action hook.
     459 * Set the activation hook for a plugin.
    460460 *
    461461 * When a plugin is activated, the action 'activate_PLUGINNAME' hook is
    462462 * activated. In the name of this hook, PLUGINNAME is replaced with the name of
     
    474474 * @access private
    475475 *
    476476 * @param string $file The filename of the plugin including the path.
    477  * @param string $function the function hooked to the 'activate_PLUGIN' action.
     477 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
    478478 */
    479479function register_activation_hook($file, $function) {
    480480        $file = plugin_basename($file);
     
    482482}
    483483
    484484/**
    485  * register_deactivation_hook() - Hook a function on a plugin deactivation action hook.
     485 * Set the deactivation hook for a plugin.
    486486 *
    487487 * When a plugin is deactivated, the action 'deactivate_PLUGINNAME' hook is
    488  * deactivated. In the name of this hook, PLUGINNAME is replaced with the name of
    489  * the plugin, including the optional subdirectory. For example, when the plugin
    490  * is located in <tt>wp-content/plugin/sampleplugin/sample.php</tt>, then the
    491  * name of this hook will become 'activate_sampleplugin/sample.php'.
     488 * deactivated. In the name of this hook, PLUGINNAME is replaced with the name
     489 * of the plugin, including the optional subdirectory. For example, when the
     490 * plugin is located in <tt>wp-content/plugin/sampleplugin/sample.php</tt>, then
     491 * the name of this hook will become 'activate_sampleplugin/sample.php'.
     492 *
    492493 * When the plugin consists of only one file and is (as by default) located at
    493494 * <tt>wp-content/plugin/sample.php</tt> the name of this hook will be
    494495 * 'activate_sample.php'.
     
    500501 * @access private
    501502 *
    502503 * @param string $file The filename of the plugin including the path.
    503  * @param string $function the function hooked to the 'activate_PLUGIN' action.
     504 * @param callback $function the function hooked to the 'activate_PLUGIN' action.
    504505 */
    505506function register_deactivation_hook($file, $function) {
    506507        $file = plugin_basename($file);
     
    508509}
    509510
    510511/**
    511  * _wp_call_all_hook() - Calls the 'all' hook, which will process the functions hooked into it.
     512 * Set the uninstallation hook for a plugin.
    512513 *
    513  * The 'all' hook passes all of the arguments or parameters that were used for the
    514  * hook, which this function was called for.
     514 * Registers the uninstall hook that will be called when the user clicks on the
     515 * uninstall link that calls for the plugin to uninstall itself. The link won't
     516 * be active unless the plugin hooks into the action.
    515517 *
    516  * This function is used internally for apply_filters(), do_action(), and do_action_ref_array()
    517  * and is not meant to be used from outside those functions. This function does not check for the
    518  * existence of the all hook, so it will fail unless the all hook exists prior to this function call.
     518 * @param string $file
     519 * @param callback $callback The callback to run when the hook is called.
     520 */
     521function register_uninstall_hook($file, $callback) {
     522        add_action('uninstall_' . plugin_basename($file), $function);
     523}
     524
     525/**
     526 * Calls the 'all' hook, which will process the functions hooked into it.
    519527 *
     528 * The 'all' hook passes all of the arguments or parameters that were used for
     529 * the hook, which this function was called for.
     530 *
     531 * This function is used internally for apply_filters(), do_action(), and
     532 * do_action_ref_array() and is not meant to be used from outside those
     533 * functions. This function does not check for the existence of the all hook, so
     534 * it will fail unless the all hook exists prior to this function call.
     535 *
    520536 * @package WordPress
    521537 * @subpackage Plugin
    522538 * @since 2.5
     
    540556}
    541557
    542558/**
    543  * _wp_filter_build_unique_id() - Build Unique ID for storage and retrieval
     559 * Build Unique ID for storage and retrieval.
    544560 *
    545561 * The old way to serialize the callback caused issues and this function is the
    546562 * solution. It works by checking for objects and creating an a new property in
     
    549565 *
    550566 * It also allows for the removal of actions and filters for objects after they
    551567 * change class properties. It is possible to include the property $wp_filter_id
    552  * in your class and set it to "null" or a number to bypass the workaround. However
    553  * this will prevent you from adding new classes and any new classes will overwrite
    554  * the previous hook by the same class.
     568 * in your class and set it to "null" or a number to bypass the workaround.
     569 * However this will prevent you from adding new classes and any new classes
     570 * will overwrite the previous hook by the same class.
    555571 *
    556  * Functions and static method callbacks are just returned as strings and shouldn't
    557  * have any speed penalty.
     572 * Functions and static method callbacks are just returned as strings and
     573 * shouldn't have any speed penalty.
    558574 *
    559575 * @package WordPress
    560576 * @subpackage Plugin
     
    596612                return $function[0].$function[1];
    597613}
    598614
    599 ?>
     615?>
     616 No newline at end of file