Skip to content

Commit

Permalink
fix: prioritize carrier "From" header
Browse files Browse the repository at this point in the history
  • Loading branch information
Dartui authored and jakubmikita committed Jul 9, 2024
1 parent 6d793a3 commit d782e30
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 51 deletions.
15 changes: 0 additions & 15 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -1165,21 +1165,6 @@ parameters:
count: 1
path: src/Repository/Carrier/BaseCarrier.php

-
message: "#^Argument of an invalid type mixed supplied for foreach, only iterables are supported\\.$#"
count: 1
path: src/Repository/Carrier/Email.php

-
message: "#^Cannot access offset 'key' on mixed\\.$#"
count: 1
path: src/Repository/Carrier/Email.php

-
message: "#^Cannot access offset 'value' on mixed\\.$#"
count: 1
path: src/Repository/Carrier/Email.php

-
message: "#^Parameter \\#1 \\$carrier of static method BracketSpace\\\\Notification\\\\Register\\:\\:carrier\\(\\) expects BracketSpace\\\\Notification\\\\Interfaces\\\\Sendable, mixed given\\.$#"
count: 3
Expand Down
16 changes: 3 additions & 13 deletions src/Admin/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace BracketSpace\Notification\Admin;

use BracketSpace\Notification\Repository\Carrier\Email;
use BracketSpace\Notification\Utils\Settings\CoreFields;
use BracketSpace\Notification\Utils\WpObjectHelper;

Expand Down Expand Up @@ -356,17 +357,6 @@ public function triggersSettings($settings)
*/
public function carriersSettings($settings)
{
if (!empty($_SERVER['SERVER_NAME'])) {
$sitename = strtolower(sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])));
if (substr($sitename, 0, 4) === 'www.') {
$sitename = substr($sitename, 4);
}
} else {
$sitename = 'example.com';
}

$defaultFromEmail = 'wordpress@' . $sitename;

$carriers = $settings->addSection(__('Carriers', 'notification'), 'carriers');

$carriers->addGroup(__('Email', 'notification'), 'email')
Expand Down Expand Up @@ -423,7 +413,7 @@ public function carriersSettings($settings)
'description' => sprintf(
// Translators: %s default value.
__('Leave blank to use default value: %s', 'notification'),
'<code>WordPress</code>'
'<code>' . Email::getDefaultFromName() . '</code>'
),
]
)
Expand All @@ -437,7 +427,7 @@ public function carriersSettings($settings)
'description' => sprintf(
// Translators: %s default value.
__('Leave blank to use default value: %s', 'notification'),
'<code>' . $defaultFromEmail . '</code>'
'<code>' . Email::getDefaultFromEmail() . '</code>'
),
]
)
Expand Down
107 changes: 84 additions & 23 deletions src/Repository/Carrier/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,36 @@ public function __construct()
);
}

/**
* Gets the default name for "From" header.
*
* @return string
*/
public static function getDefaultFromName(): string
{
return 'WordPress';
}

/**
* Gets the default email for "From" header.
*
* @return string
*/
public static function getDefaultFromEmail(): string
{
if (!empty($_SERVER['SERVER_NAME'])) {
$sitename = strtolower(sanitize_text_field(wp_unslash($_SERVER['SERVER_NAME'])));

if (substr($sitename, 0, 4) === 'www.') {
$sitename = substr($sitename, 4);
}
} else {
$sitename = 'example.com';
}

return 'wordpress@' . $sitename;
}

/**
* Used to register Carrier form fields
* Uses $this->addFormField();
Expand Down Expand Up @@ -169,23 +199,8 @@ public function send(Triggerable $trigger)
$message = ' ';
}

$headers = [];
$headers = apply_filters('notification/carrier/email/headers', $this->getHeaders(), $this, $trigger);

$fromHeader = $this->getFromHeaderSetting();
if ($fromHeader) {
$headers[] = $fromHeader;
}

if (
\Notification::settings()->getSetting('carriers/email/headers') &&
! empty($data['headers'])
) {
foreach ($data['headers'] as $header) {
$headers[] = $header['key'] . ': ' . $header['value'];
}
}

$headers = apply_filters('notification/carrier/email/headers', $headers, $this, $trigger);
$attachments = apply_filters('notification/carrier/email/attachments', [], $this, $trigger);

$errors = [];
Expand Down Expand Up @@ -253,17 +268,63 @@ public function allowUnfilteredHtmlBody($carrierData, $rawData)
}

/**
* Retrieve email "From" header from settings.
* Gets the list of headers.
*
* @return string
* @return array<string>
*/
protected function getFromHeaderSetting()
protected function getHeaders(): array
{
$headers = $this->getCarrierHeaders();
$headers['from'] ??= $this->getDefaultFromHeader();

return array_map(
static function ($value, $key) {
return sprintf('%s: %s', $key, $value);
},
array_values($headers),
array_keys($headers)
);
}

/**
* Gets organized list of carrier headers.
*
* @return array<string, string>
*/
protected function getCarrierHeaders(): array
{
if (!\Notification::settings()->getSetting('carriers/email/headers')) {
return [];
}

$data = is_array($this->data['headers'] ?? null) ? $this->data['headers'] : [];
$headers = [];

foreach ($data as $header) {
if (!is_array($header) || !is_string($header['key'] ?? null) || !is_string($header['value'] ?? null)) {
continue;
}

$headers[strtolower($header['key'])] = $header['value'];
}

return array_filter($headers);
}

/**
* Gets the default "From" header value.
*
* @return string
*/
protected function getDefaultFromHeader(): string
{
/** @var string $fromName */
$fromName = \Notification::settings()->getSetting('carriers/email/from_name');
/** @var string $fromEmail */
$fromEmail = \Notification::settings()->getSetting('carriers/email/from_email');
$fromName = \Notification::settings()->getSetting('carriers/email/from_name');

return sprintf('From: %s <%s>', $fromName, $fromEmail);
return sprintf(
'%s <%s>',
is_string($fromName) && strlen($fromName) ? $fromName : self::getDefaultFromName(),
is_string($fromEmail) && strlen($fromEmail) ? $fromEmail : self::getDefaultFromEmail()
);
}
}

0 comments on commit d782e30

Please sign in to comment.