From 7b69efea131f7217921c40ef0f36aef285d4a28f Mon Sep 17 00:00:00 2001 From: Rostislav Wolny Date: Thu, 25 Nov 2021 15:13:59 +0100 Subject: [PATCH] Show notice when migration affects tracking settings [MAILPOET-3185] --- lib/Config/Populator.php | 6 ++++ lib/Util/Notices/ChangedTrackingNotice.php | 39 ++++++++++++++++++++++ lib/Util/Notices/PermanentNotices.php | 10 ++++++ tests/integration/Config/PopulatorTest.php | 9 +++++ 4 files changed, 64 insertions(+) create mode 100644 lib/Util/Notices/ChangedTrackingNotice.php diff --git a/lib/Config/Populator.php b/lib/Config/Populator.php index 30ee2d0376..4df741f820 100644 --- a/lib/Config/Populator.php +++ b/lib/Config/Populator.php @@ -37,6 +37,7 @@ use MailPoet\Subscribers\NewSubscriberNotificationMailer; use MailPoet\Subscribers\Source; use MailPoet\Subscription\Captcha; use MailPoet\Util\Helpers; +use MailPoet\Util\Notices\ChangedTrackingNotice; use MailPoet\WP\Functions as WPFunctions; use MailPoetVendor\Carbon\Carbon; use MailPoetVendor\Doctrine\ORM\EntityManager; @@ -941,6 +942,11 @@ class Populator { $trackingLevel = $emailTracking ? TrackingConfig::LEVEL_FULL : TrackingConfig::LEVEL_BASIC; } elseif ($wooTrackingCookie) { // WooCommerce Cookie Tracking enabled $trackingLevel = TrackingConfig::LEVEL_FULL; + // Cookie was enabled but tracking disabled and we are switching to full. + // So we activate an admin notice to let the user know that we activated tracking + if (!$emailTracking) { + $this->wp->setTransient(ChangedTrackingNotice::OPTION_NAME, true); + } } else { // WooCommerce Tracking Cookie Disabled $trackingLevel = $emailTracking ? TrackingConfig::LEVEL_PARTIAL : TrackingConfig::LEVEL_BASIC; } diff --git a/lib/Util/Notices/ChangedTrackingNotice.php b/lib/Util/Notices/ChangedTrackingNotice.php new file mode 100644 index 0000000000..d9deb046c6 --- /dev/null +++ b/lib/Util/Notices/ChangedTrackingNotice.php @@ -0,0 +1,39 @@ +wp = $wp; + } + + public function init($shouldDisplay) { + if ($shouldDisplay && $this->wp->getTransient(self::OPTION_NAME)) { + return $this->display(); + } + return null; + } + + public function display() { + $text = __('Email open and click tracking is now enabled. You can change how MailPoet tracks your subscribers in [link]Settings[/link]', 'mailpoet'); + $text = Helpers::replaceLinkTags($text, 'admin.php?page=mailpoet-settings#advanced'); + $extraClasses = 'mailpoet-dismissible-notice is-dismissible'; + + return Notice::displayWarning($text, $extraClasses, self::OPTION_NAME); + } + + public function disable() { + $this->wp->deleteTransient(self::OPTION_NAME); + } +} diff --git a/lib/Util/Notices/PermanentNotices.php b/lib/Util/Notices/PermanentNotices.php index b6bcc072bc..087e08c998 100644 --- a/lib/Util/Notices/PermanentNotices.php +++ b/lib/Util/Notices/PermanentNotices.php @@ -39,6 +39,9 @@ class PermanentNotices { /** @var EmailWithInvalidSegmentNotice */ private $emailWithInvalidListNotice; + /** @var ChangedTrackingNotice */ + private $changedTrackingNotice; + public function __construct( WPFunctions $wp, TrackingConfig $trackingConfig, @@ -54,6 +57,7 @@ class PermanentNotices { $this->headersAlreadySentNotice = new HeadersAlreadySentNotice($settings, $trackingConfig, $wp); $this->deprecatedShortcodeNotice = new DeprecatedShortcodeNotice($wp); $this->emailWithInvalidListNotice = new EmailWithInvalidSegmentNotice($wp); + $this->changedTrackingNotice = new ChangedTrackingNotice($wp); } public function init() { @@ -94,6 +98,9 @@ class PermanentNotices { $this->emailWithInvalidListNotice->init( Menu::isOnMailPoetAdminPage($exclude = null, $pageId = 'mailpoet-newsletters') ); + $this->changedTrackingNotice->init( + Menu::isOnMailPoetAdminPage($excludeWizard) + ); } public function ajaxDismissNoticeHandler() { @@ -120,6 +127,9 @@ class PermanentNotices { case (EmailWithInvalidSegmentNotice::OPTION_NAME): $this->emailWithInvalidListNotice->disable(); break; + case (ChangedTrackingNotice::OPTION_NAME): + $this->changedTrackingNotice->disable(); + break; } } } diff --git a/tests/integration/Config/PopulatorTest.php b/tests/integration/Config/PopulatorTest.php index 4870081d49..36f98d9e72 100644 --- a/tests/integration/Config/PopulatorTest.php +++ b/tests/integration/Config/PopulatorTest.php @@ -4,6 +4,8 @@ namespace MailPoet\Config; use MailPoet\Settings\SettingsController; use MailPoet\Settings\TrackingConfig; +use MailPoet\Util\Notices\ChangedTrackingNotice; +use MailPoet\WP\Functions; class PopulatorTest extends \MailPoetTest { @@ -20,36 +22,43 @@ class PopulatorTest extends \MailPoetTest { } public function testItMigratesTrackingSettings() { + $wp = $this->diContainer->get(Functions::class); // WooCommerce disabled and Tracking enabled $this->settings->set('db_version', '3.74.1'); $this->settings->set('tracking', ['enabled' => true]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', null); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_FULL); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->false(); // WooCommerce disabled and Tracking disabled $this->settings->set('tracking', ['enabled' => false]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', null); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_BASIC); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->false(); // WooCommerce enabled with cookie enabled and Tracking enabled $this->settings->set('tracking', ['enabled' => true]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', "1"); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_FULL); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->false(); // WooCommerce enabled with cookie disabled and Tracking enabled $this->settings->set('tracking', ['enabled' => true]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', ""); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_PARTIAL); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->false(); // WooCommerce enabled with cookie disabled and Tracking disabled $this->settings->set('tracking', ['enabled' => false]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', ""); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_BASIC); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->false(); // WooCommerce enabled with cookie enabled and Tracking disabled $this->settings->set('tracking', ['enabled' => false]); $this->settings->set('woocommerce.accept_cookie_revenue_tracking.enabled', "1"); $this->populator->up(); expect($this->settings->get('tracking.level'))->equals(TrackingConfig::LEVEL_FULL); + expect($wp->getTransient(ChangedTrackingNotice::OPTION_NAME))->equals(true); } }