Brak opisu

Activator.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\InvalidStateException;
  5. use MailPoet\Settings\SettingsController;
  6. use MailPoet\WP\Functions as WPFunctions;
  7. class Activator {
  8. public const TRANSIENT_ACTIVATE_KEY = 'mailpoet_activator_activate';
  9. private const TRANSIENT_EXPIRATION = 120; // seconds
  10. /** @var SettingsController */
  11. private $settings;
  12. /** @var Populator */
  13. private $populator;
  14. /** @var WPFunctions */
  15. private $wp;
  16. public function __construct(
  17. SettingsController $settings,
  18. Populator $populator,
  19. WPFunctions $wp
  20. ) {
  21. $this->settings = $settings;
  22. $this->populator = $populator;
  23. $this->wp = $wp;
  24. }
  25. public function activate() {
  26. $isRunning = $this->wp->getTransient(self::TRANSIENT_ACTIVATE_KEY);
  27. if ($isRunning === false) {
  28. $this->wp->setTransient(self::TRANSIENT_ACTIVATE_KEY, '1', self::TRANSIENT_EXPIRATION);
  29. try {
  30. $this->processActivate();
  31. } finally {
  32. $this->wp->deleteTransient(self::TRANSIENT_ACTIVATE_KEY);
  33. }
  34. } else {
  35. throw new InvalidStateException(__('MailPoet version update is in progress, please refresh the page in a minute.', 'mailpoet'));
  36. }
  37. }
  38. private function processActivate(): void {
  39. $migrator = new Migrator();
  40. $migrator->up();
  41. $this->populator->up();
  42. $this->updateDbVersion();
  43. $caps = new Capabilities();
  44. $caps->setupWPCapabilities();
  45. }
  46. public function deactivate() {
  47. $migrator = new Migrator();
  48. $migrator->down();
  49. $caps = new Capabilities();
  50. $caps->removeWPCapabilities();
  51. }
  52. public function updateDbVersion() {
  53. try {
  54. $currentDbVersion = $this->settings->get('db_version');
  55. } catch (\Exception $e) {
  56. $currentDbVersion = null;
  57. }
  58. $this->settings->set('db_version', Env::$version);
  59. // if current db version and plugin version differ, log an update
  60. if (version_compare($currentDbVersion, Env::$version) !== 0) {
  61. $updatesLog = (array)$this->settings->get('updates_log', []);
  62. $updatesLog[] = [
  63. 'previous_version' => $currentDbVersion,
  64. 'new_version' => Env::$version,
  65. 'date' => date('Y-m-d H:i:s'),
  66. ];
  67. $this->settings->set('updates_log', $updatesLog);
  68. }
  69. }
  70. }