PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/wysija-newsletters/classes/WJ_Upgrade.php

https://gitlab.com/Gashler/sg
PHP | 406 lines | 291 code | 91 blank | 24 comment | 63 complexity | 810efaebd1b668cb33e54f85165adf05 MD5 | raw file
  1. <?php
  2. defined('WYSIJA') or die('Restricted access');
  3. class WJ_Upgrade extends WYSIJA_object {
  4. /**
  5. * A static variable that holds a dinamic instance of the class
  6. * @var [object||null]
  7. */
  8. public static $instance = null;
  9. public static $plugins = array( 'wysija-newsletters/index.php', 'wysija-newsletters-premium/index.php' );
  10. public static $baseurl = array(
  11. 'core' => 'https://downloads.wordpress.org/plugin/',
  12. 'packager' => 'http://packager.mailpoet.com/release/',
  13. );
  14. public static function hook(){
  15. null === self::$instance and self::$instance = new self;
  16. if ( ! is_admin() ) {
  17. return;
  18. }
  19. self::$baseurl = (object) self::$baseurl;
  20. add_action( 'current_screen', array( self::$instance, 'setup_bulk_screen' ) );
  21. add_action( 'shutdown', array( self::$instance, 'setup_bulk_screen_footer' ) );
  22. add_action( 'current_screen', array( self::$instance, 'iframe_intercept' ) );
  23. add_action( 'init', array( self::$instance, 'update_warning' ) );
  24. add_filter( 'pre_set_site_transient_update_plugins', array( self::$instance, 'pre_set_site_transient_update_plugins' ), 100 );
  25. }
  26. public function update_warning() {
  27. if ( ! is_admin() ){
  28. return;
  29. }
  30. if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
  31. return;
  32. }
  33. if ( ! ( ( is_multisite() && current_user_can( 'manage_network' ) ) || current_user_can( 'update_plugins' ) ) ){
  34. return;
  35. }
  36. if ( ! function_exists( 'get_plugin_data' ) ){
  37. include_once ABSPATH . 'wp-admin/includes/plugin.php';
  38. }
  39. $current = get_site_transient( 'update_plugins' );
  40. foreach ( self::$plugins as $plugin ){
  41. if ( isset( $current->response[ $plugin ] ) ){
  42. $data = self::get_plugin_data( $plugin );
  43. if ( version_compare( $current->response[ $plugin ]->new_version, $data->info->Version, '<=' ) ){
  44. continue;
  45. }
  46. $this->notice(
  47. sprintf(
  48. __( 'Hey! %1$s has an update (version %2$s), <a href="%3$s">click here to update</a>.', WYSIJA ),
  49. '<strong>' . esc_attr( $data->info->Name ) . '</strong>',
  50. $current->response[ $plugin ]->new_version,
  51. wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $plugin, 'upgrade-plugin_' . $plugin )
  52. ),
  53. true,
  54. true
  55. );
  56. }
  57. }
  58. }
  59. public function update_plugin_complete_actions( $update_actions, $mixed = null, $plugin = null ){
  60. $actions = array(
  61. 'refresh_page' => '<a href="#" onclick="window.parent.location.reload(true);return false;" title="' . esc_attr__( 'Refresh the page you current are!', WYSIJA ) . '" target="_parent">' . __( 'Return to MailPoet', WYSIJA ) . '</a>'
  62. );
  63. return $actions;
  64. }
  65. public function iframe_intercept( $current_screen ) {
  66. if ( $current_screen->base !== 'mailpoet_page_wysija_config' ){
  67. return;
  68. }
  69. if ( ! isset( $_GET['action'] ) || $_GET['action'] !== 'packager-switch' ){
  70. return;
  71. }
  72. // Verify if it's has been created within the last 12 hours (nonce)
  73. if ( wp_verify_nonce( $_GET['_wpnonce'], $_GET['action'] ) !== 1 ){
  74. return;
  75. }
  76. // Require the Updater classes
  77. include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
  78. $to = (isset($_GET['stable']) && $_GET['stable']?'stable':'beta');
  79. add_filter( 'pre_site_transient_update_plugins', array( $this, 'pre_site_transient_update_plugins' ) );
  80. $plugins = array();
  81. // Check for the action, it might be upgrading or installing
  82. $action = 'upgrade';
  83. if ( isset( $_GET['_mp_action'] ) && in_array( $_GET['_mp_action'], array( 'upgrade', 'install' ) ) ){
  84. $action = strtolower( $_GET['_mp_action'] );
  85. }
  86. foreach ( self::$plugins as $k => $plugin ) {
  87. if ( is_plugin_active( $plugin ) ){
  88. $plugins[] = $plugin;
  89. }
  90. }
  91. // Ajust the Padding/margin of the iFrame
  92. define( 'IFRAME_REQUEST', true );
  93. echo "<div style='margin: 0 20px;'>";
  94. // Thats how WordPress calls for an iFrame page
  95. wp_enqueue_script( 'jquery' );
  96. iframe_header();
  97. if ( $action === 'upgrade' ) {
  98. add_filter( 'update_bulk_plugins_complete_actions', array( $this, 'update_plugin_complete_actions' ), 10, 2 );
  99. $upgrader = new Plugin_Upgrader( new Bulk_Plugin_Upgrader_Skin( compact( 'nonce', 'url' ) ) );
  100. $upgrader->bulk_upgrade( $plugins );
  101. } elseif ( $action === 'install' ) {
  102. // If the action is install, it will only happen if it's the Premium
  103. add_filter( 'install_plugin_complete_actions', array( $this, 'update_plugin_complete_actions' ), 10, 3 );
  104. $upgrader = new Plugin_Upgrader( new Plugin_Installer_Skin() );
  105. $result = $upgrader->install( self::get_url( self::$plugins[1], WYSIJA::is_beta(), 'zip' ) );
  106. }
  107. iframe_footer();
  108. echo '</div>';
  109. remove_filter( 'pre_site_transient_update_plugins', array( $this, 'pre_site_transient_update_plugins' ) );
  110. $model_config = WYSIJA::get( 'config', 'model' );
  111. $model_config->save( array( 'beta_mode' => ( $to === 'stable' ? false : true ) ) );
  112. set_site_transient( 'update_plugins', '' );
  113. exit();
  114. }
  115. /**
  116. * A static method to grab the url from the packager to grab the ZIP
  117. * or the version of the plugin there.
  118. *
  119. * @uses bool_from_yn
  120. * @uses esc_url
  121. * @uses add_query_arg
  122. *
  123. * @param string $package Which package you want to grab
  124. * @param boolean|string $beta Beta URL or not
  125. * @param string $action Which kind of URL you need? [zip|check]
  126. * @param string $version The version you want the URL to be related to
  127. * @return string|null It will return the URL from the packager related to the asked action
  128. */
  129. public static function get_url( $package = null, $beta = false, $action = 'zip', $version = null ){
  130. if ( is_string( $beta ) ){
  131. if ( $beta === 'beta' ) {
  132. $beta = true;
  133. } else {
  134. $beta = bool_from_yn( $beta );
  135. }
  136. } else {
  137. $beta = (bool) $beta;
  138. }
  139. if ( ! in_array( $action, array( 'zip', 'check' ) ) ) {
  140. return null;
  141. }
  142. $slug = self::get_slug( $package );
  143. if ( true === $beta || 'wysija-newsletters-premium' === $slug ) {
  144. $url = self::$baseurl->packager . $action;
  145. $params = array(
  146. 'key' => self::get_slug( $package ),
  147. );
  148. if ( $beta === true ){
  149. $params['beta'] = 'true';
  150. }
  151. $url = add_query_arg( $params, $url );
  152. return (string) $url;
  153. } else {
  154. return (string) self::$baseurl->core . $slug . '.zip';
  155. }
  156. }
  157. public static function get_version( $package = null, $beta = false ){
  158. $request = wp_remote_get( self::get_url( $package, $beta, 'check' ) );
  159. if ( is_wp_error( $request ) ){
  160. return false;
  161. }
  162. $version = wp_remote_retrieve_body( $request );
  163. return $version;
  164. }
  165. public static function get_slug( $package = null ){
  166. switch ( $package ) {
  167. case self::$plugins[1]:
  168. case 'premium':
  169. case 'wysija-newsletters-premium':
  170. return 'wysija-newsletters-premium';
  171. break;
  172. case self::$plugins[0]:
  173. case 'base':
  174. case 'wysija-newsletters':
  175. default:
  176. return 'wysija-newsletters';
  177. break;
  178. }
  179. }
  180. public static function get_plugin_data( $package = null, $beta = false, $new_version = false ){
  181. $data = (object) array();
  182. if ( is_null( $package ) ){
  183. return $data;
  184. }
  185. $data->id = 27505;
  186. $data->slug = self::get_slug( $package );
  187. $data->package = self::get_url( $package, $beta, 'zip' );
  188. if ( function_exists( 'get_plugin_data' ) ){
  189. $data->info = (object) get_plugin_data( plugin_dir_path( dirname( dirname( __FILE__ ) ) ) . $package );
  190. }
  191. if ( $data->slug === 'wysija-newsletters' ){
  192. $data->url = "https://wordpress.org/plugins/{$data->slug}/";
  193. } else {
  194. $data->url = 'http://www.mailpoet.com/wordpress-newsletter-plugin-premium/';
  195. }
  196. $data->url = esc_url( $data->url );
  197. if ( $new_version !== false ){
  198. $data->new_version = (string) $new_version;
  199. }
  200. return $data;
  201. }
  202. public function pre_set_site_transient_update_plugins( $update_data ){
  203. if ( ! function_exists( 'get_plugin_data' ) ){
  204. return (object) array();
  205. }
  206. if ( ! is_object( $update_data ) && strlen( trim( $update_data ) ) === 0 ){
  207. return (object) array();
  208. }
  209. if ( ! isset( $update_data->last_checked ) ){
  210. $update_data->last_checked = 0;
  211. }
  212. if ( ( time() - ( 60 * 60 * 12 ) ) > ( $update_data->last_checked ) ) { // Just check once every 12 hours
  213. return $update_data;
  214. }
  215. foreach ( self::$plugins as $plugin ){
  216. if ( ! function_exists( 'is_plugin_active' ) ) {
  217. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  218. }
  219. if ( ! is_plugin_active( $plugin ) ){
  220. continue;
  221. }
  222. if ( ! WYSIJA::is_beta() && $plugin === 'wysija-newsletters/index.php' ) {
  223. continue;
  224. }
  225. $version = self::get_version( $plugin, WYSIJA::is_beta() );
  226. $update_data->last_checked = time();
  227. if ( version_compare( WYSIJA::get_version( $plugin ), $version, '>=' ) ){
  228. continue;
  229. }
  230. $update_data->response[ $plugin ] = self::get_plugin_data( $plugin, WYSIJA::is_beta(), $version );
  231. }
  232. return $update_data;
  233. }
  234. public function pre_site_transient_update_plugins( $transient ) {
  235. $update_data = (object) array(
  236. 'last_checked' => time() - 10,
  237. 'response' => array()
  238. );
  239. $to = (isset($_GET['stable']) && $_GET['stable']?'stable':'beta');
  240. foreach ( self::$plugins as $plugin ) {
  241. if ( ! function_exists( 'is_plugin_active' ) ){
  242. include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
  243. }
  244. if ( ! is_plugin_active( $plugin ) ) {
  245. continue;
  246. }
  247. $update_data->response[ $plugin ] = self::get_plugin_data( $plugin, $to, self::get_version( $plugin, $to ) );
  248. }
  249. return $update_data;
  250. }
  251. public function setup_bulk_screen( $current_screen ) {
  252. global $title, $parent_file, $submenu_file;
  253. if ( strtoupper( $_SERVER['REQUEST_METHOD'] ) === 'POST' && in_array( $current_screen->id, array( 'update-core', 'plugins' ) ) ) {
  254. if ( ! isset( $_POST['checked'] ) ){
  255. return;
  256. }
  257. $plugins = (array) $_POST['checked'];
  258. $plugins = array_map( 'urldecode', $plugins );
  259. $__intersection = array_intersect( $plugins, self::$plugins );
  260. if ( empty( $__intersection ) ){
  261. return;
  262. }
  263. $action = (isset($_POST['action']) ? $_POST['action'] : null);
  264. switch($action) {
  265. case 'delete-selected':
  266. break;
  267. case 'deactivate-selected':
  268. if ( in_array( self::$plugins[0], $plugins ) && ! in_array( self::$plugins[1], $plugins ) && is_plugin_active( self::$plugins[1] ) ){
  269. $plugins[] = self::$plugins[1];
  270. }
  271. break;
  272. case 'update-selected':
  273. case 'activate-selected':
  274. if ( in_array( self::$plugins[1], $plugins ) && ! in_array( self::$plugins[0], $plugins ) ){
  275. $plugins[] = self::$plugins[0];
  276. }
  277. break;
  278. }
  279. $_POST['checked'] = $plugins;
  280. return;
  281. }
  282. if ( $current_screen->id !== 'update' ){
  283. return;
  284. }
  285. if ( $_GET['action'] !== 'upgrade-plugin' ){
  286. return;
  287. }
  288. if ( $_GET['action'] === 'upgrade-plugin' && ! in_array( $_GET['plugin'], self::$plugins ) ){
  289. return;
  290. }
  291. foreach ( self::$plugins as $plugin ) {
  292. if ( ! is_plugin_active( $plugin ) ) {
  293. return;
  294. }
  295. }
  296. $_GET['action'] = $_REQUEST['action'] = 'update-selected';
  297. $_GET['plugins'] = $_REQUEST['plugins'] = implode( ',', array_map( 'urlencode', self::$plugins ) );
  298. $_GET['_wpnonce'] = $_REQUEST['_wpnonce'] = wp_create_nonce( 'bulk-update-plugins' );
  299. $_GET['_wysija_bulk_screen'] = $_REQUEST['_wysija_bulk_screen'] = true;
  300. $title = esc_attr__( 'Update Plugin' );
  301. $parent_file = 'plugins.php';
  302. $submenu_file = 'plugins.php';
  303. require_once(ABSPATH . 'wp-admin/admin-header.php');
  304. echo
  305. "<div class='wrap'>" .
  306. '<h2>' . esc_attr( $title ) . '</h2>';
  307. }
  308. public function setup_bulk_screen_footer(){
  309. if ( ! isset( $_GET['_wysija_bulk_screen'] ) ){
  310. return;
  311. }
  312. echo '</div>';
  313. include(ABSPATH . 'wp-admin/admin-footer.php');
  314. }
  315. }