我的用户已经安装了我的插件(我们称之为v6)。
V6版本的插件没有为upgrader_process_complete
注册处理程序。
在我的新版本中,我有upgrader_process_complete
注册做一些升级到我的数据库表。
然而,当用户使用update now
链接从插件页面升级时,似乎没有调用我的新版本的处理程序。
upgrader_process_complete
钩子在当前版本中运行,同时更新插件
假设你正在运行插件v6.0。
然后你就更新到6.1,这个版本包含了upgrader_process_complete
钩子。
下一个场景之前不会调用升级器钩子。
现在你运行的插件v6.1包含upgrader_process_complete
钩子从v6.1。
你刚刚更新到6.2,包含写ABC.txt文件的代码。
6.1的升级器钩子将被调用,而不是6.2。因此,这意味着将不会创建ABC.txt文件。
如果你正在运行插件v6.1,并且想要运行刚刚从6.2更新的新更新的代码,你必须添加一些像transient这样的东西来通知需要从新版本的代码更新。
这里是从我的插件。你可以根据MIT的许可使用。
<?php
class Upgrader
{
/**
* Display link or maybe redirect to manual update page.
*
* To understand more about new version of code, please read more on `updateProcessComplete()` method.
*
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/admin_notices Reference.
*/
public function redirectToUpdatePlugin()
{
if (get_transient('myplugin_updated') && current_user_can('update_plugins')) {
// if there is updated transient
// any background update process can be run here.
// write your new version of code that will be run after updated the plugin here.
}// endif;
}// redirectToUpdatePlugin
/**
* {@inheritDoc}
*/
public function registerHooks()
{
// on update/upgrade plugin completed. set transient and let `redirectToUpdatePlugin()` work.
add_action('upgrader_process_complete', [$this, 'updateProcessComplete'], 10, 2);
// on plugins loaded, background update the plugin with new version.
add_action('plugins_loaded', [$this, 'redirectToUpdatePlugin']);
}// registerHooks
/**
* After update plugin completed.
*
* This method will be called while running the current version of this plugin, not the new one that just updated.
* For example: You are running 1.0 and just updated to 2.0. The 2.0 version will not working here yet but 1.0 is working.
* So, any code here will not work as the new version. Please be aware!
*
* This method will add the transient to work again and will be called in `redirectToUpdatePlugin()` method.
*
* @link https://developer.wordpress.org/reference/hooks/upgrader_process_complete/ Reference.
* @link https://codex.wordpress.org/Plugin_API/Action_Reference/upgrader_process_complete Reference.
* @param 'WP_Upgrader $upgrader
* @param array $hook_extra
*/
public function updateProcessComplete('WP_Upgrader $upgrader, array $hook_extra)
{
if (is_array($hook_extra) && array_key_exists('action', $hook_extra) && array_key_exists('type', $hook_extra) && array_key_exists('plugins', $hook_extra)) {
if ($hook_extra['action'] == 'update' && $hook_extra['type'] == 'plugin' && is_array($hook_extra['plugins']) && !empty($hook_extra['plugins'])) {
$this_plugin = plugin_basename(MYPLUGIN_FILE);// MYPLUGIN_FILE is come from __FILE__ of the main plugin file.
foreach ($hook_extra['plugins'] as $key => $plugin) {
if ($this_plugin == $plugin) {
// if this plugin is in the updated plugins.
// set transient to let it run later. this transient will be called and run in `redirectToUpdatePlugin()` method.
set_transient('myplugin_updated', 1);
break;
}
}// endforeach;
unset($key, $plugin, $this_plugin);
}// endif update plugin and plugins not empty.
}// endif; $hook_extra
}// updateProcessComplete
}
请仔细阅读并修改以上代码。
在你的插件文件中,调用Upgrader->registerHooks()
方法。并在redirectToUpdatePlugin()
方法中编写代码,作为后台更新进程。
从我的代码,过程将是这样的…
运行插件v6.0 ->更新到6.1 ->6.0中的代码设置为瞬态,它刚刚更新。
重新加载页面或单击管理页面中的任何位置。→现在v6.1正在运行。→在插件加载的钩子上,它可以检测到这个插件刚刚更新。→调用redirectToUpdatePlugin()
方法,后台进程开始工作。