class-vc-updating-manager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Manage update messages and Plugins info for VC in default WordPress plugins list.
  7. */
  8. class Vc_Updating_Manager {
  9. /**
  10. * The plugin current version
  11. *
  12. * @var string
  13. */
  14. public $current_version;
  15. /**
  16. * The plugin remote update path
  17. *
  18. * @var string
  19. */
  20. public $update_path;
  21. /**
  22. * Plugin Slug (plugin_directory/plugin_file.php)
  23. *
  24. * @var string
  25. */
  26. public $plugin_slug;
  27. /**
  28. * Plugin name (plugin_file)
  29. *
  30. * @var string
  31. */
  32. public $slug;
  33. /**
  34. * Link to download VC.
  35. * @var string
  36. */
  37. protected $url = 'http://go.wpbakery.com/wpb-buy';
  38. /**
  39. * Initialize a new instance of the WordPress Auto-Update class
  40. *
  41. * @param string $current_version
  42. * @param string $update_path
  43. * @param string $plugin_slug
  44. */
  45. public function __construct( $current_version, $update_path, $plugin_slug ) {
  46. // Set the class public variables
  47. $this->current_version = $current_version;
  48. $this->update_path = $update_path;
  49. $this->plugin_slug = $plugin_slug;
  50. $t = explode( '/', $plugin_slug );
  51. $this->slug = str_replace( '.php', '', $t[1] );
  52. // define the alternative API for updating checking
  53. add_filter( 'pre_set_site_transient_update_plugins', array(
  54. $this,
  55. 'check_update',
  56. ) );
  57. // Define the alternative response for information checking
  58. add_filter( 'plugins_api', array(
  59. $this,
  60. 'check_info',
  61. ), 10, 3 );
  62. add_action( 'in_plugin_update_message-' . vc_plugin_name(), array(
  63. $this,
  64. 'addUpgradeMessageLink',
  65. ) );
  66. }
  67. /**
  68. * Add our self-hosted autoupdate plugin to the filter transient
  69. *
  70. * @param $transient
  71. *
  72. * @return object $ transient
  73. */
  74. public function check_update( $transient ) {
  75. // Extra check for 3rd plugins
  76. if ( isset( $transient->response[ $this->plugin_slug ] ) ) {
  77. return $transient;
  78. }
  79. // Get the remote version
  80. $remote_version = $this->getRemote_version();
  81. // If a newer version is available, add the update
  82. if ( version_compare( $this->current_version, $remote_version, '<' ) ) {
  83. $obj = new stdClass();
  84. $obj->slug = $this->slug;
  85. $obj->new_version = $remote_version;
  86. $obj->plugin = $this->plugin_slug;
  87. $obj->url = '';
  88. $obj->package = vc_license()->isActivated();
  89. $obj->name = vc_updater()->title;
  90. $transient->response[ $this->plugin_slug ] = $obj;
  91. }
  92. return $transient;
  93. }
  94. /**
  95. * Add our self-hosted description to the filter
  96. *
  97. * @param bool $false
  98. * @param array $action
  99. * @param object $arg
  100. *
  101. * @return bool|object
  102. */
  103. public function check_info( $false, $action, $arg ) {
  104. if ( isset( $arg->slug ) && $arg->slug === $this->slug ) {
  105. $information = $this->getRemote_information();
  106. $array_pattern = array(
  107. '/^([\*\s])*(\d\d\.\d\d\.\d\d\d\d[^\n]*)/m',
  108. '/^\n+|^[\t\s]*\n+/m',
  109. '/\n/',
  110. );
  111. $array_replace = array(
  112. '<h4>$2</h4>',
  113. '</div><div>',
  114. '</div><div>',
  115. );
  116. $information->name = vc_updater()->title;
  117. $information->sections = (array) $information->sections;
  118. $information->sections['changelog'] = '<div>' . preg_replace( $array_pattern, $array_replace, $information->sections['changelog'] ) . '</div>';
  119. return $information;
  120. }
  121. return $false;
  122. }
  123. /**
  124. * Return the remote version
  125. *
  126. * @return string $remote_version
  127. */
  128. public function getRemote_version() {
  129. // FIX SSL SNI
  130. $filter_add = true;
  131. if ( function_exists( 'curl_version' ) ) {
  132. $version = curl_version();
  133. if ( version_compare( $version['version'], '7.18', '>=' ) ) {
  134. $filter_add = false;
  135. }
  136. }
  137. if ( $filter_add ) {
  138. add_filter( 'https_ssl_verify', '__return_false' );
  139. }
  140. $request = wp_remote_get( $this->update_path, array( 'timeout' => 30 ) );
  141. if ( $filter_add ) {
  142. remove_filter( 'https_ssl_verify', '__return_false' );
  143. }
  144. if ( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
  145. return $request['body'];
  146. }
  147. return false;
  148. }
  149. /**
  150. * Get information about the remote version
  151. *
  152. * @return bool|object
  153. */
  154. public function getRemote_information() {
  155. // FIX SSL SNI
  156. $filter_add = true;
  157. if ( function_exists( 'curl_version' ) ) {
  158. $version = curl_version();
  159. if ( version_compare( $version['version'], '7.18', '>=' ) ) {
  160. $filter_add = false;
  161. }
  162. }
  163. if ( $filter_add ) {
  164. add_filter( 'https_ssl_verify', '__return_false' );
  165. }
  166. $request = wp_remote_get( $this->update_path . 'information.json', array( 'timeout' => 30 ) );
  167. if ( $filter_add ) {
  168. remove_filter( 'https_ssl_verify', '__return_false' );
  169. }
  170. if ( ! is_wp_error( $request ) || wp_remote_retrieve_response_code( $request ) === 200 ) {
  171. return json_decode( $request['body'] );
  172. }
  173. return false;
  174. }
  175. /**
  176. * Shows message on Wp plugins page with a link for updating from envato.
  177. */
  178. public function addUpgradeMessageLink() {
  179. $is_activated = vc_license()->isActivated();
  180. if ( ! $is_activated ) {
  181. $url = vc_updater()->getUpdaterUrl();
  182. echo sprintf( ' ' . esc_html__( 'To receive automatic updates license activation is required. Please visit %ssettings%s to activate your WPBakery Page Builder.', 'js_composer' ), '<a href="' . esc_url( $url ) . '" target="_blank">', '</a>' ) . sprintf( ' <a href="https://go.wpbakery.com/faq-update-in-theme" target="_blank">%s</a>', esc_html__( 'Got WPBakery Page Builder in theme?', 'js_composer' ) );
  183. }
  184. }
  185. }