一直想实现这个功能~所以找了很多教程,最终在大发的主题“Presscore Lite”找到了方法,别的我不多说~第一步,把一下内容放到主题的fuctions.php
中。
<code>
if ( !class_exists('ThemeUpdateChecker') ):
class ThemeUpdateChecker {
public $theme = '';
public $metadataUrl = '';
public $enableAutomaticChecking = true;
protected $optionName = '';
protected $automaticCheckDone = false;
protected static $filterPrefix = 'tuc_request_update_';
public function __construct($theme, $metadataUrl, $enableAutomaticChecking = true){
$this->metadataUrl = $metadataUrl;
$this->enableAutomaticChecking = $enableAutomaticChecking;
$this->theme = $theme;
$this->optionName = 'external_theme_updates-'.$this->theme;
$this->installHooks();
}
public function installHooks(){
if ( $this->enableAutomaticChecking ){
add_filter('pre_set_site_transient_update_themes', array($this, 'onTransientUpdate'));
}
add_filter('site_transient_update_themes', array($this,'injectUpdate'));
add_action('delete_site_transient_update_themes', array($this, 'deleteStoredData'));
}
public function requestUpdate($queryArgs = array()){
$queryArgs['installed_version'] = $this->getInstalledVersion();
$queryArgs = apply_filters(self::$filterPrefix.'query_args-'.$this->theme, $queryArgs);
$options = array(
'timeout' => 10,
);
$options = apply_filters(self::$filterPrefix.'options-'.$this->theme, $options);
$url = $this->metadataUrl;
if ( !empty($queryArgs) ){
$url = add_query_arg($queryArgs, $url);
}
$result = wp_remote_get($url, $options);
$themeUpdate = null;
$code = wp_remote_retrieve_response_code($result);
$body = wp_remote_retrieve_body($result);
if ( ($code == 200) && !empty($body) ){
$themeUpdate = ThemeUpdate::fromJson($body);
if ( ($themeUpdate != null) && version_compare($themeUpdate->version, $this->getInstalledVersion(), '<=') ){
$themeUpdate = null;
}
}
$themeUpdate = apply_filters(self::$filterPrefix.'result-'.$this->theme, $themeUpdate, $result);
return $themeUpdate;
}
public function getInstalledVersion(){
if ( function_exists('wp_get_theme') ) {
$theme = wp_get_theme($this->theme);
return $theme->get('Version');
}
foreach(get_themes() as $theme){
if ( $theme['Stylesheet'] === $this->theme ){
return $theme['Version'];
}
}
return '';
}
public function checkForUpdates(){
$state = get_option($this->optionName);
if ( empty($state) ){
$state = new StdClass;
$state->lastCheck = 0;
$state->checkedVersion = '';
$state->update = null;
}
$state->lastCheck = time();
$state->checkedVersion = $this->getInstalledVersion();
update_option($this->optionName, $state); //Save before checking in case something goes wrong
$state->update = $this->requestUpdate();
update_option($this->optionName, $state);
}
public function onTransientUpdate($value){
if ( !$this->automaticCheckDone ){
$this->checkForUpdates();
$this->automaticCheckDone = true;
}
return $value;
}
public function injectUpdate($updates){
$state = get_option($this->optionName);
if ( !empty($state) && isset($state->update) && !empty($state->update) ){
$updates->response[$this->theme] = $state->update->toWpFormat();
}
return $updates;
}
public function deleteStoredData(){
delete_option($this->optionName);
}
public function addQueryArgFilter($callback){
add_filter(self::$filterPrefix.'query_args-'.$this->theme, $callback);
}
public function addHttpRequestArgFilter($callback){
add_filter(self::$filterPrefix.'options-'.$this->theme, $callback);
}
public function addResultFilter($callback){
add_filter(self::$filterPrefix.'result-'.$this->theme, $callback, 10, 2);
}
}
endif;
if ( !class_exists('ThemeUpdate') ):
class ThemeUpdate {
public $version;
public $details_url;
public $download_url;
public static function fromJson($json){
$apiResponse = json_decode($json);
if ( empty($apiResponse) || !is_object($apiResponse) ){
return null;
}
$valid = isset($apiResponse->version) && !empty($apiResponse->version) && isset($apiResponse->details_url) && !empty($apiResponse->details_url);
if ( !$valid ){
return null;
}
$update = new self();
foreach(get_object_vars($apiResponse) as $key => $value){
$update->$key = $value;
}
return $update;
}
public function toWpFormat(){
$update = array(
'new_version' => $this->version,
'url' => $this->details_url,
);
if ( !empty($this->download_url) ){
$update['package'] = $this->download_url;
}
return $update;
}
}
endif;
</code>
if ( !class_exists('ThemeUpdateChecker') ):
class ThemeUpdateChecker {
public $theme = '';
public $metadataUrl = '';
public $enableAutomaticChecking = true;
protected $optionName = '';
protected $automaticCheckDone = false;
protected static $filterPrefix = 'tuc_request_update_';
public function __construct($theme, $metadataUrl, $enableAutomaticChecking = true){
$this->metadataUrl = $metadataUrl;
$this->enableAutomaticChecking = $enableAutomaticChecking;
$this->theme = $theme;
$this->optionName = 'external_theme_updates-'.$this->theme;
$this->installHooks();
}
public function installHooks(){
if ( $this->enableAutomaticChecking ){
add_filter('pre_set_site_transient_update_themes', array($this, 'onTransientUpdate'));
}
add_filter('site_transient_update_themes', array($this,'injectUpdate'));
add_action('delete_site_transient_update_themes', array($this, 'deleteStoredData'));
}
public function requestUpdate($queryArgs = array()){
$queryArgs['installed_version'] = $this->getInstalledVersion();
$queryArgs = apply_filters(self::$filterPrefix.'query_args-'.$this->theme, $queryArgs);
$options = array(
'timeout' => 10,
);
$options = apply_filters(self::$filterPrefix.'options-'.$this->theme, $options);
$url = $this->metadataUrl;
if ( !empty($queryArgs) ){
$url = add_query_arg($queryArgs, $url);
}
$result = wp_remote_get($url, $options);
$themeUpdate = null;
$code = wp_remote_retrieve_response_code($result);
$body = wp_remote_retrieve_body($result);
if ( ($code == 200) && !empty($body) ){
$themeUpdate = ThemeUpdate::fromJson($body);
if ( ($themeUpdate != null) && version_compare($themeUpdate->version, $this->getInstalledVersion(), '<=') ){
$themeUpdate = null;
}
}
$themeUpdate = apply_filters(self::$filterPrefix.'result-'.$this->theme, $themeUpdate, $result);
return $themeUpdate;
}
public function getInstalledVersion(){
if ( function_exists('wp_get_theme') ) {
$theme = wp_get_theme($this->theme);
return $theme->get('Version');
}
foreach(get_themes() as $theme){
if ( $theme['Stylesheet'] === $this->theme ){
return $theme['Version'];
}
}
return '';
}
public function checkForUpdates(){
$state = get_option($this->optionName);
if ( empty($state) ){
$state = new StdClass;
$state->lastCheck = 0;
$state->checkedVersion = '';
$state->update = null;
}
$state->lastCheck = time();
$state->checkedVersion = $this->getInstalledVersion();
update_option($this->optionName, $state); //Save before checking in case something goes wrong
$state->update = $this->requestUpdate();
update_option($this->optionName, $state);
}
public function onTransientUpdate($value){
if ( !$this->automaticCheckDone ){
$this->checkForUpdates();
$this->automaticCheckDone = true;
}
return $value;
}
public function injectUpdate($updates){
$state = get_option($this->optionName);
if ( !empty($state) && isset($state->update) && !empty($state->update) ){
$updates->response[$this->theme] = $state->update->toWpFormat();
}
return $updates;
}
public function deleteStoredData(){
delete_option($this->optionName);
}
public function addQueryArgFilter($callback){
add_filter(self::$filterPrefix.'query_args-'.$this->theme, $callback);
}
public function addHttpRequestArgFilter($callback){
add_filter(self::$filterPrefix.'options-'.$this->theme, $callback);
}
public function addResultFilter($callback){
add_filter(self::$filterPrefix.'result-'.$this->theme, $callback, 10, 2);
}
}
endif;
if ( !class_exists('ThemeUpdate') ):
class ThemeUpdate {
public $version;
public $details_url;
public $download_url;
public static function fromJson($json){
$apiResponse = json_decode($json);
if ( empty($apiResponse) || !is_object($apiResponse) ){
return null;
}
$valid = isset($apiResponse->version) && !empty($apiResponse->version) && isset($apiResponse->details_url) && !empty($apiResponse->details_url);
if ( !$valid ){
return null;
}
$update = new self();
foreach(get_object_vars($apiResponse) as $key => $value){
$update->$key = $value;
}
return $update;
}
public function toWpFormat(){
$update = array(
'new_version' => $this->version,
'url' => $this->details_url,
);
if ( !empty($this->download_url) ){
$update['package'] = $this->download_url;
}
return $update;
}
}
endif;
</code>
如果你闲上面的代码比较繁琐,那么你可以把上面的代码换成require 'theme-updates/theme-update-checker.php';
,可以尝试一下。
然后继续把以下函数再次添加到fuctions.php
中;
<code>$example_update_checker = new ThemeUpdateChecker(
'yefengs', //这个是主题的名字
'http://xxxx.com/update.json' //这个地址是主题检查更新的地址,他是一个json文件链接
); </code>
'yefengs', //这个是主题的名字
'http://xxxx.com/update.json' //这个地址是主题检查更新的地址,他是一个json文件链接
); </code>
好~接下来我们继续研究这个update.json文件了,很简单,一个是说明文件,也就说更新了哪些东西等简要说明,一个是版本,也就是主题的版本,另一个是主题的zip压缩文件包~
把一下代码放到info.json文件中,并于fuctions.php的相呼应,然后放到合适的地方。
<code>{
"version" : "1.4.3",
"details_url" : "http://xxooxxoo.com/details.html",
"download_url" : "http://xxooxxoo.com/yefengs.zip"
}</code>
"version" : "1.4.3",
"details_url" : "http://xxooxxoo.com/details.html",
"download_url" : "http://xxooxxoo.com/yefengs.zip"
}</code>