PageRenderTime 53ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/inc/_updater.php

https://github.com/TV4/Bambuser-Auto-Poster
PHP | 354 lines | 154 code | 72 blank | 128 comment | 32 complexity | 0021f7ac9e05e970be6feed7c9d99d26 MD5 | raw file
  1. <?php
  2. // Prevent loading this file directly - Busted!
  3. if ( !defined('ABSPATH') )
  4. die('-1');
  5. if ( ! class_exists( 'WPGitHubUpdater' ) ) :
  6. /**
  7. * @version 1.4
  8. * @author Joachim Kudish <info@jkudish.com>
  9. * @link http://jkudish.com
  10. * @package GithubUpdater
  11. * @license http://www.gnu.org/copyleft/gpl.html GNU Public License
  12. * @copyright Copyright (c) 2011, Joachim Kudish
  13. *
  14. * GNU General Public License, Free Software Foundation
  15. * <http://creativecommons.org/licenses/GPL/2.0/>
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  30. */
  31. class WPGitHubUpdater {
  32. /**
  33. * Class Constructor
  34. *
  35. * @since 1.0
  36. * @param array $config configuration
  37. * @return void
  38. */
  39. public function __construct( $config = array() ) {
  40. global $wp_version;
  41. $defaults = array(
  42. 'slug' => plugin_basename(__FILE__),
  43. 'proper_folder_name' => dirname( plugin_basename(__FILE__) ),
  44. 'api_url' => 'https://api.github.com/repos/jkudish/WordPress-GitHub-Plugin-Updater',
  45. 'raw_url' => 'https://raw.github.com/jkudish/WordPress-GitHub-Plugin-Updater/master',
  46. 'github_url' => 'https://github.com/jkudish/WordPress-GitHub-Plugin-Updater',
  47. 'zip_url' => 'https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/zipball/master',
  48. 'sslverify' => true,
  49. 'requires' => $wp_version,
  50. 'tested' => $wp_version
  51. );
  52. $this->config = wp_parse_args( $config, $defaults );
  53. $this->set_defaults();
  54. if ( ( defined('WP_DEBUG') && WP_DEBUG ) || ( defined('WP_GITHUB_FORCE_UPDATE') || WP_GITHUB_FORCE_UPDATE ) )
  55. add_action( 'init', array( $this, 'delete_transients' ) );
  56. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'api_check' ) );
  57. // Hook into the plugin details screen
  58. add_filter( 'plugins_api', array( $this, 'get_plugin_info' ), 10, 3 );
  59. add_filter( 'upgrader_post_install', array( $this, 'upgrader_post_install' ), 10, 3 );
  60. // set timeout
  61. add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) );
  62. // set sslverify for zip download
  63. add_filter( 'http_request_args', array( $this, 'http_request_sslverify' ), 10, 2 );
  64. }
  65. /**
  66. * Set defaults
  67. *
  68. * @since 1.2
  69. * @return void
  70. */
  71. public function set_defaults() {
  72. if ( ! isset( $this->config['new_version'] ) )
  73. $this->config['new_version'] = $this->get_new_version();
  74. if ( ! isset( $this->config['last_updated'] ) )
  75. $this->config['last_updated'] = $this->get_date();
  76. if ( ! isset( $this->config['description'] ) )
  77. $this->config['description'] = $this->get_description();
  78. $plugin_data = $this->get_plugin_data();
  79. if ( ! isset( $this->config['plugin_name'] ) )
  80. $this->config['plugin_name'] = $plugin_data['Name'];
  81. if ( ! isset( $this->config['version'] ) )
  82. $this->config['version'] = $plugin_data['Version'];
  83. if ( ! isset( $this->config['author'] ) )
  84. $this->config['author'] = $plugin_data['Author'];
  85. if ( ! isset( $this->config['homepage'] ) )
  86. $this->config['homepage'] = $plugin_data['PluginURI'];
  87. if ( ! isset( $this->config['readme'] ) )
  88. $this->config['readme'] = 'README.md';
  89. }
  90. /**
  91. * Callback fn for the http_request_timeout filter
  92. *
  93. * @since 1.0
  94. * @return int timeout value
  95. */
  96. public function http_request_timeout() {
  97. return 2;
  98. }
  99. /**
  100. * Callback fn for the http_request_args filter
  101. *
  102. * @param $args
  103. * @param $url
  104. *
  105. * @return mixed
  106. */
  107. public function http_request_sslverify ( $args, $url ) {
  108. if ( $this->config[ 'zip_url' ] == $url )
  109. $args[ 'sslverify' ] = $this->config[ 'sslverify' ];
  110. return $args;
  111. }
  112. /**
  113. * Delete transients (runs when WP_DEBUG is on)
  114. * For testing purposes the site transient will be reset on each page load
  115. *
  116. * @since 1.0
  117. * @return void
  118. */
  119. public function delete_transients() {
  120. delete_site_transient( 'update_plugins' );
  121. delete_site_transient( $this->config['slug'].'_new_version' );
  122. delete_site_transient( $this->config['slug'].'_github_data' );
  123. delete_site_transient( $this->config['slug'].'_changelog' );
  124. }
  125. /**
  126. * Get New Version from github
  127. *
  128. * @since 1.0
  129. * @return int $version the version number
  130. */
  131. public function get_new_version() {
  132. $version = get_site_transient( $this->config['slug'].'_new_version' );
  133. if ( !isset( $version ) || !$version || '' == $version ) {
  134. $raw_response = wp_remote_get(
  135. trailingslashit($this->config['raw_url']).$this->config['readme'],
  136. array(
  137. 'sslverify' => $this->config['sslverify'],
  138. )
  139. );
  140. if ( is_wp_error( $raw_response ) )
  141. return false;
  142. $__version = explode( '~Current Version:', $raw_response['body'] );
  143. if ( !isset($__version['1']) )
  144. return false;
  145. $_version = explode( '~', $__version['1'] );
  146. $version = $_version[0];
  147. // refresh every 6 hours
  148. set_site_transient( $this->config['slug'].'_new_version', $version, 60*60*6 );
  149. }
  150. return $version;
  151. }
  152. /**
  153. * Get GitHub Data from the specified repository
  154. *
  155. * @since 1.0
  156. * @return array $github_data the data
  157. */
  158. public function get_github_data() {
  159. $github_data = get_site_transient( $this->config['slug'].'_github_data' );
  160. if ( ! isset( $github_data ) || ! $github_data || '' == $github_data ) {
  161. $github_data = wp_remote_get(
  162. $this->config['api_url'],
  163. array(
  164. 'sslverify' => $this->config['sslverify'],
  165. )
  166. );
  167. if ( is_wp_error( $github_data ) )
  168. return false;
  169. $github_data = json_decode( $github_data['body'] );
  170. // refresh every 6 hours
  171. set_site_transient( $this->config['slug'].'_github_data', $github_data, 60*60*6);
  172. }
  173. return $github_data;
  174. }
  175. /**
  176. * Get update date
  177. *
  178. * @since 1.0
  179. * @return string $date the date
  180. */
  181. public function get_date() {
  182. $_date = $this->get_github_data();
  183. return ( !empty($_date->updated_at) ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false;
  184. }
  185. /**
  186. * Get plugin description
  187. *
  188. * @since 1.0
  189. * @return string $description the description
  190. */
  191. public function get_description() {
  192. $_description = $this->get_github_data();
  193. return ( !empty($_description->description) ) ? $_description->description : false;
  194. }
  195. /**
  196. * Get Plugin data
  197. *
  198. * @since 1.0
  199. * @return object $data the data
  200. */
  201. public function get_plugin_data() {
  202. include_once( ABSPATH.'/wp-admin/includes/plugin.php' );
  203. $data = get_plugin_data( WP_PLUGIN_DIR.'/'.$this->config['slug'] );
  204. return $data;
  205. }
  206. /**
  207. * Hook into the plugin update check and connect to github
  208. *
  209. * @since 1.0
  210. * @param object $transient the plugin data transient
  211. * @return object $transient updated plugin data transient
  212. */
  213. public function api_check( $transient ) {
  214. // Check if the transient contains the 'checked' information
  215. // If not, just return its value without hacking it
  216. if ( empty( $transient->checked ) )
  217. return $transient;
  218. // check the version and decide if it's new
  219. $update = version_compare( $this->config['new_version'], $this->config['version'] );
  220. if ( 1 === $update ) {
  221. $response = new stdClass;
  222. $response->new_version = $this->config['new_version'];
  223. $response->slug = $this->config['proper_folder_name'];
  224. $response->url = $this->config['github_url'];
  225. $response->package = $this->config['zip_url'];
  226. // If response is false, don't alter the transient
  227. if ( false !== $response )
  228. $transient->response[ $this->config['slug'] ] = $response;
  229. }
  230. return $transient;
  231. }
  232. /**
  233. * Get Plugin info
  234. *
  235. * @since 1.0
  236. * @param bool $false always false
  237. * @param string $action the API function being performed
  238. * @param object $args plugin arguments
  239. * @return object $response the plugin info
  240. */
  241. public function get_plugin_info( $false, $action, $response ) {
  242. // Check if this call API is for the right plugin
  243. if ( $response->slug != $this->config['slug'] )
  244. return false;
  245. $response->slug = $this->config['slug'];
  246. $response->plugin_name = $this->config['plugin_name'];
  247. $response->version = $this->config['new_version'];
  248. $response->author = $this->config['author'];
  249. $response->homepage = $this->config['homepage'];
  250. $response->requires = $this->config['requires'];
  251. $response->tested = $this->config['tested'];
  252. $response->downloaded = 0;
  253. $response->last_updated = $this->config['last_updated'];
  254. $response->sections = array( 'description' => $this->config['description'] );
  255. $response->download_link = $this->config['zip_url'];
  256. return $response;
  257. }
  258. /**
  259. * Upgrader/Updater
  260. * Move & activate the plugin, echo the update message
  261. *
  262. * @since 1.0
  263. * @param boolean $true always true
  264. * @param mixed $hook_extra not used
  265. * @param array $result the result of the move
  266. * @return array $result the result of the move
  267. */
  268. public function upgrader_post_install( $true, $hook_extra, $result ) {
  269. global $wp_filesystem;
  270. // Move & Activate
  271. $proper_destination = WP_PLUGIN_DIR.'/'.$this->config['proper_folder_name'];
  272. $wp_filesystem->move( $result['destination'], $proper_destination );
  273. $result['destination'] = $proper_destination;
  274. $activate = activate_plugin( WP_PLUGIN_DIR.'/'.$this->config['slug'] );
  275. // Output the update message
  276. $fail = __('The plugin has been updated, but could not be reactivated. Please reactivate it manually.', 'github_plugin_updater');
  277. $success = __('Plugin reactivated successfully.', 'github_plugin_updater');
  278. echo is_wp_error( $activate ) ? $fail : $success;
  279. return $result;
  280. }
  281. }
  282. endif; // endif class exists