Fix newsletter::body type errors

[MAILPOET-3146]
This commit is contained in:
Rostislav Wolny
2020-10-07 10:31:40 +02:00
committed by Veljko V
parent 1de06f5aa6
commit ab9130f845
3 changed files with 15 additions and 7 deletions

View File

@@ -45,7 +45,7 @@ class Posts {
} }
public function getAlcPostsCount($renderedNewsletter, \MailPoet\Models\Newsletter $newsletter) { 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'); $newsletterPostsCount = substr_count($renderedNewsletter['html'], 'data-post-id');
return $newsletterPostsCount - $templatePostsCount; return $newsletterPostsCount - $templatePostsCount;
} }

View File

@@ -34,7 +34,7 @@ use function MailPoetVendor\array_column;
* @property array $segments * @property array $segments
* @property string $subject * @property string $subject
* @property string $preheader * @property string $preheader
* @property string $body * @property string|array|null $body
* @property string|null $schedule * @property string|null $schedule
* @property bool|null $isScheduled * @property bool|null $isScheduled
* @property string|null $scheduledAt * @property string|null $scheduledAt
@@ -116,9 +116,7 @@ class Newsletter extends Model {
} }
if (isset($this->body) && ($this->body !== false)) { if (isset($this->body) && ($this->body !== false)) {
if (is_array($this->body)) { $this->body = $this->getBodyString();
$this->body = (string)json_encode($this->body);
}
$this->set( $this->set(
'body', 'body',
$this->body $this->body
@@ -155,7 +153,7 @@ class Newsletter extends Model {
public function setStatus($status = null) { public function setStatus($status = null) {
if ($status === self::STATUS_ACTIVE) { if ($status === self::STATUS_ACTIVE) {
if (!$this->body || empty(json_decode($this->body))) { if (!$this->body || empty(json_decode($this->getBodyString()))) {
$this->setError( $this->setError(
Helpers::replaceLinkTags( Helpers::replaceLinkTags(
__('This is an empty email without any content and it cannot be sent. Please update [link]the email[/link].'), __('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); 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() { public function withSendingQueue() {
$queue = $this->getQueue(); $queue = $this->getQueue();
if ($queue === false) { if ($queue === false) {

View File

@@ -205,7 +205,7 @@ class NewsletterTest extends \MailPoetTest {
$newsletter->type = Newsletter::TYPE_NOTIFICATION_HISTORY; $newsletter->type = Newsletter::TYPE_NOTIFICATION_HISTORY;
$newsletter->parentId = $newsletter->id; $newsletter->parentId = $newsletter->id;
// replace post id data tag with something else // 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(); $newsletter->save();
// returned result is false // returned result is false
$result = $this->newsletterTask->preProcessNewsletter($this->newsletter, $this->queue); $result = $this->newsletterTask->preProcessNewsletter($this->newsletter, $this->queue);