# double-opt-in/2.12/core/CleanUp.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/2.12/code/core/CleanUp.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/2.12/raw/core/CleanUp.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.12/code/core/CleanUp.class.php#L10-L20`.

```php
<?php

namespace forge12\contactform7\CF7DoubleOptIn {
    if(!defined('ABSPATH')){
        exit;
    }
    /**
     * This class will handle the clean up of the database
     * as defined by the user settings.
     */
    class CleanUp
    {
        public function __construct()
        {
            add_action('dailyOptinClear', array($this, 'removeUnconfirmedOptins'));
            add_action('dailyOptinClear', array($this, 'removeConfirmedOptins'));
        }

        /**
         * Delete the given hash entry
         */
        public function deleteByHash($hash){
            if(current_user_can('manage_options')){
                global $wpdb;
                $tableName = 'f12_cf7_doubleoptin';
                $wpTableName = $wpdb->prefix . $tableName;

                return $wpdb->query('DELETE FROM ' . $wpTableName . ' WHERE hash="' . $hash . '"');
            }
            return false;
        }

        /**
         * Delete all Rows from the Database which are older than the given timestamp and where the optin value matches as defined.
         * @param $timestamp
         * @param int $optin
         */
        private function removeOlderThan($timestamp, $optin = 0)
        {
            global $wpdb;
            $tableName = 'f12_cf7_doubleoptin';
            $wpTableName = $wpdb->prefix . $tableName;

            //$sql = 'DELETE FROM ' . $wpTableName . ' WHERE createtime < "' . $timestamp . '" AND doubleoptin="' . $optin . '"';
            $wpdb->query('DELETE FROM ' . $wpTableName . ' WHERE createtime < "' . $timestamp . '" AND doubleoptin="' . $optin . '"');
        }

        /**
         * Delete all DOI entries
         */
        public function reset(){
            global $wpdb;
            $tableName = 'f12_cf7_doubleoptin';
            $wpTableName = $wpdb->prefix . $tableName;

            $wpdb->query('DELETE FROM ' . $wpTableName);
        }

        /**
         * Clear all unconfirmed database entries if the period selected
         * is reached.
         */
        public function removeUnconfirmedOptins()
        {
            $Settings = CF7DoubleOptIn::getInstance()->getSettings();

            if ($Settings['delete_unconfirmed'] == 0 || !is_numeric($Settings['delete_unconfirmed'])) {
                return;
            }

            $period = $Settings['delete_unconfirmed_period'];

            $timestamp = strtotime("- " . (int)$Settings['delete_unconfirmed'] . " ".$period, time());

            $this->removeOlderThan($timestamp, 0);
        }

        /**
         * Clear all confimred database entries if the period selected
         * is reached.
         */
        public function removeConfirmedOptins()
        {
            $Settings = CF7DoubleOptIn::getInstance()->getSettings();

            if ($Settings['delete'] == 0 || !is_numeric($Settings['delete'])) {
                return;
            }

            $period = $Settings['delete_period'];

            $timestamp = strtotime("- " . (int)$Settings['delete'] . " ".$period, time());

            $this->removeOlderThan($timestamp, 1);
        }

    }
}
```
