GFAutoUpgrade

The Gravity Forms GFAutoUpgrade class.

Defined (1)

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

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