PageRenderTime 37ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-content/plugins/child-theme-configurator/includes/classes/Upgrade.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 293 lines | 215 code | 37 blank | 41 comment | 52 complexity | 677a37ed88f0e98bfe14388ceef7dc54 MD5 | raw file
  1. <?php
  2. // Exit if accessed directly
  3. if ( !defined( 'ABSPATH' ) ) exit;
  4. class ChildThemeConfiguratorUpgrade {
  5. private static $old;
  6. private static $version = '1.0.0';
  7. //private static $lilaeaupdateurl = 'http://www.lilaeamedia.com/updates/update.php?product=intelliwidget-pro';
  8. private static $lilaeaupdateurl = 'http://www.lilaeamedia.com/updates/update-replace.php';
  9. private static $update;
  10. static function get_version(){
  11. $info = get_plugins( '/' . dirname( self::$old ) );
  12. if ( isset( $info[ basename( self::$old ) ] ) )
  13. self::$version = $info[ basename( self::$old ) ][ 'Version' ];
  14. }
  15. static function check_version(){
  16. if ( is_admin() && current_user_can( 'install_plugins' ) && !self::has_dismissed() ):
  17. if ( file_exists( trailingslashit( dirname( CHLD_THM_CFG_DIR ) ) . ChildThemeConfigurator::$oldpro ) ):
  18. self::$old = ChildThemeConfigurator::$oldpro;
  19. self::get_version();
  20. if ( isset( $_REQUEST[ 'ctc_pro_upgrade' ] ) && wp_verify_nonce( $_REQUEST[ 'ctcnonce' ], 'ctc_pro_upgrade' ) ):
  21. self::do_upgrade();
  22. else:
  23. self::upgrade_notice();
  24. endif;
  25. else:
  26. // check if old version is installed but inactive
  27. self::$old = ChildThemeConfigurator::$ctcpro;
  28. self::get_version();
  29. if ( self::$version < CHLD_THM_CFG_PRO_MIN_VERSION ):
  30. // do upgrade if user requests it
  31. if ( isset( $_REQUEST[ 'ctc_pro_upgrade' ] ) && wp_verify_nonce( $_REQUEST[ 'ctcnonce' ], 'ctc_pro_upgrade' ) ):
  32. self::do_upgrade();
  33. else:
  34. // otherwise show notice
  35. self::upgrade_notice();
  36. endif;
  37. endif;
  38. endif;
  39. endif;
  40. }
  41. static function upgrade_notice(){
  42. if ( isset( $_GET[ 'action' ] )
  43. && 'activate' == $_GET[ 'action' ]
  44. && isset( $_GET[ 'plugin' ] )
  45. && self::$old == $_GET[ 'plugin' ] )
  46. unset( $_GET[ 'action' ] );
  47. deactivate_plugins( self::$old, FALSE, is_network_admin() );
  48. add_action( 'all_admin_notices', 'ChildThemeConfiguratorUpgrade::admin_notice' );
  49. }
  50. static function admin_notice(){
  51. $update_key = isset( $_POST[ 'ctc_update_key' ] ) ? sanitize_text_field( $_POST[ 'ctc_update_key' ] ) : self::get_update_key();
  52. if ( isset( $_GET[ 'invalidkey' ] ) || empty( $update_key ) ):
  53. //
  54. $input = '<input type="text" name="ctc_update_key" value="" autocomplete="off" placeholder="' . __( 'Enter your Update Key', 'child-theme-configurator' ) . '" />';
  55. if ( isset( $_GET[ 'invalidkey' ] ) ):?><div class="notice-error notice is-dismissible"><p><?php printf( __( 'Sorry, we could not validate your Update Key. Please try again or, if you need assistance, please %s', 'child-theme-configurator' ), sprintf( '<a href="%s/contact/" target="_blank">%s</a>', LILAEAMEDIA_URL, __( 'contact us.', 'child-theme-configurator' ) ) ); ?></p></div><?php endif;
  56. else:
  57. //
  58. $input = '<input type="hidden" name="ctc_update_key" value="' . esc_attr( $update_key ) . ' " />';
  59. endif;
  60. ?><div class="notice-warning notice is-dismissible ctc-pro-upgrade-notice"><form action="" method="post"><?php wp_nonce_field( 'ctc_pro_upgrade', 'ctcnonce' ) ?>
  61. <p><strong><?php echo sprintf( __( 'Child Theme Configurator Pro version %s', 'child-theme-configurator' ), self::$version ) . __( ' is not compatible with the installed version of Child Theme Configurator and has been deactivated.', 'child-theme-configurator' ); ?>
  62. </strong></p>
  63. <p><?php _e( 'You can upgrade to the latest version by clicking the button below. After validating the Update Key from your order, WordPress will retrieve the plugin from our website and install it automatically. If you no longer wish to use the premium version, you can dismiss this notice by clicking the close icon (x) at the top right.', 'child-theme-configurator' ); ?></p>
  64. <p><?php echo $input; ?> <input type="submit" name="ctc_pro_upgrade" value="<?php _e( 'Upgrade Now', 'child-theme-configurator' ); ?>" class="button button-primary" /></p></form>
  65. <script>
  66. jQuery( document ).ready(function($){
  67. $( document ).on( 'click', '.notice-dismiss', function(){ //.iwpro-upgrade-notice
  68. $.post(
  69. '<?php echo admin_url( 'admin-ajax.php' ); ?>',
  70. { '_wpnonce': '<?php echo wp_create_nonce( 'ctc_pro_dismiss' ); ?>', 'action': 'ctc_pro_dismiss' }
  71. );
  72. });
  73. });
  74. </script></div>
  75. <?php
  76. }
  77. /**
  78. * ajax callback to dismiss upgrade notice
  79. */
  80. static function ajax_dismiss_notice() {
  81. if ( wp_verify_nonce( $_POST[ '_wpnonce' ], 'ctc_pro_dismiss' ) )
  82. update_user_meta( get_current_user_id(), 'ctc_pro_upgrade_notice' , CHLD_THM_CFG_VERSION );
  83. die(0);
  84. }
  85. static function has_dismissed(){
  86. $dismissed = get_user_meta( get_current_user_id(), 'ctc_pro_upgrade_notice', TRUE );
  87. if ( $dismissed == CHLD_THM_CFG_VERSION )
  88. return TRUE;
  89. return FALSE;
  90. }
  91. static function reset_dismissed(){
  92. delete_user_meta( get_current_user_id(), 'ctc_pro_upgrade_notice' );
  93. }
  94. static function get_update_key(){
  95. if ( $options = get_site_option( CHLD_THM_CFG_OPTIONS ) )
  96. return $options[ 'update_key' ];
  97. return FALSE;
  98. }
  99. static function set_update_key( $key ){
  100. if ( !( $options = get_site_option( CHLD_THM_CFG_OPTIONS ) ) )
  101. $options = array();
  102. $options[ 'update_key' ] = $key;
  103. update_site_option( CHLD_THM_CFG_OPTIONS, $options );
  104. }
  105. static function do_upgrade(){
  106. // if $old == $new upgrade
  107. /*
  108. */
  109. // otherwise install
  110. // set Install transient
  111. // do install
  112. // delete old version
  113. // if upgrade
  114. add_filter( 'site_transient_update_plugins', 'ChildThemeConfiguratorUpgrade::injectUpdate' ); //WP 3.0+
  115. add_filter( 'transient_update_plugins', 'ChildThemeConfiguratorUpgrade::injectUpdate' ); //WP 2.8+
  116. // Clear the version number cache when something - anything - is upgraded or WP clears the update cache.
  117. add_filter( 'upgrader_post_install', 'ChildThemeConfiguratorUpgrade::clearCachedVersion' );
  118. add_action( 'delete_site_transient_update_plugins', 'ChildThemeConfiguratorUpgrade::clearCachedVersion' );
  119. $key = isset( $_REQUEST[ 'ctc_update_key' ] ) ? sanitize_text_field( $_REQUEST[ 'ctc_update_key' ] ) : self::get_update_key();
  120. //Query args to append to the URL. Plugins can add their own by using a filter callback (see addQueryArgFilter()).
  121. $args = array(
  122. 'installed_version' => self::$version,
  123. 'key' => $key,
  124. 'product' => dirname( self::$old ),
  125. );
  126. //Various options for the wp_remote_get() call. Plugins can filter these, too.
  127. $options = array(
  128. 'timeout' => 10, //seconds
  129. 'headers' => array(
  130. 'Accept' => 'application/json'
  131. ),
  132. );
  133. //The plugin info should be at 'http://your-api.com/url/here/$slug/info.json'
  134. $url = add_query_arg( $args, self::$lilaeaupdateurl );
  135. $result = wp_remote_get(
  136. $url,
  137. $options
  138. );
  139. //Try to parse the response
  140. $pluginInfo = NULL;
  141. if ( !is_wp_error( $result )
  142. && isset( $result[ 'response' ][ 'code' ] )
  143. && ( $result[ 'response' ][ 'code' ] == 200 )
  144. && !empty( $result['body'] ) ):
  145. $pluginInfo = json_decode( $result['body'] );
  146. if ( empty( $pluginInfo ) || version_compare( $pluginInfo->version, self::$version, '<' ) ):
  147. $query = array( 'invalidkey' => 1 );
  148. $url = add_query_arg( $query );
  149. wp_redirect( $url );
  150. die();
  151. endif;
  152. // create update object
  153. $update = new StdClass;
  154. $update->id = 0;
  155. $update->slug = $pluginInfo->slug;
  156. $update->new_version = $pluginInfo->version;
  157. $update->url = $pluginInfo->homepage;
  158. $update->package = $pluginInfo->download_url;
  159. if ( !empty( $pluginInfo->upgrade_notice ) )
  160. $update->upgrade_notice = $pluginInfo->upgrade_notice;
  161. self::$update = $update;
  162. // add update to cache
  163. wp_update_plugins();
  164. // run upgrader
  165. $title = __( 'Update Plugin', 'child-theme-configurator' );
  166. $plugin = self::$old;
  167. //$nonce = 'ctc_pro_upgrade'; //'upgrade-plugin_' . $plugin;
  168. $nonce = 'upgrade-plugin_' . self::$old;
  169. $url = 'update.php?action=upgrade-plugin&plugin=' . urlencode( self::$old )
  170. . '&ctc_pro_upgrade=1&ctcnonce=' . $_REQUEST[ 'ctcnonce' ];
  171. wp_enqueue_script( 'updates' );
  172. include_once( ABSPATH . 'wp-admin/includes/class-wp-upgrader.php' );
  173. require_once( ABSPATH . 'wp-admin/admin-header.php' );
  174. $upgrader = new Plugin_Upgrader( new Plugin_Upgrader_Skin( compact( 'title', 'nonce', 'url', 'plugin' ) ) );
  175. //return;
  176. $upgrader->upgrade( $plugin );
  177. include(ABSPATH . 'wp-admin/admin-footer.php');
  178. self::set_update_key( $key );
  179. endif;
  180. }
  181. static function injectUpdate( $updates ){
  182. if ( !empty( self::$update ) ):
  183. if ( !is_object( $updates ) ):
  184. $updates = new StdClass();
  185. $updates->response = array();
  186. endif;
  187. $updates->response[ self::$old ] = self::$update;
  188. elseif ( isset( $updates, $updates->response ) ):
  189. unset( $updates->response[ self::$old ] );
  190. endif;
  191. return $updates;
  192. }
  193. static function clearCachedVersion( $args = NULL ){
  194. self::$update = NULL;
  195. return $args;
  196. }
  197. /**
  198. * deletes old version of plugin without removing option settings
  199. */
  200. static function delete_old_version() {
  201. if ( isset( $_REQUEST[ 'deleted' ] ) ) return;
  202. $slug = dirname( self::$old );
  203. // clean up hooks from < 2.2.0
  204. wp_clear_scheduled_hook( 'check_plugin_updates-' . $slug );
  205. delete_option( 'external_updates-' . $slug );
  206. // remove old Pro version
  207. if ( current_user_can( 'delete_plugins' ) ):
  208. $redir = NULL;
  209. if ( isset( $_GET[ 'action' ] ) ):
  210. // unset action parameter if it is for old CTC Pro
  211. if ( 'activate' == $_GET[ 'action' ]
  212. && isset( $_GET[ 'plugin' ] )
  213. && self::$old == $_GET[ 'plugin' ] ):
  214. unset( $_GET[ 'action' ] );
  215. // handle two-step FTP Authentication form
  216. elseif ( 'delete-selected' == $_GET[ 'action' ]
  217. && isset( $_GET[ 'verify-delete' ] )
  218. && isset( $_GET[ 'checked' ] )
  219. && self::$old == $_GET[ 'checked' ][ 0 ] ):
  220. unset( $_GET[ 'action' ] );
  221. unset( $_GET[ 'checked' ] );
  222. unset( $_GET[ 'verify-delete' ] );
  223. unset( $_REQUEST[ 'action' ] );
  224. unset( $_REQUEST[ 'checked' ] );
  225. unset( $_REQUEST[ 'verify-delete' ] );
  226. $redir = self_admin_url( "plugins.php?activate=true" );
  227. elseif ( 'activate' != $_GET[ 'action' ] ):
  228. return;
  229. endif;
  230. endif;
  231. // deactivate old Pro version
  232. deactivate_plugins( self::$old, FALSE, is_network_admin() );
  233. // remove uninstall hook so that options are preserved
  234. $uninstallable_plugins = (array) get_option( 'uninstall_plugins' );
  235. if ( isset( $uninstallable_plugins[ self::$old ] ) ):
  236. unset( $uninstallable_plugins[ self::$old ] );
  237. update_option( 'uninstall_plugins', $uninstallable_plugins );
  238. endif;
  239. unset( $uninstallable_plugins );
  240. // remove old Pro version
  241. $delete_result = delete_plugins( array( self::$old ) );
  242. //Store the result in a cache rather than a URL param due to object type & length
  243. global $user_ID;
  244. set_transient( 'plugins_delete_result_' . $user_ID, $delete_result );
  245. // force plugin cache to reload
  246. wp_cache_delete( 'plugins', 'plugins' );
  247. // if this is two-step FTP authentication, redirect back to activated
  248. if ( $redir ):
  249. if ( is_wp_error( $delete_result ) )
  250. $redir = self_admin_url( "plugins.php?deleted=" . self::$old );
  251. wp_redirect( $redir );
  252. exit;
  253. endif;
  254. endif;
  255. }
  256. }