Make WordPress Core

Changeset 6580


Ignore:
Timestamp:
01/09/2008 09:37:27 AM (17 years ago)
Author:
ryan
Message:

Plugin reactivation. Props dougal. see #4176

Location:
trunk/wp-admin
Files:
2 edited

Legend:

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

    r6354 r6580  
    8787}
    8888
    89 function activate_plugin($plugin) {
     89function activate_plugin($plugin, $redirect = '') {
    9090        $current = get_option('active_plugins');
    9191        $plugin = trim($plugin);
    9292
    93         if ( validate_file($plugin) )
    94             return new WP_Error('plugin_invalid', __('Invalid plugin.'));
    95         if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
    96             return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
    97 
    98         if (!in_array($plugin, $current)) {
    99             wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), 'plugins.php?error=true&plugin=' . $plugin)); // we'll override this later if the plugin can be included without fatal error
     93        $valid = validate_plugin($plugin);
     94        if ( is_wp_error($valid) )
     95            return $valid;
     96
     97        if ( !in_array($plugin, $current) ) {
     98            if ( !empty($redirect) )
     99                wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect)); // we'll override this later if the plugin can be included without fatal error
    100100            ob_start();
    101101            @include(ABSPATH . PLUGINDIR . '/' . $plugin);
     
    113113    $current = get_option('active_plugins');
    114114
    115     if(!is_array($plugins))
     115    if ( !is_array($plugins) )
    116116        $plugins = array($plugins);
    117117
    118     foreach($plugins as $plugin) {
     118    foreach ( $plugins as $plugin ) {
    119119        array_splice($current, array_search( $plugin, $current), 1 ); // Array-fu!
    120120        do_action('deactivate_' . trim( $plugin ));
     
    126126function deactivate_all_plugins() {
    127127    $current = get_option('active_plugins');
     128    if ( empty($current) )
     129        return;
     130
    128131    deactivate_plugins($current);
     132
     133    update_option('deactivated_plugins', $current);
     134}
     135
     136function reactivate_all_plugins($redirect = '') {
     137    $plugins = get_option('deactivated_plugins');
     138
     139    if ( empty($plugins) )
     140        return;
     141
     142    if ( !empty($redirect) )
     143        wp_redirect(add_query_arg('_error_nonce', wp_create_nonce('plugin-activation-error_' . $plugin), $redirect));
     144
     145    $errors = array();
     146    foreach ( (array) $plugins as $plugin ) {
     147        $result = activate_plugin($plugin);
     148        if ( is_wp_error($result) )
     149            $errors[$plugin] = $result;
     150    }
     151
     152    delete_option('deactivated_plugins');
     153
     154    if ( !empty($errors) )
     155        return new WP_Error('plugins_invalid', __('One of the plugins is invalid.'), $errors);
     156
     157    return true;
     158}
     159
     160function validate_active_plugins() {
     161    $check_plugins = get_option('active_plugins');
     162
     163    // Sanity check.  If the active plugin list is not an array, make it an
     164    // empty array.
     165    if ( !is_array($check_plugins) ) {
     166        update_option('active_plugins', array());
     167        return;
     168    }
     169
     170    // If a plugin file does not exist, remove it from the list of active
     171    // plugins.
     172    foreach ( $check_plugins as $check_plugin ) {
     173        if ( !file_exists(ABSPATH . PLUGINDIR . '/' . $check_plugin) ) {
     174            $current = get_option('active_plugins');
     175            $key = array_search($check_plugin, $current);
     176            if ( false !== $key && NULL !== $key ) {
     177                unset($current[$key]);
     178                update_option('active_plugins', $current);
     179            }
     180        }
     181    }
     182}
     183
     184function validate_plugin($plugin) {
     185    if ( validate_file($plugin) )
     186        return new WP_Error('plugin_invalid', __('Invalid plugin.'));
     187    if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
     188        return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
     189
     190    return 0;
    129191}
    130192
  • trunk/wp-admin/plugins.php

    r6259 r6580  
    33
    44if ( isset($_GET['action']) ) {
    5     if ('activate' == $_GET['action']) {
     5    if ( 'activate' == $_GET['action'] ) {
    66        check_admin_referer('activate-plugin_' . $_GET['plugin']);
    7         $result = activate_plugin($_GET['plugin']);
    8         if( is_wp_error( $result ) )
     7        $result = activate_plugin($_GET['plugin'], 'plugins.php?error=true&plugin=' . $plugin);
     8        if ( is_wp_error( $result ) )
    99            wp_die( $result->get_error_message() );
    1010        wp_redirect('plugins.php?activate=true'); // overrides the ?error=true one above
    11     } elseif ('error_scrape' == $_GET['action']) {
     11    } elseif ( 'error_scrape' == $_GET['action'] ) {
    1212        $plugin = trim($_GET['plugin']);
    1313        check_admin_referer('plugin-activation-error_' . $plugin);
    14         if ( validate_file($plugin) )
    15             wp_die(__('Invalid plugin.'));
    16         if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
    17             wp_die(__('Plugin file does not exist.'));
     14        $valid = validate_plugin($plugin);
     15        if ( is_wp_error($valid) )
     16            wp_die($valid);
    1817        include(ABSPATH . PLUGINDIR . '/' . $plugin);
    19     } elseif ('deactivate' == $_GET['action']) {
     18    } elseif ( 'deactivate' == $_GET['action'] ) {
    2019        check_admin_referer('deactivate-plugin_' . $_GET['plugin']);
    2120        deactivate_plugins($_GET['plugin']);
    2221        wp_redirect('plugins.php?deactivate=true');
    23     } elseif ($_GET['action'] == 'deactivate-all') {
     22    } elseif ( 'deactivate-all' == $_GET['action'] ) {
    2423        check_admin_referer('deactivate-all');
    2524        deactivate_all_plugins();
    2625        wp_redirect('plugins.php?deactivate-all=true');
     26    } elseif ('reactivate-all' == $_GET['action']) {
     27        check_admin_referer('reactivate-all');
     28        reactivate_all_plugins('plugins.php?errors=true');
     29        wp_redirect('plugins.php?reactivate-all=true'); // overrides the ?error=true one above
    2730    }
     31
    2832    exit;
    2933}
     
    3236require_once('admin-header.php');
    3337
    34 // Clean up options
    35 // If any plugins don't exist, axe 'em
     38validate_active_plugins();
    3639
    37 $check_plugins = get_option('active_plugins');
    38 
    39 // Sanity check.  If the active plugin list is not an array, make it an
    40 // empty array.
    41 if ( !is_array($check_plugins) ) {
    42     $check_plugins = array();
    43     update_option('active_plugins', $check_plugins);
    44 }
    45 
    46 // If a plugin file does not exist, remove it from the list of active
    47 // plugins.
    48 foreach ($check_plugins as $check_plugin) {
    49     if (!file_exists(ABSPATH . PLUGINDIR . '/' . $check_plugin)) {
    50             $current = get_option('active_plugins');
    51             $key = array_search($check_plugin, $current);
    52             if ( false !== $key && NULL !== $key ) {
    53                 unset($current[$key]);
    54                 update_option('active_plugins', $current);
    55             }
    56     }
    57 }
    5840?>
    5941
     
    6850    ?>
    6951    </div>
     52<?php elseif ( isset($_GET['errors']) ) : ?>
     53    <div id="message" class="updated fade"><p><?php _e('Some plugins could not be reactivated because they triggered a <strong>fatal error</strong>.') ?></p></div>
    7054<?php elseif ( isset($_GET['activate']) ) : ?>
    7155    <div id="message" class="updated fade"><p><?php _e('Plugin <strong>activated</strong>.') ?></p></div>
     
    7458<?php elseif (isset($_GET['deactivate-all'])) : ?>
    7559    <div id="message" class="updated fade"><p><?php _e('All plugins <strong>deactivated</strong>.'); ?></p></div>
     60<?php elseif (isset($_GET['reactivate-all'])) : ?>
     61    <div id="message" class="updated fade"><p><?php _e('All plugins <strong>reactivated</strong>.'); ?></p></div>
    7662<?php endif; ?>
    7763
     
    149135<tr>
    150136    <td colspan="3">&nbsp;</td>
    151     <td colspan="2" style="width:12em;"><a href="<?php echo wp_nonce_url('plugins.php?action=deactivate-all', 'deactivate-all'); ?>" class="delete"><?php _e('Deactivate All Plugins'); ?></a></td>
     137    <td colspan="2" style="width:12em;">
     138    <?php
     139    $active = get_option('active_plugins');
     140    $inactive = get_option('deactivated_plugins');
     141    if ( !empty($active) ) {
     142    ?>
     143    <a href="<?php echo wp_nonce_url('plugins.php?action=deactivate-all', 'deactivate-all'); ?>" class="delete"><?php _e('Deactivate All Plugins'); ?></a>
     144    <?php
     145    } elseif ( empty($active) && !empty($inactive) ) {
     146    ?>
     147    <a href="<?php echo wp_nonce_url('plugins.php?action=reactivate-all', 'reactivate-all'); ?>" class="delete"><?php _e('Reactivate All Plugins'); ?></a>
     148    <?php
     149    } // endif active/inactive plugin check
     150    ?>
    152151</tr>
    153152
Note: See TracChangeset for help on using the changeset viewer.