Skip to content

Commit

Permalink
Update custom field object syntax to only support Subject and Default…
Browse files Browse the repository at this point in the history
… Body fields

- Removes additional processing of rendered tempaltes
- Removes addPlaceholderStyleTags and removePlaceholderStyleTags methods
  • Loading branch information
BenParizek committed Apr 8, 2020
1 parent 5ef759f commit dfaaf53
Showing 1 changed file with 2 additions and 64 deletions.
66 changes: 2 additions & 64 deletions src/base/Mailer.php
Expand Up @@ -9,9 +9,9 @@

use barrelstrength\sproutbaseemail\models\SimpleRecipient;
use barrelstrength\sproutbaseemail\models\SimpleRecipientList;
use barrelstrength\sproutbaseemail\web\twig\EmailTemplateSandboxExtension;
use Craft;
use craft\base\Component;
use craft\helpers\Html;
use craft\mail\Message;
use Exception;
use Throwable;
Expand Down Expand Up @@ -134,6 +134,7 @@ public function getMessage(EmailElement $email): Message
$this->renderObjectTemplateSafely($email, 'fromName', $object);
$this->renderObjectTemplateSafely($email, 'fromEmail', $object);
$this->renderObjectTemplateSafely($email, 'replyToEmail', $object);
$this->renderObjectTemplateSafely($email, 'defaultBody', $object);

$message->setSubject($email->subjectLine);
$message->setFrom([$email->fromEmail => $email->fromName]);
Expand All @@ -159,32 +160,6 @@ public function getMessage(EmailElement $email): Message
$email->addError('htmlBody', Craft::t('sprout-base-email', 'HTML template is blank.'));
}

$styleTags = [];

// Swap out Style tags so we don't run into conflicts with shorthand object-syntax
$htmlBody = $this->addPlaceholderStyleTags($htmlBody, $styleTags);

// Some Twig code in our email fields may need us to decode
// entities so our email doesn't throw errors when we try to
// render the field objects. Example: {variable|date("Y/m/d")}
$textBody = Html::decode($textBody);
$htmlBody = Html::decode($htmlBody);

// Process the results of the templates once more, to render any dynamic objects used in fields
try {
$textBody = Craft::$app->getView()->renderObjectTemplate($textBody, $object);
} catch (Exception $e) {
$email->addError('body', $e->getMessage());
}

try {
$htmlBody = Craft::$app->getView()->renderObjectTemplate($htmlBody, $object);
} catch (Exception $e) {
$email->addError('htmlBody', $e->getMessage());
}

$htmlBody = $this->removePlaceholderStyleTags($htmlBody, $styleTags);

$message->setTextBody($textBody);
$message->setHtmlBody($htmlBody);

Expand Down Expand Up @@ -213,41 +188,4 @@ private function renderObjectTemplateSafely(EmailElement $email, $attribute, $ob
$email->addError($email->{$attribute}, $e->getMessage());
}
}

private function addPlaceholderStyleTags($htmlBody, &$styleTags)
{
// Get the style tag
preg_match_all("/<style\\b[^>]*>(.*?)<\\/style>/s", $htmlBody, $matches);

if (!empty($matches)) {
$tags = $matches[0];

// Temporarily replace with style tags with a random string
if (!empty($tags)) {
$i = 0;
foreach ($tags as $tag) {
$key = "<!-- %style$i% -->";

$styleTags[$key] = $tag;

$htmlBody = str_replace($tag, $key, $htmlBody);

$i++;
}
}
}

return $htmlBody;
}

private function removePlaceholderStyleTags($htmlBody, $styleTags)
{
if (!empty($styleTags)) {
foreach ($styleTags as $key => $tag) {
$htmlBody = str_replace($key, $tag, $htmlBody);
}
}

return $htmlBody;
}
}

0 comments on commit dfaaf53

Please sign in to comment.