<?php

$theme_smtp_user = rwmb_meta_before_init_theme_setting("theme_smtp_user") ?: "";
$theme_smtp_pass = rwmb_meta_before_init_theme_setting("theme_smtp_pass") ?: "";
$theme_smtp_host = rwmb_meta_before_init_theme_setting("theme_smtp_host") ?: "";
$theme_smtp_port = rwmb_meta_before_init_theme_setting("theme_smtp_port") ?: "";
$theme_smtp_secure =
  rwmb_meta_before_init_theme_setting("theme_smtp_secure") ?: "ssl";
$theme_smtp_auth = rwmb_meta_before_init_theme_setting("theme_smtp_auth") ?: 1;
$theme_smtp_debug =
  rwmb_meta_before_init_theme_setting("theme_smtp_debug") ?: 1;

if (!defined("SMTP_USER")) {
  define("SMTP_USER", $theme_smtp_user);
}
if (!defined("SMTP_FROM")) {
  define("SMTP_FROM", $theme_smtp_user);
}
if (!defined("SMTP_NAME")) {
  define("SMTP_NAME", $theme_smtp_user);
}
if (!defined("SMTP_PASS")) {
  define("SMTP_PASS", $theme_smtp_pass);
}
if (!defined("SMTP_HOST")) {
  define("SMTP_HOST", $theme_smtp_host);
}
if (!defined("SMTP_PORT")) {
  define("SMTP_PORT", $theme_smtp_port);
}
if (!defined("SMTP_SECURE")) {
  define("SMTP_SECURE", $theme_smtp_secure);
}
if (!defined("SMTP_AUTH")) {
  define("SMTP_AUTH", $theme_smtp_auth);
}
if (!defined("SMTP_DEBUG")) {
  define("SMTP_DEBUG", $theme_smtp_debug);
}

// Change the From address.
add_filter("wp_mail_from", function ($old) use ($theme_smtp_user) {
  return $theme_smtp_user ?: $old;
});

// Change the From name.
add_filter("wp_mail_from_name", function ($old) use ($theme_smtp_user) {
  return $theme_smtp_user ?: $old;
});
