From e30ff4369f1408dd70dd024abcf996b5e08e10e7 Mon Sep 17 00:00:00 2001 From: bencroker Date: Thu, 27 Oct 2022 22:34:51 -0500 Subject: [PATCH] Render the page template only for HTML --- src/controllers/SendoutsController.php | 18 +++++++++------ src/elements/CampaignElement.php | 31 ++++++++++++++------------ 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/src/controllers/SendoutsController.php b/src/controllers/SendoutsController.php index cd00f024..3227dbe5 100644 --- a/src/controllers/SendoutsController.php +++ b/src/controllers/SendoutsController.php @@ -90,15 +90,19 @@ public function actionGetPendingRecipientCount(): Response */ public function actionGetHtmlBody(): Response { - $sendoutId = $this->request->getRequiredParam('sendoutId'); - $sendout = Campaign::$plugin->sendouts->getSendoutById($sendoutId); + $sendout = $this->_getSendoutFromParamId(); + $this->response->content = $sendout->getHtmlBody(); - if ($sendout === null) { - throw new NotFoundHttpException(Craft::t('campaign', 'Sendout not found.')); - } + return $this->response; + } - // Prep the response - $this->response->content = $sendout->getHtmlBody(); + /** + * Returns the plaintext body. + */ + public function actionGetPlaintextBody(): Response + { + $sendout = $this->_getSendoutFromParamId(); + $this->response->content = $sendout->getPlaintextBody(); return $this->response; } diff --git a/src/elements/CampaignElement.php b/src/elements/CampaignElement.php index dd84c505..6901c708 100644 --- a/src/elements/CampaignElement.php +++ b/src/elements/CampaignElement.php @@ -956,6 +956,16 @@ private function _getBody(string $templateType = null, ContactElement $contact = // Get body from rendered template with variables $template = $templateType == 'html' ? $this->getCampaignType()->htmlTemplate : $this->getCampaignType()->plaintextTemplate; + $variables = [ + 'campaign' => $this, + 'browserVersionUrl' => $this->url, + 'contact' => $contact, + 'sendout' => $sendout, + 'mailingList' => $mailingList, + 'unsubscribeUrl' => $contact->getUnsubscribeUrl($sendout), + 'isWebRequest' => false, + ]; + $templateMode = View::TEMPLATE_MODE_SITE; // Set the current site from the campaign's site ID Craft::$app->getSites()->setCurrentSite($this->siteId); @@ -965,20 +975,13 @@ private function _getBody(string $templateType = null, ContactElement $contact = Craft::$app->language = $this->_getLanguage(); try { - // Render the page template to prevent Yii block tags being left behind - $body = Craft::$app->getView()->renderPageTemplate( - $template, - [ - 'campaign' => $this, - 'browserVersionUrl' => $this->url, - 'contact' => $contact, - 'sendout' => $sendout, - 'mailingList' => $mailingList, - 'unsubscribeUrl' => $contact->getUnsubscribeUrl($sendout), - 'isWebRequest' => false, - ], - View::TEMPLATE_MODE_SITE, - ); + // Render the page template only for HTML, to prevent Yii block tags being left behind + if ($templateType == 'html') { + $body = Craft::$app->getView()->renderPageTemplate($template, $variables, $templateMode); + } + else { + $body = Craft::$app->getView()->renderTemplate($template, $variables, $templateMode); + } } catch (Error $exception) { Campaign::$plugin->log($exception->getMessage());