# double-opt-in/2.15/core/Ajax.class.php

Double Opt-In for Contact Form 7 – Secure, GDPR-Compliant Email Verification, version 2.15. 204 lines.

- Page: https://pluginprobe.com/plugins/double-opt-in/2.15/code/core/Ajax.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/2.15/raw/core/Ajax.class.php
- Modified: 2022-12-16T11:04:54+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/double-opt-in/2.15/code/core/Ajax.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if (!defined('ABSPATH')) {
        exit;
    }

    /**
     * Class Ajax
     * Responsible to handle the admin settings for the double opt-in field
     *
     * @package forge12\contactform7\CF7OptIn
     */
    class Ajax
    {
        /**
         * Admin constructor.
         */
        public function __construct()
        {
            add_action('wp_ajax_f12_doi_details', array($this, 'getDetails'));
            add_action('wp_ajax_f12_doi_templateloader', array($this, 'getTemplate'));
        }

        /**
         * Load and get the template we need.
         */
        public function getTemplate()
        {
            $content = '';
            if (isset($_POST['template']) && wp_verify_nonce($_POST['nonce'], 'f12_doi_templateloader')) {
                $template_path = plugin_dir_path(dirname(__FILE__)) . 'mails/' . esc_attr(sanitize_text_field($_POST['template'])) . '.html';

                if (file_exists($template_path)) {
                    $content = file_get_contents($template_path);
                }

            }
            echo wp_json_encode(['status' => 200, 'content' => $content]);
            wp_die();
        }

        /**
         * Return the Popup for the HASH DOI
         */
        public function getDetails()
        {
            if (isset($_POST['hash']) && wp_verify_nonce($_POST['nonce'], 'f12_doi_details')) {
                global $wpdb;
                $tableName = $wpdb->prefix . 'f12_cf7_doubleoptin';
                $hash = sanitize_text_field($_POST['hash']);

                $OptIn = OptIn::get_by_hash($hash);

                if (null == $OptIn) {
                    ob_start();
                    ?>
                    <h2><?php _e('Ooops!', 'double-opt-in'); ?></h2>
                    <p>
                        <?php _e('Something went wrong. The given DOI wasn\'t found. Maybe it got removed?', 'double-opt-in'); ?>
                    </p>
                    <?php
                    $content = ob_get_contents();
                    ob_end_clean();
                    echo wp_json_encode(['status' => 200, 'content' => $content]);
                    wp_die();
                }

                $formfields = maybe_unserialize($OptIn->get_content());
                ob_start();
                ?>
                <h2><?php echo esc_html($OptIn->get_hash()); ?></h2>
                <?php if (current_user_can('manage_options')): ?>
                    <div class="options">
                        <a href="<?php echo esc_url($OptIn->get_link_delete()); ?>"><?php _e('Delete DOI', 'double-opt-in'); ?></a> |
                        <a href="<?php echo esc_url($OptIn->get_link_ui()); ?>"><?php _e('Details', 'double-opt-in'); ?></a>
                    </div>
                <?php endif; ?>
                <table>
                    <tr>
                        <td><?php _e('Key', 'double-opt-in'); ?></td>
                        <td><?php _e('Value', 'double-opt-in'); ?></td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('ID', 'f12-cf7-doupleoptin'); ?>
                        </td>
                        <td>
                            <?php echo esc_html($OptIn->get_id()); ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('CF7 Form ID', 'double-opt-in'); ?>
                        </td>
                        <td>
                            <?php echo esc_html($OptIn->get_cf_form_id()); ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('Registration Date', 'double-opt-in'); ?>
                        </td>
                        <td>
                            <?php
                            echo esc_html($OptIn->get_createtime('formatted'));
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('Registration IP', 'double-opt-in'); ?>
                        </td>
                        <td>
                            <?php echo esc_html($OptIn->get_ipaddr_register()); ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('Confirmation Date', 'double-opt-in'); ?>
                        </td>
                        <td>
                            <?php
                            if ($OptIn->is_confirmed()) {
                                echo esc_html($OptIn->get_updatetime('formatted'));
                            }
                            ?>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            <?php _e('Confirmation IP', 'double-opt-in'); ?>
                        </td>
                        <td>
                            <?php
                            echo esc_html($OptIn->get_ipaddr_confirmation());
                            ?>
                        </td>
                    </tr>
                </table>

                <h3><?php _e('Form Fields', 'double-opt-in'); ?></h3>
                <table>
                    <tr>
                        <td><?php _e('Key', 'double-opt-in'); ?></td>
                        <td><?php _e('Value', 'double-opt-in'); ?></td>
                    </tr>
                    <?php if (isset($formfields['fields'])): ?>
                        <?php foreach ($formfields['fields'] as $key => $value): ?>
                            <tr>
                                <td>
                                    <?php esc_attr_e($key); ?>
                                </td>
                                <td>
                                    <?php if (is_array($value)) {
                                        echo esc_html(implode(',', $value));
                                    } else {
                                        echo esc_html($value);
                                    }
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php else: ?>
                        <?php foreach ($formfields as $key => $value): ?>
                            <tr>
                                <td>
                                    <?php esc_attr_e($key); ?>
                                </td>
                                <td>
                                    <?php if (is_array($value)) {
                                        echo esc_html(implode(',', $value));
                                    } else {
                                        echo esc_html($value);
                                    }
                                    ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </table>
                <?php
                $content = ob_get_contents();
                ob_end_clean();
                echo wp_json_encode(['status' => 200, 'content' => $content]);
                exit;
            }
            wp_die(0);
        }

        /**
         * Add the styles for the form
         */
        public function addStyles($hook)
        {
            if ($hook == 'tools_page_f12doubleoptin') {
                wp_enqueue_style('f12-cf7-doubleoptin-admin', plugins_url('assets/admin-style.css', __FILE__));
                wp_enqueue_script('f12-cf7-doubleoptin-admin', plugins_url('assets/f12-cf7-popup.js', __FILE__), array('jquery'));
            }
        }
    }

    new Ajax();
}
```
