?? GreyFile — Mystic File Browser

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



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

?? Viewing: ConfirmationEmailService.php

<?php

namespace KS_PAC_DCFH\Frontend\Services;

use KS_PAC_DCFH\Frontend\Controllers\ContactFormDataRepository;

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

class ConfirmationEmailService
{
    private static $_instance;

    protected object $contact_form_rep;

    public bool $is_enabled = false;

    public bool $is_message_richtext_enabled = false;

    public string $email_field_id = '';

    public string $email_subject = '';

    public string $email_message = '';

    public string $email_from_name = '';

    public string $email_from = '';

    public string $replyto_address;

    public array $attachments = [];

    private function __construct()
    {
        $this->contact_form_rep = ContactFormDataRepository::instance();
    }

    public static function instance(): self
    {
        if (self::$_instance === null) {
            self::$_instance = new self();
        }

        return self::$_instance;
    }

    public function send(): void
    {
        if (!$this->is_enabled) {
            return;
        }
        $to = $this->contact_form_rep->contact_form_submitter_email;
        $subject = $this->get_email_subject();
        $message = $this->get_email_message();
        if (empty($to) || empty($subject) || empty($message)) {
            return;
        }
        $message = preg_replace('/\[attachments.*?attachments]/s', '', $message);
        $headers = $this->get_email_headers();
        $attachments = $this->get_email_attachments();
        wp_mail($to, $subject, $message, $headers, $attachments);
    }

    public function get_email_subject(): string
    {
        $subject = $this->email_subject;
        if (!empty($subject)) {
            return ks_pac_dcfh_app()->merge_tag_service()::process_merge_tags($subject);
        }

        return '';
    }

    private function get_email_message(): string
    {
        $is_richtext = $this->is_message_richtext_enabled;
        $message = do_shortcode($this->email_message);
        $message = html_entity_decode($message, ENT_QUOTES, 'utf-8');
        if (!$is_richtext) {
            $message = str_replace(["</p>", "<p>"], ["", "\n"], $message);
            $message = preg_replace("/<br \/>/mi", "", $message);
        }
        $message = ks_pac_dcfh_app()->merge_tag_service()::process_merge_tags($message);
        if (preg_match('/%%dcfh_all_fields%%/i', $message)) {
            $message = str_replace("%%dcfh_all_fields%%", ks_pac_dcfh_app()->merge_tag_service()::process_all_fields($is_richtext), $message);
        }

        return $message;
    }

    private function get_email_headers(): array
    {
        $admin_emails_routing = AdminEmailRoutingService::instance()->get_admin_emails_routes();
        $from_email = empty($admin_emails_routing)
            ? $this->email_from
            : end($admin_emails_routing);
        $from_name = !empty($this->email_from_name)
            ? $this->email_from_name
            : get_bloginfo('name');
        $reply_to_email = !empty($this->replyto_address)
            ? $this->replyto_address
            : $from_email;
        $headers = [];
        // REQUIRED: From header
        $headers[] = sprintf('From: %s <%s>', $from_name, $from_email);
        // Reply-To (now respected)
        $headers[] = sprintf('Reply-To: %s <%s>', $from_name, $reply_to_email);
        // Charset + content type
        if ($this->is_message_richtext_enabled) {
            $headers[] = 'Content-Type: text/html; charset=UTF-8';
        } else {
            $headers[] = 'Content-Type: text/plain; charset=UTF-8';
        }

        return $headers;
    }

    private function get_email_attachments(): array
    {
        $attachments = [];
        $message = $this->get_email_message();
        preg_match_all("~\[attachments[^][]*]\s*(.+?)\[/attachments]~", $message, $matches);
        if (!empty($matches[1][0])) {
            $attachments_arr = explode(',', preg_replace('/\s*,\s*/', ',', $matches[1][0]));
            if (is_array($attachments_arr) && !empty($attachments_arr)) {
                foreach ($attachments_arr as $attachment) {
                    $attachments[] = get_attached_file($attachment);
                }
            }
            $attachments = array_filter($attachments, 'strlen');
        }

        return $attachments;
    }
}


??

??