Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d4/app/Admin/Controllers/
?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d4/app/Admin
<?php
namespace KS_PAC_DCFH\Admin\Controllers;
use KS_PAC_DCFH\Frontend\Request\Post_Repository;
if (!defined('ABSPATH')) {
exit;
}
class Email_Handler
{
private static $_instance;
public $admin_email_routing = [];
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public function set_mail_content_type_html(string $content_type = 'text/html'): void
{
add_filter('wp_mail_content_type', static function () use ($content_type) {
return $content_type;
});
}
public function set_mail_from($custom_mail_from): void
{
if (!empty($custom_mail_from)) {
add_filter('wp_mail_from', static function () use ($custom_mail_from) {
return $custom_mail_from;
});
}
}
public function set_mail_from_name($custom_mail_from_name): void
{
if (!empty($custom_mail_from_name)) {
add_filter('wp_mail_from_name', static function () use ($custom_mail_from_name) {
return $custom_mail_from_name;
}, 999);
}
}
public function is_blacklisted_email(): bool
{
$is_blacklisted_email = false;
$post_repository = Post_Repository::instance();
$sender_email = $post_repository->sender_email;
if (!empty($sender_email)) {
$sender_email = strtolower($sender_email);
// Blocklisted Email Addresses
$blacklisted_emails = array_map('trim', explode(',', et_get_option('pwh_dcfh_blacklisted_emails')));
if (!empty($blacklisted_emails) && in_array($sender_email, $blacklisted_emails, true)) {
$is_blacklisted_email = true;
}
// Blocklisted Email Address Domains
$blacklisted_email_domains = array_map('trim', explode(',', et_get_option('pwh_dcfh_blacklisted_email_domains')));
if (!empty($blacklisted_email_domains)) {
$email_domain = substr(strrchr($sender_email, '@'), 1);
if (in_array($email_domain, $blacklisted_email_domains, true)) {
$is_blacklisted_email = true;
}
}
// Blocklisted Keywords
$blacklisted_keywords = array_filter(array_map('trim', explode(',', strtolower(et_get_option('pwh_dcfh_blacklisted_keywords')))));
if (!empty($blacklisted_keywords)) {
$contact_form_data = $post_repository->contact_form_data;
foreach ($contact_form_data as $field) {
$field_value = strtolower($field['value']);
foreach ($blacklisted_keywords as $keyword) {
if (preg_match('/\b'.preg_quote($keyword, '/').'\b/', $field_value)) {
$is_blacklisted_email = true;
break 2;
}
}
}
}
}
return $is_blacklisted_email;
}
public function set_admin_emails_routing($email_pattern): void
{
if (!empty($email_pattern)) {
$routes = explode(',', wp_strip_all_tags(htmlspecialchars_decode($email_pattern)));
if (is_array($routes)) {
$routes = array_filter(array_map('trim', $routes));
foreach ($routes as $route) {
$outer_arr = explode('::', $route);
if (isset($outer_arr[0], $outer_arr[1])) {
$field_id = ks_pac_dcfh_general_helper()::sanitize_key($outer_arr[0]);
$inner_arr = explode('|', $outer_arr[1]);
foreach ($inner_arr as $inner_val) {
$data_arr = explode(':', $inner_val);
if (!empty($data_arr[0]) && !empty($data_arr[1])) {
$index = ks_pac_dcfh_general_helper()::sanitize_key($data_arr[0]);
$email = sanitize_email($data_arr[1]);
$this->admin_email_routing[$field_id][$index] = $email;
}
}
}
}
}
}
}
public function get_admin_emails_routing(): array
{
$admin_emails = [];
$emails_routing = $this->admin_email_routing;
if (!empty($emails_routing)) {
$post_repository = Post_Repository::instance();
$processed_fields_values = $post_repository->contact_form_data;
foreach ($processed_fields_values as $value) {
if (isset($value['id'], $value['value'])) {
$field_id = $value['id'];
$field_data = explode(',', $value['value']);
if (!empty($field_data)) {
foreach ($field_data as $field_value) {
$field_value = ks_pac_dcfh_general_helper()::sanitize_key($field_value);
if (isset($emails_routing[$field_id][$field_value])) {
$admin_emails[] = $emails_routing[$field_id][$field_value];
}
}
}
}
}
}
return $admin_emails;
}
public function add_admin_headers($headers)
{
$post_repository = Post_Repository::instance();
$header_map = [
'Cc' => $post_repository->admin_email_cc,
'Bcc' => $post_repository->admin_email_bcc,
];
foreach ($header_map as $type => $emails) {
if (!empty($emails)) {
$emails = preg_replace('/\s*,\s*/', ',', $emails);
$emails = array_filter(explode(',', $emails));
foreach ($emails as $email) {
$headers[] = sprintf('%s: %s', $type, sanitize_email($email));
}
}
}
return $headers;
}
}