I have a simple dependency check at the start of some plugins – which check if another plugin is installed and active (using class_exists()
).
This works well in the normal WordPress flow – as the plugin that includes the required class is set to instantiate earlier and I also re-order the available plugins so this plugin is loaded first – like this:
/**
* Force this plugin to load first
*
* @since 0.1
* @return void
*/
public function load_this_plugin_first()
{
// grab plugin directory + file name ##
$path = plugin_basename(dirname(__FILE__)).'/'.basename(__FILE__);
// grab the list of active plugins ##
if ( $plugins = get_option( 'active_plugins' ) ) {
// check if out plugin is in there ##
if ( $key = array_search( $path, $plugins ) ) {
// push it to the top of the list ##
array_splice( $plugins, $key, 1 );
array_unshift( $plugins, $path );
update_option( 'active_plugins', $plugins );
}
}
}
This all works well until I need to update a plugin – then the other plugins are all deactivated – as all the class_exists()
checks return false.
So, I’d like to know if there is a constant – such as DOING_AUTOSAVE for plugin updates – something like DOING_PLUGIN_UPDATE?
Instead of re-ordering your plugins to load in a specific order, have each of them that requires some other plugin to be loaded to defer their initialization until the “plugins_loaded” action hook.
In other words, when a plugin with a dependency is included and ran, it shouldn’t do anything directly. Instead, it should have functions, and then add_action calls to hook those functions to plugins_loaded. That function that is called later can do the class_exists check and act accordingly.
How about using WP_INSTALLING? – this check is placed above the main plugin Class…
Then the actual plugin class is below: