Skip to content

Commit

Permalink
Add siteId parameter to forms controller
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Feb 3, 2023
1 parent 26205eb commit 5f068f2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
22 changes: 14 additions & 8 deletions src/controllers/FormsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.'));
}
Expand Down Expand Up @@ -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.'));
}
Expand Down Expand Up @@ -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.
*/
Expand Down
10 changes: 8 additions & 2 deletions src/services/MailingListsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace putyourlightson\campaign\services;

use Craft;
use craft\base\Component;
use DateTime;
use putyourlightson\campaign\elements\ContactElement;
Expand Down Expand Up @@ -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();
}

Expand Down

0 comments on commit 5f068f2

Please sign in to comment.