Render newsletters without clicked links

[MAILPOET-1571]
This commit is contained in:
Pavel Dohnal
2019-01-22 13:15:20 +01:00
parent de106e8828
commit 9eeda50b07
3 changed files with 45 additions and 5 deletions

View File

@ -100,11 +100,11 @@ class Worker {
->withStatistics();
}
private function prepareContext(Newsletter $newsletter, NewsletterLink $link) {
private function prepareContext(Newsletter $newsletter, NewsletterLink $link = null) {
$clicked = ($newsletter->statistics['clicked'] * 100) / $newsletter->total_sent;
$opened = ($newsletter->statistics['opened'] * 100) / $newsletter->total_sent;
$unsubscribed = ($newsletter->statistics['unsubscribed'] * 100) / $newsletter->total_sent;
return [
$context = [
'subject' => $newsletter->subject,
'preheader' => sprintf(_x(
'%1$s%% opens, %2$s%% clicks, %3$s%% unsubscribes in a nutshell.', 'newsletter open rate, click rate and unsubscribe rate', 'mailpoet'),
@ -112,14 +112,18 @@ class Worker {
number_format($opened, 2),
number_format($unsubscribed, 2)
),
'topLinkClicks' => (int)$link->clicksCount,
'topLink' => $link->url,
$context['topLinkClicks'] = 0,
'linkSettings' => get_site_url(null, '/wp-admin/admin.php?page=mailpoet-settings#basics'),
'linkStats' => get_site_url(null, '/wp-admin/admin.php?page=mailpoet-newsletters#/stats/' . $newsletter->id()),
'premiumPluginActive' => is_plugin_active('mailpoet-premium/mailpoet-premium.php'),
'clicked' => $clicked,
'opened' => $opened,
];
if($link) {
$context['topLinkClicks'] = (int)$link->clicksCount;
$context['topLink'] = $link->url;
}
return $context;
}
private function markTaskAsFinished(ScheduledTask $task) {

View File

@ -7,7 +7,7 @@ class NewsletterLink extends Model {
public static $_table = MP_NEWSLETTER_LINKS_TABLE;
static function findTopLinkForNewsletter(Newsletter $newsletter) {
return self::selectExpr('links.*')
$link = self::selectExpr('links.*')
->selectExpr('count(*)', 'clicksCount')
->tableAlias('links')
->innerJoin(StatisticsClicks::$_table,
@ -18,6 +18,10 @@ class NewsletterLink extends Model {
->orderByDesc('clicksCount')
->limit(1)
->findOne();
if(!$link) {
return null;
}
return $link;
}
}

View File

@ -186,4 +186,36 @@ class WorkerTest extends \MailPoetTest {
$this->stats_notifications->process();
}
function testItWorksForNewsletterWithNoStats() {
$newsletter = Newsletter::createOrUpdate([
'subject' => 'Email Subject2',
'type' => Newsletter::TYPE_STANDARD,
]);
$sending_task = ScheduledTask::createOrUpdate([
'type' => 'sending',
'status' => ScheduledTask::STATUS_COMPLETED,
]);
$stats_notifications_task = ScheduledTask::createOrUpdate([
'type' => Worker::TASK_TYPE,
'status' => ScheduledTask::STATUS_SCHEDULED,
'scheduled_at' => '2016-01-02 12:13:14',
'processed_at' => null,
]);
StatsNotification::createOrUpdate([
'newsletter_id' => $newsletter->id(),
'task_id' => $stats_notifications_task->id(),
]);
SendingQueue::createOrUpdate([
'newsletter_rendered_subject' => 'Email Subject2',
'task_id' => $sending_task->id(),
'newsletter_id' => $newsletter->id(),
'count_processed' => 15,
]);
$this->mailer->expects($this->once())
->method('send');
$this->stats_notifications->process();
}
}