?? 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: SendEmail.php

<?php

namespace KS_PAC_DCFH\Admin\Pages;

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

class SendEmail
{
    private int $post_id;

    private array $templates_list;

    public function load(): void
    {
        $this->post_id = isset($_GET['post_id']) ? sanitize_text_field($_GET['post_id']) : 0; // phpcs:ignore WordPress.Security.NonceVerification
        $page = isset($_GET['page']) ? sanitize_text_field($_GET['page']) : ''; // phpcs:ignore WordPress.Security.NonceVerification
        add_action('admin_menu', [$this, 'maybe_add_menu_item']);
        if ('send_email' === $page && !empty($this->post_id)) {
            $this->templates_list = ks_pac_dcfh_app()->email_template_manager()::get_templates_list();
            if ('' === get_post($this->post_id)) {
                wp_safe_redirect(add_query_arg(['post_type' => 'pwh_dcfh'], admin_url('edit.php')));
                exit();
            }
            add_filter('dcfh_admin_localization_data', [$this, 'maybe_filter_localization']);
        }
    }

    public function maybe_filter_localization($localizations)
    {
        $localizations['templatesList'] = wp_json_encode($this->templates_list);

        return $localizations;
    }

    public function maybe_add_menu_item(): void
    {
        add_submenu_page(
                'edit.php?post_type=pwh_dcfh',
                __('Send Email', 'divi-contact-form-helper'),
                '',
                '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['_wpnonce']) && !wp_verify_nonce(sanitize_text_field($_POST['_wpnonce']), 'nonce_send_email')) {
            wp_die(esc_html__('The security check failed due to an invalid nonce. Please try again.', 'divi-contact-form-helper'));
        }
        if (isset($_POST['btn_send_email'])) {
            $post_request = $_POST;
            $email_from = isset($_POST['email_from']) ? sanitize_text_field($_POST['email_from']) : '';
            $email_from_name = isset($_POST['email_from_name']) ? sanitize_text_field($_POST['email_from_name']) : '';
            $email_to = isset($_POST['email_to']) ? sanitize_text_field($_POST['email_to']) : '';
            $email_subject = isset($_POST['email_subject']) ? sanitize_text_field(htmlspecialchars_decode(stripslashes(sanitize_text_field($_POST['email_subject'])), ENT_QUOTES)) : ''; // phpcs:ignore
            $email_body = isset($_POST['email_body']) ? wpautop(htmlspecialchars_decode(stripslashes($_POST['email_body']), ENT_QUOTES)) : '';                                           // phpcs:ignore
            $validate_request = 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'),
                    'email_body' => __('Message body 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 ($validate_request) {
                $post_obj = get_post($this->post_id);
                if (is_serialized($post_obj->post_excerpt)) {
                    $form_entries = ks_pac_dcfh_app()->misc_utils()::maybe_unserialize($post_obj->post_excerpt);
                    $email_keywords = ks_pac_dcfh_app()->placeholder_helper()::get_email_placeholders($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 = ks_pac_dcfh_app()->mail_handler();
                    $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_app()->log_utils()->get_log_count('sent_email_log', $this->post_id);
                        ks_pac_dcfh_app()->log_utils()->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('nonce_admin_notice'),
                                '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('nonce_admin_notice'),
                                'email_sent_to' => $email_to,
                        ], admin_url('post.php')));
                    }
                    exit();
                }
            }
        }
        $this->display_form($post_request);
    }

    public function display_form($post_request): void
    { ?>
        <div class="wrap 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' => 'pwh_dcfh'], 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' => 'pwh_dcfh', '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_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>
                                        <?php if (!empty($this->templates_list)) { ?>
                                            <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->templates_list as $key => $value) { ?>
                                                            <option value="<?php echo esc_html($key); ?>"><?php echo esc_html($value['tpl_name']); ?></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 echo esc_html($post_request['email_from']); ?>" placeholder="<?php
                                                esc_html_e('Email From', 'divi-contact-form-helper'); ?>">
                                            </td>
                                        </tr>
                                        <tr>
                                            <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>
                                            <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>
                                            <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_app()->user_manager()::get_user_emails($this->post_id) as $key => $email) { ?>
                                                        <option value="<?php echo esc_html($key); ?>" <?php selected($key, $post_request['email_to']); ?>><?php echo esc_html($email); ?></option>
                                                    <?php } ?>
                                                </select>
                                            </td>
                                        </tr>
                                        <tr>
                                            <th><?php esc_html_e('Merge Tags:', 'divi-contact-form-helper'); ?></th>
                                            <td>
                                                <div id="merge__tags_list">
                                                    <ul>
                                                        <?php
                                                        $fields = ks_pac_dcfh_app()->entry_meta_manager()::get_contact_form_fields_meta_value($this->post_id);
                                                        if (!empty($fields)) {
                                                            foreach ($fields as $field) { ?>
                                                                <li class='keyword'>%%<?php echo esc_html($field); ?>
                                                                    %%
                                                                </li>
                                                                <?php
                                                            }
                                                        }
                                                        foreach (ks_pac_dcfh_app()->placeholder_helper()::get_system_placeholders() as $placeholder) { ?>
                                                            <li class='keyword'>%%<?php echo esc_html($placeholder); ?>
                                                                %%
                                                            </li>
                                                        <?php } ?>
                                                    </ul>
                                                </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('Use these keywords to create email template.'),
                                                        esc_url($create_template_url), esc_html('Click Here'))
                                                ?>
                                            </td>
                                        </tr>
                                        <tr>
                                            <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' => false,
                                                        'textarea_name' => 'email_body',
                                                        'textarea_rows' => 6
                                                ]);
                                                ?>
                                            </td>
                                        </tr>
                                        </tbody>
                                    </table>
                                    <?php
                                    wp_nonce_field('nonce_send_email');
                                    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/widget/documentation-widget.php";
                        require_once dirname(__DIR__)."/template/widget/support-widget.php";
                        ?>
                    </div>
                </div>
                <br class="clear">
            </div>
        </div>
        <?php
    }
}


??

??