diff --git a/lib/Cron/Workers/SendingQueue/Tasks/Posts.php b/lib/Cron/Workers/SendingQueue/Tasks/Posts.php index 3c31edaa94..2b670b60ff 100644 --- a/lib/Cron/Workers/SendingQueue/Tasks/Posts.php +++ b/lib/Cron/Workers/SendingQueue/Tasks/Posts.php @@ -45,7 +45,7 @@ class Posts { } public function getAlcPostsCount($renderedNewsletter, \MailPoet\Models\Newsletter $newsletter) { - $templatePostsCount = substr_count($newsletter->body, 'data-post-id'); + $templatePostsCount = substr_count($newsletter->getBodyString(), 'data-post-id'); $newsletterPostsCount = substr_count($renderedNewsletter['html'], 'data-post-id'); return $newsletterPostsCount - $templatePostsCount; } diff --git a/lib/Models/Newsletter.php b/lib/Models/Newsletter.php index 714e4d9f5f..d0aa34f7e9 100644 --- a/lib/Models/Newsletter.php +++ b/lib/Models/Newsletter.php @@ -34,7 +34,7 @@ use function MailPoetVendor\array_column; * @property array $segments * @property string $subject * @property string $preheader - * @property string $body + * @property string|array|null $body * @property string|null $schedule * @property bool|null $isScheduled * @property string|null $scheduledAt @@ -116,9 +116,7 @@ class Newsletter extends Model { } if (isset($this->body) && ($this->body !== false)) { - if (is_array($this->body)) { - $this->body = (string)json_encode($this->body); - } + $this->body = $this->getBodyString(); $this->set( 'body', $this->body @@ -155,7 +153,7 @@ class Newsletter extends Model { public function setStatus($status = null) { if ($status === self::STATUS_ACTIVE) { - if (!$this->body || empty(json_decode($this->body))) { + if (!$this->body || empty(json_decode($this->getBodyString()))) { $this->setError( Helpers::replaceLinkTags( __('This is an empty email without any content and it cannot be sent. Please update [link]the email[/link].'), @@ -342,6 +340,16 @@ class Newsletter extends Model { return SendingTask::getByNewsletterId($this->id); } + public function getBodyString(): string { + if (is_array($this->body)) { + return (string)json_encode($this->body); + } + if ($this->body === null) { + return ''; + } + return $this->body; + } + public function withSendingQueue() { $queue = $this->getQueue(); if ($queue === false) { diff --git a/tests/integration/Cron/Workers/SendingQueue/Tasks/NewsletterTest.php b/tests/integration/Cron/Workers/SendingQueue/Tasks/NewsletterTest.php index 9f0de145c9..7b642db2e5 100644 --- a/tests/integration/Cron/Workers/SendingQueue/Tasks/NewsletterTest.php +++ b/tests/integration/Cron/Workers/SendingQueue/Tasks/NewsletterTest.php @@ -205,7 +205,7 @@ class NewsletterTest extends \MailPoetTest { $newsletter->type = Newsletter::TYPE_NOTIFICATION_HISTORY; $newsletter->parentId = $newsletter->id; // replace post id data tag with something else - $newsletter->body = str_replace('data-post-id', 'id', $newsletter->body); + $newsletter->body = str_replace('data-post-id', 'id', $newsletter->getBodyString()); $newsletter->save(); // returned result is false $result = $this->newsletterTask->preProcessNewsletter($this->newsletter, $this->queue);