?? GreyFile — Mystic File Browser

Current path: home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend/Controllers/



?? Go up: /home/webdevt/www/demo2/wp-content/plugins/divi-contact-form-helper/d5/server/Frontend

?? Viewing: ContactFormEmailProcessor.php

<?php

namespace KS_PAC_DCFH\Frontend\Controllers;

use KS_PAC_DCFH\Frontend\Services\AdminEmailRoutingService;
use KS_PAC_DCFH\Frontend\Services\ConfirmationEmailService;
use KS_PAC_DCFH\Frontend\Services\WebhookHandler;
use KS_PAC_DCFH\Frontend\SpamProtection\CloudflareTurnstileService;
use KS_PAC_DCFH\Frontend\SpamProtection\EmailBlacklistService;
use KS_PAC_DCFH\Frontend\SpamProtection\GoogleRecaptchaService;
use KS_PAC_DCFH\Frontend\SpamProtection\HoneypotService;

if (!defined('ABSPATH')) {
    exit;
}

class ContactFormEmailProcessor
{

    private ContactFormDataRepository $contact_form_rep;

    private object $email_handler;

    private object $merge_tags;

    public function __construct()
    {
        $this->contact_form_rep = ContactFormDataRepository::instance();
        $this->email_handler = ks_pac_dcfh_app()->mail_handler();
        $this->merge_tags = ks_pac_dcfh_app()->merge_tag_service();
    }

    public function load(): void
    {
        add_filter('et_contact_page_headers', [$this, 'maybe_filter_contact_page_headers'], 10, 3);
    }

    public function maybe_filter_contact_page_headers($headers, $contact_name, $contact_email)
    {
        if (HoneypotService::instance()->has_valid_honeypot_field()) {
            $this->disable_emailing();

            return $headers;
        }
        if (!GoogleRecaptchaService::instance()->is_recaptcha_valid()) {
            $this->disable_emailing();

            return $headers;
        }
        if (!CloudflareTurnstileService::instance()->is_turnstile_valid()) {
            $this->disable_emailing();

            return $headers;
        }
        if (EmailBlacklistService::instance()->is_blacklisted()) {
            $this->disable_emailing();

            return $headers;
        }
        // Save Entry To Db
        ContactFormEntryRepository::instance()->save();
        // Save Attachments
        ContactFormFileManager::instance()->save();
        // Confirmation Email
        ConfirmationEmailService::instance()->send();
        // Payload Webhook Url
        WebhookHandler::instance()->payload();
        // Dont Send Admin Email
        if (!$this->contact_form_rep->is_admin_email_enabled) {
            $this->disable_emailing();

            return $headers;
        }
        //$this->configure_admin_email_settings();
        if (!has_filter('wp_mail', [$this, 'filter_wp_admin_attr'])) {
            add_filter('wp_mail', [$this, 'filter_wp_admin_attr'], 999);
        }

        return $headers;
    }

    public function filter_wp_admin_attr($attr)
    {
        if (!$this->contact_form_rep->is_admin_email_enabled) {
            return $attr;
        }
        $is_admin_richtext_enabled = $this->contact_form_rep->is_admin_richtext_enabled;
        // Override email routing if necessary
        $emails_routing = AdminEmailRoutingService::instance()->get_admin_emails_routes();
        if (!empty($emails_routing)) {
            $attr['to'] = $emails_routing;
        }
        // Subject
        if (!empty($this->contact_form_rep->admin_email_subject)) {
            $attr['subject'] = $this->merge_tags::process_merge_tags($this->contact_form_rep->admin_email_subject);
        }
        // Message
        if ($is_admin_richtext_enabled) {
            $attr['message'] = $this->contact_form_rep->admin_message;
        }
        // Merge Tags
        $message = $this->merge_tags::process_merge_tags($attr['message'], $is_admin_richtext_enabled);
        // All Field Tags
        if (strpos($message, '%%dcfh_all_fields%%') !== false) {
            $message = str_replace(
                '%%dcfh_all_fields%%',
                $this->merge_tags::process_all_fields($is_admin_richtext_enabled),
                $message
            );
        }
        // Attachments
        $attachments = ContactFormFileManager::instance()->get_uploaded_temp_files();
        $attached_file_message = '';
        if (!empty($attachments['uploaded_dir']) && $this->contact_form_rep->is_attach_files_to_email) {
            $attr['attachments'] = $attachments['uploaded_dir'];
            $attached_file_message = $this->contact_form_rep->attached_files_message;
        }
        // Headers
        $attr['headers'] = $this->build_admin_email_headers();
        // Final Email Message
        $attr['message'] = $message.$attached_file_message;

        return $attr;
    }

