php - 我的插件没有正确更新(upgrader_process_complete 问题)

标签 php wordpress

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

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

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

但是,当用户使用 立即更新 链接从插件页面升级时,我的新版本的处理程序似乎没有被调用。

谁能阐明这个问题?

最佳答案

upgrader_process_complete Hook 在更新插件时在当前版本中运行。


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

现在您已经运行了 v 6.1 插件,它包含自 v 6.1 以来的 upgrader_process_complete Hook 。
你刚刚更新到 6.2,其中包含编写 ABC.txt 文件的代码。
将调用 6.1 的升级程序 Hook ,而不是 6.2。因此,这意味着不会创建 ABC.txt 文件。

如果您正在运行插件 v 6.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() 方法中编写代码以用作后台更新进程。


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

关于php - 我的插件没有正确更新(upgrader_process_complete 问题),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32196219/

相关文章:

css - 我在更改主题时遇到问题

php - 如何使用 Retrofit 2 下载 pdf 文件

php - 我怎样才能确保我不会从 php 测试站点向客户发送任何电子邮件

javascript - WordPress 的各个版本之间存在哪些独特的帖子引用?

css - 使用带有工具箱主题的 Wordpress 时如何在帖子之间添加空格

php - 单选按钮未按选中状态显示

jquery - 将标题附加到 Google 事件跟踪代码

php - Woocommerce 可选产品属性

php - md5 和 hash 的问题

PHP - 用于使产品 ID 持久化的 session ?