Skip to content

Commit

Permalink
Accept env variables in from names and emails
Browse files Browse the repository at this point in the history
  • Loading branch information
bencroker committed Oct 31, 2024
1 parent ef6d3ea commit 928112c
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Release Notes for Campaign

## 3.5.6 - Unreleased
## 3.5.6 - 2024-10-31

### Changed

- The “from names and emails” setting now accepts environment variables ([#503](https://github.com/putyourlightson/craft-campaign/issues/503)).

### Fixed

Expand Down
4 changes: 2 additions & 2 deletions src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,8 +390,8 @@ protected function createSettingsModel(): SettingsModel
$settings->apiKey = StringHelper::randomString(16);
$settings->fromNamesEmails = [
[
App::parseEnv($mailSettings->fromName),
App::parseEnv($mailSettings->fromEmail),
$mailSettings->fromName,
$mailSettings->fromEmail,
'',
Craft::$app->getSites()->getPrimarySite()->id,
],
Expand Down
10 changes: 5 additions & 5 deletions src/helpers/SettingsHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static function getFromNameEmail(int $siteId = null): array
{
// Get first from name and email
$firstFromNameEmail = [];
$fromNamesEmails = Campaign::$plugin->settings->fromNamesEmails;
$fromNamesEmails = Campaign::$plugin->settings->getFromNamesEmails();

foreach ($fromNamesEmails as $fromNameEmail) {
if ($siteId === null || empty($fromNameEmail[3]) || $fromNameEmail[3] == $siteId) {
Expand All @@ -57,9 +57,9 @@ public static function getFromNameEmail(int $siteId = null): array
$mailSettings = App::mailSettings();

$firstFromNameEmail = [
'name' => $mailSettings->fromName,
'email' => $mailSettings->fromEmail,
'replyTo' => '',
'name' => App::parseEnv($mailSettings->fromName),
'email' => App::parseEnv($mailSettings->fromEmail),
'replyTo' => App::parseEnv($mailSettings->replyToEmail),
];
}

Expand All @@ -72,7 +72,7 @@ public static function getFromNameEmail(int $siteId = null): array
public static function getFromNameEmailOptions(int $siteId = null): array
{
$fromNameEmailOptions = [];
$fromNamesEmails = Campaign::$plugin->settings->fromNamesEmails;
$fromNamesEmails = Campaign::$plugin->settings->getFromNamesEmails();

foreach ($fromNamesEmails as $fromNameEmail) {
$fromSiteId = $fromNameEmail[3] ?? null;
Expand Down
26 changes: 24 additions & 2 deletions src/models/SettingsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,26 @@ public function getTurnstileErrorMessage(): string
return App::parseEnv($this->turnstileErrorMessage) ?? '';
}

/**
* Returns the parsed from names and emails.
*
* @since 3.5.6
*/
public function getFromNamesEmails(): array
{
$fromNamesEmails = [];
foreach ($this->fromNamesEmails as $fromNameEmail) {
$fromNamesEmails[] = [
App::parseEnv($fromNameEmail[0]) ?? '',
App::parseEnv($fromNameEmail[1]) ?? '',
App::parseEnv($fromNameEmail[2]) ?? '',
$fromNameEmail[3] ?? null,
];
}

return $fromNamesEmails;
}

/**
* Returns the contact field layout.
*/
Expand Down Expand Up @@ -469,12 +489,14 @@ public function getContactFields(): array
*/
public function validateFromNamesEmails(mixed $attribute): void
{
if (empty($this->fromNamesEmails)) {
$fromNamesEmails = $this->getFromNamesEmails();

if (empty($fromNamesEmails)) {
$this->addError($attribute, Craft::t('campaign', 'You must enter at least one name and email.'));
return;
}

foreach ($this->fromNamesEmails as $fromNameEmail) {
foreach ($fromNamesEmails as $fromNameEmail) {
if ($fromNameEmail[0] === '' || $fromNameEmail[1] === '') {
$this->addError($attribute, Craft::t('campaign', 'The name and email cannot be blank.'));
return;
Expand Down

0 comments on commit 928112c

Please sign in to comment.