diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon
index a34b0cf8..303f0179 100644
--- a/phpstan-baseline.neon
+++ b/phpstan-baseline.neon
@@ -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
diff --git a/src/Admin/Settings.php b/src/Admin/Settings.php
index 0109f9c9..66593dd8 100644
--- a/src/Admin/Settings.php
+++ b/src/Admin/Settings.php
@@ -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;
@@ -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')
@@ -423,7 +413,7 @@ public function carriersSettings($settings)
'description' => sprintf(
// Translators: %s default value.
__('Leave blank to use default value: %s', 'notification'),
- 'WordPress
'
+ '' . Email::getDefaultFromName() . '
'
),
]
)
@@ -437,7 +427,7 @@ public function carriersSettings($settings)
'description' => sprintf(
// Translators: %s default value.
__('Leave blank to use default value: %s', 'notification'),
- '' . $defaultFromEmail . '
'
+ '' . Email::getDefaultFromEmail() . '
'
),
]
)
diff --git a/src/Repository/Carrier/Email.php b/src/Repository/Carrier/Email.php
index b1bf8281..06f4f859 100644
--- a/src/Repository/Carrier/Email.php
+++ b/src/Repository/Carrier/Email.php
@@ -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();
@@ -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 = [];
@@ -253,17 +268,63 @@ public function allowUnfilteredHtmlBody($carrierData, $rawData)
}
/**
- * Retrieve email "From" header from settings.
+ * Gets the list of headers.
*
- * @return string
+ * @return array
*/
- 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
+ */
+ 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()
+ );
}
}