class-vc-updater.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * WPBakery WPBakery Page Builder updater
  7. *
  8. * @package WPBakeryPageBuilder
  9. *
  10. */
  11. /**
  12. * Vc updating manager.
  13. */
  14. class Vc_Updater {
  15. /**
  16. * @var string
  17. */
  18. protected $version_url = 'http://updates.wpbakery.com/';
  19. /**
  20. * Proxy URL that returns real download link
  21. *
  22. * @var string
  23. */
  24. protected $download_link_url = 'http://support.wpbakery.com/updates/download-link';
  25. /**
  26. * @var string
  27. */
  28. public $title = 'WPBakery Page Builder';
  29. /**
  30. * @var bool
  31. */
  32. protected $auto_updater;
  33. public function init() {
  34. add_filter( 'upgrader_pre_download', array(
  35. $this,
  36. 'preUpgradeFilter',
  37. ), 10, 4 );
  38. }
  39. /**
  40. * Setter for manager updater.
  41. *
  42. * @param Vc_Updating_Manager $updater
  43. */
  44. public function setUpdateManager( Vc_Updating_Manager $updater ) {
  45. $this->auto_updater = $updater;
  46. }
  47. /**
  48. * Getter for manager updater.
  49. *
  50. * @return Vc_Updating_Manager|bool
  51. */
  52. public function updateManager() {
  53. return $this->auto_updater;
  54. }
  55. /**
  56. * Get url for version validation
  57. * @return string
  58. */
  59. public function versionUrl() {
  60. return $this->version_url;
  61. }
  62. /**
  63. * Get unique, short-lived download link
  64. *
  65. * @return array|boolean JSON response or false if request failed
  66. */
  67. public function getDownloadUrl() {
  68. $url = $this->getUrl();
  69. // FIX SSL SNI
  70. $filter_add = true;
  71. if ( function_exists( 'curl_version' ) ) {
  72. $version = curl_version();
  73. if ( version_compare( $version['version'], '7.18', '>=' ) ) {
  74. $filter_add = false;
  75. }
  76. }
  77. if ( $filter_add ) {
  78. add_filter( 'https_ssl_verify', '__return_false' );
  79. }
  80. $response = wp_remote_get( $url, array( 'timeout' => 30 ) );
  81. if ( $filter_add ) {
  82. remove_filter( 'https_ssl_verify', '__return_false' );
  83. }
  84. if ( is_wp_error( $response ) ) {
  85. return false;
  86. }
  87. return json_decode( $response['body'], true );
  88. }
  89. /**
  90. * @return string
  91. */
  92. protected function getUrl() {
  93. $host = esc_url( vc_license()->getSiteUrl() );
  94. $key = rawurlencode( vc_license()->getLicenseKey() );
  95. $url = $this->download_link_url . '?product=vc&url=' . $host . '&key=' . $key . '&version=' . WPB_VC_VERSION;
  96. return $url;
  97. }
  98. /**
  99. * @return string|void
  100. */
  101. public static function getUpdaterUrl() {
  102. return vc_is_network_plugin() ? network_admin_url( 'admin.php?page=vc-updater' ) : admin_url( 'admin.php?page=vc-updater' );
  103. }
  104. /**
  105. * Get link to newest VC
  106. *
  107. * @param $reply
  108. * @param $package
  109. * @param WP_Upgrader $updater
  110. *
  111. * @return mixed|string|WP_Error
  112. */
  113. public function preUpgradeFilter( $reply, $package, $updater ) {
  114. $condition1 = isset( $updater->skin->plugin ) && vc_plugin_name() === $updater->skin->plugin;
  115. $condition2 = isset( $updater->skin->plugin_info ) && $updater->skin->plugin_info['Name'] === $this->title;
  116. if ( ! $condition1 && ! $condition2 ) {
  117. return $reply;
  118. }
  119. $res = $updater->fs_connect( array( WP_CONTENT_DIR ) );
  120. if ( ! $res ) {
  121. return new WP_Error( 'no_credentials', esc_html__( "Error! Can't connect to filesystem", 'js_composer' ) );
  122. }
  123. if ( ! vc_license()->isActivated() ) {
  124. if ( vc_is_as_theme() && vc_get_param( 'action' ) !== 'update-selected' ) {
  125. return false;
  126. }
  127. $url = self::getUpdaterUrl();
  128. return new WP_Error( 'no_credentials', 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' ) ) );
  129. }
  130. $updater->strings['downloading_package_url'] = esc_html__( 'Getting download link...', 'js_composer' );
  131. $updater->skin->feedback( 'downloading_package_url' );
  132. $response = $this->getDownloadUrl();
  133. if ( ! $response ) {
  134. return new WP_Error( 'no_credentials', esc_html__( 'Download link could not be retrieved', 'js_composer' ) );
  135. }
  136. if ( ! $response['status'] ) {
  137. return new WP_Error( 'no_credentials', $response['error'] );
  138. }
  139. $updater->strings['downloading_package'] = esc_html__( 'Downloading package...', 'js_composer' );
  140. $updater->skin->feedback( 'downloading_package' );
  141. $downloaded_archive = download_url( $response['url'] );
  142. if ( is_wp_error( $downloaded_archive ) ) {
  143. return $downloaded_archive;
  144. }
  145. $plugin_directory_name = dirname( vc_plugin_name() );
  146. // WP will use same name for plugin directory as archive name, so we have to rename it
  147. if ( basename( $downloaded_archive, '.zip' ) !== $plugin_directory_name ) {
  148. $new_archive_name = dirname( $downloaded_archive ) . '/' . $plugin_directory_name . time() . '.zip';
  149. if ( rename( $downloaded_archive, $new_archive_name ) ) {
  150. $downloaded_archive = $new_archive_name;
  151. }
  152. }
  153. return $downloaded_archive;
  154. }
  155. }