Нет описания

Changelog.php 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace MailPoet\Config;
  3. if (!defined('ABSPATH')) exit;
  4. use MailPoet\Settings\SettingsController;
  5. use MailPoet\Util\Url;
  6. use MailPoet\WooCommerce\Helper;
  7. use MailPoet\WP\Functions as WPFunctions;
  8. class Changelog {
  9. /** @var WPFunctions */
  10. private $wp;
  11. /** @var SettingsController */
  12. private $settings;
  13. /** @var Helper */
  14. private $wooCommerceHelper;
  15. /** @var Url */
  16. private $urlHelper;
  17. /** @var MP2Migrator */
  18. private $mp2Migrator;
  19. public function __construct(
  20. SettingsController $settings,
  21. WPFunctions $wp,
  22. Helper $wooCommerceHelper,
  23. Url $urlHelper,
  24. MP2Migrator $mp2Migrator
  25. ) {
  26. $this->wooCommerceHelper = $wooCommerceHelper;
  27. $this->settings = $settings;
  28. $this->wp = $wp;
  29. $this->urlHelper = $urlHelper;
  30. $this->mp2Migrator = $mp2Migrator;
  31. }
  32. public function init() {
  33. $doingAjax = (bool)(defined('DOING_AJAX') && DOING_AJAX);
  34. // don't run any check when it's an ajax request
  35. if ($doingAjax) {
  36. return;
  37. }
  38. // don't run any check when we're not on our pages
  39. if (
  40. !(isset($_GET['page']))
  41. or
  42. (isset($_GET['page']) && strpos($_GET['page'], 'mailpoet') !== 0)
  43. ) {
  44. return;
  45. }
  46. WPFunctions::get()->addAction(
  47. 'admin_init',
  48. [$this, 'check']
  49. );
  50. }
  51. public function check() {
  52. $version = $this->settings->get('version');
  53. $this->checkMp2Migration($version);
  54. if ($version === null) {
  55. $this->setupNewInstallation();
  56. $this->checkWelcomeWizard();
  57. }
  58. $this->checkWooCommerceListImportPage();
  59. $this->checkRevenueTrackingPermissionPage();
  60. }
  61. private function checkMp2Migration($version) {
  62. if (!in_array($_GET['page'], ['mailpoet-migration', 'mailpoet-settings']) && $this->mp2Migrator->isMigrationStartedAndNotCompleted()) {
  63. // Force the redirection if the migration has started but is not completed
  64. return $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration'));
  65. }
  66. if ($version === null && $this->mp2Migrator->isMigrationNeeded()) {
  67. $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-migration'));
  68. }
  69. }
  70. private function setupNewInstallation() {
  71. $this->settings->set('show_congratulate_after_first_newsletter', true);
  72. }
  73. private function checkWelcomeWizard() {
  74. $skipWizard = $this->wp->applyFilters('mailpoet_skip_welcome_wizard', false);
  75. if (!$skipWizard) {
  76. $this->terminateWithRedirect($this->wp->adminUrl('admin.php?page=mailpoet-welcome-wizard'));
  77. }
  78. }
  79. private function checkWooCommerceListImportPage() {
  80. if ($this->wp->applyFilters('mailpoet_skip_woocommerce_import_page', false)) {
  81. return;
  82. }
  83. if (
  84. !in_array($_GET['page'], ['mailpoet-woocommerce-setup', 'mailpoet-welcome-wizard', 'mailpoet-migration'])
  85. && !$this->settings->get('woocommerce_import_screen_displayed')
  86. && $this->wooCommerceHelper->isWooCommerceActive()
  87. && $this->wooCommerceHelper->getOrdersCountCreatedBefore($this->settings->get('installed_at')) > 0
  88. && $this->wp->currentUserCan('administrator')
  89. ) {
  90. $this->urlHelper->redirectTo($this->wp->adminUrl('admin.php?page=mailpoet-woocommerce-setup'));
  91. }
  92. }
  93. private function checkRevenueTrackingPermissionPage() {
  94. if (
  95. !in_array($_GET['page'], ['mailpoet-woocommerce-setup', 'mailpoet-welcome-wizard', 'mailpoet-migration'])
  96. && ($this->settings->get('woocommerce.accept_cookie_revenue_tracking.set') === null)
  97. && $this->settings->get('tracking.enabled')
  98. && $this->wooCommerceHelper->isWooCommerceActive()
  99. && $this->wp->currentUserCan('administrator')
  100. ) {
  101. $this->urlHelper->redirectTo($this->wp->adminUrl('admin.php?page=mailpoet-woocommerce-setup'));
  102. }
  103. }
  104. private function terminateWithRedirect($redirectUrl) {
  105. // save version number
  106. $this->settings->set('version', Env::$version);
  107. $this->urlHelper->redirectWithReferer($redirectUrl);
  108. }
  109. }