fix(email): lock templates to built-in catalog
This commit is contained in:
+27
-66
@@ -28,66 +28,17 @@ final class Mail {
|
||||
];
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
public static function get_email_event_config(string $event = ''): array {
|
||||
/** Send a fixed catalog event. */
|
||||
public static function send_event_email(string $event, string $to, array $placeholders = []): bool {
|
||||
$definitions = MailTemplates::definitions();
|
||||
$defaults = MailTemplates::defaults();
|
||||
$settings = Settings::get_all();
|
||||
$global = [
|
||||
'description' => (string) ($defaults['mail_description'] ?? ''),
|
||||
'more_content' => (string) ($defaults['mail_more_content'] ?? ''),
|
||||
];
|
||||
foreach (['mail_description', 'mail_more_content'] as $key) {
|
||||
if (array_key_exists($key, $settings) && trim((string) $settings[$key]) !== '') {
|
||||
$global[str_replace('mail_', '', $key)] = $key === 'mail_more_content'
|
||||
? wp_kses_post((string) $settings[$key])
|
||||
: sanitize_textarea_field((string) $settings[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
if ($event === '') {
|
||||
$events = [];
|
||||
foreach (array_keys($definitions) as $key) {
|
||||
$events[$key] = self::event_config($key, $settings, $defaults);
|
||||
}
|
||||
return ['global' => $global, 'events' => $events];
|
||||
}
|
||||
if (!isset($definitions[$event])) {
|
||||
return ['enabled' => false, 'subject' => '', 'content' => '', 'placeholders' => [], 'global' => $global];
|
||||
}
|
||||
return array_merge(self::event_config($event, $settings, $defaults), ['global' => $global]);
|
||||
}
|
||||
|
||||
public static function is_email_event_enabled(string $event): bool {
|
||||
$definitions = MailTemplates::definitions();
|
||||
if (!isset($definitions[$event])) {
|
||||
return false;
|
||||
}
|
||||
$settings = Settings::get_all();
|
||||
return !array_key_exists("email_{$event}_enabled", $settings) || !empty($settings["email_{$event}_enabled"]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a configured event. The optional defaults keep existing callers source-compatible.
|
||||
*/
|
||||
public static function send_event_email(
|
||||
string $event,
|
||||
string $to,
|
||||
string $default_subject = '',
|
||||
string $default_content = '',
|
||||
array $placeholders = []
|
||||
): bool {
|
||||
$to = sanitize_email($to);
|
||||
if ($to === '' || !is_email($to) || !isset(MailTemplates::definitions()[$event]) || !self::is_email_event_enabled($event)) {
|
||||
if ($to === '' || !is_email($to) || !isset($definitions[$event]) || !self::is_email_event_enabled($event)) {
|
||||
return false;
|
||||
}
|
||||
$cfg = self::get_email_event_config($event);
|
||||
$definition = MailTemplates::definitions()[$event];
|
||||
$subject = (string) ($cfg['subject'] ?? $default_subject ?: $definition['title']);
|
||||
$content = (string) ($cfg['content'] ?? $default_content ?: $definition['content']);
|
||||
$subject = self::replace_placeholders($subject, $placeholders);
|
||||
$content = self::replace_placeholders($content, $placeholders);
|
||||
$content = self::wrap_html($content, $cfg['global'] ?? []);
|
||||
$definition = $definitions[$event];
|
||||
$subject = self::replace_placeholders((string) $definition['title'], $placeholders);
|
||||
$content = self::replace_placeholders((string) $definition['content'], $placeholders);
|
||||
$content = self::wrap_html($content, self::get_global_config());
|
||||
$headers = ['Content-Type: text/html; charset=UTF-8'];
|
||||
return (bool) wp_mail($to, $subject, $content, $headers);
|
||||
}
|
||||
@@ -129,17 +80,27 @@ final class Mail {
|
||||
return $error;
|
||||
}
|
||||
|
||||
/** @return array<string, mixed> */
|
||||
private static function event_config(string $event, array $settings, array $defaults): array {
|
||||
$definition = MailTemplates::definitions()[$event];
|
||||
$subject_key = "email_{$event}_subject";
|
||||
$content_key = "email_{$event}_content";
|
||||
return [
|
||||
'enabled' => !array_key_exists("email_{$event}_enabled", $settings) || !empty($settings["email_{$event}_enabled"]),
|
||||
'subject' => trim((string) ($settings[$subject_key] ?? '')) ?: (string) ($defaults[$subject_key] ?? $definition['title']),
|
||||
'content' => (string) ($settings[$content_key] ?? '') !== '' ? (string) $settings[$content_key] : (string) ($defaults[$content_key] ?? $definition['content']),
|
||||
'placeholders' => $definition['placeholders'],
|
||||
private static function is_email_event_enabled(string $event): bool {
|
||||
$settings = Settings::get_all();
|
||||
return !array_key_exists("email_{$event}_enabled", $settings) || !empty($settings["email_{$event}_enabled"]);
|
||||
}
|
||||
|
||||
/** @return array<string, string> */
|
||||
private static function get_global_config(): array {
|
||||
$defaults = MailTemplates::defaults();
|
||||
$settings = Settings::get_all();
|
||||
$global = [
|
||||
'description' => (string) ($defaults['mail_description'] ?? ''),
|
||||
'more_content' => (string) ($defaults['mail_more_content'] ?? ''),
|
||||
];
|
||||
foreach (['mail_description', 'mail_more_content'] as $key) {
|
||||
if (array_key_exists($key, $settings) && trim((string) $settings[$key]) !== '') {
|
||||
$global[str_replace('mail_', '', $key)] = $key === 'mail_more_content'
|
||||
? wp_kses_post((string) $settings[$key])
|
||||
: sanitize_textarea_field((string) $settings[$key]);
|
||||
}
|
||||
}
|
||||
return $global;
|
||||
}
|
||||
|
||||
private static function replace_placeholders(string $value, array $placeholders): string {
|
||||
|
||||
Reference in New Issue
Block a user