# contact-forms/2.1.2/classes/Element/Turnstile.php

Contact Forms by Cimatti, version 2.1.2. 112 lines.

- Page: https://pluginprobe.com/plugins/contact-forms/2.1.2/code/classes/Element/Turnstile.php
- Raw: https://pluginprobe.com/plugins/contact-forms/2.1.2/raw/classes/Element/Turnstile.php
- Modified: 2026-04-08T09:49:58+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/contact-forms/2.1.2/code/classes/Element/Turnstile.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Cloudflare Turnstile Element for Contact Forms
 * 
 * @package Contact Forms
 * @subpackage Elements
 * @since 1.9.14
 */

class AccuaForm_Element_Turnstile extends Element {
	protected $siteKey = "";
	protected $secretKey = "";

	public function __construct($label = "", $unused = null, ?array $properties = null) {
		if ($properties === null && is_array($unused)) {
			$properties = $unused;
		}
		parent::__construct($label, "cf-turnstile-response", $properties);
		
		// Get Turnstile keys from Simple Cloudflare Turnstile plugin if available
		if (function_exists('get_option')) {
			$stored_key = get_option('cfturnstile_key');
			$stored_secret = get_option('cfturnstile_secret');
			
			if (!empty($stored_key)) {
				$this->siteKey = $stored_key;
			}
			if (!empty($stored_secret)) {
				$this->secretKey = $stored_secret;
			}
		}
		
		$validator = new AccuaForm_Validation_Turnstile(__("Turnstile verification failed. Please try again.", 'contact-forms'));
		$validator->configure(array('secretKey' => $this->secretKey));
		$this->setValidation($validator);
	}

	public function render() {
		// Check if Simple Cloudflare Turnstile plugin is installed
		if (!function_exists('cfturnstile_field_show')) {
			echo '<div class="accua-forms-turnstile-notice" style="padding: 10px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; margin: 10px 0;">';
			echo '<strong>' . esc_html__('Plugin Required:', 'contact-forms') . '</strong> ';
			echo esc_html__('The Turnstile field requires the', 'contact-forms') . ' ';
			echo '<a href="https://it.wordpress.org/plugins/simple-cloudflare-turnstile/" target="_blank">';
			echo esc_html__('Simple Cloudflare Turnstile', 'contact-forms');
			echo '</a> ' . esc_html__('plugin to be installed and activated.', 'contact-forms');
			echo '</div>';
			return;
		}
		
		// Check if Turnstile is configured
		if (empty($this->siteKey) || empty($this->secretKey)) {
			echo '<div class="accua-forms-turnstile-notice" style="padding: 10px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; margin: 10px 0;">';
			echo '<strong>' . esc_html__('Configuration Required:', 'contact-forms') . '</strong> ';
			echo esc_html__('Please configure your Cloudflare Turnstile API keys in', 'contact-forms') . ' ';
			echo '<a href="' . esc_url(admin_url('options-general.php?page=cfturnstile')) . '">';
			echo esc_html__('Settings → Cloudflare Turnstile', 'contact-forms');
			echo '</a>.';
			echo '</div>';
			return;
		}
		
		// Generate unique ID for this instance
		$unique_id = '-accua-form-' . esc_attr($this->attributes['id']);
		$form_id = $this->form ? $this->form->getId() : 'unknown';
		
		// Use Simple Cloudflare Turnstile plugin's function to render the widget
		// This ensures consistent rendering and avoids double-initialization
		// Note: Pass empty action to avoid Turnstile's strict alphanumeric validation
		echo '<div class="accua-forms-turnstile-container">';
		cfturnstile_field_show('', '', '', $unique_id, 'accua-forms-turnstile');
		echo '</div>';
		
		// Add form reset handlers for AJAX submissions
		if ($this->form) {
			$container_id = 'cf-turnstile' . $unique_id;
			?>
			<script type="text/javascript">
			(function() {
				var containerId = '<?php echo esc_js($container_id); ?>';
				var formId = '<?php echo esc_js($form_id); ?>';
				
				// Reset Turnstile on form submission (AJAX forms)
				jQuery(document).on('accua_form_submitted_' + formId, function() {
					if (typeof turnstile !== 'undefined') {
						setTimeout(function() {
							var container = document.getElementById(containerId);
							if (container) {
								turnstile.reset('#' + containerId);
							}
						}, 1000);
					}
				});
				
				// Also reset on form errors
				jQuery(document).on('accua_form_error_' + formId, function() {
					if (typeof turnstile !== 'undefined') {
						var container = document.getElementById(containerId);
						if (container) {
							turnstile.reset('#' + containerId);
						}
					}
				});
			})();
			</script>
			<?php
		}
	}
}

```
