从私人 GitHub 存储库更新 WordPress 主题/插件

发布于 2025-01-20 23:19:15 字数 2079 浏览 5 评论 0原文

背景

我正在为我的 WordPress 网站开发一个自定义主题,我想从私人 GitHub 存储库进行管理。 (这个主题永远不会被推入 WordPress 市场)总体想法是,我使用存储库来管理代码,然后一旦我标记新版本,该标记就会触发 WordPress 主题的更新。

我使用以下内容作为模板来使用此模式:
https://github.com/krafit/wp-gitlab-updater
(是的,我知道该存储库适用于 Gitlab,而不是 GitHub)

由于我的存储库是私有的,因此我需要生成一个用户令牌以允许更新主题。而且由于用户令牌能够访问我所有的私人存储库,因此从安全角度来看,与另一个插件共享用户令牌的想法是令人不安的。 (意思是,我使用这样的插件感到不舒服: https://github.com/afragen/git- updater)

问题

问题是 GitHub 有 已弃用使用 access_token 作为查询字符串参数,因此所有令牌都必须作为 Authorization 标头发送。

如何将授权标头添加到 WordPress 发送的下载工件的请求中?

我尝试过的方法

当我检查新标签时,我使用代码:

  protected function fetch_tags_from_repo( $git_url, $repo, $access_token ) {
    $request_url = "$git_url/repos/$repo/tags?access_token=$access_token";
    $args     = [
      "headers" => [
        "Accept" => "application/vnd.github.v3+json",
        "Authorization" => "token " . $access_token
      ]
    ];
    $request     = wp_safe_remote_get( $request_url, $args );

    return $request;
  }

这可以正常工作,没有任何问题。但是...

pre_set_site_transient_update_themes 挂钩期间,我返回一个如下所示的对象:

  $transient->response[ $theme['name'] ]['theme']       = $theme['name'];
  $transient->response[ $theme['name'] ]['new_version'] = $latest_version;
  $transient->response[ $theme['name'] ]['package']     = $theme_package;

问题是,我无法将 Authorization 标头添加到瞬态响应对象。因此,当 WP 稍后尝试下载该工件时,它会失败。

注意:$theme_package 字符串是一个 URL,如下所示:

$theme_package = "$git_url/repos/$repo/zipball/refs/tags/$latest_version";

感谢您的支持,谢谢!

Background

I am working on a custom theme for my WordPress site which I would like to manage from a private GitHub repo. (This theme will never be pushed into the WordPress market place) The general idea would be that I use the repo to manage the code and then once I tag a new version, the tag would trigger an update for the WordPress theme.

I have this pattern working using the following as a template:
https://github.com/krafit/wp-gitlab-updater
(Yes, I know the repo is for Gitlab and not GitHub)

Since my repo is private, I will need to generate a user token to allow the theme to be updated. And because the user token is capable of accessing all my private repos, the idea of sharing the user token with another plugin is discomforting from a security standpoint. (Meaning, I'm uncomfortable using a plugin like: https://github.com/afragen/git-updater)

Question

The problem is that GitHub has deprecated the use of access_token as a query string parameter, so all tokens must be sent over as an Authorization header.

How do I add an authorization header to the request WordPress sends to download the artifact?

What I've Tried

When I check for new tags I use the code:

  protected function fetch_tags_from_repo( $git_url, $repo, $access_token ) {
    $request_url = "$git_url/repos/$repo/tags?access_token=$access_token";
    $args     = [
      "headers" => [
        "Accept" => "application/vnd.github.v3+json",
        "Authorization" => "token " . $access_token
      ]
    ];
    $request     = wp_safe_remote_get( $request_url, $args );

    return $request;
  }

This works without any issues. However...

During the pre_set_site_transient_update_themes hook I return an object that looks like:

  $transient->response[ $theme['name'] ]['theme']       = $theme['name'];
  $transient->response[ $theme['name'] ]['new_version'] = $latest_version;
  $transient->response[ $theme['name'] ]['package']     = $theme_package;

The problem is, I have no way of adding an Authorization header to the transient response object. Therefore, when WP later tries to download the artifact, it fails.

Note: The $theme_package string is a URL which looks like:

$theme_package = "$git_url/repos/$repo/zipball/refs/tags/$latest_version";

Any support appreciated, thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

两仪 2025-01-27 23:19:15

从 GitHub 中弹出并使用 Gitlab,因为它们仍然支持 access_token 作为标头。他们有无限的免费私人存储库 <5GB 存储空间。

Eject from GitHub and use Gitlab because they still support access_token as a header. They have unlimited free private repos <5gb storage.

漫雪独思 2025-01-27 23:19:15

如果您计划使用许可证分发私有存储库,我建议您不要在脚本中公开您的访问凭据。

相反,您应该将 GitHub PHP API 与您在存储库设置中设置的 SSH 密钥或在存储库上授予访问权限的 GitHub 应用程序一起使用。

这是一个可靠的 SDK 起点:

https://github.com/KnpLabs/php-github -api

或者,正如您在答案中建议的那样,可以使用第三方服务代表您管理凭据。

Gitlab 是一个不错的通用且低成本的选择,但如果您正在寻找专门用于 WordPress 开发的东西,我推荐 WP 包编辑器 (WP2E )

除此之外,该服务使用注册的 GitHub 应用程序从公共/私有 GitHub 存储库中提取最新版本:

https://github.com/marketplace/wp-package-editor

这是引用自 有关如何使用 GitHub 实现的文档

将脚本成功导入到存储库库并稍后作为安装程序依赖项进行同步,有 4 个条件:

  1. GitHub 应用程序必须连接到 WP2E 帐户
  2. 必须向 WP2E GitHub 应用程序授予对存储库的“只读”访问权限
  3. 脚本必须是有效的 WP 主题或插件
  4. 存储库必须在 GitHub 上至少有一个“版本”

注意:为了与 GitHub 帐户/存储库同步,GitHub 应用程序应通过 saas 面板集成(而不是直接通过 GitHub Marketplace)

If you are planning to distribute the private repo with a license I recommend you not to expose your access credentials in the script.

Instead you should use the GitHub PHP API together with a SSH Key that you setup in your repo settings or a GitHub App with access permission granted on your repo.

Here is a solid SDK to start from:

https://github.com/KnpLabs/php-github-api

Alternatively as you suggested it in your answer, a third party service could be used to manage the credentials on your behalf.

Gitlab is a nice generic and low cost option but if you are looking for something dedicated to Wordpress development I recommend WP Package Editor (WP2E)

Among other things the service uses a registered GitHub App to pull the latest version from public / private GitHub repositories:

https://github.com/marketplace/wp-package-editor

This is quoted from the documentation regarding how it is implemented with GitHub:

For a script to be successfully imported to the library of repositories and later be synchronized as an installer dependency there are 4 conditions :

  1. The GitHub App must be connected to a WP2E account
  2. The “read-only” access to the repository must be granted to the WP2E GitHub App
  3. The script must be a valid WP theme or plugin
  4. The repository must have at least one “release” on GitHub

Note: In order to synchronize with the GitHub account/repo the GitHub App should be integrated via the saas panel ( not directly via the GitHub Marketplace )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文