update.class.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderUpdate {
  9. private $plugin_url = 'https://codecanyon.net/item/slider-revolution-responsive-wordpress-plugin/2751380';
  10. private $remote_url = 'check_for_updates.php';
  11. private $remote_url_info = 'revslider/revslider.php';
  12. private $remote_temp_active = 'temp_activate.php';
  13. private $plugin_slug = 'revslider';
  14. private $plugin_path = 'revslider/revslider.php';
  15. private $version;
  16. private $plugins;
  17. private $option;
  18. public function __construct($version) {
  19. $this->option = $this->plugin_slug . '_update_info';
  20. $this->_retrieve_version_info();
  21. $this->version = $version;
  22. }
  23. public function add_update_checks(){
  24. add_filter('pre_set_site_transient_update_plugins', array(&$this, 'set_update_transient'));
  25. add_filter('plugins_api', array(&$this, 'set_updates_api_results'), 10, 3);
  26. }
  27. public function set_update_transient($transient) {
  28. $this->_check_updates();
  29. if(isset($transient) && !isset($transient->response)) {
  30. $transient->response = array();
  31. }
  32. if(!empty($this->data->basic) && is_object($this->data->basic)) {
  33. if(version_compare($this->version, $this->data->basic->version, '<')) {
  34. $this->data->basic->new_version = $this->data->basic->version;
  35. $transient->response[$this->plugin_path] = $this->data->basic;
  36. }
  37. }
  38. return $transient;
  39. }
  40. public function set_updates_api_results($result, $action, $args) {
  41. $this->_check_updates();
  42. if(isset($args->slug) && $args->slug == $this->plugin_slug && $action == 'plugin_information') {
  43. if(is_object($this->data->full) && !empty($this->data->full)) {
  44. $result = $this->data->full;
  45. }
  46. }
  47. return $result;
  48. }
  49. protected function _check_updates() {
  50. //reset saved options
  51. //update_option($this->option, false);
  52. $force_check = false;
  53. if(isset($_GET['checkforupdates']) && $_GET['checkforupdates'] == 'true') $force_check = true;
  54. // Get data
  55. if(empty($this->data)) {
  56. $data = get_option($this->option, false);
  57. $data = $data ? $data : new stdClass;
  58. $this->data = is_object($data) ? $data : maybe_unserialize($data);
  59. }
  60. $last_check = get_option('revslider-update-check');
  61. if($last_check == false){ //first time called
  62. $last_check = time();
  63. update_option('revslider-update-check', $last_check);
  64. }
  65. // Check for updates
  66. if(time() - $last_check > 172800 || $force_check == true){
  67. $data = $this->_retrieve_update_info();
  68. if(isset($data->basic)) {
  69. update_option('revslider-update-check', time());
  70. $this->data->checked = time();
  71. $this->data->basic = $data->basic;
  72. $this->data->full = $data->full;
  73. update_option('revslider-stable-version', $data->full->stable);
  74. update_option('revslider-latest-version', $data->full->version);
  75. }
  76. }
  77. // Save results
  78. update_option($this->option, $this->data);
  79. }
  80. public function _retrieve_update_info() {
  81. global $wp_version, $rslb;
  82. $data = new stdClass;
  83. // Build request
  84. $code = get_option('revslider-code', '');
  85. $validated = get_option('revslider-valid', 'false');
  86. $stable_version = get_option('revslider-stable-version', '4.2');
  87. $rattr = array(
  88. 'code' => urlencode($code),
  89. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION)
  90. );
  91. if($validated !== 'true' && version_compare(RevSliderGlobals::SLIDER_REVISION, $stable_version, '<')){ //We'll get the last stable only now!
  92. $rattr['get_stable'] = 'true';
  93. }
  94. $done = false;
  95. $count = 0;
  96. do{
  97. $url = $rslb->get_url('updates');
  98. $request = wp_remote_post($url.'/'.$this->remote_url_info, array(
  99. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  100. 'body' => $rattr
  101. ));
  102. $response_code = wp_remote_retrieve_response_code( $request );
  103. if($response_code == 200){
  104. $done = true;
  105. }else{
  106. $rslb->move_server_list();
  107. }
  108. $count++;
  109. }while($done == false && $count < 5);
  110. if(!is_wp_error($request)) {
  111. if($response = maybe_unserialize($request['body'])) {
  112. if(is_object($response)) {
  113. $data = $response;
  114. $data->basic->url = $this->plugin_url;
  115. $data->full->url = $this->plugin_url;
  116. $data->full->external = 1;
  117. }
  118. }
  119. }
  120. return $data;
  121. }
  122. public function _retrieve_version_info($force_check = false) {
  123. global $wp_version, $rslb;
  124. $last_check = get_option('revslider-update-check-short');
  125. if($last_check == false){ //first time called
  126. $last_check = time();
  127. update_option('revslider-update-check-short', $last_check);
  128. }
  129. // Check for updates
  130. if(time() - $last_check > 172800 || $force_check == true){
  131. update_option('revslider-update-check-short', time());
  132. $purchase = (get_option('revslider-valid', 'false') == 'true') ? get_option('revslider-code', '') : '';
  133. $done = false;
  134. $count = 0;
  135. do{
  136. $url = $rslb->get_url('updates');
  137. $response = wp_remote_post($url.'/'.$this->remote_url, array(
  138. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  139. 'body' => array(
  140. 'item' => urlencode(RS_PLUGIN_SLUG),
  141. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION),
  142. 'code' => urlencode($purchase)
  143. ),
  144. 'timeout' => 45
  145. ));
  146. $response_code = wp_remote_retrieve_response_code( $response );
  147. $version_info = wp_remote_retrieve_body( $response );
  148. if($response_code == 200){
  149. $done = true;
  150. }else{
  151. $rslb->move_server_list();
  152. }
  153. $count++;
  154. }while($done == false && $count < 5);
  155. if ( $response_code != 200 || is_wp_error( $version_info ) ) {
  156. update_option('revslider-connection', false);
  157. return false;
  158. }else{
  159. update_option('revslider-connection', true);
  160. }
  161. $version_info = json_decode($version_info);
  162. if(isset($version_info->version)){
  163. update_option('revslider-latest-version', $version_info->version);
  164. }
  165. if(isset($version_info->stable)){
  166. update_option('revslider-stable-version', $version_info->stable);
  167. }
  168. if(isset($version_info->notices)){
  169. update_option('revslider-notices', $version_info->notices);
  170. }
  171. if(isset($version_info->dashboard)){
  172. update_option('revslider-dashboard', $version_info->dashboard);
  173. }
  174. if(isset($version_info->addons)){
  175. update_option('revslider-addons', $version_info->addons);
  176. }
  177. if(isset($version_info->deactivated) && $version_info->deactivated === true){
  178. if(get_option('revslider-valid', 'false') == 'true'){
  179. //remove validation, add notice
  180. update_option('revslider-valid', 'false');
  181. update_option('revslider-deact-notice', true);
  182. }
  183. }
  184. }
  185. if($force_check == true){ //force that the update will be directly searched
  186. update_option('revslider-update-check', '');
  187. }
  188. }
  189. public function add_temp_active_check($force = false){
  190. global $wp_version, $rslb;
  191. $last_check = get_option('revslider-activate-temp-short');
  192. if($last_check == false){ //first time called
  193. $last_check = time();
  194. update_option('revslider-activate-temp-short', $last_check);
  195. }
  196. // Check for updates
  197. if(time() - $last_check > 3600 || $force == true){
  198. $done = false;
  199. $count = 0;
  200. do{
  201. $url = $rslb->get_url('updates');
  202. $response = wp_remote_post($url.'/'.$this->remote_temp_active, array(
  203. 'user-agent' => 'WordPress/'.$wp_version.'; '.get_bloginfo('url'),
  204. 'body' => array(
  205. 'item' => urlencode(RS_PLUGIN_SLUG),
  206. 'version' => urlencode(RevSliderGlobals::SLIDER_REVISION),
  207. 'code' => urlencode(get_option('revslider-code', ''))
  208. ),
  209. 'timeout' => 45
  210. ));
  211. $response_code = wp_remote_retrieve_response_code( $response );
  212. $version_info = wp_remote_retrieve_body( $response );
  213. if($response_code == 200){
  214. $done = true;
  215. }else{
  216. $rslb->move_server_list();
  217. }
  218. $count++;
  219. }while($done == false && $count < 5);
  220. if ( $response_code != 200 || is_wp_error( $version_info ) ) {
  221. //wait, cant connect
  222. }else{
  223. if($version_info == 'valid'){
  224. update_option('revslider-valid', 'true');
  225. update_option('revslider-temp-active', 'false');
  226. }elseif($version_info == 'temp_valid'){
  227. //do nothing,
  228. }elseif($version_info == 'invalid'){
  229. //invalid, deregister plugin!
  230. update_option('revslider-valid', 'false');
  231. update_option('revslider-temp-active', 'false');
  232. update_option('revslider-temp-active-notice', 'true');
  233. }
  234. }
  235. $last_check = time();
  236. update_option('revslider-activate-temp-short', $last_check);
  237. }
  238. }
  239. }
  240. /**
  241. * old classname extends new one (old classnames will be obsolete soon)
  242. * @since: 5.0
  243. **/
  244. class UniteUpdateClassRev extends RevSliderUpdate {}
  245. ?>