# double-opt-in/2.12/CF7DoubleOptIn.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/2.12/code/CF7DoubleOptIn.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/2.12/raw/CF7DoubleOptIn.class.php
- Modified: 2023-01-21T14:56:18+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.12/code/CF7DoubleOptIn.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if (!defined('ABSPATH')) {
        exit;
    }
    /**
     * Dependencies
     */
    require_once('core/Messages.class.php');
    require_once('core/Ajax.class.php');
    require_once('core/Compatibility.class.php');
    require_once('core/CleanUp.class.php');
    require_once('core/HTMLSelect.class.php');
    require_once('core/UI.class.php');
    require_once('core/UIPage.class.php');
    require_once('core/UIPageForm.class.php');
    require_once('core/OptIn.class.php');
    require_once('core/OptInLimitFilter.class.php');
    require_once('core/OptInSearchFilter.class.php');
    require_once('core/Category.class.php');
    require_once('core/CategoryOptions.class.php');
    require_once('core/Pagination.class.php');
    require_once('core/Support.class.php');

    /**
     * Plugin Name: Double Opt-In (Contact Form 7, Avada) - GDPR Ready
     * Plugin URI: https://www.forge12.com/blog/so-verwendest-du-das-double-opt-in-fuer-contact-form-7/
     * Description: This plugin allows you to add a double OptIn System to your Contact Form 7 & Avada Forms.
     * Text Domain: double-opt-in
     * Domain Path: /languages
     * Version: 2.12
     * Author: Forge12 Interactive GmbH
     * Author URI: https://www.forge12.com
     */
    define('FORGE12_OPTIN_VERSION', '2.12');
    define('FORGE12_OPTIN_SLUG', 'f12-cf7-doubleoptin');
    define('FORGE12_OPTIN_BASENAME', plugin_basename(__FILE__));

    /**
     * Class CF7DoubleOptIn
     * Controller for the Custom Links.
     *
     * @package forge12\contactform7
     */
    class CF7DoubleOptIn
    {
        /**
         * @var CF7DoubleOptIn|Null
         */
        private static $_instance = null;

        /**
         * Get the instance of the custom links controller
         *
         * @return CF7DoubleOptIn
         */
        public static function getInstance()
        {
            if (self::$_instance == null) {
                self::$_instance = new CF7DoubleOptIn();
            }

            return self::$_instance;
        }

        /**
         * @param string $template_name The name of the template file
         * @param array  $atts          The parameter send to the template.
         *
         * @return void|null
         */
        public function renderTemplate($template_name, $atts = array())
        {
            $template = $this->getTemplate($template_name, $atts);

            if (empty($template)) {
                return null;
            }

            echo $template;
        }

        /**
         * Return the Content of the Template
         *
         * @param string $template_name The name of the template file
         * @param array  $atts          The parameter send to the template.
         *
         * @return string
         */
        public function getTemplate($template_name, $atts = array())
        {
            $template_path = plugin_dir_path(__FILE__) . '/templates/' . $template_name . '.php';

            if (!file_exists($template_path)) {
                return '';
            }

            extract($atts);

            ob_start();
            require($template_path);
            $content = ob_get_contents();
            ob_end_clean();

            return $content;
        }

        /**
         * Return a list containing the array with all data stored within the contact form 7 form
         *
         * @param int $postID
         *
         * @return array
         */
        public function getParameter($postID)
        {
            $data = array();

            $data = apply_filters('f12_cf7_doubleoptin_get_parameter', $data);

            if (!$postID) {
                return $data;
            }

            $options = get_post_meta($postID, 'f12-cf7-doubleoptin', true);

            if (!$options) {
                return $data;
            }

            return array_merge($data, $options);
        }

        /**
         * CustomLinks constructor.
         */
        private function __construct()
        {
            $UI = new UI(FORGE12_OPTIN_SLUG, 'Forge12 Double Opt-In', 'manage_options');

            add_action('after_setup_theme', [$this, 'init']);
            add_action( 'init', [$this, 'load_text_domain'] );

            $Compatibility = new Compatibility(FORGE12_OPTIN_SLUG);

            $CleanUp = new CleanUp();

            // Pagination
            Pagination::getInstance();

            // initialize filter
            CategoryOptions::getInstance();
            OptInLimitFilter::getInstance();
            OptInSearchFilter::getInstance();

            // Support
            Support::getInstance();
        }

        public function load_text_domain(){
            load_plugin_textdomain( 'double-opt-in', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
        }

        /**
         * @private WordPress Hook
         */
        public function init()
        {
            do_action('f12_cf7_doubleoptin_register_implementations');
        }

        /**
         * @return string
         */
        public function getIPAdress()
        {
            //whether ip is from share internet
            if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
                $ip_address = sanitize_text_field($_SERVER['HTTP_CLIENT_IP']);
            } //whether ip is from proxy
            elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
                $ip_address = sanitize_text_field($_SERVER['HTTP_X_FORWARDED_FOR']);
            } //whether ip is from remote address
            else {
                $ip_address = sanitize_text_field($_SERVER['REMOTE_ADDR']);
            }

            return $ip_address;
        }

        public function sanitize_array($data){
            if(!is_array($data)){
                return [];
            }

            $sanitized_data = [];
            foreach($data as $key => $value){
                if(is_array($value)){
                    $sanitized_data[sanitize_text_field($key)] = $this->sanitize_array($value);
                }else {
                    $sanitized_data[sanitize_text_field($key)] = wp_kses_post($value);
                }
            }
            return $sanitized_data;
        }

        /**
         * Return all saved settings
         *
         * @param string $single The Key of the setting to return only the required setting
         *
         * @return array<mixed>
         */
        public function getSettings($single = '', $container = null)
        {
            $default = array();

            $default = apply_filters('f12_cf7_doubleoptin_settings', $default);

            $settings = get_option('f12-doi-settings');

            if (!is_array($settings)) {
                $settings = array();
            }

            foreach ($default as $key => $data) {
                if (isset($settings[$key])) {
                    if (is_array($default[$key])) {
                        $default[$key] = array_merge($default[$key], $settings[$key]);
                    } else {
                        $default[$key] = $settings[$key];
                    }
                }
            }

            $settings = $default;

            if (!empty($single)) {
                if ($container != null) {
                    if (isset($settings[$container]) && isset($settings[$container][$single])) {
                        $settings = $settings[$container][$single];
                    }
                }
            } else {
                if (isset($settings[$single])) {
                    $settings = $settings[$single];
                }
            }

            return $settings;
        }
    }

    /**
     * Create the table to store the opt ins.
     *
     * @return void
     */
    function createTableOptIn()
    {
        global $wpdb;

        $tableName = 'f12_cf7_doubleoptin';
        $wpTableName = $wpdb->prefix . $tableName;

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $sql = "CREATE TABLE " . $wpTableName . " (
        id int(11) NOT NULL auto_increment, 
        cf_form_id int(11) NOT NULL, 
        doubleoptin int(2), 
        content text, 
        files text,
        hash VARCHAR(255),
        ipaddr_register varchar(255) NOT NULL DEFAULT '',
        ipaddr_confirmation varchar(255) NOT NULL DEFAULT '',
        createtime varchar(255) DEFAULT '', 
        updatetime varchar(255) DEFAULT '',
        category int(11),
        email varchar(255),
        form text,
        mail_optin text,
        PRIMARY KEY  (id)
        )";
        dbDelta($sql);
    }

    /**
     * Create the table for the categories
     *
     * @return void
     */
    function createTableOptinCategories()
    {
        global $wpdb;

        $tableName = 'f12_cf7_doubleoptin_categories';
        $wpTableName = $wpdb->prefix . $tableName;

        require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

        $sql = "CREATE TABLE " . $wpTableName . " (
        id int(11) NOT NULL auto_increment, 
        name varchar(255),
        createtime varchar(255) DEFAULT '', 
        updatetime varchar(255) DEFAULT '',
        PRIMARY KEY  (id)
        )";
        dbDelta($sql);
    }

    /**
     * On Activation create the custom table to store the required information
     * for the double opt in system.
     */
    function onActivation()
    {
        createTableOptin();
        createTableOptinCategories();

        // Add cron
        if (!wp_next_scheduled('dailyOptinClear')) {
            wp_schedule_event(time(), 'daily', 'dailyOptinClear');
        }
    }

    register_activation_hook(__FILE__, 'forge12\contactform7\CF7DoubleOptIn\onActivation');

    /**
     * On deactivation delete all tables
     */
    function onDeactivation()
    {
        if (!defined('WP_UINSTALL_PLUGIN')) {
            return;
        }

        global $wpdb;

        // Backup Table
        $tableName = 'f12_cf7_doubleoptin';
        $wpTableName = $wpdb->prefix . $tableName;

        $wpdb->query("DROP TABLE IF EXISTS " . $wpTableName);

        # clear cron
        wp_clear_scheduled_hook('dailyOptinClear');
    }

    function onUpdate()
    {
        // Only run if the version installed not exist or the version is < 1.7
        // this will add categories to already existing plugins
        if (version_compare(get_site_option(FORGE12_OPTIN_SLUG . '_version'), '1.7') < 0) {
            createTableOptinCategories();
            createTableOptin();

            update_option(FORGE12_OPTIN_SLUG . '_version', '1.7');
        }

        // Only run if the version installed not exist or the version is < 2.0
        // this will add categories to already existing plugins
        if (version_compare(get_site_option(FORGE12_OPTIN_SLUG . '_version'), '2.0') < 0) {
            createTableOptin();

            update_option(FORGE12_OPTIN_SLUG . '_version', '2.0');
        }
    }

    onUpdate();

    register_deactivation_hook(__FILE__, 'forge12\contactform7\CF7DoubleOptIn\onDeactivation');

    CF7DoubleOptIn::getInstance();

    add_filter( 'safe_style_css', function( $styles ) {
        $styles[] = 'display';
        return $styles;
    } );
}
```
