# double-opt-in/trunk/core/SpamMechanics.class.php

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

- Page: https://pluginprobe.com/plugins/double-opt-in/trunk/code/core/SpamMechanics.class.php
- Raw: https://pluginprobe.com/plugins/double-opt-in/trunk/raw/core/SpamMechanics.class.php
- Modified: 2026-02-13T13:23:06+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/trunk/code/core/SpamMechanics.class.php#L10-L20`.

```php
<?php

namespace Forge12\ContactForm7\CF7DoubleOptIn;

use Forge12\Shared\LoggerInterface;

if (!defined('ABSPATH')) {
    exit;
}

class SpamMechanics
{
    /** @var LoggerInterface */
    private $logger;

    /**
     * Konstruktor
     *
     * @param LoggerInterface $logger
     */
    public function __construct(LoggerInterface $logger)
    {
        $this->logger = $logger;
        $this->initialize();
    }

    /**
     * Initialisiert die Spam-Mechanik-Deaktivierung,
     * falls ein gültiger Opt-in-Link aufgerufen wurde.
     *
     * @return void
     */
    private function initialize()
    {
        $this->logDebug('disableSpamMechanics called');

        $hash = isset($_GET['optin']) ? sanitize_text_field($_GET['optin']) : '';
        if (empty($hash)) {
            $this->logDebug('No opt-in hash found, skipping disableSpamMechanics');
            return;
        }

        $optIn = OptIn::get_by_hash($hash);
        if (!$optIn) {
            $this->logDebug('Invalid opt-in hash, skipping disableSpamMechanics');
            return;
        }

        if ($optIn->is_confirmed()) {
            $this->logDebug('Opt-in already confirmed, skipping disableSpamMechanics');
            return;
        }

        $this->disableForge12Captcha();
        $this->disableGoogleRecaptcha();
        $this->disableHCaptcha();

        $this->logDebug('All spam protection filters removed');
    }

    /**
     * Deaktiviert Forge12 Captcha-Integrationen für CF7, Avada und Elementor.
     *
     * @return void
     */
    private function disableForge12Captcha()
    {
        $filters = array(
            'f12_cf7_captcha_is_installed_cf7',
            'f12_cf7_captcha_is_installed_avada',
            'f12_cf7_captcha_is_installed_elementor', // Tippfehler korrigiert
        );

        foreach ($filters as $filter) {
            add_filter($filter, function ($is_active) {
                return false;
            }, 999, 1);
        }

        $this->logDebug('Forge12 Captcha filters removed');
    }

    /**
     * Entfernt Google reCAPTCHA Filter für CF7.
     *
     * @return void
     */
    private function disableGoogleRecaptcha()
    {
        remove_filter('wpcf7_spam', 'wpcf7_recaptcha_verify_response', 9);
        $this->logDebug('Google reCAPTCHA filter removed');
    }

    /**
     * Deaktiviert hCaptcha-Validierung für CF7.
     *
     * @return void
     */
    private function disableHCaptcha()
    {
        if (class_exists('\HCaptcha\CF7\CF7')) {
            global $wp_filter;

            if (isset($wp_filter['wpcf7_validate'])) {
                foreach ($wp_filter['wpcf7_validate']->callbacks as $priority => $hooks) {
                    foreach ($hooks as $key => $hook) {
                        if (is_array($hook['function']) && $hook['function'][0] instanceof \HCaptcha\CF7\CF7) {
                            remove_filter('wpcf7_validate', $hook['function'], $priority);
                            $this->logDebug('hCaptcha CF7 validation filter removed');
                            return;
                        }
                    }
                }
            }
        }
    }

    /**
     * Vereinheitlichter Logger für Debug-Nachrichten.
     *
     * @param string $message
     * @param array  $context
     * @return void
     */
    private function logDebug($message, $context = array())
    {
        $defaultContext = array(
            'plugin' => 'double-opt-in',
            'class'  => __CLASS__,
        );

        $context = array_merge($defaultContext, $context);

        $this->logger->debug($message, $context);
    }

    /**
     * @return LoggerInterface
     */
    public function get_logger()
    {
        return $this->logger;
    }
}
```
