EDD_SL_Plugin_Updater.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. // uncomment this line for testing
  3. //set_site_transient( 'update_plugins', null );
  4. // Exit if accessed directly
  5. if ( ! defined( 'ABSPATH' ) ) exit;
  6. /**
  7. * Allows plugins to use their own update API.
  8. *
  9. * @author Pippin Williamson
  10. * @version 1.6.5
  11. */
  12. class EDD_SL_Plugin_Updater {
  13. private $api_url = '';
  14. private $api_data = array();
  15. private $name = '';
  16. private $slug = '';
  17. private $version = '';
  18. private $wp_override = false;
  19. /**
  20. * Class constructor.
  21. *
  22. * @uses plugin_basename()
  23. * @uses hook()
  24. *
  25. * @param string $_api_url The URL pointing to the custom API endpoint.
  26. * @param string $_plugin_file Path to the plugin file.
  27. * @param array $_api_data Optional data to send with API calls.
  28. */
  29. public function __construct( $_api_url, $_plugin_file, $_api_data = null ) {
  30. global $edd_plugin_data;
  31. $this->api_url = trailingslashit( $_api_url );
  32. $this->api_data = $_api_data;
  33. $this->name = plugin_basename( $_plugin_file );
  34. $this->slug = basename( $_plugin_file, '.php' );
  35. $this->version = $_api_data['version'];
  36. $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false;
  37. $edd_plugin_data[ $this->slug ] = $this->api_data;
  38. // Set up hooks.
  39. $this->init();
  40. }
  41. /**
  42. * Set up WordPress filters to hook into WP's update process.
  43. *
  44. * @uses add_filter()
  45. *
  46. * @return void
  47. */
  48. public function init() {
  49. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
  50. add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
  51. remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 );
  52. add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 );
  53. add_action( 'admin_init', array( $this, 'show_changelog' ) );
  54. }
  55. /**
  56. * Check for Updates at the defined API endpoint and modify the update array.
  57. *
  58. * This function dives into the update API just when WordPress creates its update array,
  59. * then adds a custom API call and injects the custom plugin data retrieved from the API.
  60. * It is reassembled from parts of the native WordPress plugin update code.
  61. * See wp-includes/update.php line 121 for the original wp_update_plugins() function.
  62. *
  63. * @uses api_request()
  64. *
  65. * @param array $_transient_data Update array build by WordPress.
  66. * @return array Modified update array with custom plugin data.
  67. */
  68. public function check_update( $_transient_data ) {
  69. global $pagenow;
  70. if ( ! is_object( $_transient_data ) ) {
  71. $_transient_data = new stdClass;
  72. }
  73. if ( 'plugins.php' == $pagenow && is_multisite() ) {
  74. return $_transient_data;
  75. }
  76. if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) {
  77. return $_transient_data;
  78. }
  79. $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
  80. if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
  81. if ( version_compare( $this->version, $version_info->new_version, '<' ) ) {
  82. $_transient_data->response[ $this->name ] = $version_info;
  83. }
  84. $_transient_data->last_checked = current_time( 'timestamp' );
  85. $_transient_data->checked[ $this->name ] = $this->version;
  86. }
  87. return $_transient_data;
  88. }
  89. /**
  90. * show update nofication row -- needed for multisite subsites, because WP won't tell you otherwise!
  91. *
  92. * @param string $file
  93. * @param array $plugin
  94. */
  95. public function show_update_notification( $file, $plugin ) {
  96. if ( is_network_admin() ) {
  97. return;
  98. }
  99. if( ! current_user_can( 'update_plugins' ) ) {
  100. return;
  101. }
  102. if( ! is_multisite() ) {
  103. return;
  104. }
  105. if ( $this->name != $file ) {
  106. return;
  107. }
  108. // Remove our filter on the site transient
  109. remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 );
  110. $update_cache = get_site_transient( 'update_plugins' );
  111. $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass();
  112. if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) {
  113. $cache_key = md5( 'edd_plugin_' . sanitize_key( $this->name ) . '_version_info' );
  114. $version_info = get_transient( $cache_key );
  115. if( false === $version_info ) {
  116. $version_info = $this->api_request( 'plugin_latest_version', array( 'slug' => $this->slug ) );
  117. set_transient( $cache_key, $version_info, 3600 );
  118. }
  119. if( ! is_object( $version_info ) ) {
  120. return;
  121. }
  122. if( version_compare( $this->version, $version_info->new_version, '<' ) ) {
  123. $update_cache->response[ $this->name ] = $version_info;
  124. }
  125. $update_cache->last_checked = current_time( 'timestamp' );
  126. $update_cache->checked[ $this->name ] = $this->version;
  127. set_site_transient( 'update_plugins', $update_cache );
  128. } else {
  129. $version_info = $update_cache->response[ $this->name ];
  130. }
  131. // Restore our filter
  132. add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) );
  133. if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) {
  134. // build a plugin list row, with update notification
  135. $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
  136. # <tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange">
  137. echo '<tr class="plugin-update-tr" id="' . $this->slug . '-update" data-slug="' . $this->slug . '" data-plugin="' . $this->slug . '/' . $file . '">';
  138. echo '<td colspan="3" class="plugin-update colspanchange">';
  139. echo '<div class="update-message notice inline notice-warning notice-alt">';
  140. $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' );
  141. if ( empty( $version_info->download_link ) ) {
  142. printf(
  143. __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s.', 'easy-digital-downloads' ),
  144. esc_html( $version_info->name ),
  145. '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
  146. esc_html( $version_info->new_version ),
  147. '</a>'
  148. );
  149. } else {
  150. printf(
  151. __( 'There is a new version of %1$s available. %2$sView version %3$s details%4$s or %5$supdate now%6$s.', 'easy-digital-downloads' ),
  152. esc_html( $version_info->name ),
  153. '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">',
  154. esc_html( $version_info->new_version ),
  155. '</a>',
  156. '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) .'">',
  157. '</a>'
  158. );
  159. }
  160. do_action( "in_plugin_update_message-{$file}", $plugin, $version_info );
  161. echo '</div></td></tr>';
  162. }
  163. }
  164. /**
  165. * Updates information on the "View version x.x details" page with custom data.
  166. *
  167. * @uses api_request()
  168. *
  169. * @param mixed $_data
  170. * @param string $_action
  171. * @param object $_args
  172. * @return object $_data
  173. */
  174. public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
  175. if ( $_action != 'plugin_information' ) {
  176. return $_data;
  177. }
  178. if ( ! isset( $_args->slug ) || ( $_args->slug != $this->slug ) ) {
  179. return $_data;
  180. }
  181. $to_send = array(
  182. 'slug' => $this->slug,
  183. 'is_ssl' => is_ssl(),
  184. 'fields' => array(
  185. 'banners' => false, // These will be supported soon hopefully
  186. 'reviews' => false
  187. )
  188. );
  189. $cache_key = 'edd_api_request_' . substr( md5( serialize( $this->slug ) ), 0, 15 );
  190. //Get the transient where we store the api request for this plugin for 24 hours
  191. $edd_api_request_transient = get_site_transient( $cache_key );
  192. //If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now.
  193. if ( empty( $edd_api_request_transient ) ){
  194. $api_response = $this->api_request( 'plugin_information', $to_send );
  195. //Expires in 1 day
  196. set_site_transient( $cache_key, $api_response, DAY_IN_SECONDS );
  197. if ( false !== $api_response ) {
  198. $_data = $api_response;
  199. }
  200. }
  201. return $_data;
  202. }
  203. /**
  204. * Disable SSL verification in order to prevent download update failures
  205. *
  206. * @param array $args
  207. * @param string $url
  208. * @return object $array
  209. */
  210. public function http_request_args( $args, $url ) {
  211. // If it is an https request and we are performing a package download, disable ssl verification
  212. if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
  213. $args['sslverify'] = false;
  214. }
  215. return $args;
  216. }
  217. /**
  218. * Calls the API and, if successfull, returns the object delivered by the API.
  219. *
  220. * @uses get_bloginfo()
  221. * @uses wp_remote_post()
  222. * @uses is_wp_error()
  223. *
  224. * @param string $_action The requested action.
  225. * @param array $_data Parameters for the API action.
  226. * @return false|object
  227. */
  228. private function api_request( $_action, $_data ) {
  229. global $wp_version;
  230. $data = array_merge( $this->api_data, $_data );
  231. if ( $data['slug'] != $this->slug ) {
  232. return;
  233. }
  234. if( $this->api_url == trailingslashit (home_url() ) ) {
  235. return false; // Don't allow a plugin to ping itself
  236. }
  237. $api_params = array(
  238. 'edd_action' => 'get_version',
  239. 'license' => ! empty( $data['license'] ) ? $data['license'] : '',
  240. 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
  241. 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
  242. 'slug' => $data['slug'],
  243. 'author' => $data['author'],
  244. 'url' => home_url()
  245. );
  246. $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
  247. if ( ! is_wp_error( $request ) ) {
  248. $request = json_decode( wp_remote_retrieve_body( $request ) );
  249. }
  250. if ( $request && isset( $request->sections ) ) {
  251. $request->sections = maybe_unserialize( $request->sections );
  252. } else {
  253. $request = false;
  254. }
  255. return $request;
  256. }
  257. public function show_changelog() {
  258. global $edd_plugin_data;
  259. if( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' != $_REQUEST['edd_sl_action'] ) {
  260. return;
  261. }
  262. if( empty( $_REQUEST['plugin'] ) ) {
  263. return;
  264. }
  265. if( empty( $_REQUEST['slug'] ) ) {
  266. return;
  267. }
  268. if( ! current_user_can( 'update_plugins' ) ) {
  269. wp_die( __( 'You do not have permission to install plugin updates', 'easy-digital-downloads' ), __( 'Error', 'easy-digital-downloads' ), array( 'response' => 403 ) );
  270. }
  271. $data = $edd_plugin_data[ $_REQUEST['slug'] ];
  272. $cache_key = md5( 'edd_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_version_info' );
  273. $version_info = get_transient( $cache_key );
  274. if( false === $version_info ) {
  275. $api_params = array(
  276. 'edd_action' => 'get_version',
  277. 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false,
  278. 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false,
  279. 'slug' => $_REQUEST['slug'],
  280. 'author' => $data['author'],
  281. 'url' => home_url()
  282. );
  283. $request = wp_remote_post( $this->api_url, array( 'timeout' => 15, 'sslverify' => false, 'body' => $api_params ) );
  284. if ( ! is_wp_error( $request ) ) {
  285. $version_info = json_decode( wp_remote_retrieve_body( $request ) );
  286. }
  287. if ( ! empty( $version_info ) && isset( $version_info->sections ) ) {
  288. $version_info->sections = maybe_unserialize( $version_info->sections );
  289. } else {
  290. $version_info = false;
  291. }
  292. set_transient( $cache_key, $version_info, 3600 );
  293. }
  294. if( ! empty( $version_info ) && isset( $version_info->sections['changelog'] ) ) {
  295. echo '<div style="background:#fff;padding:10px;">' . $version_info->sections['changelog'] . '</div>';
  296. }
  297. exit;
  298. }
  299. }