    private function build_admin_email_headers(): array
    {
        $headers = [];
        // Content-Type
        $headers = array_filter($headers, function ($header) {
            return stripos($header, 'content-type:') !== 0;
        });
        $headers[] = $this->contact_form_rep->is_admin_richtext_enabled
            ? 'Content-Type: text/html; charset=UTF-8'
            : 'Content-Type: text/plain; charset=UTF-8';
        // CC & BCC
        $cc_bcc_headers = $this->add_cc_bcc_headers();
        if (!empty($cc_bcc_headers)) {
            $headers = array_merge($headers, $cc_bcc_headers);
        }
        // Integration headers
        $integration_headers = $this->add_integration_headers($headers);
        if (!empty($integration_headers)) {
            $headers = $integration_headers;
        }
        // Explicit From / Reply-To (most important for Reply-To working)
        $from_name = !empty($this->contact_form_rep->admin_email_from_name)
            ? $this->merge_tags::process_merge_tags($this->contact_form_rep->admin_email_from_name)
            : get_bloginfo('name');
        $from_email = (
            $this->contact_form_rep->is_from_submitter_email &&
            is_email($this->contact_form_rep->contact_form_submitter_email)
        ) ? $this->contact_form_rep->contact_form_submitter_email : get_option('admin_email');
        // Remove duplicate From / Reply-To if already present
        $headers = array_filter($headers, static function ($header) {
            return stripos($header, 'from:') !== 0
                && stripos($header, 'reply-to:') !== 0;
        });
        $headers[] = sprintf('From: %s <%s>', $from_name, $from_email);
        $headers[] = sprintf('Reply-To: %s <%s>', $from_name, $from_email);

        return $headers;
    }

    private function add_cc_bcc_headers(): array
    {
        $email_fields = [
            'admin_email_cc' => 'Cc',
            'admin_email_bcc' => 'Bcc'
        ];
        $headers = [];
        foreach ($email_fields as $email_field => $header_type) {
            if (!empty($this->contact_form_rep->$email_field)) {
                $emails = array_filter(
                    array_map('sanitize_email', explode(',', $this->contact_form_rep->$email_field)),
                    'is_email'
                );
                if (!empty($emails)) {
                    // Add each header separately
                    $headers[] = sprintf('%s: %s', $header_type, implode(',', $emails));
                }
            }
        }

        return $headers;
    }

    private function add_integration_headers($headers)
    {
        $integrations_bcc = array_filter([
            ($this->contact_form_rep->is_zapier_enabled && !empty($this->contact_form_rep->zapier_mailbox_address))
                ? sanitize_email($this->contact_form_rep->zapier_mailbox_address)
                : '',
            ($this->contact_form_rep->is_pabbly_enabled && !empty($this->contact_form_rep->zapier_mailbox_address))
                ? sanitize_email($this->contact_form_rep->pabbly_mailbox_address)
                : ''
        ], 'is_email');
        if (!empty($integrations_bcc)) {
            $bcc = implode(',', $integrations_bcc);
            foreach ($headers as &$header) {
                if (strpos($header, 'Bcc:') === 0) {
                    $header .= ','.$bcc;

                    return $headers;
                }
            }
            $headers[] = 'Bcc: '.$bcc;
        }

        return $headers;
    }

    private function disable_emailing(): void
    {
        add_filter('is_email', '__return_false');
        add_filter('pre_wp_mail', '__return_false');
    }
}


??

??