/wp-content/themes/Avada/framework/plugins/envato-wordpress-toolkit-library/class-envato-wordpress-theme-upgrader.php

https://bitbucket.org/rchlmrtn/chiari · PHP · 383 lines · 203 code · 61 blank · 119 comment · 31 complexity · 0ef919ad3aa840e0e980e82abcc34426 MD5 · raw file

  1. e<?php
  2. /**
  3. * Envato Theme Upgrader class to extend the WordPress Theme_Upgrader class.
  4. *
  5. * @package Envato WordPress Updater
  6. * @author Arman Mirkazemi, Derek Herman <derek@valendesigns.com>
  7. * @since 1.0
  8. */
  9. include_once( ABSPATH . 'wp-admin/includes/file.php' );
  10. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
  11. include_once( 'class-envato-protected-api.php' );
  12. if ( class_exists( 'Theme_Upgrader' ) && ! class_exists( 'Envato_WordPress_Theme_Upgrader' ) ) {
  13. /**
  14. * Envato Wordpress Theme Upgrader class to extend the WordPress Theme_Upgrader class.
  15. *
  16. * @package Envato WordPress Theme Upgrader Library
  17. * @author Derek Herman <derek@valendesigns.com>, Arman Mirkazemi
  18. * @since 1.0
  19. */
  20. class Envato_WordPress_Theme_Upgrader extends Theme_Upgrader
  21. {
  22. protected $api_key;
  23. protected $username;
  24. protected $api;
  25. protected $installation_feedback;
  26. public function __construct( $username, $api_key )
  27. {
  28. parent::__construct(new Envato_Theme_Installer_Skin($this));
  29. $this->constants();
  30. $this->installation_feedback = array();
  31. $this->username = $username;
  32. $this->api_key = $api_key;
  33. $this->api = &new Envato_Protected_API( $this->username, $this->api_key );
  34. }
  35. /**
  36. * Checks for theme updates on ThemeForest marketplace
  37. *
  38. * @since 1.0
  39. * @access public
  40. *
  41. * @param string Name of the theme. If not set checks for updates for the current theme. Default ''.
  42. * @param bool Allow API calls to be cached. Default true.
  43. * @return object A stdClass object.
  44. */
  45. public function check_for_theme_update( $theme_name = '', $allow_cache = true )
  46. {
  47. $result = new stdClass();
  48. $purchased_themes = $this->api->wp_list_themes( $allow_cache );
  49. if ( $errors = $this->api->api_errors() )
  50. {
  51. $result->errors = array();
  52. foreach( $errors as $k => $v ) {
  53. array_push( $result->errors , $v);
  54. }
  55. return $result;
  56. }
  57. if ( empty($theme_name) ) {
  58. $theme_name = get_current_theme();
  59. }
  60. $purchased_themes = $this->filter_purchased_themes_by_name($purchased_themes, $theme_name);
  61. $result->updated_themes = $this->get_updated_themes(get_themes(), $purchased_themes);
  62. $result->updated_themes_count = count($result->updated_themes);
  63. return $result;
  64. }
  65. /**
  66. * Upgrades theme to its latest version
  67. *
  68. * @since 1.0
  69. * @access public
  70. *
  71. * @param string Name of the theme. If not set checks for updates for the current theme. Default ''.
  72. * @param bool Allow API calls to be cached. Default true.
  73. * @return object A stdClass object.
  74. */
  75. public function upgrade_theme( $theme_name = '', $allow_cache = true )
  76. {
  77. $result = new stdClass();
  78. $result->success = false;
  79. if ( empty($theme_name) ) {
  80. $theme_name = get_current_theme();
  81. }
  82. $installed_theme = $this->is_theme_installed($theme_name);
  83. if ($installed_theme == null) {
  84. $result->errors = array("'$theme_name' theme is not installed");
  85. return $result;
  86. }
  87. $purchased_themes = $this->api->wp_list_themes( $allow_cache );
  88. $marketplace_theme_data = null;
  89. if ( $errors = $this->api->api_errors() )
  90. {
  91. $result->errors = array();
  92. foreach( $errors as $k => $v ) {
  93. array_push( $result->errors , $v);
  94. }
  95. return $result;
  96. }
  97. foreach( $purchased_themes as $purchased ) {
  98. if ( $this->is_matching_themes( $installed_theme, $purchased ) && $this->is_newer_version_available( $installed_theme['Version'], $purchased->version ) ) {
  99. $marketplace_theme_data = $purchased;
  100. break;
  101. }
  102. }
  103. if ( $marketplace_theme_data == null ) {
  104. $result->errors = array( "There is no update available for '$theme_name'" );
  105. return $result;
  106. }
  107. $result->success = $this->do_upgrade_theme( $installed_theme['Title'], $marketplace_theme_data->item_id);
  108. $result->installation_feedback = $this->installation_feedback;
  109. return $result;
  110. }
  111. /**
  112. * @since 1.0
  113. * @access internal
  114. *
  115. * @return array Void.
  116. */
  117. public function set_installation_message($message)
  118. {
  119. $this->installation_feedback[] = $message;
  120. }
  121. /**
  122. * @since 1.0
  123. * @access internal
  124. *
  125. * @return array Array.
  126. */
  127. protected function filter_purchased_themes_by_name( $all_purchased_themes, $theme_name )
  128. {
  129. $result = $all_purchased_themes;
  130. if ( empty($theme_name) )
  131. return $result;
  132. for ( $i = count($result) - 1; $i >= 0; $i-- ) {
  133. $entry = $result[$i];
  134. if ( $entry->theme_name != $theme_name )
  135. unset($result[$i]);
  136. }
  137. return $result;
  138. }
  139. /**
  140. * @since 1.0
  141. * @access internal
  142. *
  143. * @return array Void.
  144. */
  145. protected function constants()
  146. {
  147. define( 'ETU_MAX_EXECUTION_TIME' , 60 * 10);
  148. }
  149. /**
  150. * @since 1.0
  151. * @access internal
  152. *
  153. * @return array Array.
  154. */
  155. protected function get_updated_themes($installed_themes, $purchased_themes)
  156. {
  157. $result = array();
  158. if ( count( $purchased_themes ) <= 0 ) {
  159. return $result;
  160. }
  161. foreach( $purchased_themes as $purchased )
  162. {
  163. foreach( $installed_themes as $installed => $installed_theme )
  164. {
  165. if ( $this->is_matching_themes( $installed_theme, $purchased ) && $this->is_newer_version_available( $installed_theme['Version'], $purchased->version ) )
  166. {
  167. $installed_theme['envato-theme'] = $purchased;
  168. array_push($result, $installed_theme);
  169. }
  170. }
  171. }
  172. return $result;
  173. }
  174. /**
  175. * @since 1.0
  176. * @access internal
  177. *
  178. * @return array Boolean.
  179. */
  180. protected function is_matching_themes($installed_theme, $purchased_theme)
  181. {
  182. return $installed_theme['Title'] == $purchased_theme->theme_name AND $installed_theme['Author Name'] == $purchased_theme->author_name;
  183. }
  184. protected function is_newer_version_available($installed_vesion, $latest_version)
  185. {
  186. return version_compare($installed_vesion, $latest_version, '<');
  187. }
  188. protected function is_theme_installed($theme_name)
  189. {
  190. $installed_themes = get_themes();
  191. foreach($installed_themes as $entry)
  192. {
  193. if (strcmp($entry['Name'], $theme_name) == 0) {
  194. return $entry;
  195. }
  196. }
  197. return null;
  198. }
  199. /**
  200. * @since 1.0
  201. * @access internal
  202. *
  203. * @return array Boolean.
  204. */
  205. protected function do_upgrade_theme( $installed_theme_name, $marketplace_theme_id )
  206. {
  207. $result = false;
  208. $callback = array( &$this , '_http_request_args' );
  209. add_filter( 'http_request_args', $callback, 10, 1 );
  210. $result = $this->upgrade( $installed_theme_name, $this->api->wp_download( $marketplace_theme_id ) );
  211. remove_filter( 'http_request_args', $callback );
  212. return $result;
  213. }
  214. /**
  215. * @since 1.0
  216. * @access internal
  217. *
  218. * @return array Array.
  219. */
  220. public function _http_request_args($r)
  221. {
  222. if ((int)ini_get("max_execution_time") < ETU_MAX_EXECUTION_TIME)
  223. {
  224. set_time_limit( ETU_MAX_EXECUTION_TIME );
  225. }
  226. $r['timeout'] = ETU_MAX_EXECUTION_TIME;
  227. return $r;
  228. }
  229. /**
  230. * @since 1.0
  231. * @access internal
  232. *
  233. * @return array Void.
  234. */
  235. public function upgrade_strings() {
  236. parent::upgrade_strings();
  237. $this->strings['downloading_package'] = __( 'Downloading upgrade package from the Envato API&#8230;', 'envato' );
  238. }
  239. /**
  240. * @since 1.0
  241. * @access internal
  242. *
  243. * @return array Void.
  244. */
  245. public function install_strings() {
  246. parent::install_strings();
  247. $this->strings['downloading_package'] = __( 'Downloading install package from the Envato API&#8230;', 'envato' );
  248. }
  249. /**
  250. * @since 1.0
  251. * @access internal
  252. *
  253. * @return array Boolean.
  254. */
  255. public function upgrade( $theme, $package ) {
  256. $this->init();
  257. $this->upgrade_strings();
  258. $options = array(
  259. 'package' => $package,
  260. 'destination' => WP_CONTENT_DIR . '/themes',
  261. 'clear_destination' => true,
  262. 'clear_working' => true,
  263. 'hook_extra' => array(
  264. 'theme' => $theme
  265. )
  266. );
  267. $this->run( $options );
  268. if ( ! $this->result || is_wp_error($this->result) )
  269. return $this->result;
  270. return true;
  271. }
  272. }
  273. /**
  274. * Envato Theme Installer Skin class to extend the WordPress Theme_Installer_Skin class.
  275. *
  276. * @package Envato WordPress Theme Upgrader Library
  277. * @author Arman Mirkazemi
  278. * @since 1.0
  279. */
  280. class Envato_Theme_Installer_Skin extends Theme_Installer_Skin {
  281. protected $envato_theme_updater;
  282. function __construct($envato_theme_updater)
  283. {
  284. parent::__construct();
  285. $this->envato_theme_updater = $envato_theme_updater;
  286. }
  287. /**
  288. * @since 1.0
  289. * @access internal
  290. *
  291. * @return array Void.
  292. */
  293. function feedback($string)
  294. {
  295. if ( isset( $this->upgrader->strings[$string] ) )
  296. $string = $this->upgrader->strings[$string];
  297. if ( strpos($string, '%') !== false ) {
  298. $args = func_get_args();
  299. $args = array_splice($args, 1);
  300. if ( !empty($args) )
  301. $string = vsprintf($string, $args);
  302. }
  303. if ( empty($string) )
  304. return;
  305. $this->envato_theme_updater->set_installation_message($string);
  306. }
  307. /**
  308. * @since 1.0
  309. * @access internal
  310. *
  311. * @return array Void.
  312. */
  313. function header(){}
  314. /**
  315. * @since 1.0
  316. * @access internal
  317. *
  318. * @return array Void.
  319. */
  320. function footer(){}
  321. }
  322. }