2010-11-27, 11:05 PM
I have started using a generic cache entry that hopefully plugin authors will start to piggy back off of. In my plugins from now on, I will be including the following code:
Basically, it will try to read a cache entry called "plugin_versions" where the key is the plugin name and the value is the version code. Obviously you need to replace <plugin_name> with the 'codename' of your plugin. This is the base filename of your plugin and the same as you would use for the _install and _info and _activate functions.
Upon start of activation (and install) the get_version function will be called via a hook.
Upon end of the activation (and install) the set_version function will be called via a hook.
Upon uninstall and not just deactivation, hence the $install_uninstall conditional, unset_version function will clear the plugin_cache of the plugin's entry, leaving all of the other plugin versions in place. Handling only uninstall allows users to deactivate and maintain the 'old' version number after overwritting the plugin file with the new version. Since uninstall should be removing all items modified by the plugin, upgrading is not an issue.
Once nice thing about this setup, is that it also adds 'old' and 'new' keys to the $plugin_versions array so the authors can compare versions and upgrade accordingly.
Can't use the existing 'plugins' cache entry as it is updated via core files and I want to avoid editing those.
Thoughts from other plugin authors?
actually I found that this is not working without some core file edits unfortunately.
the issue is that the hooks I am trying to use in the plugin are being called prior to including the plugin file so the functions do not exist at the time the ACP is trying to use them.
since the hooks are in the main body they get added upon plugin system startup, but the functions for the hooks are not available since the plugin file has not yet been included in the ACP code.
I actually think that this may be a bug, because why would you hook into your own or any other plugin before it is actually included in the system. One accesses hooks via plugins, so what cases would there be to use a hook before any plugins are included?
In \admin\modules\config\plugins.php, at or around line 339 find
and "cut" that text and "paste" it at the now line 355 to make this work (after the require_once line)
//handle version info
$plugins->add_hook("admin_config_plugins_activate", "<plugin_name>_get_version");
$plugins->add_hook("admin_config_plugins_activate_commit", "<plugin_name>_set_version");
$plugins->add_hook("admin_config_plugins_deactivate_commit", "<plugin_name>_unset_version");
/**
* Get plugin version from cache
*/
function <plugin_name>_get_version()
{
global $cache, $plugin_versions;
//get version from this plugin file
$<plugin_name>_info = <plugin_name>_info();
//load plugin version cache
$plugin_versions = $cache->read('plugin_versions');
if(!is_array($plugin_versions))
{
$plugin_versions = array();
}
//if plugin name already exists, get old version
if(isset($plugin_versions['<plugin_name>']))
{
$plugin_versions['old'] = $plugin_versions['<plugin_name>'];
}
//set new version variable
$plugin_versions['new'] = $<plugin_name>_info['version'];
}
/**
* Set plugin version in cache
*/
function <plugin_name>_set_version()
{
global $cache, $plugin_versions;
//update cache to latest version regardless of activate or install
$plugin_versions['<plugin_name>'] = $plugin_versions['new'];
unset($plugin_verisons['old']);
unset($plugin_verisons['new']);
$cache->update('plugin_versions', $plugin_versions);
}
/**
* Remove (unset) plugin version in cache
*/
function <plugin_name>_unset_version()
{
global $cache, $plugin_versions, $install_uninstall;
//cleanup plugin version cache if uninstalled
if($install_uninstall)
{
unset($plugin_verisons['old']);
unset($plugin_verisons['new']);
unset($plugin_verisons['<plugin_name>']);
$cache->update('plugin_versions', $plugin_versions);
}
}
Basically, it will try to read a cache entry called "plugin_versions" where the key is the plugin name and the value is the version code. Obviously you need to replace <plugin_name> with the 'codename' of your plugin. This is the base filename of your plugin and the same as you would use for the _install and _info and _activate functions.
Upon start of activation (and install) the get_version function will be called via a hook.
Upon end of the activation (and install) the set_version function will be called via a hook.
Upon uninstall and not just deactivation, hence the $install_uninstall conditional, unset_version function will clear the plugin_cache of the plugin's entry, leaving all of the other plugin versions in place. Handling only uninstall allows users to deactivate and maintain the 'old' version number after overwritting the plugin file with the new version. Since uninstall should be removing all items modified by the plugin, upgrading is not an issue.
Once nice thing about this setup, is that it also adds 'old' and 'new' keys to the $plugin_versions array so the authors can compare versions and upgrade accordingly.
Can't use the existing 'plugins' cache entry as it is updated via core files and I want to avoid editing those.
Thoughts from other plugin authors?
actually I found that this is not working without some core file edits unfortunately.
the issue is that the hooks I am trying to use in the plugin are being called prior to including the plugin file so the functions do not exist at the time the ACP is trying to use them.
since the hooks are in the main body they get added upon plugin system startup, but the functions for the hooks are not available since the plugin file has not yet been included in the ACP code.
I actually think that this may be a bug, because why would you hook into your own or any other plugin before it is actually included in the system. One accesses hooks via plugins, so what cases would there be to use a hook before any plugins are included?
In \admin\modules\config\plugins.php, at or around line 339 find
if($mybb->input['action'] == "activate")
{
$plugins->run_hooks("admin_config_plugins_activate");
}
else
{
$plugins->run_hooks("admin_config_plugins_deactivate");
}
and "cut" that text and "paste" it at the now line 355 to make this work (after the require_once line)