我的插件未正确更新(upgrader_process_complete 问题)

2023-12-23

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

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

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

然而,似乎当用户使用插件页面升级时update now链接,我的新版本的处理程序没有被调用。

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


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


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

现在您已经运行了插件 v 6.1,其中包含upgrader_process_complete自 v 6.1 起挂钩。
你刚刚更新到 6.2,其中包含编写的代码ABC.txt file.
将调用 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->registerHooks() method.
并在里面写上你的代码redirectToUpdatePlugin()方法作为后台更新过程。


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

我的插件未正确更新(upgrader_process_complete 问题) 的相关文章

随机推荐

  • 在 Eclipse Mars 中将 m2e 降级到 1.5 版本

    我有个问题 我需要在 Eclipse Mars 中使用 Maven 2 x 它有 m2e 1 6 不支持 Maven 2 x 我必须将 m2e 降级到 1 5 版本 但我不知道如何 不可能在 Eclipse 中卸载它 我从包含 m2e 文件
  • Hyperledger Fabric v1.0 上的隐私

    您能否解释一下 Hyperledger Fabric v1 0 中的一些隐私机制 那些已经实现的以及计划在未来实现的机制 我知道渠道就像单独的区块链 我还浏览了该文档https jira hyperledger org browse FAB
  • tabindex="-1" 的模态将焦点放在选项卡上

    我目前正在使用 Twitter Bootstrap 并且在模态的 tabindex 中遇到一个奇怪的问题 我试图通过模式内的表单元素进行切换 但在最后一个按钮之后 焦点在返回到关闭按钮之前消失了 我在控制台中添加了一行来记录正在聚焦的元素
  • 在 Meteor 事件处理程序中访问模板帮助程序字典

    在 Meteor 中 我将两个对象从我的数据库发送到模板 Template myTemplate helpers helper1 function var object1 this data context set in iron rout
  • Route53 中 A Rec 和 CNAME 之间的区别

    由于Amazon Route 53允许您使用Alias将A记录或CNAME设置为不同的子域名 我现在想知道A记录和CNAME之间有什么区别 为了澄清我的问题 传统NameServer中的CNAME和A记录之间的区别在于 只有CNAME可以用
  • 调用多个api url并同时调用

    我有三个 API url 每个都有相同的对象名称 我希望同时调用所有 api 到目前为止我的js document ready function var first https first var second https second v
  • 确定 JavaScript e.keyCode 是否为可打印(非控制)字符

    我只是想知道 JavaScript 的范围keyCode对应于可输入字符的 s 或者 不可输入 控制 字符的范围 如退格 转义 命令 移位等 这样我就可以忽略它们 我问的原因是打电话String fromCharCode 导致控制键出现奇数
  • UIView 中只有白色填充颜色是透明的

    我有一个UIView即设置为opaque NO一切都很好 在里面drawRect我正在做自定义绘图 这有效 CGContextSetFillColor context CGColorGetComponents UIColor blueCol
  • 在Tomcat上部署war文件

    有没有办法在 Tomcat 服务器上部署给定的 war 文件 我想在不使用网络界面的情况下执行此操作 部署 Tomcat Web 应用程序有多种方法 正如已经提到的 进入 CATALINA HOME webapps Using your b
  • 如何在 Flutter 中处理 Android 设备的 BACK 按钮按下?

    如何处理设备后退按钮onPressed Android 版 Flutter 中 我知道我必须为 iOS 手动添加一个后退按钮 但 Android 设备有内置的后退按钮 用户可以按下它 怎么处理呢 您可以使用WillPopScope为了达成这
  • 有没有办法在 r 中自动重新安装软件包及其依赖项?

    所以我在Rstudio上使用installR将R更新到版本4 0 0 并将我的包文件复制到R中的库文件中 目录是 C Users Ibrahim Documents R R 4 0 0 library 每当我调用一个包时 例如 tidyte
  • 在 Linux 命令行中使用低功耗蓝牙

    我正在研究在 Linux 上使用蓝牙低功耗调制解调器 我正在使用命令行选项 即hcitool 我可以使用以下命令找到设备 hcitool scan 它对我来说工作正常 我也可以使用以下方式广播我的设备 sudo hciconfig hci0
  • RecyclerView 中最后一个子元素的边距/填充

    我试图在最后一行添加填充 边距底部 并在第一行添加填充 边距顶部 我不能在项目 xml 中执行此操作 因为它会影响我所有的孩子 我的 RecyclerView 适配器中有标题和子项 所以我无法使用 android padding 4dp a
  • Django REST Framework 序列化速度极慢

    我使用的是 Python 2 7 和 Django 1 7 1 以及 django rest 框架 我有一个 API 它返回一些从数据库中获取的特定值 它使用如下所示的自定义序列化器 class InventarioSerializer s
  • 使用 MLCP 复制数据时出现异常

    我正在尝试使用 MLCP 将 100 万个文档从一个数据库复制到另一个数据库 但出现以下异常 19 08 30 11 48 08 ERROR contentpump DatabaseContentReader RuntimeExceptio
  • 我如何搜索文件并将它们压缩到一个 zip 文件中

    我尝试使用以下命令搜索文件并压缩它们 find regexpression exec zip 但是它不起作用 我怎样才能做到这一点 您使用的命令将分别对每个文件运行 zip 请尝试以下操作 find name
  • IIS 站点和 nant/nantcontrib?

    是否可以使用 NAnt 管理 IIS Web 应用程序 例如停止或启动它 Nant 具有 servicecontroller 任务 您可以使用它仅停止 启动 Web 服务器或整个 IIS 我通常使用它来停止 启动 Web 服务器
  • 如何在 php 中正确为我的 tbl_item 制作下拉列表菜单

    我的下拉列表菜单遇到问题 它不断出现 wamp 错误 我的下拉列表菜单将来自 tbl 项目的项目名称 postgres DB funcContainer php 页面 function DropdownListMenu label name
  • 扩展 MIDL 接口和 COM 对象设计

    我读过有关各种 COM 设计模式的详细信息COM 程序员的食谱 http msdn microsoft com en us library ms809982 aspx以及一些相关的 SO 线程 特别是讨论组合与多重继承的线程 https s
  • 我的插件未正确更新(upgrader_process_complete 问题)

    我有安装了我的插件的用户 我们将其称为 v6 我的插件的 V6 版本没有注册处理程序upgrader process complete 在我的新版本中 我有upgrader process complete注册对我的数据库表进行一些升级 然