Make WordPress Core

Changeset 8585


Ignore:
Timestamp:
08/07/2008 11:39:27 PM (16 years ago)
Author:
ryan
Message:

Plugin uninstall hooks from santosj. fixes #5625

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/plugin.php

    r8514 r8585  
    158158}
    159159
    160 function is_plugin_active($plugin){
     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 */
     168function is_plugin_active($plugin) {
    161169    return in_array($plugin, get_option('active_plugins'));
    162170}
     
    278286
    279287    foreach( $plugins as $plugin_file ) {
     288        // Run Uninstall hook
     289        if ( is_uninstallable_plugin( $plugin_file ) )
     290            uninstall_plugin($plugin_file);
     291
    280292        $this_plugin_dir = trailingslashit( dirname($plugins_dir . $plugin_file) );
    281293        // If plugin is in its own directory, recursively delete the directory.
     
    327339
    328340    return 0;
     341}
     342
     343/**
     344 * Whether the plugin can be uninstalled.
     345 *
     346 * @since {@internal Version Unknown}}
     347 *
     348 * @param string $plugin Plugin path to check.
     349 * @return bool Whether plugin can be uninstalled.
     350 */
     351function is_uninstallable_plugin($plugin) {
     352    $file = plugin_basename($plugin);
     353
     354    $uninstallable_plugins = (array) get_option('uninstall_plugins');
     355    if ( isset( $uninstallable_plugins[$file] ) || file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) )
     356        return true;
     357
     358    return false;
     359}
     360
     361/**
     362 * Uninstall a single plugin.
     363 *
     364 * Calls the uninstall hook, if it is available.
     365 *
     366 * @param string $plugin Relative plugin path from Plugin Directory.
     367 */
     368function uninstall_plugin($plugin) {
     369    $file = plugin_basename($plugin);
     370
     371    $uninstallable_plugins = (array) get_option('uninstall_plugins');
     372    if ( file_exists( WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php' ) ) {
     373        if ( isset( $uninstallable_plugins[$file] ) ) {
     374            unset($uninstallable_plugins[$file]);
     375            update_option('uninstall_plugins', $uninstallable_plugins);
     376        }
     377        unset($uninstallable_plugins);
     378
     379        define('WP_UNINSTALL_PLUGIN', $file);
     380        include WP_PLUGIN_DIR . '/' . dirname($file) . '/uninstall.php';
     381
     382        return true;
     383    }
     384
     385    if ( isset( $uninstallable_plugins[$file] ) ) {
     386        $callable = $uninstallable_plugins[$file];
     387        unset($uninstallable_plugins[$file]);
     388        update_option('uninstall_plugins', $uninstallable_plugins);
     389        unset($uninstallable_plugins);
     390
     391        include WP_PLUGIN_DIR . '/' . $file;
     392
     393        add_action( 'uninstall_' . $file, $callable );
     394        do_action( 'uninstall_' . $file );
     395    }
    329396}
    330397
  • trunk/wp-includes/plugin.php

    r8572 r8585  
    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
     
    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
     
    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
     
    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
     
    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 *
     
    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
     
    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
     
    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>.
     
    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
     
    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
     
    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
     
    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
     
    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.
     
    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
     
    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) {
     
    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
     
    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) {
     
    509510
    510511/**
    511  * _wp_call_all_hook() - Calls the 'all' hook, which will process the functions hooked into it.
    512  *
    513  * The 'all' hook passes all of the arguments or parameters that were used for the
    514  * hook, which this function was called for.
    515  *
    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.
     512 * Set the uninstallation hook for a plugin.
     513 *
     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.
     517 *
     518 * The plugin should not run arbitrary code outside of functions, when
     519 * registering the uninstall hook. In order to run using the hook, the plugin
     520 * will have to be included, which means that any code laying outside of a
     521 * function will be run during the uninstall process. The plugin should not
     522 * hinder the uninstall process.
     523 *
     524 * If the plugin can not be written without running code within the plugin, then
     525 * the plugin should create a file named 'uninstall.php' in the base plugin
     526 * folder. This file will be called, if it exists, during the uninstall process
     527 * bypassing the uninstall hook. The plugin, when using the 'uninstall.php'
     528 * should always check for the 'WP_UNINSTALLING_PLUGIN' constant, before
     529 * executing.
     530 *
     531 * @param string $file
     532 * @param callback $callback The callback to run when the hook is called.
     533 */
     534function register_uninstall_hook($file, $callback) {
     535    // The option should not be autoloaded, because it is not needed in most
     536    // cases. Emphasis should be put on using the 'uninstall.php' way of
     537    // uninstalling the plugin.
     538    $uninstallable_plugins = (array) get_option('uninstall_plugins');
     539    $uninstallable_plugins[plugin_basename($file)] = $callback;
     540    update_option('uninstall_plugins', $uninstallable_plugins);
     541}
     542
     543/**
     544 * Calls the 'all' hook, which will process the functions hooked into it.
     545 *
     546 * The 'all' hook passes all of the arguments or parameters that were used for
     547 * the hook, which this function was called for.
     548 *
     549 * This function is used internally for apply_filters(), do_action(), and
     550 * do_action_ref_array() and is not meant to be used from outside those
     551 * functions. This function does not check for the existence of the all hook, so
     552 * it will fail unless the all hook exists prior to this function call.
    519553 *
    520554 * @package WordPress
     
    541575
    542576/**
    543  * _wp_filter_build_unique_id() - Build Unique ID for storage and retrieval
     577 * Build Unique ID for storage and retrieval.
    544578 *
    545579 * The old way to serialize the callback caused issues and this function is the
     
    550584 * It also allows for the removal of actions and filters for objects after they
    551585 * 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.
    555  *
    556  * Functions and static method callbacks are just returned as strings and shouldn't
    557  * have any speed penalty.
     586 * in your class and set it to "null" or a number to bypass the workaround.
     587 * However this will prevent you from adding new classes and any new classes
     588 * will overwrite the previous hook by the same class.
     589 *
     590 * Functions and static method callbacks are just returned as strings and
     591 * shouldn't have any speed penalty.
    558592 *
    559593 * @package WordPress
Note: See TracChangeset for help on using the changeset viewer.