PageRenderTime 54ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/gravityforms 2/includes/addon/class-gf-auto-upgrade.php

https://gitlab.com/plusplusminus/aevitas
PHP | 256 lines | 198 code | 53 blank | 5 comment | 38 complexity | 757904ac03313d8a0995578d3f6df423 MD5 | raw file
  1. <?php
  2. if ( ! class_exists( 'GFForms' ) ) {
  3. die();
  4. }
  5. class GFAutoUpgrade {
  6. protected $_version;
  7. protected $_min_gravityforms_version;
  8. protected $_slug;
  9. protected $_title;
  10. protected $_full_path;
  11. protected $_path;
  12. protected $_url;
  13. protected $_is_gravityforms_supported;
  14. public function __construct( $slug, $version, $min_gravityforms_version, $title, $full_path, $path, $url, $is_gravityforms_supported ) {
  15. $this->_slug = $slug;
  16. $this->_version = $version;
  17. $this->_min_gravityforms_version = $min_gravityforms_version;
  18. $this->_title = $title;
  19. $this->_full_path = $full_path;
  20. $this->_path = $path;
  21. $this->_url = $url;
  22. $this->_is_gravityforms_supported = $is_gravityforms_supported;
  23. add_action( 'init', array( $this, 'init' ) );
  24. }
  25. public function init() {
  26. if ( is_admin() ) {
  27. load_plugin_textdomain( $this->_slug, false, $this->_slug . '/languages' );
  28. add_filter( 'transient_update_plugins', array( $this, 'check_update' ) );
  29. add_filter( 'site_transient_update_plugins', array( $this, 'check_update' ) );
  30. add_action( 'install_plugins_pre_plugin-information', array( $this, 'display_changelog' ) );
  31. add_action( 'gform_after_check_update', array( $this, 'flush_version_info' ) );
  32. add_action( 'gform_updates', array( $this, 'display_updates' ) );
  33. if ( RG_CURRENT_PAGE == 'plugins.php' ) {
  34. add_action( 'after_plugin_row_' . $this->_path, array( $this, 'rg_plugin_row' ) );
  35. }
  36. }
  37. // ManageWP premium update filters
  38. add_filter( 'mwp_premium_update_notification', array( $this, 'premium_update_push' ) );
  39. add_filter( 'mwp_premium_perform_update', array( $this, 'premium_update' ) );
  40. }
  41. public function rg_plugin_row() {
  42. if ( ! $this->_is_gravityforms_supported ) {
  43. $message = sprintf( __( 'Gravity Forms ' . $this->_min_gravityforms_version . ' is required. Activate it now or %spurchase it today!%s', 'gravityforms' ), "<a href='http://www.gravityforms.com'>", '</a>' );
  44. GFAddOn::display_plugin_message( $message, true );
  45. } else {
  46. $version_info = $this->get_version_info( $this->_slug );
  47. if ( ! rgar( $version_info, 'is_valid_key' ) ) {
  48. $title = $this->_title;
  49. $new_version = version_compare( $this->_version, $version_info['version'], '<' ) ? __( "There is a new version of {$title} available.", 'gravityforms' ) . " <a class='thickbox' title='{$title}' href='plugin-install.php?tab=plugin-information&plugin=" . $this->_slug . "&TB_iframe=true&width=640&height=808'>" . sprintf( __( 'View version %s Details', 'gravityforms' ), $version_info['version'] ) . '</a>. ' : '';
  50. $message = $new_version . sprintf( __( '%sRegister%s your copy of Gravity Forms to receive access to automatic upgrades and support. Need a license key? %sPurchase one now%s.', 'gravityforms' ), '<a href="admin.php?page=gf_settings">', '</a>', '<a href="http://www.gravityforms.com">', '</a>' ) . '</div></td>';
  51. GFAddOn::display_plugin_message( $message );
  52. }
  53. }
  54. }
  55. //Integration with ManageWP
  56. public function premium_update_push( $premium_update ) {
  57. if ( ! function_exists( 'get_plugin_data' ) ) {
  58. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  59. }
  60. $update = $this->get_version_info( $this->_slug );
  61. if ( rgar( $update, 'is_valid_key' ) == true && version_compare( $this->_version, $update['version'], '<' ) ) {
  62. $plugin_data = get_plugin_data( $this->_full_path );
  63. $plugin_data['type'] = 'plugin';
  64. $plugin_data['slug'] = $this->_path;
  65. $plugin_data['new_version'] = isset( $update['version'] ) ? $update['version'] : false;
  66. $premium_update[] = $plugin_data;
  67. }
  68. return $premium_update;
  69. }
  70. //Integration with ManageWP
  71. public function premium_update( $premium_update ) {
  72. if ( ! function_exists( 'get_plugin_data' ) ) {
  73. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  74. }
  75. $update = $this->get_version_info( $this->_slug );
  76. if ( rgar( $update, 'is_valid_key' ) == true && version_compare( $this->_version, $update['version'], '<' ) ) {
  77. $plugin_data = get_plugin_data( $this->_full_path );
  78. $plugin_data['slug'] = $this->_path;
  79. $plugin_data['type'] = 'plugin';
  80. $plugin_data['url'] = isset( $update['url'] ) ? $update['url'] : false; // OR provide your own callback function for managing the update
  81. array_push( $premium_update, $plugin_data );
  82. }
  83. return $premium_update;
  84. }
  85. public function flush_version_info() {
  86. $this->set_version_info( $this->_slug, false );
  87. }
  88. private function set_version_info( $plugin_slug, $version_info ) {
  89. if ( function_exists( 'set_site_transient' ) ) {
  90. set_site_transient( $plugin_slug . '_version', $version_info, 60 * 60 * 12 );
  91. } else {
  92. set_transient( $plugin_slug . '_version', $version_info, 60 * 60 * 12 );
  93. }
  94. }
  95. public function check_update( $option ) {
  96. $key = $this->get_key();
  97. $version_info = $this->get_version_info( $this->_slug );
  98. if ( rgar( $version_info, 'is_error' ) == '1' ) {
  99. return $option;
  100. }
  101. if ( empty( $option->response[ $this->_path ] ) ) {
  102. $option->response[ $this->_path ] = new stdClass();
  103. }
  104. //Empty response means that the key is invalid. Do not queue for upgrade
  105. if ( ! rgar( $version_info, 'is_valid_key' ) || version_compare( $this->_version, $version_info['version'], '>=' ) ) {
  106. unset( $option->response[ $this->_path ] );
  107. } else {
  108. $option->response[ $this->_path ]->url = $this->_url;
  109. $option->response[ $this->_path ]->slug = $this->_slug;
  110. $option->response[ $this->_path ]->package = str_replace( '{KEY}', $key, $version_info['url'] );
  111. $option->response[ $this->_path ]->new_version = $version_info['version'];
  112. $option->response[ $this->_path ]->id = '0';
  113. }
  114. return $option;
  115. }
  116. private function display_upgrade_message( $plugin_name, $plugin_title, $version, $message, $localization_namespace ) {
  117. $upgrade_message = $message . ' <a class="thickbox" title="' . $plugin_title . '" href="plugin-install.php?tab=plugin-information&plugin=' . $plugin_name . '&TB_iframe=true&width=640&height=808">' . sprintf( __( 'View version %s Details', $localization_namespace ), $version ) . '</a>. ';
  118. GFAddOn::display_plugin_message( $upgrade_message );
  119. }
  120. // Displays current version details on plugins page and updates page
  121. public function display_changelog() {
  122. if ( $_REQUEST['plugin'] != $this->_slug ) {
  123. return;
  124. }
  125. $change_log = $this->get_changelog();
  126. echo $change_log;
  127. exit;
  128. }
  129. private function get_changelog() {
  130. $key = $this->get_key();
  131. $body = "key={$key}";
  132. $options = array( 'method' => 'POST', 'timeout' => 3, 'body' => $body );
  133. $options['headers'] = array(
  134. 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
  135. 'Content-Length' => strlen( $body ),
  136. 'User-Agent' => 'WordPress/' . get_bloginfo( 'version' ),
  137. 'Referer' => get_bloginfo( 'url' )
  138. );
  139. $raw_response = GFCommon::post_to_manager( 'changelog.php', $this->get_remote_request_params( $this->_slug, $key, $this->_version ), $options );
  140. if ( is_wp_error( $raw_response ) || 200 != $raw_response['response']['code'] ) {
  141. $text = sprintf( __( 'Oops!! Something went wrong.%sPlease try again or %scontact us%s.', 'gravityforms' ), '<br/>', "<a href='http://www.gravityforms.com'>", '</a>' );
  142. } else {
  143. $text = $raw_response['body'];
  144. if ( substr( $text, 0, 10 ) != '<!--GFM-->' ) {
  145. $text = '';
  146. }
  147. }
  148. return stripslashes( $text );
  149. }
  150. private function get_version_info( $offering, $use_cache = true ) {
  151. $version_info = GFCommon::get_version_info( $use_cache );
  152. $is_valid_key = rgar( $version_info, 'is_valid_key' ) && rgars( $version_info, "offerings/{$offering}/is_available" );
  153. $info = array( 'is_valid_key' => $is_valid_key, 'version' => rgars( $version_info, "offerings/{$offering}/version" ), 'url' => rgars( $version_info, "offerings/{$offering}/url" ) );
  154. return $info;
  155. }
  156. private function get_remote_request_params( $offering, $key, $version ) {
  157. global $wpdb;
  158. return sprintf( 'of=%s&key=%s&v=%s&wp=%s&php=%s&mysql=%s', urlencode( $offering ), urlencode( $key ), urlencode( $version ), urlencode( get_bloginfo( 'version' ) ), urlencode( phpversion() ), urlencode( $wpdb->db_version() ) );
  159. }
  160. private function get_key() {
  161. if ( $this->_is_gravityforms_supported ) {
  162. return GFCommon::get_key();
  163. } else {
  164. return '';
  165. }
  166. }
  167. public function display_updates() {
  168. ?>
  169. <div class="wrap <?php echo GFCommon::get_browser_class() ?>">
  170. <h2><?php echo $this->_title ?></h2>
  171. <?php
  172. $force_check = rgget( 'force-check' ) == 1;
  173. $version_info = $this->get_version_info( $this->_slug, ! $force_check );
  174. if ( ! rgar( $version_info, 'is_valid_key' ) ) {
  175. ?>
  176. <div class="gf_update_expired alert_red">
  177. <?php _e( sprintf( '%sRegister%s your copy of Gravity Forms to receive access to automatic updates and support. Need a license key? %sPurchase one now%s.', '<a href="admin.php?page=gf_settings">','</a>','<a href="http://www.gravityforms.com">', '</a>' ), 'gravityforms' ); ?>
  178. </div>
  179. <?php
  180. } elseif ( version_compare( $this->_version, $version_info['version'], '<' ) ) {
  181. if ( rgar( $version_info, 'is_valid_key' ) ) {
  182. $plugin_file = $this->_path;
  183. $upgrade_url = wp_nonce_url( 'update.php?action=upgrade-plugin&amp;plugin=' . urlencode( $plugin_file ), 'upgrade-plugin_' . $plugin_file );
  184. $details_url = self_admin_url( 'plugin-install.php?tab=plugin-information&plugin=' . urlencode( $this->_slug ) . '&section=changelog&TB_iframe=true&width=600&height=800' );
  185. $message = sprintf( __( 'There is a new version of %1$s available. <a href="%2$s" class="thickbox" title="%3$s">View version %4$s details</a>.', 'gravityforms' ), $this->_title, esc_url( $details_url ), esc_attr( $this->_title ), $version_info['version'] );
  186. ?>
  187. <div class="gf_update_outdated alert_yellow">
  188. <?php echo $message . ' ' . sprintf( __( '<p>You can update to the latest version automatically or download the update and install it manually. %sUpdate Automatically%s %sDownload Update%s', 'gravityforms' ), "</p><a class='button-primary' href='{$upgrade_url}'>", '</a>', "&nbsp;<a class='button' href='{$version_info['url']}'>", '</a>' ); ?>
  189. </div>
  190. <?php
  191. }
  192. } else {
  193. ?>
  194. <div class="gf_update_current alert_green">
  195. <?php printf( __( 'Your version of %s is up to date.', 'gravityforms' ), $this->_title ); ?>
  196. </div>
  197. <?php
  198. }
  199. ?>
  200. </div>
  201. <?php
  202. }
  203. }