問題描述

如果我寫一個私人外掛,有沒有辦法使用 WordPress auto-update 機制來更新它?

我想封裝這個功能,但它是我自己的 5 個左右的部落格,因此它不是一個很好的公共外掛資源的候選人。但我喜歡 easy-updating 機制。

有沒有辦法做到這一點?

最佳解決方案

看起來像適用的程式碼是在 wp-includes/update.phpwp_update_plugins()

$to_send = (object) compact('plugins', 'active');

$options = array(
    'timeout' => ( ( defined('DOING_CRON') && DOING_CRON ) ? 30 : 3),
    'body' => array( 'plugins' => serialize( $to_send ) ),
    'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
);

$raw_response = wp_remote_post('http://api.wordpress.org/plugins/update-check/1.0/', $options);

它具體檢查 api.wordpress.org 。從技術上說,可以透過 $to_send 中的一個金鑰委託支票,但是據我所知,這不是一個支援的功能。

如果您掛接到 set_site_transient_update_plugins,您可以將自己的包細節新增到此變數中。當您執行外掛更新程式時,這些值將被信任。參見 wp-admin/update.phpwp-admin/includes/class-wp-upgrader.php 。鑑於這兩個函式中的程式碼,我認為可以注入自己的更新伺服器,您只需要檢視包細節是如何格式化和匹配的。

次佳解決方案

這個外掛為你做:

Automatic Updates For Private And Commercial Plugins

Since time immemorial, only plugins hosted in the official WordPress.org plugin directory have supported automatic updates. Now, I』ve written a PHP library that you can use to add automatic update capabilities to any plugin. Public, private and commercial plugins alike – all can now enjoy the benefits of automatic update notifications and one-click upgrades.

 GitHub repository

第三種解決方案

我正在尋找同樣的事情。我發現有幾個連結可能有幫助:

  • WordPress.org 外掛資訊 API 檔案
  • get_api_data() 在 Pastebin 上

第一個是升級 API 的一些資訊和指標。第二個是程式碼片段,顯示如何從 API 中實際請求資訊。

第四種方案

如果您喜歡免費… 並且想要使用像 GitHub,GitLab 或 BitBucket 這樣的東西,這是一個很好的質量和支援的外掛來處理它 (支援私人和企業):

 https://github.com/afragen/github-updater

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。