activate_plugin(), deactivate_plugins(), and deactivate_all_plugins() from Quandary. fixes #5210

git-svn-id: http://svn.automattic.com/wordpress/trunk@6259 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2007-10-17 17:14:58 +00:00
parent 36caa9623c
commit 2c5016e8fa
2 changed files with 47 additions and 28 deletions

View File

@ -86,6 +86,48 @@ function get_plugins() {
return $wp_plugins; return $wp_plugins;
} }
function activate_plugin($plugin) {
$current = get_option('active_plugins');
$plugin = trim($plugin);
if ( validate_file($plugin) )
return new WP_Error('plugin_invalid', __('Invalid plugin.'));
if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
return new WP_Error('plugin_not_found', __('Plugin file does not exist.'));
if (!in_array($plugin, $current)) {
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
ob_start();
@include(ABSPATH . PLUGINDIR . '/' . $plugin);
$current[] = $plugin;
sort($current);
update_option('active_plugins', $current);
do_action('activate_' . $plugin);
ob_end_clean();
}
return null;
}
function deactivate_plugins($plugins) {
$current = get_option('active_plugins');
if(!is_array($plugins))
$plugins = array($plugins);
foreach($plugins as $plugin) {
array_splice($current, array_search( $plugin, $current), 1 ); // Array-fu!
do_action('deactivate_' . trim( $plugin ));
}
update_option('active_plugins', $current);
}
function deactivate_all_plugins() {
$current = get_option('active_plugins');
deactivate_plugins($current);
}
// //
// Menu // Menu
// //

View File

@ -4,22 +4,9 @@ require_once('admin.php');
if ( isset($_GET['action']) ) { if ( isset($_GET['action']) ) {
if ('activate' == $_GET['action']) { if ('activate' == $_GET['action']) {
check_admin_referer('activate-plugin_' . $_GET['plugin']); check_admin_referer('activate-plugin_' . $_GET['plugin']);
$current = get_option('active_plugins'); $result = activate_plugin($_GET['plugin']);
$plugin = trim($_GET['plugin']); if( is_wp_error( $result ) )
if ( validate_file($plugin) ) wp_die( $result->get_error_message() );
wp_die(__('Invalid plugin.'));
if ( ! file_exists(ABSPATH . PLUGINDIR . '/' . $plugin) )
wp_die(__('Plugin file does not exist.'));
if (!in_array($plugin, $current)) {
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
ob_start();
@include(ABSPATH . PLUGINDIR . '/' . $plugin);
$current[] = $plugin;
sort($current);
update_option('active_plugins', $current);
do_action('activate_' . $plugin);
ob_end_clean();
}
wp_redirect('plugins.php?activate=true'); // overrides the ?error=true one above wp_redirect('plugins.php?activate=true'); // overrides the ?error=true one above
} elseif ('error_scrape' == $_GET['action']) { } elseif ('error_scrape' == $_GET['action']) {
$plugin = trim($_GET['plugin']); $plugin = trim($_GET['plugin']);
@ -31,21 +18,11 @@ if ( isset($_GET['action']) ) {
include(ABSPATH . PLUGINDIR . '/' . $plugin); include(ABSPATH . PLUGINDIR . '/' . $plugin);
} elseif ('deactivate' == $_GET['action']) { } elseif ('deactivate' == $_GET['action']) {
check_admin_referer('deactivate-plugin_' . $_GET['plugin']); check_admin_referer('deactivate-plugin_' . $_GET['plugin']);
$current = get_option('active_plugins'); deactivate_plugins($_GET['plugin']);
array_splice($current, array_search( $_GET['plugin'], $current), 1 ); // Array-fu!
update_option('active_plugins', $current);
do_action('deactivate_' . trim( $_GET['plugin'] ));
wp_redirect('plugins.php?deactivate=true'); wp_redirect('plugins.php?deactivate=true');
} elseif ($_GET['action'] == 'deactivate-all') { } elseif ($_GET['action'] == 'deactivate-all') {
check_admin_referer('deactivate-all'); check_admin_referer('deactivate-all');
$current = get_option('active_plugins'); deactivate_all_plugins();
foreach ($current as $plugin) {
array_splice($current, array_search($plugin, $current), 1);
do_action('deactivate_' . $plugin);
}
update_option('active_plugins', array());
wp_redirect('plugins.php?deactivate-all=true'); wp_redirect('plugins.php?deactivate-all=true');
} }
exit; exit;