生命不息,奋斗不止/创造价值-传递价值-获得价值
所谓迷茫,就是才华配不上梦想 每一个让你难堪的现在,都有一个不够努力的曾经

Updates.php给自己的主题添加在线更新功能

前言

使用过 WordPress 的都知道 WordPress 默认主题都有在线更新的功能,但是我们一般使用的主题却木有,在对欲思主题的二次开发的时候,博猪就用到了一个自动更新的功能,在这里和大家说说吧

程序简介

theme-update-check 是国外的牛人开发的一个终于 WordPress 主题更新的 PHP 类,类似于我们熟悉的裁图工具 timthumb.php,我们可以很方便的将他集成到我们的主题里面,实现主题的自动更新服务。

文件说明

首先在文末下载文件包,解压出来是三个文件,updates.php 和 info.json。
首先打开 info.json,你会发现是类似下面的代码

  1. {
  2. “version”:“2.0”,
  3. “details_url”:“http://git.oschina.net/yunluo/yusi”,
  4. “download_url”:“http://git.oschina.net/yunluo/yusi/repository/archive?ref=master”
  5. }

当然,这个是我自己主题的文件,应该都能看懂吧。
主要就三个参数
第一个是版本号
第二个是主题发布页
第三个是主题下载直链,就是输入这个链接,浏览器自动下载主题的那种。

再看一下 updates.php 文件,其他你不需要看,你只需要把主题名字(别名)和 info.json 文件的路径填写进去就好了,其他就不用管了。
然后在主题的 functions.php 写入下面代码

  1. //主题自动更新服务
  2. require‘updates.php’;
  3. $example_update_checker =newThemeUpdateChecker(
  4. ‘yusi’,
  5. ‘info.json 文件路径’//此路径可以开发者自己定义,保证 info.json 文件能够公开访问以及不能被除开发者之外的人修改即可
  6. );

文件部署

首先将 updates.php 文件放在主题根目录,然后将 info.json 放在主题开发者自己的网站根目录里面,当然,其实并不一定是根目录,放在哪里,然后 updates.php 文件里面的路径就是这个,info.json 文件必须保证能够公开访问,然后 over 了!

推送更新

程序是将主题的开发者留下的 meta 文件和主题样式 style..css 里面的版本号进行比对,如果主题开发者的 info.json 文件里面的版本号比使用者的主题文件 style.css 里面的版本号大,那么就会在 WordPress 后台的更新工具进行更新提醒,和正常的 WordPress 更新,插件更新完全一样,如果你选择更新,就自动从主题直链在线安装主题。

注意事项

在使用中,博猪发现了一些小问题,在这里也顺便说一下。
最重要的是保证一致性。保证自动更新里面的主题名字和你使用的主题名字一样,比如我目前使用的主题名字叫 yusi,对,这里说的是主题文件夹名字,而不是其他中文名字,需要保持一致的地方有 info.json,functions.php 中的初始化函数代码,updates.php 文件这四个地方,另外下载链接所下载的主题包文件名字也必须是一样的。
另外有很多朋友和博猪一样喜欢使用 github,gitcafe,oschina,coding 等等使用 GIT 版本控制系统的代码托管网站,如果采用这里提供的直链下载的话,你的项目名字也必须和主题名字一样,比如我之前的项目名字叫 yusi-theme,结果自动更新之后主题文件夹多了一个名字叫 yusi-theme 的主题文件夹。好吧,明白怎么回事了吧!

后语

使用这种自动更新功能可以给主题使用者更新鲜的体验以及免去了多次下载的麻烦,自动检查更新的功能最好弄一个开关,让用户选择是否启用,因为主题更新会全部覆盖主题文件,如果使用者在主题里面自己添加了什么代码,最后都可能被覆盖掉,所以,主题开发者尽量给主题多留一些框框,尽量让使用者在主题设置里面设置,而不是直接在主题里面。

