| 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 |
|
| 68 |
// Use Simple Cloudflare Turnstile plugin's function to render the widget |
| 69 |
// This ensures consistent rendering and avoids double-initialization |
| 70 |
// Note: Pass empty action to avoid Turnstile's strict alphanumeric validation |
| 71 |
// |
| 72 |
// Tokens are single-use: the widget (div id "cf-turnstile{$unique_id}") is |
| 73 |
// reset after every AJAX response by the generated form JS in AccuaForm.php, |
| 74 |
// next to the recaptcha2/v3 and Cap resets - no per-instance script needed |
| 75 |
echo '<div class="accua-forms-turnstile-container">'; |
| 76 |
cfturnstile_field_show('', '', '', $unique_id, 'accua-forms-turnstile'); |
| 77 |
echo '</div>'; |
| 78 |
} |
| 79 |
} |
| 80 |
|