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 6e098c1 commit 9035e2e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/Campaign.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,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 @@ -453,6 +453,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 @@ -505,12 +525,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 9035e2e

Please sign in to comment.