spivurno
1/17/2013 - 11:04 PM

gistfile1.php

<?php

/**
 * Allows activation of plugins via WP Bulter using the "activate" keyword (ie "activate Gravity Forms")
 * 
 */
add_filter('wp_butler_ajax_keyword_actions', 'wp_butler_switch_plugins_action');
function wp_butler_switch_plugins_action($term_and_actions) {
    
    list($term, $actions) = $term_and_actions;
    
    $term_words = explode( ' ', $_REQUEST['term'] );
    $keyword = $term_words[0];
    
    if($keyword != 'activate')
        return $term_and_actions;
    
    array_shift( $term_words );
    $term = implode( ' ', $term_words );
    
    $plugins = get_plugins();
    $plugin_matches = array();
    
    foreach($plugins as $plugin_file => $plugin) {
        if ( preg_match( '/' . $term . '/i', $plugin['Title'] ) && !is_plugin_active($plugin_file) ) {
            $actions[] = array(
                'label' => html_entity_decode( "{$plugin['Title']} v{$plugin['Version']}", ENT_QUOTES, get_option( 'blog_charset' ) ),
                'url' => add_query_arg( '_wpnonce', wp_create_nonce( 'activate-plugin_' . $plugin_file ), "plugins.php?action=activate&plugin={$plugin_file}&deactivate_existing=1")
            );
        }
    }
    
    return array($term, $actions);
}

/**
 * Optionally support deactivating any active plugin with the same name as the plugin being activated.
 * This is useful when working on different branches of the same plugin and needing to switch between them
 * more quickly.
 * 
 */
add_action('init', 'maybe_deactivate_existing_plugin');
function maybe_deactivate_existing_plugin() {
    
    if(!isset($_REQUEST['deactivate_existing']))
        return;
        
    require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
    
    $plugins = get_plugins();
    $current_plugin_file = $_REQUEST['plugin'];
    $current_plugin = $plugins[$current_plugin_file];
    
    foreach($plugins as $plugin_file => $plugin) {
        
        $not_current_plugin = $plugin_file != $current_plugin_file;
        $has_same_name      = $plugin['Title'] == $current_plugin['Title'];
        $is_active          = is_plugin_active($plugin_file);
        
        if( $not_current_plugin  && $has_same_name && $is_active ) {
            deactivate_plugins( $plugin_file, false, is_network_admin() );
            wp_redirect(remove_query_arg('deactivate_existing'));
            exit;
        }
        
    }
    
}