| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; |
| 3 |
|
| 4 |
/** |
| 5 |
* Cloudflare Turnstile Element for Contact Forms |
| 6 |
* |
| 7 |
* @package Contact Forms |
| 8 |
* @subpackage Elements |
| 9 |
* @since 1.9.14 |
| 10 |
*/ |
| 11 |
|
| 12 |
class AccuaForm_Element_Turnstile extends Element { |
| 13 |
protected $siteKey = ""; |
| 14 |
protected $secretKey = ""; |
| 15 |
|
| 16 |
public function __construct($label = "", $unused = null, ?array $properties = null) { |
| 17 |
if ($properties === null && is_array($unused)) { |
| 18 |
$properties = $unused; |
| 19 |
} |
| 20 |
parent::__construct($label, "cf-turnstile-response", $properties); |
| 21 |
|
| 22 |
// Get Turnstile keys from Simple Cloudflare Turnstile plugin if available |
| 23 |
if (function_exists('get_option')) { |
| 24 |
$stored_key = get_option('cfturnstile_key'); |
| 25 |
$stored_secret = get_option('cfturnstile_secret'); |
| 26 |
|
| 27 |
if (!empty($stored_key)) { |
| 28 |
$this->siteKey = $stored_key; |
| 29 |
} |
| 30 |
if (!empty($stored_secret)) { |
| 31 |
$this->secretKey = $stored_secret; |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
$validator = new AccuaForm_Validation_Turnstile(__("Turnstile verification failed. Please try again.", 'contact-forms')); |
| 36 |
$validator->configure(array('secretKey' => $this->secretKey)); |
| 37 |
$this->setValidation($validator); |
| 38 |
} |
| 39 |
|
| 40 |
public function render() { |
| 41 |
// Check if Simple Cloudflare Turnstile plugin is installed |
| 42 |
if (!function_exists('cfturnstile_field_show')) { |
| 43 |
echo '<div class="accua-forms-turnstile-notice" style="padding: 10px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; margin: 10px 0;">'; |
| 44 |
echo '<strong>' . esc_html__('Plugin Required:', 'contact-forms') . '</strong> '; |
| 45 |
echo esc_html__('The Turnstile field requires the', 'contact-forms') . ' '; |
| 46 |
echo '<a href="https://it.wordpress.org/plugins/simple-cloudflare-turnstile/" target="_blank">'; |
| 47 |
echo esc_html__('Simple Cloudflare Turnstile', 'contact-forms'); |
| 48 |
echo '</a> ' . esc_html__('plugin to be installed and activated.', 'contact-forms'); |
| 49 |
echo '</div>'; |
| 50 |
return; |
| 51 |
} |
| 52 |
|
| 53 |
// Check if Turnstile is configured |
| 54 |
if (empty($this->siteKey) || empty($this->secretKey)) { |
| 55 |
echo '<div class="accua-forms-turnstile-notice" style="padding: 10px; background: #fff3cd; border: 1px solid #ffc107; border-radius: 4px; margin: 10px 0;">'; |
| 56 |
echo '<strong>' . esc_html__('Configuration Required:', 'contact-forms') . '</strong> '; |
| 57 |
echo esc_html__('Please configure your Cloudflare Turnstile API keys in', 'contact-forms') . ' '; |
| 58 |
echo '<a href="' . esc_url(admin_url('options-general.php?page=cfturnstile')) . '">'; |
| 59 |
echo esc_html__('Settings → Cloudflare Turnstile', 'contact-forms'); |
| 60 |
echo '</a>.'; |
| 61 |
echo '</div>'; |
| 62 |
return; |
| 63 |
} |
| 64 |
|
| 65 |
// Generate unique ID for this instance |
| 66 |
$unique_id = '-accua-form-' . esc_attr($this->attributes['id']); |
| 67 |
$form_id = $this->form ? $this->form->getId() : 'unknown'; |
| 68 |
|
| 69 |
// Use Simple Cloudflare Turnstile plugin's function to render the widget |
| 70 |
// This ensures consistent rendering and avoids double-initialization |
| 71 |
// Note: Pass empty action to avoid Turnstile's strict alphanumeric validation |
| 72 |
echo '<div class="accua-forms-turnstile-container">'; |
| 73 |
cfturnstile_field_show('', '', '', $unique_id, 'accua-forms-turnstile'); |
| 74 |
echo '</div>'; |
| 75 |
|
| 76 |
// Add form reset handlers for AJAX submissions |
| 77 |
if ($this->form) { |
| 78 |
$container_id = 'cf-turnstile' . $unique_id; |
| 79 |
?> |
| 80 |
<script type="text/javascript"> |
| 81 |
(function() { |
| 82 |
var containerId = '<?php echo esc_js($container_id); ?>'; |
| 83 |
var formId = '<?php echo esc_js($form_id); ?>'; |
| 84 |
|
| 85 |
// Reset Turnstile on form submission (AJAX forms) |
| 86 |
jQuery(document).on('accua_form_submitted_' + formId, function() { |
| 87 |
if (typeof turnstile !== 'undefined') { |
| 88 |
setTimeout(function() { |
| 89 |
var container = document.getElementById(containerId); |
| 90 |
if (container) { |
| 91 |
turnstile.reset('#' + containerId); |
| 92 |
} |
| 93 |
}, 1000); |
| 94 |
} |
| 95 |
}); |
| 96 |
|
| 97 |
// Also reset on form errors |
| 98 |
jQuery(document).on('accua_form_error_' + formId, function() { |
| 99 |
if (typeof turnstile !== 'undefined') { |
| 100 |
var container = document.getElementById(containerId); |
| 101 |
if (container) { |
| 102 |
turnstile.reset('#' + containerId); |
| 103 |
} |
| 104 |
} |
| 105 |
}); |
| 106 |
})(); |
| 107 |
</script> |
| 108 |
<?php |
| 109 |
} |
| 110 |
} |
| 111 |
} |
| 112 |
|