Няма описание

class-pum-admin-upgrades.php 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. <?php
  2. /*******************************************************************************
  3. * Copyright (c) 2019, Code Atlantic LLC
  4. ******************************************************************************/
  5. // Exit if accessed directly
  6. if ( ! defined( 'ABSPATH' ) ) {
  7. exit;
  8. }
  9. /**
  10. * Class PUM_Upgrades
  11. */
  12. class PUM_Admin_Upgrades {
  13. /**
  14. * @var PUM_Admin_Upgrades The one true PUM_Admin_Upgrades
  15. */
  16. private static $instance;
  17. /**
  18. * @var $upgrade_args
  19. */
  20. public $upgrade_args = array();
  21. public $page = null;
  22. public $doing_upgrades = false;
  23. public $required_cap = 'manage_options';
  24. public $current_routine = null;
  25. public $next_routine = null;
  26. /**
  27. * Initialize the actions needed to process upgrades.
  28. */
  29. public static function instance() {
  30. if ( ! isset( self::$instance ) && ! ( self::$instance instanceof PUM_Admin_Upgrades ) ) {
  31. self::$instance = new PUM_Admin_Upgrades;
  32. self::$instance->init();
  33. }
  34. return self::$instance;
  35. }
  36. /**
  37. * Initialize the actions needed to process upgrades.
  38. */
  39. public function init() {
  40. $this->update_plugin_version();
  41. $this->required_cap = apply_filters( 'pum_upgrade_required_cap', 'manage_options' );
  42. // bail if this plugin data doesn't need updating
  43. if ( pum_get_db_ver() >= Popup_Maker::$DB_VER ) {
  44. return;
  45. }
  46. add_action( 'admin_menu', array( $this, 'register_pages' ) );
  47. add_action( 'network_admin_menu', array( $this, 'register_pages' ) );
  48. add_action( 'admin_init', array( $this, 'process_upgrade_args' ) );
  49. add_action( 'wp_ajax_pum_trigger_upgrades', array( $this, 'trigger_upgrades' ) );
  50. add_action( 'admin_notices', array( $this, 'show_upgrade_notices' ) );
  51. }
  52. public function update_plugin_version() {
  53. $current_ver = get_option( 'pum_ver', false );
  54. if ( ! $current_ver ) {
  55. $deprecated_ver = get_site_option( 'popmake_version', false );
  56. $current_ver = $deprecated_ver ? $deprecated_ver : Popup_Maker::$VER;
  57. add_option( 'pum_ver', Popup_Maker::$VER );
  58. }
  59. if ( version_compare( $current_ver, Popup_Maker::$VER, '<' ) ) {
  60. // Save Upgraded From option
  61. update_option( 'pum_ver_upgraded_from', $current_ver );
  62. update_option( 'pum_ver', Popup_Maker::$VER );
  63. }
  64. }
  65. /**
  66. * Registers the pum-upgrades admin page.
  67. */
  68. public function register_pages() {
  69. global $pum_upgrades_page;
  70. $parent = null;
  71. /*
  72. if ( function_exists( 'is_network_admin' ) && is_network_admin() ) {
  73. add_menu_page(
  74. __( 'Popup Maker', 'popup-maker' ),
  75. __( 'Popup Maker', 'popup-maker' ),
  76. 'manage_network_plugins',
  77. 'popup-maker',
  78. '',
  79. pum_get_svg_icon( true )
  80. );
  81. $parent = 'popup-maker';
  82. }
  83. */
  84. $this->page = add_submenu_page(
  85. $parent,
  86. __( 'Popup Maker Upgrades', 'popup-maker' ),
  87. __( 'Popup Maker Upgrades', 'popup-maker' ),
  88. $this->required_cap,
  89. 'pum-upgrades',
  90. array( $this, 'upgrades_screen' )
  91. );
  92. $pum_upgrades_page = $this->page;
  93. }
  94. /**
  95. * Process upgrade args.
  96. */
  97. public function process_upgrade_args() {
  98. $page = isset( $_GET['page'] ) ? $_GET['page'] : '';
  99. if ( ! ( defined( 'DOING_AJAX' ) && DOING_AJAX && $_REQUEST['action'] == 'pum_trigger_upgrades' ) && $page != 'pum-upgrades' ) {
  100. return;
  101. }
  102. $this->doing_upgrades = true;
  103. $action = isset( $_REQUEST['pum-upgrade'] ) ? sanitize_text_field( $_REQUEST['pum-upgrade'] ) : $this->get_pum_db_ver() + 1;
  104. $step = isset( $_REQUEST['step'] ) ? absint( $_REQUEST['step'] ) : 1;
  105. $total = isset( $_REQUEST['total'] ) ? absint( $_REQUEST['total'] ) : false;
  106. $custom = isset( $_REQUEST['custom'] ) ? absint( $_REQUEST['custom'] ) : 0;
  107. $number = isset( $_REQUEST['number'] ) ? absint( $_REQUEST['number'] ) : 100;
  108. $completed = isset( $_REQUEST['completed'] ) ? absint( $_REQUEST['completed'] ) : false;
  109. $steps = ceil( $total / $number );
  110. if ( $step > $steps ) {
  111. // Prevent a weird case where the estimate was off. Usually only a couple.
  112. $steps = $step;
  113. }
  114. $this->upgrade_args = array(
  115. 'page' => 'pum-upgrades',
  116. 'pum-upgrade' => $action,
  117. 'step' => $step,
  118. 'total' => $total,
  119. 'custom' => $custom,
  120. 'steps' => $steps,
  121. 'number' => $number,
  122. 'completed' => $completed,
  123. );
  124. update_option( 'pum_doing_upgrade', $this->upgrade_args );
  125. }
  126. /**
  127. * Get upgrade arg.
  128. *
  129. * @param string $key
  130. *
  131. * @return bool|null
  132. */
  133. public function set_arg( $key, $value = null ) {
  134. $this->upgrade_args[ $key ] = $value;
  135. if ( $key == 'number' || $key == 'total' ) {
  136. $this->upgrade_args['steps'] = ceil( $this->upgrade_args['total'] / $this->upgrade_args['number'] );
  137. }
  138. if ( $this->upgrade_args['step'] > $this->upgrade_args['steps'] ) {
  139. // Prevent a weird case where the estimate was off. Usually only a couple.
  140. $this->upgrade_args['steps'] = $this->upgrade_args['step'];
  141. } elseif ( $this->upgrade_args['step'] * $this->upgrade_args['steps'] ) {
  142. update_option( 'pum_doing_upgrade', $this->upgrade_args );
  143. }
  144. }
  145. /**
  146. * Get upgrade arg.
  147. *
  148. * @param string $key
  149. *
  150. * @return bool|null
  151. */
  152. public function get_arg( $key = null ) {
  153. if ( ! $key ) {
  154. return null;
  155. }
  156. if ( ! isset( $this->upgrade_args[ $key ] ) ) {
  157. return false;
  158. }
  159. return $this->upgrade_args[ $key ];
  160. }
  161. public function get_args() {
  162. return $this->upgrade_args;
  163. }
  164. public function doing_upgrades() {
  165. return $this->doing_upgrades;
  166. }
  167. /**
  168. * Display Upgrade Notices
  169. *
  170. * @return void
  171. */
  172. public function show_upgrade_notices() {
  173. $screen = get_current_screen();
  174. if ( $screen->id == $this->page ) {
  175. return; // Don't show notices on the upgrades page
  176. }
  177. if ( ! $this->has_upgrades() ) {
  178. return;
  179. }
  180. // Sequential Orders was the first stepped upgrade, so check if we have a stalled upgrade
  181. $resume_upgrade = $this->maybe_resume_upgrade();
  182. if ( ! empty( $resume_upgrade ) ) {
  183. $resume_url = add_query_arg( $resume_upgrade, admin_url( 'index.php' ) );
  184. printf(
  185. '<div class="error"><p>' . __( 'Popup Maker needs to complete a database upgrade that was previously started, click <a href="%s">here</a> to resume the upgrade.', 'popup-maker' ) . '</p></div>',
  186. esc_url( $resume_url )
  187. );
  188. } else {
  189. printf(
  190. '<div class="error"><p><strong>%s:</strong> <span class="dashicons dashicons-warning" style="color: #dc3232;"></span> %s %s %s</p></div>',
  191. __( 'Popup Maker', 'popup-maker' ),
  192. __( 'Important', 'popup-maker' ),
  193. __( 'Database upgrades required.', 'popup-maker' ),
  194. sprintf(
  195. __( 'Please click %shere%s to complete these changes now.', 'popup-maker' ),
  196. '<a href="' . esc_url( admin_url( 'options.php?page=pum-upgrades' ) ) . '">',
  197. '</a>'
  198. )
  199. );
  200. }
  201. }
  202. /**
  203. * Triggers all upgrade functions
  204. *
  205. * This function is usually triggered via AJAX
  206. *
  207. * @return void
  208. */
  209. public function trigger_upgrades() {
  210. if ( ! current_user_can( $this->required_cap ) ) {
  211. wp_die( __( 'You do not have permission to do upgrades', 'popup-maker' ), __( 'Error', 'popup-maker' ), array( 'response' => 403 ) );
  212. }
  213. $deprecated_ver = get_site_option( 'popmake_version', false );
  214. $current_ver = get_option( 'pum_ver', $deprecated_ver );
  215. // Save Upgraded From option
  216. if ( $current_ver ) {
  217. update_option( 'pum_ver_upgraded_from', $current_ver );
  218. }
  219. update_option( 'pum_ver', Popup_Maker::$VER );
  220. // Process DB Upgrades
  221. $this->process_upgrades();
  222. if ( DOING_AJAX ) {
  223. echo wp_json_encode( array(
  224. 'complete' => true,
  225. 'status' => sprintf(
  226. '<strong>%s</strong><br/>%s',
  227. __( 'Upgrades have been completed successfully.', 'popup-maker' ),
  228. sprintf( 'You will automatically be redirected in %s seconds', '<span id="pum-countdown">5</span>' )
  229. ),
  230. 'redirect' => admin_url( 'edit.php?post_type=popup' ),
  231. 'countdown' => 5000,
  232. ) ); // Let AJAX know that the upgrade is complete
  233. exit;
  234. }
  235. }
  236. /**
  237. * Updates the pum_db_ver to the passed $version.
  238. *
  239. * If no $version is passed a default value will be established.
  240. *
  241. * @param null $version
  242. */
  243. public function set_pum_db_ver( $version = null ) {
  244. if ( $version ) {
  245. $version = preg_replace( '/[^0-9.].*/', '', $version );
  246. update_option( 'pum_db_ver', $version );
  247. return;
  248. }
  249. $upgraded_from = get_option( 'pum_ver_upgraded_from', false );
  250. // this is the current database schema version number
  251. $current_db_ver = pum_get_db_ver();
  252. // If no current db version, but prior install detected, set db version correctly.
  253. if ( ! $current_db_ver ) {
  254. if ( $upgraded_from ) {
  255. if ( version_compare( $upgraded_from, '1.3.0', '<' ) ) {
  256. $current_db_ver = 1;
  257. } else {
  258. $current_db_ver = 2;
  259. }
  260. } else {
  261. $current_db_ver = Popup_Maker::$DB_VER;
  262. }
  263. add_option( 'pum_db_ver', $current_db_ver );
  264. }
  265. }
  266. /**
  267. * Gets the pum_db_ver or sets and returns the correct one.
  268. *
  269. * @see PUM_Utils_Upgrades::set_pum_db_ver()
  270. *
  271. * return $pum_db_ver
  272. */
  273. public function get_pum_db_ver() {
  274. static $pum_db_ver;
  275. if ( ! isset( $pum_db_ver ) ) {
  276. // this is the current database schema version number
  277. $pum_db_ver = pum_get_db_ver();
  278. }
  279. if ( ! $pum_db_ver ) {
  280. $this->set_pum_db_ver();
  281. $pum_db_ver = pum_get_db_ver();
  282. }
  283. return preg_replace( '/[^0-9.].*/', '', $pum_db_ver );
  284. }
  285. /**
  286. * Process upgrades in a stepped succession.
  287. *
  288. * Starts with the current version and loops until reaching the target version.
  289. */
  290. public function process_upgrades() {
  291. // this is the target version that we need to reach
  292. $target_db_ver = Popup_Maker::$DB_VER;
  293. // this is the current database schema version number
  294. $current_db_ver = $this->get_pum_db_ver();
  295. // Run upgrade routine until target version reached.
  296. while ( $current_db_ver < $target_db_ver ) {
  297. // increment the current db_ver by one
  298. $current_db_ver ++;
  299. $this->current_routine = $current_db_ver;
  300. $this->next_routine = $current_db_ver == $target_db_ver ? null : $current_db_ver + 1;
  301. if ( file_exists( POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php" ) ) {
  302. require_once POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php";
  303. $func = "PUM_Admin_Upgrade_Routine_{$current_db_ver}::run";
  304. if ( is_callable( $func ) ) {
  305. call_user_func( $func );
  306. }
  307. }
  308. }
  309. }
  310. public function current_routine() {
  311. return $this->current_routine;
  312. }
  313. public function next_routine() {
  314. return $this->next_routine;
  315. }
  316. /**
  317. * Process upgrades in a stepped succession.
  318. *
  319. * Starts with the current version and loops until reaching the target version.
  320. */
  321. public function get_upgrades() {
  322. // this is the target version that we need to reach
  323. $target_db_ver = Popup_Maker::$DB_VER;
  324. // this is the current database schema version number
  325. $current_db_ver = $this->get_pum_db_ver();
  326. $upgrades = array();
  327. // Run upgrade routine until target version reached.
  328. while ( $current_db_ver < $target_db_ver ) {
  329. // increment the current db_ver by one
  330. $current_db_ver ++;
  331. if ( file_exists( POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php" ) ) {
  332. require_once POPMAKE_DIR . "includes/admin/upgrades/class-pum-admin-upgrade-routine-{$current_db_ver}.php";
  333. $func = "PUM_Admin_Upgrade_Routine_{$current_db_ver}::description";
  334. if ( is_callable( $func ) ) {
  335. $upgrades[ $current_db_ver ] = call_user_func( $func );
  336. }
  337. }
  338. }
  339. return $upgrades;
  340. }
  341. public function get_upgrade( $version = null ) {
  342. $upgrades = $this->get_upgrades();
  343. if ( isset ( $upgrades[ $version ] ) ) {
  344. return $upgrades[ $version ];
  345. } else {
  346. return false;
  347. }
  348. }
  349. /**
  350. * Returns true if there are unprocessed upgrades.
  351. *
  352. * @return bool
  353. */
  354. public function has_upgrades() {
  355. return boolval( count( $this->get_upgrades() ) );
  356. }
  357. /**
  358. * For use when doing 'stepped' upgrade routines, to see if we need to start somewhere in the middle
  359. *
  360. * @return mixed When nothing to resume returns false, otherwise starts the upgrade where it left off
  361. */
  362. public function maybe_resume_upgrade() {
  363. $doing_upgrade = get_option( 'pum_doing_upgrade', false );
  364. if ( empty( $doing_upgrade ) ) {
  365. return false;
  366. }
  367. return $doing_upgrade;
  368. }
  369. /**
  370. * Adds an upgrade action to the completed upgrades array
  371. *
  372. * @param string $upgrade_action The action to add to the competed upgrades array
  373. *
  374. * @return bool If the function was successfully added
  375. */
  376. public function set_upgrade_complete( $upgrade_action = '' ) {
  377. if ( empty( $upgrade_action ) ) {
  378. return false;
  379. }
  380. $completed_upgrades = $this->get_completed_upgrades();
  381. $completed_upgrades[] = $upgrade_action;
  382. // Remove any blanks, and only show uniques
  383. $completed_upgrades = array_unique( array_values( $completed_upgrades ) );
  384. return update_option( 'pum_completed_upgrades', $completed_upgrades );
  385. }
  386. /**
  387. * Check if the upgrade routine has been run for a specific action
  388. *
  389. * @param string $upgrade_action The upgrade action to check completion for
  390. *
  391. * @return bool If the action has been added to the copmleted actions array
  392. */
  393. public function has_upgrade_completed( $upgrade_action = '' ) {
  394. if ( empty( $upgrade_action ) ) {
  395. return false;
  396. }
  397. $completed_upgrades = $this->get_completed_upgrades();
  398. return in_array( $upgrade_action, $completed_upgrades );
  399. }
  400. /**
  401. * Get's the array of completed upgrade actions
  402. *
  403. * @return array The array of completed upgrades
  404. */
  405. public function get_completed_upgrades() {
  406. $completed_upgrades = get_option( 'pum_completed_upgrades' );
  407. if ( false === $completed_upgrades ) {
  408. $completed_upgrades = array();
  409. }
  410. return $completed_upgrades;
  411. }
  412. public function step_up() {
  413. $step = $this->upgrade_args['step'];
  414. if ( $step >= $this->upgrade_args['steps'] ) {
  415. $this->upgrade_args['step'] = $this->upgrade_args['steps'];
  416. return false;
  417. }
  418. $this->upgrade_args['step'] ++;
  419. return true;
  420. }
  421. /**
  422. * Renders the upgrades screen.
  423. */
  424. public function upgrades_screen() { ?>
  425. <div class="wrap">
  426. <h2>
  427. <?php _e( 'Popup Maker - Upgrades', 'popup-maker' ); ?>
  428. <img src="<?php echo POPMAKE_URL . '/assets/images/admin/loading.gif'; ?>" id="pum-upgrade-loader"/>
  429. </h2>
  430. <style>
  431. #pum-upgrade-status {
  432. max-height: 300px;
  433. background: #fff;
  434. box-shadow: inset 0 1px 1px rgba(0, 0, 0, .5);
  435. overflow-y: scroll;
  436. text-overflow: ellipsis;
  437. padding: 0 1.5em;
  438. }
  439. </style>
  440. <p>
  441. <?php _e( 'The upgrade process has started, please be patient. This could take several minutes. You will be automatically redirected when the upgrade is finished.', 'popup-maker' ); ?>
  442. </p>
  443. <div id="pum-upgrade-status"></div>
  444. <script type="text/javascript">
  445. (function ($, document, undefined) {
  446. var $loader = $('#pum-upgrade-loader').hide(),
  447. $status_box = $('#pum-upgrade-status'),
  448. $timer,
  449. timer = 500;
  450. function update_status(message) {
  451. $('<p>')
  452. .html(message)
  453. .appendTo($status_box);
  454. $status_box.animate({
  455. scrollTop: $status_box.get(0).scrollHeight
  456. }, {
  457. duration: 'slow',
  458. queue: false
  459. });
  460. }
  461. function countdown(timer, callback) {
  462. var time_left = timer - 1000;
  463. if (time_left >= 0) {
  464. setTimeout(function () {
  465. $timer.text(time_left / 1000);
  466. countdown(time_left, callback);
  467. }, 1000);
  468. } else {
  469. callback();
  470. }
  471. }
  472. function next_step(args) {
  473. $loader.show();
  474. if (args === undefined) {
  475. args = {};
  476. }
  477. $.ajax({
  478. url: ajaxurl,
  479. data: $.extend({action: 'pum_trigger_upgrades'}, args),
  480. type: 'GET',
  481. dataType: 'json'
  482. })
  483. .done(function (response) {
  484. if (response.status !== undefined) {
  485. update_status(response.status);
  486. }
  487. if (response.complete !== undefined) {
  488. $loader.hide();
  489. } else if (response.next !== undefined && typeof response.next === 'object') {
  490. next_step(response.next);
  491. }
  492. if (response.redirect !== undefined) {
  493. if (response.countdown === undefined) {
  494. setTimeout(function () {
  495. document.location.href = response.redirect;
  496. }, timer);
  497. } else {
  498. $timer = $('#pum-countdown');
  499. countdown(response.countdown, function () {
  500. document.location.href = response.redirect;
  501. });
  502. }
  503. }
  504. })
  505. .fail(function () {
  506. update_status("<?php _e( 'Upgrade failed, please try again.', 'popup-maker' ); ?>");
  507. });
  508. }
  509. $(document).ready(function () {
  510. // Trigger upgrades on page load
  511. next_step(<?php echo wp_json_encode( $this->get_args() ); ?>);
  512. update_status('<?php printf( '<strong>%s</strong>', $this->get_upgrade( $this->get_arg( 'pum-upgrade' ) ) ); ?>');
  513. });
  514. }(jQuery, document));
  515. </script>
  516. </div>
  517. <?php
  518. }
  519. }
  520. PUM_Admin_Upgrades::instance();