WPGitHubUpdater

The Pods - Custom Content Types and Fields WPGitHubUpdater class.

Defined (1)

The class is defined in the following location(s).

/includes/updater.php  
  1. class WPGitHubUpdater { 
  2.  
  3. /** 
  4. * Class Constructor 
  5. * @since 1.0 
  6. * @param array $config configuration 
  7. * @return void 
  8. */ 
  9. public function __construct ( $config = array() ) { 
  10.  
  11. global $wp_version; 
  12.  
  13. $defaults = array( 
  14. 'slug' => plugin_basename( __FILE__ ),  
  15. 'proper_folder_name' => dirname( plugin_basename( __FILE__ ) ),  
  16. 'api_url' => 'https://api.github.com/repos/jkudish/WordPress-GitHub-Plugin-Updater',  
  17. 'raw_url' => 'https://raw.github.com/jkudish/WordPress-GitHub-Plugin-Updater/master',  
  18. 'github_url' => 'https://github.com/jkudish/WordPress-GitHub-Plugin-Updater',  
  19. 'zip_url' => 'https://github.com/jkudish/WordPress-GitHub-Plugin-Updater/zipball/master',  
  20. 'sslverify' => true,  
  21. 'requires' => $wp_version,  
  22. 'tested' => $wp_version,  
  23. ); 
  24.  
  25. $this->config = wp_parse_args( $config, $defaults ); 
  26.  
  27. $this->set_defaults(); 
  28.  
  29. if ( ( defined( 'WP_DEBUG' ) && WP_DEBUG ) || ( defined( 'WP_GITHUB_FORCE_UPDATE' ) || WP_GITHUB_FORCE_UPDATE ) ) 
  30. add_action( 'init', array( $this, 'delete_transients' ) ); 
  31.  
  32. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'api_check' ) ); 
  33.  
  34. // Hook into the plugin details screen 
  35. add_filter( 'plugins_api', array( $this, 'get_plugin_info' ), 10, 3 ); 
  36. add_filter( 'upgrader_post_install', array( $this, 'upgrader_post_install' ), 10, 3 ); 
  37.  
  38. // set timeout 
  39. add_filter( 'http_request_timeout', array( $this, 'http_request_timeout' ) ); 
  40.  
  41. // set sslverify for zip download 
  42. add_filter( 'http_request_args', array( $this, 'http_request_sslverify' ), 10, 2 ); 
  43.  
  44. /** 
  45. * Set defaults 
  46. * @since 1.2 
  47. * @return void 
  48. */ 
  49. public function set_defaults () { 
  50.  
  51. if ( !isset( $this->config[ 'new_version' ] ) ) 
  52. $this->config[ 'new_version' ] = $this->get_new_version(); 
  53.  
  54. if ( !isset( $this->config[ 'last_updated' ] ) ) 
  55. $this->config[ 'last_updated' ] = $this->get_date(); 
  56.  
  57. if ( !isset( $this->config[ 'description' ] ) ) 
  58. $this->config[ 'description' ] = $this->get_description(); 
  59.  
  60. $plugin_data = $this->get_plugin_data(); 
  61. if ( !isset( $this->config[ 'plugin_name' ] ) ) 
  62. $this->config[ 'plugin_name' ] = $plugin_data[ 'Name' ]; 
  63.  
  64. if ( !isset( $this->config[ 'version' ] ) ) 
  65. $this->config[ 'version' ] = $plugin_data[ 'Version' ]; 
  66.  
  67. if ( !isset( $this->config[ 'author' ] ) ) 
  68. $this->config[ 'author' ] = $plugin_data[ 'Author' ]; 
  69.  
  70. if ( !isset( $this->config[ 'homepage' ] ) ) 
  71. $this->config[ 'homepage' ] = $plugin_data[ 'PluginURI' ]; 
  72.  
  73. /** 
  74. * Callback fn for the http_request_timeout filter 
  75. * @since 1.0 
  76. * @return int timeout value 
  77. */ 
  78. public function http_request_timeout () { 
  79. return 2; 
  80.  
  81. /** 
  82. * Callback fn for the http_request_args filter 
  83. * @param $args 
  84. * @param $url 
  85. * @return mixed 
  86. */ 
  87. public function http_request_sslverify ( $args, $url ) { 
  88. if ( $this->config[ 'zip_url' ] == $url ) 
  89. $args[ 'sslverify' ] = $this->config[ 'sslverify' ]; 
  90.  
  91. return $args; 
  92.  
  93. /** 
  94. * Delete transients (runs when WP_DEBUG is on) 
  95. * For testing purposes the site transient will be reset on each page load 
  96. * @since 1.0 
  97. * @return void 
  98. */ 
  99. public function delete_transients () { 
  100. delete_site_transient( 'update_plugins' ); 
  101. delete_site_transient( $this->config[ 'slug' ] . '_new_version' ); 
  102. delete_site_transient( $this->config[ 'slug' ] . '_github_data' ); 
  103. delete_site_transient( $this->config[ 'slug' ] . '_changelog' ); 
  104.  
  105. /** 
  106. * Get New Version from github 
  107. * @since 1.0 
  108. * @return int $version the version number 
  109. */ 
  110. public function get_new_version () { 
  111. $version = get_site_transient( $this->config[ 'slug' ] . '_new_version' ); 
  112.  
  113. if ( !isset( $version ) || !$version || '' == $version ) { 
  114.  
  115. $raw_response = wp_remote_get( 
  116. trailingslashit( $this->config[ 'raw_url' ] ) . 'readme.txt',  
  117. array( 
  118. 'sslverify' => $this->config[ 'sslverify' ],  
  119. ); 
  120.  
  121. $__version = false; 
  122.  
  123. if ( !is_wp_error( $raw_response ) ) { 
  124. $__version = explode( '~Current Version:', $raw_response[ 'body' ] ); 
  125.  
  126. if ( !isset( $__version[ '1' ] ) ) 
  127. $__version = false; 
  128.  
  129. if ( !$__version || is_wp_error( $raw_response ) ) { 
  130. $raw_response = wp_remote_get( 
  131. trailingslashit( $this->config[ 'raw_url' ] ) . 'README.md',  
  132. array( 
  133. 'sslverify' => $this->config[ 'sslverify' ],  
  134. ); 
  135.  
  136. if ( is_wp_error( $raw_response ) ) 
  137. return false; 
  138.  
  139. $__version = explode( '~Current Version:', $raw_response[ 'body' ] ); 
  140.  
  141. if ( !isset( $__version[ '1' ] ) ) 
  142. return false; 
  143.  
  144. $_version = explode( '~', $__version[ '1' ] ); 
  145.  
  146. $version = trim( $_version[ 0 ] ); 
  147.  
  148. // refresh every 6 hours 
  149. set_site_transient( $this->config[ 'slug' ] . '_new_version', $version, 60 * 60 * 6 ); 
  150.  
  151. return $version; 
  152.  
  153. /** 
  154. * Get GitHub Data from the specified repository 
  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.  
  161. if ( !isset( $github_data ) || !$github_data || '' == $github_data ) { 
  162. $github_data = wp_remote_get( 
  163. $this->config[ 'api_url' ],  
  164. array( 
  165. 'sslverify' => $this->config[ 'sslverify' ],  
  166. ); 
  167.  
  168. if ( is_wp_error( $github_data ) ) 
  169. return false; 
  170.  
  171. $github_data = json_decode( $github_data[ 'body' ] ); 
  172.  
  173. // refresh every 6 hours 
  174. set_site_transient( $this->config[ 'slug' ] . '_github_data', $github_data, 60 * 60 * 6 ); 
  175.  
  176. return $github_data; 
  177.  
  178. /** 
  179. * Get update date 
  180. * @since 1.0 
  181. * @return string $date the date 
  182. */ 
  183. public function get_date () { 
  184. $_date = $this->get_github_data(); 
  185. return ( !empty( $_date->updated_at ) ) ? date( 'Y-m-d', strtotime( $_date->updated_at ) ) : false; 
  186.  
  187. /** 
  188. * Get plugin description 
  189. * @since 1.0 
  190. * @return string $description the description 
  191. */ 
  192. public function get_description () { 
  193. $_description = $this->get_github_data(); 
  194. return ( !empty( $_description->description ) ) ? $_description->description : false; 
  195.  
  196. /** 
  197. * Get Plugin data 
  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. * @since 1.0 
  209. * @param object $transient the plugin data transient 
  210. * @return object $transient updated plugin data transient 
  211. */ 
  212. public function api_check ( $transient ) { 
  213.  
  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.  
  219. // check the version and decide if it's new 
  220. $update = version_compare( $this->config[ 'new_version' ], $this->config[ 'version' ] ); 
  221.  
  222. if ( 1 === $update ) { 
  223. $response = new stdClass; 
  224. $response->new_version = $this->config[ 'new_version' ]; 
  225. $response->slug = $this->config[ 'proper_folder_name' ]; 
  226. $response->url = $this->config[ 'github_url' ]; 
  227. $response->package = $this->config[ 'zip_url' ]; 
  228.  
  229. // If response is false, don't alter the transient 
  230. if ( false !== $response ) 
  231. $transient->response[ $this->config[ 'slug' ] ] = $response; 
  232.  
  233. return $transient; 
  234.  
  235. /** 
  236. * Get Plugin info 
  237. * @since 1.0 
  238. * @param bool $false always false 
  239. * @param string $action the API function being performed 
  240. * @param object $args plugin arguments 
  241. * @return object $response the plugin info 
  242. */ 
  243. public function get_plugin_info ( $false, $action, $response ) { 
  244.  
  245. // Check if this call API is for the right plugin 
  246. if ( !is_object( $response ) || !isset( $response->slug ) || $response->slug != $this->config[ 'slug' ] ) 
  247. return false; 
  248.  
  249. $response->slug = $this->config[ 'slug' ]; 
  250. $response->plugin_name = $this->config[ 'plugin_name' ]; 
  251. $response->version = $this->config[ 'new_version' ]; 
  252. $response->author = $this->config[ 'author' ]; 
  253. $response->homepage = $this->config[ 'homepage' ]; 
  254. $response->requires = $this->config[ 'requires' ]; 
  255. $response->tested = $this->config[ 'tested' ]; 
  256. $response->downloaded = 0; 
  257. $response->last_updated = $this->config[ 'last_updated' ]; 
  258. $response->sections = array( 'description' => $this->config[ 'description' ] ); 
  259. $response->download_link = $this->config[ 'zip_url' ]; 
  260.  
  261. return $response; 
  262.  
  263. /** 
  264. * Upgrader/Updater 
  265. * Move & activate the plugin, echo the update message 
  266. * @since 1.0 
  267. * @param boolean $true always true 
  268. * @param mixed $hook_extra not used 
  269. * @param array $result the result of the move 
  270. * @return array $result the result of the move 
  271. */ 
  272. public function upgrader_post_install ( $true, $hook_extra, $result ) { 
  273.  
  274. global $wp_filesystem; 
  275.  
  276. // Move & Activate 
  277. $proper_destination = WP_PLUGIN_DIR . '/' . $this->config[ 'proper_folder_name' ]; 
  278. $wp_filesystem->move( $result[ 'destination' ], $proper_destination ); 
  279. $result[ 'destination' ] = $proper_destination; 
  280. $activate = activate_plugin( WP_PLUGIN_DIR . '/' . $this->config[ 'slug' ] ); 
  281.  
  282. // Output the update message 
  283. $fail = __( 'The plugin has been updated, but could not be reactivated. Please reactivate it manually.', 'pods' ); 
  284. $success = __( 'Plugin reactivated successfully.', 'pods' ); 
  285. echo is_wp_error( $activate ) ? $fail : $success; 
  286. return $result; 
  287.  
  288.