WJ_Upgrade

The MailPoet Newsletters WJ Upgrade class.

Defined (1)

The class is defined in the following location(s).

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