Current path: home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Admin/Pages/
?? Go up: /home/webdevt/www/cryptoimpot.fr/wp-content/plugins/divi-contact-form-helper/app/Admin
<?php
namespace KS_PAC_DCFH\Admin\Pages;
use KS_PAC_DCFH\Admin\Controllers\Email_Handler;
if (!defined('ABSPATH')) {
exit;
}
class Page_Send_Email
{
private $_post_type;
private int $_post_id;
private $email_templates;
public function init(): void
{
$_page = ks_pac_dcfh_general_helper()::current_page();
$this->_post_id = isset($_GET['post_id']) ? sanitize_key($_GET['post_id']) : 0; // phpcs:ignore
$this->_post_type = 'pwh_dcfh';
add_action('admin_menu', [$this, 'menu']);
if ('send_email' === $_page && !empty($this->_post_id)) {
$this->email_templates = ks_pac_dcfh_template()::get_templates();
if (null === get_post($this->_post_id)) {
wp_safe_redirect(add_query_arg(['post_type' => $this->_post_type], admin_url('edit.php')));
exit();
}
add_action('view_send_email', [$this, 'display_form'], 10, 1);
add_action('admin_init', [$this, 'admin_hooks']);
add_filter('dcfh_f_admin_localizations', [$this, 'load_email_templates']);
}
}
public function admin_hooks(): void
{
ob_start();
add_action('wp_mail_failed', [$this, 'wp_mailed_failed_error']);
}
public function load_email_templates($localizations): array
{
$localizations['emailTemplates'] = wp_json_encode($this->email_templates);
return $localizations;
}
public function menu(): void
{
add_submenu_page('edit.php?post_type=pwh_dcfh', __('Send Email', 'divi-contact-form-helper'), null, 'perform_entry_actions_pwh_dcfh', 'send_email', [$this, 'process_form']);
}
public function process_form(): void
{
$post_request = [
'email_from' => get_bloginfo('admin_email'),
'email_from_name' => htmlspecialchars_decode(get_bloginfo('name')),
'email_to' => '',
'email_subject' => '',
'email_body' => '',
];
if (isset($_POST['btn_send_email'], $_POST['_wpnonce'])) {
if (!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'action_send_email')) {
wp_die(esc_html__('Unable to submit this form, please refresh and try again.', 'divi-contact-form-helper'));
}
$post_request = $_POST;
$email_from = isset($_POST['email_from']) ? sanitize_text_field($_POST['email_from']) : null;
$email_from_name = isset($_POST['email_from_name']) ? sanitize_text_field($_POST['email_from_name']) : null;
$email_to = isset($_POST['email_to']) ? sanitize_text_field($_POST['email_to']) : null;
$email_subject = isset($_POST['email_subject']) ? sanitize_text_field(htmlspecialchars_decode(stripslashes(sanitize_text_field($_POST['email_subject'])), ENT_QUOTES)) : null; // phpcs:ignore
$email_body = isset($_POST['email_body']) ? wpautop(htmlspecialchars_decode(stripslashes($_POST['email_body']), ENT_QUOTES)) : null; // phpcs:ignore
$validate_request = true;
if (empty($email_from)) {
ks_pac_dcfh_alert()::add_error_message(__('Email from is required.', 'divi-contact-form-helper'));
$validate_request = false;
}
if (empty($email_from_name)) {
ks_pac_dcfh_alert()::add_error_message(__('Email from name is required.', 'divi-contact-form-helper'));
$validate_request = false;
}
if (empty($email_subject)) {
ks_pac_dcfh_alert()::add_error_message(__('Email subject is required.', 'divi-contact-form-helper'));
$validate_request = false;
}
if (empty($email_body)) {
ks_pac_dcfh_alert()::add_error_message(__('Email body is required.', 'divi-contact-form-helper'));
$validate_request = false;
}
if ($validate_request) {
$post_obj = get_post($this->_post_id);
if (is_serialized($post_obj->post_excerpt)) {
$form_entries = ks_pac_dcfh_general_helper()::maybe_unserialize($post_obj->post_excerpt);
$email_keywords = ks_pac_dcfh_merge_tags()::get_send_email_static_keywords_values($post_obj);
if (!empty($form_entries) && is_array($form_entries)) {
foreach ($form_entries as $form_entry) {
$field_id = $form_entry['id'];
$email_keywords['%%'.$field_id.'%%'] = $form_entry['value'];
}
}
// Email Body
if (!empty($email_keywords)) {
$email_body = str_replace(array_keys($email_keywords), array_values($email_keywords), $email_body);
}
$email_handler = Email_Handler::instance();
$email_handler->set_mail_content_type_html();
$email_handler->set_mail_from($email_from);
$email_handler->set_mail_from_name($email_from_name);
// Send Email
if (wp_mail($email_to, $email_subject, $email_body)) {
// Update Replies
$last_log_count = ks_pac_dcfh_logger()->get_log_count('sent_email_log', $this->_post_id);
ks_pac_dcfh_logger()->add_log('email-sent-log-'.$last_log_count, maybe_serialize([
'email_from' => $email_from,
'email_from_name' => $email_from_name,
'email_to' => $email_to,
'email_body' => $email_body,
]), 'sent_email_log', $this->_post_id);
// Redirect
wp_safe_redirect(add_query_arg([
'post' => $this->_post_id,
'action' => 'edit',
'success' => true,
'_wpnonce' => wp_create_nonce('entry_action'),
'email_sent_to' => $email_to,
], admin_url('post.php')));
} else {
wp_safe_redirect(add_query_arg([
'post' => $this->_post_id,
'action' => 'edit',
'success' => false,
'_wpnonce' => wp_create_nonce('entry_action'),
'email_sent_to' => $email_to,
], admin_url('post.php')));
}
exit();
}
}
}
do_action('view_send_email', $post_request);
}
public function display_form($post_request): void
{ ?>
<div class="wrap pwh-dcfh-page" id="page-send-email">
<h1 class="wp-heading-inline"><?php esc_html_e('Send Email', 'divi-contact-form-helper'); ?></h1>
<a href="<?php echo esc_url(add_query_arg(['post_type' => $this->_post_type], admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Entries',
'divi-contact-form-helper'); ?></a>
<a href="<?php echo esc_url(add_query_arg(['post_type' => $this->_post_type, 'page' => 'create_email_template'],
admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Add New Template', 'divi-contact-form-helper'); ?></a>
<hr class="wp-header-end">
<?php ks_pac_dcfh_alert()::show_admin_notice(); ?>
<span class="spinner"></span>
<div id="poststuff">
<div id="post-body" class="metabox-holder columns-2">
<div class="postbox-container">
<div class="postbox">
<div class="inside">
<form method="POST" autocomplete="off" enctype="application/x-www-form-urlencoded">
<table class="form-table">
<tbody>
<?php if (!empty($this->email_templates)) { ?>
<tr>
<th><?php esc_html_e('Choose Template:', 'divi-contact-form-helper'); ?></th>
<td>
<select title="" name="email_template" id="email_template" class="form-control jsEmailTemplate">
<option value=""><?php esc_html_e('Select Template', 'divi-contact-form-helper'); ?></option>
<?php foreach ($this->email_templates as $key => $value) { ?>
<option value="<?php esc_html_e($key, 'divi-contact-form-helper'); ?>"><?php esc_html_e($value['tpl_name'], 'divi-contact-form-helper'); ?></option>
<?php } ?>
</select>
</td>
</tr>
<?php } ?>
<tr>
<th><?php esc_html_e('Email From:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td>
<input title="" type="email" name="email_from" id="email_from" class="form-control" value="<?php esc_html_e($post_request['email_from'], 'divi-contact-form-helper'); ?>" placeholder="<?php
esc_html_e('Email From', 'divi-contact-form-helper'); ?>">
</td>
</tr>
<tr>
<th><?php esc_html_e('Email From Name:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td>
<input title="" type="text" name="email_from_name" id="email_from_name" class="form-control" value="<?php esc_html_e($post_request['email_from_name'], 'divi-contact-form-helper'); ?>" placeholder="<?php
esc_html_e('Email From Name', 'divi-contact-form-helper'); ?>">
</td>
</tr>
<tr>
<th><?php esc_html_e('Email Subject:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td>
<input title="" type="text" name="email_subject" id="email_subject" class="form-control" value="<?php esc_html_e($post_request['email_subject'], 'divi-contact-form-helper');
?>" placeholder="<?php
esc_html_e('Subject', 'divi-contact-form-helper'); ?>">
</td>
</tr>
<tr>
<th><?php esc_html_e('Reply To:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td colspan="4">
<select title="" name="email_to" id="email_to" class="form-control">
<?php foreach (ks_pac_dcfh_user()::get_user_emails($this->_post_id) as $key => $email) { ?>
<option value="<?php esc_html_e($key, 'divi-contact-form-helper'); ?>" <?php selected($key, $post_request['email_to']); ?>><?php esc_html_e($email, 'divi-contact-form-helper'); ?></option>
<?php } ?>
</select>
</td>
</tr>
<tr>
<th><?php esc_html_e('Email Data Tags:', 'divi-contact-form-helper'); ?></th>
<td>
<div id="merge-data-tags-list">
<?php ks_pac_dcfh_post_meta()::get_contact_form_send_email_data($this->_post_id); ?>
</div>
<?php
$create_template_url = esc_url(add_query_arg(['post_id' => $this->_post_id, 'page' => 'create_email_template'], admin_url('edit.php?post_type=pwh_dcfh')));
echo sprintf('<p class="helper">%1$s <a href="%2$s">%3$s</a></p>', esc_html(' Or use these keywords to create email template.'),
esc_url($create_template_url), esc_html('Click Here'))
?>
</td>
</tr>
<tr>
<th><?php esc_html_e('Email Message:', 'divi-contact-form-helper'); ?>
<span class="required">*</span></th>
<td>
<?php wp_editor($post_request['email_body'], 'email_body', [
'wpautop' => true,
'media_buttons' => false,
'textarea_name' => 'email_body',
'textarea_rows' => 6
]);
?>
</td>
</tr>
</tbody>
</table>
<?php
wp_nonce_field('action_send_email', '_wpnonce');
submit_button(__('Send Email', 'divi-contact-form-helper'), 'primary large', 'btn_send_email');
?>
</form>
</div>
</div>
</div>
<div id="postbox-container-1" class="postbox-container">
<?php
require_once dirname(__DIR__)."/template/documentation-widget.php";
require_once dirname(__DIR__)."/template/support-widget.php";
?>
</div>
</div>
<br class="clear">
</div>
</div>
<?php
}
public function wp_mailed_failed_error($wp_error): void
{
$wp_mail_failed = $wp_error->errors['wp_mail_failed'] ?? null;
if (!empty($wp_mail_failed)) {
$error_messages = implode(', ', $wp_mail_failed);
delete_transient('dcfh_failed_email_data');
set_transient('dcfh_failed_email_data', $error_messages, MINUTE_IN_SECONDS);
}
}
}