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

时间:2015-08-25 05:33:57

标签: php wordpress-plugin wordpress

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

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

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

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

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

1 个答案:

答案 0 :(得分:2)

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

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

现在你运行的插件v 6.1包含自v 6.1以来的upgrader_process_complete钩子 您刚刚更新到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


    }

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

从我的代码中,程序将是这样的...
运行插件v 6.0 - &gt;更新到6.1 - &gt; 6.0中的代码设置瞬态,它刚刚更新。
重新加载页面或单击管理页面中的任意位置。 - &GT;现在v 6.1正在运行。 - &GT;在thr插件加载的钩子上,它可以检测到这个插件刚刚更新。 - &GT;调用redirectToUpdatePlugin()方法和后台流程开始在此处工作。