Add meta when sending stats notifications

[MAILPOET-2333]
This commit is contained in:
Amine Ben hammou
2019-09-13 02:32:59 +01:00
committed by Jack Kitterhing
parent 723cb368c4
commit d40747e0ac
7 changed files with 46 additions and 6 deletions

View File

@ -7,6 +7,7 @@ use MailPoet\Config\Renderer;
use MailPoet\Cron\Workers\SimpleWorker;
use MailPoet\Features\FeaturesController;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
use MailPoet\Models\Newsletter;
use MailPoet\Models\ScheduledTask;
use MailPoet\Settings\SettingsController;
@ -28,6 +29,9 @@ class AutomatedEmails extends SimpleWorker {
/** @var WCHelper */
private $woocommerce_helper;
/** @var MetaInfo */
private $mailerMetaInfo;
/** @var float */
public $timer;
@ -36,6 +40,7 @@ class AutomatedEmails extends SimpleWorker {
Renderer $renderer,
SettingsController $settings,
WCHelper $woocommerce_helper,
MetaInfo $mailerMetaInfo,
$timer = false
) {
parent::__construct($timer);
@ -43,6 +48,7 @@ class AutomatedEmails extends SimpleWorker {
$this->settings = $settings;
$this->renderer = $renderer;
$this->woocommerce_helper = $woocommerce_helper;
$this->mailerMetaInfo = $mailerMetaInfo;
$this->timer = $timer ?: microtime(true);
}
@ -71,7 +77,10 @@ class AutomatedEmails extends SimpleWorker {
$settings = $this->settings->get(Worker::SETTINGS_KEY);
$newsletters = $this->getNewsletters();
if ($newsletters) {
$this->mailer->send($this->constructNewsletter($newsletters), $settings['address']);
$extra_params = [
'meta' => $this->mailerMetaInfo->getStatsNotificationMetaInfo(),
];
$this->mailer->send($this->constructNewsletter($newsletters), $settings['address'], $extra_params);
}
} catch (\Exception $e) {
if (WP_DEBUG) {

View File

@ -6,6 +6,7 @@ use Carbon\Carbon;
use MailPoet\Config\Renderer;
use MailPoet\Cron\CronHelper;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterLink;
use MailPoet\Models\ScheduledTask;
@ -35,11 +36,15 @@ class Worker {
/** @var WCHelper */
private $woocommerce_helper;
/** @var MetaInfo */
private $mailerMetaInfo;
function __construct(
Mailer $mailer,
Renderer $renderer,
SettingsController $settings,
WCHelper $woocommerce_helper,
MetaInfo $mailerMetaInfo,
$timer = false
) {
$this->timer = $timer ?: microtime(true);
@ -47,6 +52,7 @@ class Worker {
$this->mailer = $mailer;
$this->settings = $settings;
$this->woocommerce_helper = $woocommerce_helper;
$this->mailerMetaInfo = $mailerMetaInfo;
}
/** @throws \Exception */
@ -54,7 +60,10 @@ class Worker {
$settings = $this->settings->get(self::SETTINGS_KEY);
foreach (self::getDueTasks() as $task) {
try {
$this->mailer->send($this->constructNewsletter($task), $settings['address']);
$extra_params = [
'meta' => $this->mailerMetaInfo->getStatsNotificationMetaInfo(),
];
$this->mailer->send($this->constructNewsletter($task), $settings['address'], $extra_params);
} catch (\Exception $e) {
if (WP_DEBUG) {
throw $e;

View File

@ -20,6 +20,7 @@ use MailPoet\Services\AuthorizedEmailsController;
use MailPoet\Statistics\Track\WooCommercePurchases;
use MailPoet\WooCommerce\Helper as WooCommerceHelper;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
use MailPoet\Settings\SettingsController;
use MailPoet\Subscribers\InactiveSubscribersController;
use MailPoet\WP\Functions as WPFunctions;
@ -61,6 +62,9 @@ class WorkersFactory {
/** @var SubscribersFinder */
private $subscribers_finder;
/** @var MetaInfo */
private $mailerMetaInfo;
public function __construct(
SendingErrorHandler $sending_error_handler,
StatsNotificationScheduler $statsNotificationsScheduler,
@ -72,7 +76,8 @@ class WorkersFactory {
WooCommerceHelper $woocommerce_helper,
WooCommercePurchases $woocommerce_purchases,
AuthorizedEmailsController $authorized_emails_controller,
SubscribersFinder $subscribers_finder
SubscribersFinder $subscribers_finder,
MetaInfo $mailerMetaInfo
) {
$this->sending_error_handler = $sending_error_handler;
$this->statsNotificationsScheduler = $statsNotificationsScheduler;
@ -85,6 +90,7 @@ class WorkersFactory {
$this->woocommerce_purchases = $woocommerce_purchases;
$this->authorized_emails_controller = $authorized_emails_controller;
$this->subscribers_finder = $subscribers_finder;
$this->mailerMetaInfo = $mailerMetaInfo;
}
/** @return SchedulerWorker */
@ -99,12 +105,12 @@ class WorkersFactory {
/** @return StatsNotificationsWorker */
function createStatsNotificationsWorker($timer) {
return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $timer);
return new StatsNotificationsWorker($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $this->mailerMetaInfo, $timer);
}
/** @return StatsNotificationsWorkerForAutomatedEmails */
function createStatsNotificationsWorkerForAutomatedEmails($timer) {
return new StatsNotificationsWorkerForAutomatedEmails($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $timer);
return new StatsNotificationsWorkerForAutomatedEmails($this->mailer, $this->renderer, $this->settings, $this->woocommerce_helper, $this->mailerMetaInfo, $timer);
}
/** @return SendingServiceKeyCheckWorker */

View File

@ -10,6 +10,10 @@ class MetaInfo {
return $this->makeMetaInfo('preview', 'unknown', 'administrator');
}
function getStatsNotificationMetaInfo() {
return $this->makeMetaInfo('email_stats_notification', 'unknown', 'administrator');
}
private function makeMetaInfo($email_type, $subscriber_status, $subscriber_source) {
return [
'email_type' => $email_type,

View File

@ -4,6 +4,7 @@ namespace MailPoet\Cron\Workers\StatsNotifications;
use MailPoet\Config\Renderer;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
use MailPoet\Models\Newsletter;
use MailPoet\Models\ScheduledTask;
use MailPoet\Settings\SettingsController;
@ -43,6 +44,7 @@ class AutomatedEmailsTest extends \MailPoetTest {
$this->renderer,
$this->settings,
$this->makeEmpty(WCHelper::class),
new MetaInfo,
])
->setMethods(['getNewsletters'])
->getMock();

View File

@ -4,6 +4,7 @@ namespace MailPoet\Cron\Workers\StatsNotifications;
use MailPoet\Config\Renderer;
use MailPoet\Mailer\Mailer;
use MailPoet\Mailer\MetaInfo;
use MailPoet\Models\Newsletter;
use MailPoet\Models\NewsletterLink;
use MailPoet\Models\ScheduledTask;
@ -45,7 +46,7 @@ class WorkerTest extends \MailPoetTest {
$this->mailer = $this->createMock(Mailer::class);
$this->renderer = $this->createMock(Renderer::class);
$this->settings = new SettingsController();
$this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class));
$this->stats_notifications = new Worker($this->mailer, $this->renderer, $this->settings, $this->makeEmpty(WCHelper::class), new MetaInfo);
$this->settings->set(Worker::SETTINGS_KEY, [
'enabled' => true,
'address' => 'email@example.com',

View File

@ -27,4 +27,13 @@ class MetaInfoTest extends \MailPoetUnitTest {
'subscriber_source' => 'administrator',
]);
}
function testItGetsMetaInfoForStatsNotifications() {
expect($this->meta->getStatsNotificationMetaInfo())->equals([
'email_type' => 'email_stats_notification',
'subscriber_status' => 'unknown',
'subscriber_source' => 'administrator',
]);
}
}