源代码

  1. <?php
  2. /**
  3. * Theme Update Checker Library 1.2
  4. * http://w-shadow.com/
  5. *
  6. * Copyright 2012 Janis Elsts
  7. * Licensed under the GNU GPL license.
  8. * http://www.gnu.org/licenses/gpl.html
  9. */
  10.  
  11. if(!class_exists(‘ThemeUpdateChecker’)):
  12.  
  13. classThemeUpdateChecker{
  14. public $theme =‘Git-master’;//待检查更新的主题名
  15. public $metadataUrl =‘https://coding.net/u/googlo/p/File/git/raw/master/info.json’;//元数据文件的路径
  16. public $enableAutomaticChecking =true;//是否启用自动更新
  17. protected $optionName =;//更新时候的文字信息
  18. protected $automaticCheckDone =false;
  19. protectedstatic $filterPrefix =‘tuc_request_update_’;
  20.  
  21. publicfunction __construct($theme, $metadataUrl, $enableAutomaticChecking =true){
  22. $this->metadataUrl = $metadataUrl;
  23. $this->enableAutomaticChecking = $enableAutomaticChecking;
  24. $this->theme = $theme;
  25. $this->optionName =‘external_theme_updates-‘.$this->theme;
  26. $this->installHooks();
  27. }
  28.  
  29. publicfunction installHooks(){
  30.  
  31. if( $this->enableAutomaticChecking ){
  32. add_filter(‘pre_set_site_transient_update_themes’, array($this,‘onTransientUpdate’));
  33. }
  34.  
  35. //Insert our update info into the update list maintained by WP.
  36. add_filter(‘site_transient_update_themes’, array($this,‘injectUpdate’));
  37.  
  38. //Delete our update info when WP deletes its own.
  39. //This usually happens when a theme is installed, removed or upgraded.
  40. add_action(‘delete_site_transient_update_themes’, array($this,‘deleteStoredData’));
  41. }
  42.  
  43. publicfunction requestUpdate($queryArgs = array()){
  44. //Query args to append to the URL. Themes can add their own by using a filter callback (see addQueryArgFilter()).
  45. $queryArgs[‘installed_version’]= $this->getInstalledVersion();
  46. $queryArgs = apply_filters(self::$filterPrefix.‘query_args-‘.$this->theme, $queryArgs);
  47.  
  48. //Various options for the wp_remote_get() call. Themes can filter these, too.
  49. $options = array(
  50. ‘timeout’=>20,//seconds
  51. );
  52. $options = apply_filters(self::$filterPrefix.‘options-‘.$this->theme, $options);
  53.  
  54. $url = $this->metadataUrl;
  55. if(!empty($queryArgs)){
  56. $url = add_query_arg($queryArgs, $url);
  57. }
  58.  
  59. //Send the request.
  60. $result = wp_remote_get($url, $options);
  61.  
  62. //Try to parse the response
  63. $themeUpdate =null;
  64. $code = wp_remote_retrieve_response_code($result);
  65. $body = wp_remote_retrieve_body($result);
  66. if(($code ==200)&&!empty($body)){
  67. $themeUpdate =ThemeUpdate::fromJson($body);
  68. //The update should be newer than the currently installed version.
  69. if(($themeUpdate !=null)&& version_compare($themeUpdate->version, $this->getInstalledVersion(),‘<=’)){
  70. $themeUpdate =null;
  71. }
  72. }
  73.  
  74. $themeUpdate = apply_filters(self::$filterPrefix.‘result-‘.$this->theme, $themeUpdate, $result);
  75. return $themeUpdate;
  76. }
  77.  
  78. publicfunction getInstalledVersion(){
  79. if( function_exists(‘wp_get_theme’)){
  80. $theme = wp_get_theme($this->theme);
  81. return $theme->get(‘Version’);
  82. }
  83.  
  84. foreach(get_themes()as $theme){
  85. if( $theme[‘Stylesheet’]=== $this->theme ){
  86. return $theme[‘Version’];
  87. }
  88. }
  89. return;
  90. }
  91.  
  92. publicfunction checkForUpdates(){
  93. $state = get_option($this->optionName);
  94. if( empty($state)){
  95. $state =newStdClass;
  96. $state->lastCheck =0;
  97. $state->checkedVersion =;
  98. $state->update =null;
  99. }
  100.  
  101. $state->lastCheck = time();
  102. $state->checkedVersion = $this->getInstalledVersion();
  103. update_option($this->optionName, $state);//Save before checking in case something goes wrong
  104.  
  105. $state->update = $this->requestUpdate();
  106. update_option($this->optionName, $state);
  107. }
  108.  
  109. publicfunction onTransientUpdate($value){
  110. if(!$this->automaticCheckDone ){
  111. $this->checkForUpdates();
  112. $this->automaticCheckDone =true;
  113. }
  114. return $value;
  115. }
  116.  
  117. publicfunction injectUpdate($updates){
  118. $state = get_option($this->optionName);
  119.  
  120. //Is there an update to insert?
  121. if(!empty($state)&& isset($state->update)&&!empty($state->update)){
  122. $updates->response[$this->theme]= $state->update->toWpFormat();
  123. }
  124.  
  125. return $updates;
  126. }
  127.  
  128. publicfunction deleteStoredData(){
  129. delete_option($this->optionName);
  130. }
  131.  
  132. publicfunction addQueryArgFilter($callback){
  133. add_filter(self::$filterPrefix.‘query_args-‘.$this->theme, $callback);
  134. }
  135.  
  136. publicfunction addHttpRequestArgFilter($callback){
  137. add_filter(self::$filterPrefix.‘options-‘.$this->theme, $callback);
  138. }
  139.  
  140. publicfunction addResultFilter($callback){
  141. add_filter(self::$filterPrefix.‘result-‘.$this->theme, $callback,10,2);
  142. }
  143. }
  144.  
  145. endif;
  146.  
  147. if(!class_exists(‘ThemeUpdate’)):
  148.  
  149. classThemeUpdate{
  150. public $version;//Version number.
  151. public $details_url;//The URL where the user can learn more about this version.
  152. public $download_url;//The download URL for this version of the theme. Optional.
  153.  
  154. publicstaticfunction fromJson($json){
  155. $apiResponse = json_decode($json);
  156. if( empty($apiResponse)||!is_object($apiResponse)){
  157. returnnull;
  158. }
  159.  
  160. //Very, very basic validation.
  161. $valid = isset($apiResponse->version)&&!empty($apiResponse->version)&& isset($apiResponse->details_url)&&!empty($apiResponse->details_url);
  162. if(!$valid ){
  163. returnnull;
  164. }
  165.  
  166. $update =newself();
  167. foreach(get_object_vars($apiResponse)as $key => $value){
  168. $update->$key = $value;
  169. }
  170.  
  171. return $update;
  172. }
  173.  
  174. publicfunction toWpFormat(){
  175. $update = array(
  176. ‘new_version’=> $this->version,
  177. ‘url’=> $this->details_url,
  178. );
  179.  
  180. if(!empty($this->download_url)){
  181. $update[‘package’]= $this->download_url;
  182. }
  183.  
  184. return $update;
  185. }
  186. }
  187.  
  188. endif;
  189.  

 

 

下载:201801242051498

赞(0)
未经允许不得转载:jack361博客 » Updates.php给自己的主题添加在线更新功能

如果你爱我或恨我,请↓

联系我