Skip to content

Instantly share code, notes, and snippets.

@enejb
Created April 23, 2013 20:57
Show Gist options
  • Save enejb/5447330 to your computer and use it in GitHub Desktop.
Save enejb/5447330 to your computer and use it in GitHub Desktop.
A WordPress plugin that would allow you to update a plugin folder without producing a alert box for the user
<?php
/**
* olt_rename_plugin function.
* removed the
* @access public
* @param mixed $old_plugin. (default: null)
* @param mixed $new_plugin. (default: null)
* @param bool $add_to_network. (default: false)
* @return void
*/
function olt_rename_plugin( $old_plugin = null, $new_plugin = null, $sitewide = false ) {
$current = get_option( 'active_plugins' );
$network_active_plugins = (array) get_site_option( 'active_sitewide_plugins', array() );
$network_plugins = is_multisite() ? wp_get_active_network_plugins() : false;
$old_plugin = plugin_basename( trim( $old_plugin ) );
$new_plugin = plugin_basename( trim( $new_plugin ) );
$update_current = false;
$update_network_current = false;
$old_plugin_gone = false;
// don't remove the plugin if the file exists
if( !file_exists( WP_PLUGIN_DIR . '/' . $old_plugin ) ):
// remove the old plugin
if( !empty( $old_plugin ) && in_array( $old_plugin, $current ) && !$sitewide):
$current_count = 0;
foreach($current as $current_plugin):
if( $current_plugin == $old_plugin )
$remove_count = $current_count;
$current_count++;
endforeach;
if( isset($remove_count) ):
unset($current[$remove_count]);
unset($current[$remove_count]);
$update_current = true;
//var_dump('plugin file doesnt exits any more and was removed');
endif;
endif;
// remove the in network plugin
if( !empty( $old_plugin ) && $sitewide):
$remove_network_plugin = false;
foreach($network_active_plugins as $plugin => $time):
if($old_plugin == $plugin)
$remove_network_plugin = true;
endforeach;
if($remove_network_plugin):
unset($network_active_plugins[$old_plugin]);
$update_network_current = true;
endif;
endif; // end of remove the in network plugin
//var_dump('plugin file doesnt exits any more and was removed');
$old_plugin_gone = true;
endif; // end of is file still exits check
// var_dump( $old_plugin_gone, $network_active_plugins,$current , $update_current, "<br />");
// only activate the plugin if the old plugin is gone and the update current is set to true
if( $old_plugin_gone ):
// active the new plugin
if ( ! validate_file( $new_plugin ) // $plugin must validate as file
&& '.php' == substr( $new_plugin, -4 ) // $plugin must end with '.php'
&& file_exists( WP_PLUGIN_DIR . '/' . $new_plugin ) // $plugin must exist
// not already included as a network plugin
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $new_plugin, $network_plugins ) )
&& !empty( $new_plugin ) // plugin exists as a string
&& !in_array( $new_plugin, $current ) // is not enavled already
&& !$sitewide
): // plugin not active
$current[] = $new_plugin;
sort( $current );
// var_dump($current);
// var_dump('activated the plugin:'. $new_plugin);
// do_action( 'activate_plugin', trim( $new_plugin_name ) );
$update_current = true;
//do_action( 'activate_' . trim( $new_plugin_name ) );
// do_action( 'activated_plugin', trim( $new_plugin_name ) );
// add the plugin network wide
elseif( ! validate_file( $new_plugin ) // $plugin must validate as file
&& '.php' == substr( $new_plugin, -4 ) // $plugin must end with '.php'
&& file_exists( WP_PLUGIN_DIR . '/' . $new_plugin ) // $plugin must exist
// not already included as a network plugin
&& ( ! $network_plugins || ! in_array( WP_PLUGIN_DIR . '/' . $new_plugin, $network_plugins ) )
&& !empty( $new_plugin ) // plugin exists as a string
&& $sitewide ):
$network_active_plugins[$new_plugin] = time();
$update_network_current = true;
endif;
else:
// var_dump('Nothing happends');
endif; // end of old plugin gone is gone
if( $update_current ):
update_option( 'active_plugins', $current );
// var_dump($current);
endif;
if( $update_network_current ):
update_site_option( 'active_sitewide_plugins', $network_active_plugins );
// var_dump($network_active_plugins);
endif;
return null;
}
/* not tested yet */
function olt_add_plugin($new_plugin){
olt_rename_plugin( null , $old_plugin );
}
/* not tested yet */
function olt_remove_plugin( $new_plugin, $sitewide=false ){
olt_rename_plugin( $old_plugin, null, $sitewide );
}
// === how to rename the plugin ===
// 1. edit this file and add the rule for renaming save the changes
// olt_rename_plugin( 'tabs-shortcode/tabs-shotcode.php', 'tabs-shortcode/tabs-shortcode.php', true );
// olt_rename_plugin( 'tabs-shortcode/tabs-shotcode.php', 'tabs-shortcode/tabs-shortcode.php' );
// 2. update the plugin file
// - upload the new plugin
// - delete the old plugin file
// the rules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment