?? GreyFile — Mystic File Browser

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



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

?? Viewing: CreateEmailTemplates.php

<?php

namespace KS_PAC_DCFH\Admin\Pages;

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

class CreateEmailTemplates
{
    private array $contact_form_data;

    public function load(): void
    {
        $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
        add_action('admin_menu', [$this, 'maybe_add_menu_item']);
        if ('create_email_template' === $page) {
            add_filter('dcfh_admin_localization_data', [$this, 'maybe_filter_localization']);
        }
    }

    public function maybe_add_menu_item(): 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 maybe_filter_localization($localizations): array
    {
        $this->contact_form_data = ks_pac_dcfh_app()->general_db_manager()::get_email_template_data();
        $localizations['contactFormTemplateData'] = htmlentities(wp_json_encode($this->contact_form_data));

        return $localizations;
    }

    public function process_form(): void
    {
        $post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : null;
        $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : 'create';
        $contact_form_id = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_id_meta_value($post_id);
        //$wp_error = new WP_Error();
        $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' => '',
        ];
        if (isset($_REQUEST['_wpnonce']) && !wp_verify_nonce(sanitize_text_field($_REQUEST['_wpnonce']), "nonce_add_template")) {
            wp_die(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
        }
        // Create
        if (isset($_POST['btn_create_template'])) {
            $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;
            //$wp_error->add('tpl_name_error', __('Template name is required.', 'divi-contact-form-helper'));
            if (empty($tpl_name)) {
                 ks_pac_dcfh_app()->admin_alert()::error(__('Template name is required.', 'divi-contact-form-helper'));
                $validate_request = false;
            }
            if (!in_array($tpl_type, ['admin', 'confirmation'], true)) {
                $required_fields = [
                    'email_from' => __('From email is required.', 'divi-contact-form-helper'),
                    'email_from_name' => __('From name is required.', 'divi-contact-form-helper'),
                    'email_subject' => __('Subject is required.', 'divi-contact-form-helper'),
                ];
                foreach ($required_fields as $field => $error_message) {
                    if (empty($$field)) {
                         ks_pac_dcfh_app()->admin_alert()::error($error_message);
                        $validate_request = false;
                    }
                }
            }
            if (empty($email_body)) {
                 ks_pac_dcfh_app()->admin_alert()::error(__('Message body is required.', 'divi-contact-form-helper'));
                $validate_request = false;
            }
            $tpl_name = ks_pac_dcfh_app()->misc_utils()::sanitize_string($tpl_name);
            if ($action === 'create') {
                $tpl_id = ks_pac_dcfh_app()->email_template_manager()::generate_template_id($tpl_name);
                if (!empty($tpl_name) && ks_pac_dcfh_app()->email_template_manager()::is_template_exist($tpl_id)) {
                     ks_pac_dcfh_app()->admin_alert()::error(__('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_data = [];
                if ($action === 'create') {
                    $template_data = [
                        '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_data['created_by'] = get_current_user_id();
                    $template_data['modified_by'] = '';
                    $template_data['created_at'] = current_time('mysql');
                    $template_data['modified_at'] = current_time('mysql');
                } elseif ($action === 'edit') {
                    $template_data = get_option($tpl_id);
                    $template_data['contact_form_id'] = $contact_form_id;
                    $template_data['tpl_type'] = $tpl_type;
                    $template_data['tpl_name'] = $tpl_name;
                    $template_data['email_from'] = $email_from;
                    $template_data['email_from_name'] = $email_from_name;
                    $template_data['email_subject'] = $email_subject;
                    $template_data['email_body'] = $email_body;
                    $template_data['modified_by'] = get_current_user_id();
                    $template_data['modified_at'] = current_time('mysql');
                }
                $response = update_option($tpl_id, $template_data, 'no');
                if ($response) {
                    wp_safe_redirect(add_query_arg([
                        'post_type' => 'pwh_dcfh',
                        'page' => 'email_templates_list',
                        'success' => true,
                        'action' => $action,
                    ], admin_url('edit.php')));
                } else {
                    wp_safe_redirect(add_query_arg([
                        'post_type' => 'pwh_dcfh',
                        'page' => 'email_templates_list',
                        'success' => false,
                        'action' => '',
                    ], admin_url('edit.php')));
                }
                exit();
            }
        }
        // Edit
        if (!empty($_GET['tpl_id']) && 'edit' === $action) {
            $tpl_id = sanitize_text_field($_GET['tpl_id']);
            $options = get_option($tpl_id);
            if (empty($options)) {
                wp_safe_redirect(add_query_arg([
                    'post_type' => 'pwh_dcfh',
                    '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'];
        }
        $this->display_form($post_request);
    }

    public function display_form($post_request): void
    {
        /* if ($wp_error->has_errors()) {
             echo '<div class="error">';
             foreach ($wp_error->get_error_messages() as $error_message) {
                 echo '<p>'.esc_html($error_message).'</p>';
             }
             echo '</div>';
         }*/
        $contact_forms_data = $this->contact_form_data; ?>
        <div class="wrap dcfh__page" id="page__create_email_templates">
            <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' => 'pwh_dcfh', '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_app()->admin_alert()::show_message(); ?>
            <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_app()->email_template_manager()::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__tags_list"></div>
                                            </td>
                                        </tr>
                                        <tr class="email_from">
                                            <th><?php esc_html_e('From Email', '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('From Email',
                                                    'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="email_from_name">
                                            <th><?php esc_html_e('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('From Name',
                                                    'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr class="email_subject">
                                            <th><?php esc_html_e('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('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('nonce_add_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/widget/documentation-widget.php";
                        require_once dirname(__DIR__)."/template/widget/support-widget.php";
                        ?>
                    </div>
                </div>
                <br class="clear">
            </div>
        </div>
        <?php
    }
}


??

??