?? GreyFile — Mystic File Browser

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

?? Viewing: Page_Create_Email_Template.php

<?php

namespace KS_PAC_DCFH\Admin\Pages;

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

class Page_Create_Email_Template
{
    private $_post_type;

    private $contact_forms_data;

    public function init(): void
    {
        $this->_post_type = 'pwh_dcfh';
        $_page = ks_pac_dcfh_general_helper()::current_page();
        add_action('admin_menu', [$this, 'menu']);
        if ('create_email_template' === $_page) {
            $this->contact_forms_data = ks_pac_dcfh_db()::get_contact_form_template_data();
            add_action('admin_init', static function () {
                ob_start();
            });
            add_action('view_create_email_template', [$this, 'display_form']);
            add_filter('dcfh_f_admin_localizations', [$this, 'load_tpl_data']);
        }
    }

    public function menu(): void
    {
        add_submenu_page('edit.php?post_type=pwh_dcfh', __('Create Template', 'divi-contact-form-helper'), null, 'perform_entry_actions_pwh_dcfh', 'create_email_template', [$this, 'process_form']);
    }

    public function load_tpl_data(): array
    {
        $localizations['tplData'] = wp_json_encode($this->contact_forms_data);

        return $localizations;
    }

    public function process_form(): void
    {
        $_post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : null;
        $contact_form_id = ks_pac_dcfh_post_meta()::get_contact_form_id_meta_value($_post_id);
        $post_request = [
            'action' => 'create',
            'tpl_id' => '',
            'contact_form_id' => $contact_form_id,
            'tpl_type' => '',
            'tpl_name' => '',
            'email_from' => get_bloginfo('admin_email'),
            'email_from_name' => htmlspecialchars_decode(get_bloginfo('name')),
            'email_subject' => '',
            'email_body' => '',
        ];
        // Create
        if (isset($_POST['btn_create_template'], $_POST['_wpnonce'])) {
            $action = isset($_POST['action']) ? sanitize_text_field($_POST['action']) : 'create';
            $old_tpl_id = isset($_POST['old_tpl_id']) ? sanitize_text_field($_POST['old_tpl_id']) : null;
            $contact_form_id = isset($_POST['contact_form_id']) ? sanitize_text_field($_POST['contact_form_id']) : null;
            $tpl_type = isset($_POST['tpl_type']) ? sanitize_text_field($_POST['tpl_type']) : null;
            $tpl_name = isset($_POST['tpl_name']) ? sanitize_text_field($_POST['tpl_name']) : null;
            $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_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
            $post_request = $_POST;                                                                                                                                                        // phpcs:ignore
            $validate_request = true;
            if (!wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), $action.'_email_template')) {
                wp_die(esc_html__('Unable to submit this form, please refresh and try again.', 'divi-contact-form-helper'));
            }
            if (empty($tpl_name)) {
                ks_pac_dcfh_alert()::add_error_message(__('Template name is required.', 'divi-contact-form-helper'));
                $validate_request = false;
            }
            if (!in_array($tpl_type, ['admin', 'confirmation'], 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;
            }
            $tpl_name = ks_pac_dcfh_general_helper()::sanitize_string($tpl_name);
            if ($action === 'create') {
                $tpl_id = ks_pac_dcfh_template()::set_template_id($tpl_name);
                if (!empty($tpl_name) && ks_pac_dcfh_template()::is_template_exist($tpl_id)) {
                    ks_pac_dcfh_alert()::add_error_message(__('The template with same name already exist. Please choose another name.', 'divi-contact-form-helper'));
                    $validate_request = false;
                }
            } else {
                $tpl_id = $old_tpl_id;
            }
            if ($validate_request) {
                $template = [];
                if ($action === 'create') {
                    $template = [
                        'contact_form_id' => $contact_form_id,
                        'tpl_type' => $tpl_type,
                        'tpl_name' => $tpl_name,
                        'email_from' => $email_from,
                        'email_from_name' => $email_from_name,
                        'email_subject' => $email_subject,
                        'email_body' => $email_body,
                    ];
                    $template['created_by'] = get_current_user_id();
                    $template['modified_by'] = '';
                    $template['created_at'] = current_time('mysql');
                    $template['modified_at'] = current_time('mysql');
                } elseif ($action === 'edit') {
                    $template = get_option($tpl_id);
                    $template['contact_form_id'] = $contact_form_id;
                    $template['tpl_type'] = $tpl_type;
                    $template['tpl_name'] = $tpl_name;
                    $template['email_from'] = $email_from;
                    $template['email_from_name'] = $email_from_name;
                    $template['email_subject'] = $email_subject;
                    $template['email_body'] = $email_body;
                    $template['modified_by'] = get_current_user_id();
                    $template['modified_at'] = current_time('mysql');
                }
                $response = update_option($tpl_id, $template, 'no');
                if ($response) {
                    delete_transient('dcfh_tpl_list_transient');
                    delete_transient('dcfh_tpl_shortcode_transient');
                    wp_safe_redirect(add_query_arg([
                        'post_type' => $this->_post_type,
                        'page' => 'email_templates_list',
                        'success' => true,
                        'action' => $action,
                    ], admin_url('edit.php')));
                } else {
                    wp_safe_redirect(add_query_arg([
                        'post_type' => $this->_post_type,
                        'page' => 'email_templates_list',
                        'success' => false,
                        'action' => '',
                    ], admin_url('edit.php')));
                }
                exit();
            }
        }
        // Edit
        if (isset($_GET['_wpnonce'], $_GET['action'], $_GET['tpl_id']) && 'edit' === $_GET['action']) {
            if (!wp_verify_nonce(sanitize_text_field($_GET['_wpnonce']), 'edit_email_template')) {
                wp_safe_redirect(add_query_arg([
                    'post_type' => $this->_post_type,
                    'page' => 'email_templates_list',
                ], admin_url('edit.php')));
                exit();
            }
            $tpl_id = sanitize_text_field($_GET['tpl_id']);
            $options = get_option($tpl_id);
            if (empty($options)) {
                wp_safe_redirect(add_query_arg([
                    'post_type' => $this->_post_type,
                    'page' => 'email_templates_list',
                ], admin_url('edit.php')));
                exit();
            }
            $post_request['action'] = sanitize_text_field($_GET['action']);
            $post_request['tpl_id'] = $tpl_id;
            $post_request['contact_form_id'] = $options['contact_form_id'];
            $post_request['tpl_type'] = $options['tpl_type'] ?? '';
            $post_request['tpl_name'] = $options['tpl_name'];
            $post_request['email_from'] = $options['email_from'] ?? '';
            $post_request['email_from_name'] = $options['email_from_name'] ?? '';
            $post_request['email_subject'] = $options['email_subject'] ?? '';
            $post_request['email_body'] = $options['email_body'];
        }
        do_action('view_create_email_template', $post_request);
    }

    public function display_form($post_request): void
    {
        $contact_forms_data = $this->contact_forms_data;
        ?>
        <div class="wrap pwh-dcfh-page" id="page-create-email-template">
            <h1 class="wp-heading-inline"><?php esc_html_e('Create Template', 'divi-contact-form-helper'); ?></h1>
            <a href="<?php echo esc_url(add_query_arg(['post_type' => $this->_post_type, 'page' => 'email_templates_list'],
                admin_url('edit.php'))); ?>" class="page-title-action"><?php esc_html_e('Templates', '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>
                                        <tr class="tpl_type">
                                            <th><?php esc_html_e('Template Type:', 'divi-contact-form-helper'); ?></th>
                                            <td>
                                                <select title="" name="tpl_type" id="tpl_type" class="form-control">
                                                    <?php foreach (ks_pac_dcfh_constant()::get_templates_types() as $key => $type) { ?>
                                                        <option value="<?php echo esc_html($key); ?>" <?php selected($key,
                                                            $post_request['tpl_type']); ?>><?php echo esc_html($type); ?></option>
                                                    <?php } ?>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr class="tpl_name">
                                            <th><?php esc_html_e('Template Name:', 'divi-contact-form-helper'); ?>
                                                <span class="required">*</span></th>
                                            <td>
                                                <input title="" type="text" name="tpl_name" id="tpl_name" class="form-control" value="<?php echo esc_html($post_request['tpl_name']); ?>" placeholder="<?php
                                                esc_html_e('Template Name', 'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="contact_form_id">
                                            <th><?php esc_html_e('Contact Form:', 'divi-contact-form-helper'); ?></th>
                                            <td>
                                                <select title="" name="contact_form_id" id="contact_form_id" class="form-control">
                                                    <option value=""><?php esc_html_e('Please Select Contact Form', 'divi-contact-form-helper'); ?></option>
                                                    <?php if (!empty($contact_forms_data)) {
                                                        foreach ($contact_forms_data as $key => $value) { ?>
                                                            <option value="<?php echo esc_html($key); ?>" <?php selected($key,
                                                                $post_request['contact_form_id']); ?>><?php echo esc_html($value['contact_form_title']); ?></option>
                                                        <?php }
                                                    } ?>
                                                </select>
                                                <div id="merge-data-tags-list"></div>
                                            </td>
                                        </tr>
                                        <tr class="email_from">
                                            <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 echo esc_html($post_request['email_from']); ?>" placeholder="<?php esc_html_e('Email From',
                                                    'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="email_from_name">
                                            <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 echo esc_html($post_request['email_from_name']); ?>" placeholder="<?php esc_html_e('Email From Name',
                                                    'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="email_subject">
                                            <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 echo esc_html($post_request['email_subject']); ?>" placeholder="<?php esc_html_e('Subject',
                                                    'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="email_body">
                                            <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' => true,
                                                    'textarea_name' => 'email_body',
                                                    'textarea_rows' => 10,
                                                ]);
                                                ?>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                    <input type="hidden" name="action" value="<?php echo esc_attr($post_request['action']); ?>">
                                    <input type="hidden" name="old_tpl_id" value="<?php echo isset($post_request['tpl_id']) ? esc_attr($post_request['tpl_id']) : ''; ?>">
                                    <?php
                                    $button_text = 'create' === $post_request['action'] ? __('Create Template', 'divi-contact-form-helper') : __('Update Template', 'divi-contact-form-helper');
                                    wp_nonce_field(esc_html($post_request['action']).'_email_template');
                                    submit_button($button_text, 'primary large action-btn', 'btn_create_template');
                                    ?>
                                </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
    }

}


??

??