From d58b2ed40eb592dfab3c196b64b92ad14e229f89 Mon Sep 17 00:00:00 2001 From: Pavel Dohnal Date: Wed, 17 Jul 2019 14:41:13 +0200 Subject: [PATCH] Refactor code to the common place [MAILPOET-1859] --- .../StatsNotifications/AutomatedEmails.php | 2 +- lib/Twig/Functions.php | 60 +++++++++++++++++++ views/emails/statsNotification.html | 44 +++----------- views/emails/statsNotification.txt | 16 +---- views/emails/statsNotificationLayout.html | 2 +- 5 files changed, 72 insertions(+), 52 deletions(-) diff --git a/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php b/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php index 344560e14d..69062e943f 100644 --- a/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php +++ b/lib/Cron/Workers/StatsNotifications/AutomatedEmails.php @@ -44,7 +44,7 @@ class AutomatedEmails extends SimpleWorker { function processTaskStrategy(ScheduledTask $task) { // TODO // TODO refactor \MailPoet\Cron\Workers\StatsNotifications\Worker and Scheduler and share the code - // TODO refactor the views templates mailpoet/views/emails/statsNotification.html and txt and share them + // TODO settings isn't saved properly :( } static function getNextRunDate() { diff --git a/lib/Twig/Functions.php b/lib/Twig/Functions.php index 159060460f..e7ea6b2cdb 100644 --- a/lib/Twig/Functions.php +++ b/lib/Twig/Functions.php @@ -128,6 +128,26 @@ class Functions extends AbstractExtension { [$this, 'getWPStartOfWeek'], ['is_safe' => ['all']] ), + new TwigFunction( + 'opened_stats_color', + [$this, 'openedStatsColor'], + ['is_safe' => ['all']] + ), + new TwigFunction( + 'clicked_stats_color', + [$this, 'clickedStatsColor'], + ['is_safe' => ['all']] + ), + new TwigFunction( + 'opened_stats_text', + [$this, 'openedStatsText'], + ['is_safe' => ['all']] + ), + new TwigFunction( + 'clicked_stats_text', + [$this, 'clickedStatsText'], + ['is_safe' => ['all']] + ), ]; } @@ -225,4 +245,44 @@ class Functions extends AbstractExtension { function isWoocommerceActive() { return $this->woocommerce_helper->isWooCommerceActive(); } + + function openedStatsColor($opened) { + if ($opened > 30) { + return '#2993ab'; + } elseif ($opened > 10) { + return '#f0b849'; + } else { + return '#d54e21'; + } + } + + function clickedStatsColor($clicked) { + if ($clicked > 3) { + return '#2993ab'; + } elseif ($clicked > 1) { + return '#f0b849'; + } else { + return '#d54e21'; + } + } + + function openedStatsText($opened) { + if ($opened > 30) { + return __('EXCELLENT', 'mailpoet'); + } elseif ($opened > 10) { + return __('GOOD', 'mailpoet'); + } else { + return __('BAD', 'mailpoet'); + } + } + + function clickedStatsText($clicked) { + if ($clicked > 3) { + return __('EXCELLENT', 'mailpoet'); + } elseif ($clicked > 1) { + return __('GOOD', 'mailpoet'); + } else { + return __('BAD', 'mailpoet'); + } + } } diff --git a/views/emails/statsNotification.html b/views/emails/statsNotification.html index 7d6a562163..072cb5ceb4 100644 --- a/views/emails/statsNotification.html +++ b/views/emails/statsNotification.html @@ -1,21 +1,5 @@ <% extends 'emails/statsNotificationLayout.html' %> -<% if opened > 30 %> - <% set openedColor = '2993ab' %> -<% elseif opened > 10 %> - <% set openedColor = 'f0b849' %> -<% else %> - <% set openedColor = 'd54e21' %> -<% endif %> - -<% if clicked > 3 %> - <% set clickedColor = '2993ab' %> -<% elseif clicked > 1 %> - <% set clickedColor = 'f0b849' %> -<% else %> - <% set clickedColor = 'd54e21' %> -<% endif %> - <% block content %> @@ -82,14 +66,8 @@
- - <% if opened > 30 %> - <%= __('EXCELLENT') %> - <% elseif opened > 10 %> - <%= __('GOOD') %> - <% else %> - <%= __('BAD') %> - <% endif %> + + <%= opened_stats_text(opened) %>
@@ -99,7 +77,7 @@

- + <%= number_format_i18n(opened, 1) %>%

@@ -110,7 +88,7 @@ @@ -133,14 +111,8 @@
- + <%= __('open rate') %>
@@ -151,7 +123,7 @@
- - <% if clicked > 3 %> - <%= __('EXCELLENT') %> - <% elseif clicked > 1 %> - <%= __('GOOD') %> - <% else %> - <%= __('BAD') %> - <% endif %> + + <%= clicked_stats_text(clicked) %>

- + <%= number_format_i18n(clicked, 1) %>%

@@ -162,7 +134,7 @@ diff --git a/views/emails/statsNotification.txt b/views/emails/statsNotification.txt index b089898ef7..3809f80e6c 100644 --- a/views/emails/statsNotification.txt +++ b/views/emails/statsNotification.txt @@ -6,22 +6,10 @@ <%= subject %> <%= __('open rate') %>: <%= number_format_i18n(opened) %>% -<% if opened > 30 %> - <%= __('EXCELLENT') %> -<% elseif opened > 10 %> - <%= __('GOOD') %> -<% else %> - <%= __('BAD') %> -<% endif %> + <%= opened_stats_text(opened) %> <%= __('click rate') %>: <%= number_format_i18n(clicked) %>% -<% if clicked > 3 %> - <%= __('EXCELLENT') %> -<% elseif clicked > 1 %> - <%= __('GOOD') %> -<% else %> - <%= __('BAD') %> -<% endif %> + <%= clicked_stats_text(opened) %> <% if topLinkClicks > 0 %> <%= __('Most clicked link') %> diff --git a/views/emails/statsNotificationLayout.html b/views/emails/statsNotificationLayout.html index c2df27e555..1fa4056546 100644 --- a/views/emails/statsNotificationLayout.html +++ b/views/emails/statsNotificationLayout.html @@ -28,7 +28,7 @@
- + <%= __('click rate') %>
- <% if preheader %> + <% if preheader is defined %>