借助Github 为第三方WordPress 主题/插件添加“自动更新”功能

借助Github 为第三方WordPress 主题/插件添加“自动更新”功能

WordPress 官方的主题/插件依赖与官方服务器可以实现后台自动更新或升级。如果你的作品托管到Github 上则可以通过下面几个projects 方便实现WordPress 主题/插件添加“更新升级”功能——就像官方的那样自动升级。

WordPress-GitHub-Plugin-Updater 简介

WordPress-GitHub-Plugin-Updater 是一个WordPress 的第三方类。如果你的WordPress 插件是托管到Github 上的,借助这个类可以实现类似WordPress 官方的自动更新升级机制。难得可贵的是,这个类不仅仅支持公共仓库(public repositories),也支持私有仓库的项目(private repositories)——当然前提你是GitHub 的付费用户。支持私有仓库意味着你可以将你的收费插件托管到Github 上而不必担心文件泄露的问题。

WordPress-GitHub-Plugin-Updater 使用方法

Github 主页:https://github.com/jkudish/WordPress-GitHub-Plugin-Updater

下载zip 压缩包解压后安装WordPress后台上,项目本身就是一个插件,你可以安装来查看是如何的运作的。

要集成到你的插件的话,将updater.php 放到你的插件目录下,然后在插件主文件写入下面的代码,注意相关文件名称要修改为你的插件的:

add_action( 'init', 'github_plugin_updater_test_init' );
function github_plugin_updater_test_init() {
 
	include_once 'updater.php';
 
	define( 'WP_GITHUB_FORCE_UPDATE', true );
 
	if (is_admin()) { // note the use of is_admin() to double check that this is happening in the admin
    $config = array(
        'slug' => plugin_basename(__FILE__), // this is the slug of your plugin
        'proper_folder_name' => 'plugin-name', // this is the name of the folder your plugin lives in
        'api_url' => 'https://api.github.com/repos/username/repository-name', // the github API url of your github repo
        'raw_url' => 'https://raw.github.com/username/repository-name/master', // the github raw url of your github repo
        'github_url' => 'https://github.com/username/repository-name', // the github url of your github repo
        'zip_url' => 'https://github.com/username/repository-name/zipball/master', // the zip url of the github repo
        'sslverify' => true // wether WP should check the validity of the SSL cert when getting an update, see https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/2 and https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/issues/4 for details
        'requires' => '3.0', // which version of WordPress does your plugin require?
        'tested' => '3.3', // which version of WordPress is your plugin tested up to?
        'readme' => 'README.md', // which file to use as the readme for the version number
        'access_token' => '', // Access private repositories by authorizing under Appearance > Github Updates when this example plugin is installed
    );
    new WP_GitHub_Updater($config);
}
 
}

access_token 这个参数是私有仓库才要的,如果是公开项目不必填写之。私有项目必须按照下面的步骤去获取Github 应用的Client ID 与Client Secret 激活之。

配置的最后一步是在Github 的readme.md 文件任意一行添加如下代码识别最新版本:

~Current Version:1.4~

如果填写正确,那么后台会适时提示更新,如下图:

其他类似的projects

不过经过Jeff 的测试,目前这个类的最新版(写本文时候是1.6)尚不稳定,会有报错信息。官方readme.md 文件上也写着1.6 (in development)。如果喜欢,可以下载旧版本使用。

或者采用其他类似功能的projects:

Plugin-Update

该project 在tutsplus上有详细介绍(地址), Jeff 本人没有深入研究。

github-updater

一个插件,支持托管在Github 上的公开仓库的WordPress 主题或插件的更新,你所要做的只要在主题或插件的相关文件添加类似的代码:

GitHub Theme URI: afragen/test-child
GitHub Theme URI: https://github.com/afragen/test-child

or

GitHub Plugin URI: afragen/github-updater
GitHub Plugin URI: https://github.com/afragen/github-updater

Theme-Updater

跟上面的插件差不多,不过单单适合于主题,你额外要做的是添加类似下面的代码:

Github Theme URI: https://github.com/username/repo

后记

看完本文后你可能会发现有些标题党的味道,因为本文更多是介绍“插件”的升级。哎哎我必须承认之,因为就目前Jeff 收集到的资料都是倾向于插件的。

老外也开发过不少projects 可以实现自托管(selfhost)的第三方主题/插件(免费或商业)实现类似WordPress 官方的升级机制。但这不是本文的讨论范围。

顺便一提:Jeff 开发的主题的自动更新机制不是采用上面某个的,也不是采用在国内wp圈子广为转载的某种通过json直链文件那种。至于是什么,当然是机密~

评分:
当前平均分 0.00 (0%) - 0 个投票
2 条 评论
  1. 今晚准备用下这个

    10年前 回复
  2. 手动更新是有点麻烦~~

    10年前 回复
发表评论