/wp-content/plugins/js_composer/include/classes/updaters/class-vc-updater.php

https://gitlab.com/juanito.abelo/nlmobile · PHP · 142 lines · 84 code · 8 blank · 50 comment · 4 complexity · 868891b8a2b48eacaf08d7c60a603cec MD5 · raw file

  1. <?php
  2. /**
  3. * WPBakery Visual Composer updater
  4. *
  5. * @package WPBakeryVisualComposer
  6. *
  7. */
  8. /**
  9. * Vc updating manager.
  10. */
  11. class Vc_Updater {
  12. /**
  13. * @var string
  14. */
  15. protected $version_url = 'http://wpbakery.com/version/?';
  16. /**
  17. * @var string
  18. */
  19. public $title = 'WPBakery Visual Composer';
  20. /**
  21. * @var bool
  22. */
  23. protected $auto_updater = false;
  24. /**
  25. * @var bool
  26. */
  27. protected $upgrade_manager = false;
  28. /**
  29. * @var bool
  30. */
  31. protected $iframe = false;
  32. /**
  33. *
  34. */
  35. public function init() {
  36. add_filter( 'upgrader_pre_download', array( $this, 'upgradeFilterFromEnvato' ), 10, 4 );
  37. add_action( 'upgrader_process_complete', array( $this, 'removeTemporaryDir' ) );
  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
  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 . time();
  61. }
  62. /**
  63. * Downloads new VC from Envato marketplace and unzips into temporary directory.
  64. *
  65. * @param $reply
  66. * @param $package
  67. * @param $updater
  68. *
  69. * @return mixed|string|WP_Error
  70. */
  71. public function upgradeFilterFromEnvato( $reply, $package, $updater ) {
  72. global $wp_filesystem;
  73. if ( ( isset( $updater->skin->plugin ) && $updater->skin->plugin === vc_plugin_name() ) ||
  74. ( isset( $updater->skin->plugin_info ) && $updater->skin->plugin_info['Name'] === $this->title )
  75. ) {
  76. $updater->strings['download_envato'] = __( 'Downloading package from envato market...', 'js_composer' );
  77. $updater->skin->feedback( 'download_envato' );
  78. $package_filename = 'js_composer.zip';
  79. $res = $updater->fs_connect( array( WP_CONTENT_DIR ) );
  80. if ( ! $res ) {
  81. return new WP_Error( 'no_credentials', __( "Error! Can't connect to filesystem", 'js_composer' ) );
  82. }
  83. $username = vc_settings()->get( 'envato_username' );
  84. $api_key = vc_settings()->get( 'envato_api_key' );
  85. $purchase_code = vc_settings()->get( 'js_composer_purchase_code' );
  86. if ( ! vc_license()->isActivated() || empty( $username ) || empty( $api_key ) || empty( $purchase_code ) ) {
  87. return new WP_Error( 'no_credentials', __( 'To receive automatic updates license activation is required. Please visit <a href="' . admin_url( 'options-general.php?page=vc_settings&tab=updater' ) . '' . '" target="_blank">Settings</a> to activate your Visual Composer.', 'js_composer' ) );
  88. }
  89. $json = wp_remote_get( $this->envatoDownloadPurchaseUrl( $username, $api_key, $purchase_code ) );
  90. $result = json_decode( $json['body'], true );
  91. if ( ! isset( $result['download-purchase']['download_url'] ) ) {
  92. return new WP_Error( 'no_credentials', __( 'Error! Envato API error' . ( isset( $result['error'] ) ? ': ' . $result['error'] : '.' ), 'js_composer' ) );
  93. }
  94. $result['download-purchase']['download_url'];
  95. $download_file = download_url( $result['download-purchase']['download_url'] );
  96. if ( is_wp_error( $download_file ) ) {
  97. return $download_file;
  98. }
  99. $upgrade_folder = $wp_filesystem->wp_content_dir() . 'uploads/js_composer_envato_package';
  100. if ( is_dir( $upgrade_folder ) ) {
  101. $wp_filesystem->delete( $upgrade_folder );
  102. }
  103. $result = unzip_file( $download_file, $upgrade_folder );
  104. if ( $result && is_file( $upgrade_folder . '/' . $package_filename ) ) {
  105. return $upgrade_folder . '/' . $package_filename;
  106. }
  107. return new WP_Error( 'no_credentials', __( 'Error on unzipping package', 'js_composer' ) );
  108. }
  109. return $reply;
  110. }
  111. /**
  112. *
  113. */
  114. public function removeTemporaryDir() {
  115. global $wp_filesystem;
  116. if ( is_dir( $wp_filesystem->wp_content_dir() . 'uploads/js_composer_envato_package' ) ) {
  117. $wp_filesystem->delete( $wp_filesystem->wp_content_dir() . 'uploads/js_composer_envato_package', true );
  118. }
  119. }
  120. /**
  121. * @param $username
  122. * @param $api_key
  123. * @param $purchase_code
  124. *
  125. * @return string
  126. */
  127. protected function envatoDownloadPurchaseUrl( $username, $api_key, $purchase_code ) {
  128. return 'http://marketplace.envato.com/api/edge/' . rawurlencode( $username ) . '/' . rawurlencode( $api_key ) . '/download-purchase:' . rawurlencode( $purchase_code ) . '.json';
  129. }
  130. }