Current path: home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Frontend/Request/
?? Go up: /home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Frontend
<?php
namespace KS_PAC_DCFH\Frontend\Request;
use KS_PAC_DCFH\Admin\Controllers\Email_Handler;
if (!defined('ABSPATH')) {
exit;
}
class Confirmation_Email_Request
{
private static $_instance;
protected $post_request;
protected $post_repository;
protected $email_handler;
public $is_confirmation_email_enabled = false;
public $is_confirmation_message_richtext = false;
public $confirmation_email_field_id;
public $confirmation_email_subject;
public $confirmation_email_from_name;
public $confirmation_email_from;
public $confirmation_replyto_address;
public $confirmation_email_message;
public $attachments = [];
private function __construct()
{
$this->post_request = new Post_Request(false);
$this->post_repository = Post_Repository::instance();
$this->email_handler = Email_Handler::instance();
if (ks_pac_dcfh_wp_helper()::is_divi_4131_or_above()) {
add_action('et_pb_contact_form_submit', [$this, 'maybe_divi_confirmation_email'], 10, 3);
} else {
add_action('phpmailer_init', [$this, 'register_phpmailer_action']);
add_action('ks_pac_dcfh_confirmation_email', [$this, 'maybe_phpmailer_confirmation_email'], 10, 8);
}
}
public static function instance(): self
{
if (self::$_instance === null) {
self::$_instance = new self();
}
return self::$_instance;
}
public function get_post_request(): Post_Request
{
return $this->post_request;
}
public function get_post_repository(): Post_Repository
{
return $this->post_repository;
}
public function maybe_divi_confirmation_email($processed_fields_values, $et_contact_error, $contact_form_info): void
{
// Check Error Not Empty Return
if (!empty($et_contact_error)) {
return;
}
if (!$this->is_confirmation_email_enabled) {
return;
}
$this->send_email();
}
public function register_phpmailer_action($phpmailer): void
{
$phpmailer->action_function = 'ks_pac_dcfh_confirmation_email';
}
public function maybe_phpmailer_confirmation_email($is_email_sent, $to, $cc, $bcc, $subject, $body, $from, $extra): void
{
if ($is_email_sent && did_action('phpmailer_init') === 1) {
$this->send_email();
}
}
private function send_email(): void
{
$to = $this->get_email();
$subject = $this->get_email_subject();
$message = $this->get_email_message();
if (empty($to) || empty($subject) || empty($message)) {
return;
}
//$message = preg_replace("/\[attachments.+attachments]/", '', $message);
$message = preg_replace('/\[attachments.*?attachments]/s', '', $message);
$headers = $this->get_email_headers();
$attachments = $this->get_email_attachments();
wp_mail($to, $subject, $message, $headers, $attachments);
}
private function get_email()
{
return $this->get_post_repository()->sender_email;
}
public function get_email_subject()
{
$subject = $this->confirmation_email_subject;
if (!empty($subject)) {
return ks_pac_dcfh_merge_tags()::get_merge_tags_values($subject);
}
return null;
}
private function get_email_message()
{
$is_richtext = $this->is_confirmation_message_richtext;
$message = do_shortcode($this->confirmation_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_merge_tags()::get_merge_tags_values($message);
if (preg_match('/%%dcfh_all_fields%%/i', $message)) {
$message = str_replace("%%dcfh_all_fields%%", ks_pac_dcfh_merge_tags()::get_all_fields_data($is_richtext), $message);
}
return $message;
}
private function get_email_headers(): array
{
$email_handler = $this->email_handler;
$admin_emails_routing = $this->email_handler->get_admin_emails_routing();
$from_email = empty($admin_emails_routing) ? $this->confirmation_email_from : end($admin_emails_routing);
$from_name = $this->confirmation_email_from_name;
$email_handler->set_mail_from($from_email);
$email_handler->set_mail_from_name($from_name);
if ($this->is_confirmation_message_richtext) {
$email_handler->set_mail_content_type_html();
}
$reply_to_email = !empty($this->confirmation_replyto_address) ? $this->confirmation_replyto_address : $from_email;
$headers = [];
if (!empty($from_name) && !empty($reply_to_email)) {
$headers[] = sprintf('Reply-To: %s <%s>', $from_name, $reply_to_email);
}
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;
}
}