Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
/*
Plugin Name: Eliminate Plugin Clutter
Plugin URI: http://mikeschinkel.com/wordpress-plugins/eliminate-plugin-clutter/
Description: Provides an "Archiving" feature that allows you to clear currently unused plugins from view without having to delete them yet having all of your archived plugins only one "Unarchive" click away.
Version: 0.1
Author: Mike Schinkel
Author URI: http://mikeschinkel.com
*/
/*
* TODO: Add an undo feature
* TODO: Implement bulk actions.
* TODO: Enhance /wp-admin/plugins.php to support more hooks
* TODO: Disable archived plugins from triggering update notifications
*
*/
add_action('plugin_action_links','add_archive_plugin_action_link',10,4);
function add_archive_plugin_action_link($actions, $plugin_file, $plugin_data, $context) {
global $active_plugins;
$archived_plugins = get_option('archived_plugins');
if (!is_array($archived_plugins))
$archived_plugins = array();
$archived_keys = array_flip($archived_plugins);
$action = (isset($archived_keys[$plugin_file]) ? 'unarchive' : 'archive');
if (!isset($active_plugins[$plugin_file]) || $action=='unarchive') {
$actions['archive'] = '<a href="' . wp_nonce_url(
"plugins.php?action={$action}&amp;plugin={$plugin_file}&amp;plugin_status={$context}&amp;paged={$page}",
"{$action}-plugin_{$plugin_file}"
) .
'" title="' . __(ucfirst($action) . ' this plugin') .
'" class="'.$action.'">' . __(ucfirst($action)) . '</a>';
}
return $actions;
}
add_filter('plugin_actions','add_bulk_archive_unarchive_plugin_actions',10,2);
function add_bulk_archive_unarchive_plugin_actions($actions,$context) {
global $pagenow;
if ($pagenow=='plugins.php') {
if (isset($_GET['plugin_status']) || $_GET['plugin_status']=='archived')
$actions['unarchive'] = 'Unarchive';
else
$actions['archive'] = 'Archive';
}
return $actions;
}
add_filter('all_plugins','filter_archived_plugins');
function filter_archived_plugins($plugins) {
global $pagenow;
global $archived_plugins;
global $archived_and_non_archived_plugins;
$archived_plugins = array();
$archived_and_non_archived_plugins = $plugins;
$saved_archived_plugins = get_option('archived_plugins');
if (is_array($saved_archived_plugins))
foreach($saved_archived_plugins as $plugin_slug) {
$archived_plugins[$plugin_slug] = $plugins[$plugin_slug];
unset($plugins[$plugin_slug]);
}
if ($pagenow=='plugins.php' && isset($_GET['plugin_status']) && $_GET['plugin_status']=='archived') {
$plugins = $archived_plugins;
}
return $plugins;
}
add_action('pre_current_active_plugins','start_buffering_for_plugins_status');
function start_buffering_for_plugins_status($all_plugins) {
ob_start();
}
add_action('admin_footer','end_buffering_for_plugins_status');
function end_buffering_for_plugins_status() {
global $pagenow;
global $archived_plugins;
global $archived_and_non_archived_plugins;
$all_count = count($archived_and_non_archived_plugins);
$html = ob_get_clean();
$archive_count = count($archived_plugins);
if ($pagenow=='plugins.php') {
$archived_link = '<li><a href="plugins.php?plugin_status=archived">Archived <span class="count">'."({$archive_count})</span></a></li>";
if ($archive_count==0 && isset($_GET['plugin_status']) && in_array($_GET['plugin_status'],array('archived','unarchived'))) {
$html = str_replace('All <span class="count">(0)</span></a></li></ul>','All <span class="count">('.$all_count.')</span></a> | </li>'.$archived_link.'</ul>',$html);
} else {
if ($_GET['plugin_status']=='archived') {
$plugins = $archived_plugins;
$statuses = '<li><a href="plugins.php?plugin_status=all">All <span class="count">'."({$all_count})</span></a> | </li>" . $archived_link;
} else {
$statuses = preg_replace('#^.*?<ul class="subsubsub">(.*?)</ul>.*$#is',"\$1",$html);
preg_match_all('#<li>(.*?)</li>#is',$statuses,$match);
$statuses = '<li>'.implode('</li><li>',$match[1]). (count($archived_plugins)==0 ? '</li>' : " | </li>{$archived_link}");;
}
$html = preg_replace('#(<ul class="subsubsub">)(.*?)(</ul>\s*?<div class="tablenav">)#is',"\$1{$statuses}\$3",$html);
}
}
echo $html;
}
add_action('admin_init','archive_unarchive_plugin');
function archive_unarchive_plugin() {
global $pagenow;
if ($pagenow=='plugins.php') {
if (
(isset($_POST['action']) && in_array($_POST['action'],array('archive-selected','unarchive-selected'))) ||
(isset($_POST['action2']) && in_array($_POST['action2'],array('archive-selected','unarchive-selected')))
) {
wp_die('Sorry, not currently implemented. Please use your browser\'s "Back" feature.');
exit;
} else if (isset($_GET['action']) && in_array($_GET['action'],array('archive','unarchive')) && isset($_GET['plugin'])) {
$plugin = $_GET['plugin'];
$action = $_GET['action'];
$status = ($action=='unarchive' ? 'archived' : 'all');
check_admin_referer("{$action}-plugin_{$plugin}");
$archived_plugins = get_option('archived_plugins');
switch ($action) {
case 'archive':
$archived_plugins[] = $plugin;
break;
case 'unarchive':
$archived_keys = array_flip($archived_plugins);
unset($archived_plugins[$archived_keys[$plugin]]);
break;
default:
}
update_option('archived_plugins',$archived_plugins);
wp_redirect("plugins.php?plugin_status={$status}&{$action}d=true");
exit;
}
}
}
// This next function is renamed (with three leading underscores) and is my modified version/future patch for /wp-admin/plugins.php
/**
* @ignore
*
* @param string $context
*/
function ___print_plugin_actions($context, $field_name = 'action' ) {
if ( in_array( $context, array( 'mustuse', 'dropins' ) ) )
return;
$actions = array('' => 'Bulk Actions');
if ( 'active' != $context )
$actions['activate'] = 'Activate';
if ( is_multisite() && 'network' != $context )
$actions['network-activate'] = 'Network Activate';
if ( 'inactive' != $context && 'recent' != $context )
$actions['deactivate'] = 'Deactivate';
if ( current_user_can( 'update_plugins' ) )
$actions['update'] = 'Upgrade';
if ( current_user_can('delete_plugins') && ( 'active' != $context ) )
$actions['delete'] = 'Delete';
$actions = apply_filters('plugin_actions',$actions,$context);
$options = array();
foreach($actions as $key => $action) {
$value = (empty($key) ? '' : "{$key}-") . 'selected';
$options[] = "<option value=\"{$value}\">" . __($action) . '</option>';
}
?>
<div class="alignleft actions">
<select name="<?php echo $field_name; ?>">
<?php echo implode("\n",$options); ?>
</select>
<input type="submit" name="doaction_active" value="<?php esc_attr_e('Apply'); ?>" class="button-secondary action" />
<?php if ( 'recent' == $context ) : ?>
<input type="submit" name="clear-recent-list" value="<?php esc_attr_e('Clear List') ?>" class="button-secondary" />
<?php endif; ?>
</div>
<?php
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment