From 5f068f268d3543621a16aa6b283c531476cf50d6 Mon Sep 17 00:00:00 2001 From: bencroker Date: Fri, 3 Feb 2023 09:05:30 -0600 Subject: [PATCH] Add `siteId` parameter to forms controller --- CHANGELOG.md | 4 ++++ composer.json | 2 +- src/controllers/FormsController.php | 22 ++++++++++++++-------- src/services/MailingListsService.php | 10 ++++++++-- 4 files changed, 27 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 90e8b3f2..5e391617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Release Notes for Campaign +## 2.5.3 - 2023-02-03 +### Changed +- The front-end subscribe and unsubscribe forms now accept an optional `siteId` parameter ([#355](https://github.com/putyourlightson/craft-campaign/issues/363)). + ## 2.5.2 - 2023-01-24 ### Fixed - Fixed unauthorised access to some settings pages in environments where administrative changes are disallowed. diff --git a/composer.json b/composer.json index c3cb178e..e5fc64d5 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "putyourlightson/craft-campaign", "description": "Send and manage email campaigns, contacts and mailing lists.", - "version": "2.5.2", + "version": "2.5.3", "type": "craft-plugin", "homepage": "https://putyourlightson.com/plugins/campaign", "license": "proprietary", diff --git a/src/controllers/FormsController.php b/src/controllers/FormsController.php index c6e3be91..6003a4c4 100755 --- a/src/controllers/FormsController.php +++ b/src/controllers/FormsController.php @@ -10,6 +10,7 @@ use putyourlightson\campaign\base\BaseMessageController; use putyourlightson\campaign\Campaign; use putyourlightson\campaign\elements\ContactElement; +use putyourlightson\campaign\elements\MailingListElement; use putyourlightson\campaign\helpers\RecaptchaHelper; use yii\web\ForbiddenHttpException; use yii\web\NotFoundHttpException; @@ -30,10 +31,7 @@ public function actionSubscribe(): ?Response $this->requirePostRequest(); $this->_validateRecaptcha(); - // Get mailing list by slug - $mailingListSlug = $this->request->getRequiredParam('mailingList'); - $mailingList = Campaign::$plugin->mailingLists->getMailingListBySlug($mailingListSlug); - + $mailingList = $this->_getMailingListFromParams(); if ($mailingList === null) { throw new NotFoundHttpException(Craft::t('campaign', 'Mailing list not found.')); } @@ -72,10 +70,7 @@ public function actionUnsubscribe(): ?Response $this->requirePostRequest(); $this->_validateRecaptcha(); - // Get mailing list by slug - $mailingListSlug = $this->request->getRequiredParam('mailingList'); - $mailingList = Campaign::$plugin->mailingLists->getMailingListBySlug($mailingListSlug); - + $mailingList = $this->_getMailingListFromParams(); if ($mailingList === null) { throw new NotFoundHttpException(Craft::t('campaign', 'Mailing list not found.')); } @@ -234,6 +229,17 @@ public function actionVerifyUnsubscribe(): ?Response ]); } + /** + * Returns a mailing list from the posted parameters. + */ + private function _getMailingListFromParams(): ?MailingListElement + { + $mailingListSlug = $this->request->getRequiredBodyParam('mailingList'); + $siteId = $this->request->getBodyParam('siteId'); + + return Campaign::$plugin->mailingLists->getMailingListBySlug($mailingListSlug, $siteId); + } + /** * Validates reCAPTCHA if enabled. */ diff --git a/src/services/MailingListsService.php b/src/services/MailingListsService.php index b3749860..45ba31a1 100644 --- a/src/services/MailingListsService.php +++ b/src/services/MailingListsService.php @@ -5,6 +5,7 @@ namespace putyourlightson\campaign\services; +use Craft; use craft\base\Component; use DateTime; use putyourlightson\campaign\elements\ContactElement; @@ -52,13 +53,18 @@ public function getMailingListsByIds(?array $mailingListIds): array } /** - * Returns a mailing list by its slug, in the current site. + * Returns a mailing list by its slug, in the provided site or the current site. */ - public function getMailingListBySlug(string $mailingListSlug): ?MailingListElement + public function getMailingListBySlug(string $mailingListSlug, ?int $siteId = null): ?MailingListElement { + if ($siteId === null) { + $siteId = Craft::$app->getSites()->getCurrentSite()->id; + } + /** @var MailingListElement|null */ return MailingListElement::find() ->slug($mailingListSlug) + ->siteId($siteId) ->one(); }