';
/** Ordner bekannter Vorlagen-Plugins: dann nicht doppelt gestalten. */
private const ANDERE = ['wp-html-mail' => 'WP HTML Mail', 'email-templates' => 'Email Templates', 'wp-better-emails' => 'WP Better Emails', 'wp-email-template' => 'WP Email Template'];
public static function registrieren(): void
{
add_filter('wp_mail', [self::class, 'gestalten'], 900);
}
/** Name eines aktiven Vorlagen-Plugins oder null. */
public static function anderesPlugin(): ?string
{
foreach ((array) get_option('active_plugins', []) as $datei) {
$ordner = strtok((string) $datei, '/');
if (isset(self::ANDERE[$ordner])) {
return self::ANDERE[$ordner];
}
}
return null;
}
public static function aktiv(): bool
{
return (bool) Einstellungen::wert('design') && self::anderesPlugin() === null;
}
public static function gestalten($atts)
{
if (!is_array($atts) || !self::aktiv() || !Einstellungen::versandAktiv()) {
return $atts;
}
$text = (string) ($atts['message'] ?? '');
if ($text === '' || str_contains($text, self::MARKE) || self::istHtml($atts)) {
return $atts;
}
if (Einstellungen::wert('design_umfang') !== 'alle' && Protokoll::quelle() !== 'wordpress') {
return $atts;
}
$atts['message'] = self::html($text, (string) ($atts['subject'] ?? ''));
$kopf = $atts['headers'] ?? [];
$zeilen = is_array($kopf) ? $kopf : array_filter(explode("\n", str_replace("\r\n", "\n", (string) $kopf)));
$zeilen = array_values(array_filter($zeilen, static fn($z) => !preg_match('/^\s*content-type\s*:/i', (string) $z)));
$zeilen[] = 'Content-Type: text/html; charset=UTF-8';
$atts['headers'] = $zeilen;
return $atts;
}
/** Schon HTML: Content-Type-Kopf, Filter wp_mail_content_type oder Inhalt mit HTML-Grundgeruest. */
private static function istHtml(array $atts): bool
{
$kopf = $atts['headers'] ?? [];
$kopf = is_array($kopf) ? implode("\n", $kopf) : (string) $kopf;
if (preg_match('/content-type\s*:\s*text\/html/i', $kopf) || preg_match('/content-type\s*:\s*multipart/i', $kopf)) {
return true;
}
// Filter von WordPress selbst (wp_mail), kein eigener Hook
if (apply_filters('wp_mail_content_type', 'text/plain') === 'text/html') { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound
return true;
}
return (bool) preg_match('/<(html|body|table|div|p|br)\b/i', (string) $atts['message']);
}
/** Text in die Vorlage setzen: maskiert, Links klickbar, Absaetze erhalten. */
public static function html(string $text, string $betreff): string
{
$farbe = sanitize_hex_color((string) Einstellungen::wert('design_farbe')) ?: '#0f766e';
$logo = self::logoUrl();
$inhalt = make_clickable(nl2br(esc_html(trim($text)), false));
$fuss = trim((string) Einstellungen::wert('design_fuss'));
$fuss = $fuss !== '' ? nl2br(esc_html($fuss), false) : esc_html((string) get_bloginfo('name')) . ' · ' . esc_html((string) wp_parse_url(home_url(), PHP_URL_HOST)) . '';
$kopf = $logo !== ''
? ''
: '' . esc_html((string) get_bloginfo('name')) . '';
return self::MARKE . '
'
. '
' . $fuss . ' ' . ' |