我已经构建了自己的插件,连接到
site_transient_update_plugins
transient_update_plugins
plugins_api
自动查询,同时将当前插件版本传递给指定的端点,作为回报,该端点根据提供的版本从S3存储桶中获取临时URL zip文件。
这基本上允许我管理每个特定WordPress环境的许多插件版本,而无需推送我的私有代码WordPress。这一切都很棒。喂食后我的终点?v=1.0.0
将返回指向的URL。来自S3存储桶的zip文件2.0.0
发布,如所示:
https://BUCKET.s3.amazonaws.com/wp-plugin/2.0.0/plugin-name.zip?X-Amz-Content-Sha256=UNSIGNED-PAYLOAD&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=...X-Amz-Signature=...
解压缩plugin-name.zip
生成的单个目录plugin-name
, 其中包含100%正确的版本文件。没问题。
WordPress正确地告诉我插件有更新。我可以查看我从端点提供的自定义详细信息,如描述、屏幕截图、变更日志、版本、测试、必需、横幅等。
然而,当我在WordPress中升级版本时,一切正常,下载并安装新版本(甚至从1.0.0
到2.0.0
), 但一旦我重新加载/导航到/离开页面,就会出现以下错误:
The plugin plugin-name/plugin-name.php has been deactivated due to an error: Plugin file does not exist.
我在
wp-content/plugins
查找我的插件已从
plugin-name
到
plugin-name-<random-slug>
.
<random-slug>
总是不同的,但它总是包含新版本代码(在我的例子中是2.0.0)。
我可以重新激活插件,但现在如果我运行wp plugin list
从CLI中,我可以plugin-name-<random-slug>
, 在我的代码中,我明确地依赖于plugin-name/plugin-name.zip
这是我第一次在WordPress中实时执行类似操作,所以有点不确定如何修复。如何连接到升级中,以便在下载zip并解压缩后,我可以将文件夹重命名回plugin-name
?
任何想法都将不胜感激!
add_filter(\'site_transient_update_plugins\', array($this, \'register_update_check\'));
add_filter(\'transient_update_plugins\', array($this, \'register_update_check\'));
add_filter(\'plugins_api\', array($this, \'register_plugin_details_overrides\'), 20, 3);
//
public function register_update_check($updates)
{
if (! is_object($updates)){
return $updates;
}
if (! isset($updates->response ) || ! is_array($updates->response)) {
$updates->response = array();
}
// Query WordPress plugins available
$this->response = $this->queryPluginVersions();
// Compare the version
// If returned version is greater than installed version,
// mock & return WordPress response, feeding it a .zip
// file which WordPress downloads, unzips the zip,
// completely replacing the plugin and its files
if ($this->response->version > $this->version) {
// Only mock our plugin
$updates->response[\'plugin-name/plugin-name.php\'] = (object) array(
\'slug\' => \'plugin-name\',
\'new_version\' => $this->response->version,
\'url\' => $this->response->url,
\'package\' => $this->response->download_url,
\'sections\' => array(
\'description\' => $this->response->sections->description,
\'installation\' => $this->response->sections->installation,
\'changelog\' => $this->response->sections->changelog,
\'screenshots\' => $this->response->sections->screenshots,
)
);
}
return $updates;
}
//
public function register_plugin_details_overrides($result, $action, $args)
{
if ($action !== \'plugin_information\') {
return $result;
}
if (\'plugin-name\' !== $args->slug) {
return $result;
}
return (object) json_decode(json_encode($this->response), true);
}
//
public function queryPluginVersions()
{
// Build the query, appending ?v=<version>
$url = sprintf(
\'%s?v=%s\',
$this->endpoint_url,
$this->version
);
$remote = wp_remote_get($url, array(
\'timeout\' => 10,
\'headers\' => array(
\'Accept\' => \'application/json\'
))
);
return json_decode($remote[\'body\']);
}
//
假设端点URL返回正确的信息,并带有有效的&;更正v2.0.0。拉链/释放