PageRenderTime 44ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/ninja-forms/includes/Integrations/EDD/EDD_SL_Plugin_Updater.php

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