我的插件未正确更新(与upgradeder_process_complete一起出现问题)

Gez*_*zim 5 php wordpress wordpress-plugin

我有安装了我的插件的用户(我们称之为v6).

我的插件的V6版本没有注册处理程序upgrader_process_complete.

在我的新版本中,我已upgrader_process_complete注册对数据库表进行一些升级.

但是,当用户使用update now链接从插件页面升级时,似乎不会调用我的新版本的处理程序.

有人能解释一下这个问题吗?

vee*_*vee 2

upgrader_process_complete更新插件时,挂钩在当前版本中运行。


假设您正在运行插件 v 6.0。
然后你刚刚更新到6.1,upgrader_process_complete这个版本包含hook。
直到下一个场景才会调用升级程序挂钩。

现在您正在运行插件 v 6.1,其中包含upgrader_process_complete自 v 6.1 以来的钩子。您刚刚更新到 6.2,其中包含写入ABC.txt
文件 的代码。 将调用 6.1 的升级程序挂钩,而不是 6.2。因此,这意味着ABC.txt文件将不会被创建。

如果您正在运行插件 v 6.1 并想要运行刚刚更新的 6.2 中的新更新代码,您必须添加诸如瞬态之类的内容以注意到需要从新版本代码进行更新。
这是我的插件。您可以在 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


    }
Run Code Online (Sandbox Code Playgroud)

请仔细阅读并修改上面的代码。
在您的插件文件中,调用Upgrader->registerHooks()方法。
并在方法中编写代码redirectToUpdatePlugin()以作为后台更新过程。


从我的代码来看,过程将是这样的...
运行插件 v 6.0 -> 更新到 6.1 -> 6.0 中的代码设置了刚刚更新的瞬态。
重新加载页面或单击管理页面中的任意位置。-> 现在 v 6.1 正在运行。-> 在插件加载钩子上,可以检测到该插件刚刚更新。-> 对redirectToUpdatePlugin()方法和后台进程的调用从这里开始工作。