Files
cake-contact-us/src/View/Helper/TurnstileHelper.php
T

175 lines
4.2 KiB
PHP
Raw Normal View History

2026-11-14 01:54:14 -08:00
<?php
declare(strict_types=1);
namespace CakeContactUs\View\Helper;
use Cake\Core\Configure;
use Cake\View\Helper;
use Cake\View\View;
/**
* Turnstile helper
*
* @property \Cake\View\Helper\HtmlHelper $Html
*/
class TurnstileHelper extends Helper {
/**
2026-11-14 01:54:14 -08:00
* Default configuration.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'eventListeners' => true,
];
2026-11-14 01:54:14 -08:00
/**
2026-11-14 01:54:14 -08:00
* @var array|string[]
*/
protected array $helpers = ['Html'];
2026-11-14 01:54:14 -08:00
/**
2026-11-14 01:54:14 -08:00
* @param \Cake\View\View $view
* @param array<string, mixed> $config
*/
public function __construct(View $view, array $config = []) {
$config += (array)Configure::read('Turnstile');
2026-11-14 01:54:14 -08:00
parent::__construct($view, $config);
}
2026-11-14 01:54:14 -08:00
/**
2026-11-14 01:54:14 -08:00
* @return string
*/
public function preConnect() {
return '<link rel="preconnect" href="https://challenges.cloudflare.com" />';
}
2026-11-14 01:54:14 -08:00
/**
2026-11-14 01:54:14 -08:00
* @param array $options
*
* @return string
*/
public function render(array $options = []) {
$options += [
'class' => 'cf-turnstile',
'data-sitekey' => $this->getConfigOrFail('siteKey'),
'data-theme' => 'auto',
'data-size' => 'flexible',
'eventListeners' => $this->getConfig('eventListeners'),
];
if ($options['eventListeners']) {
$options += [
'data-callback' => 'onTurnstileSuccess',
'data-error-callback' => 'onTurnstileError',
'data-expired-callback' => 'onTurnstileExpired',
];
}
unset($options['eventListeners']);
return $this->Html->div($options['class'], '', $options);
}
/**
* @param array $options
*
2026-11-14 01:54:14 -08:00
* @return string|null
*/
public function script(array $options = []) {
$options += [
'async' => true,
'defer' => true,
];
$scriptOptions = $options;
unset($scriptOptions['eventListeners']);
2026-11-14 01:54:14 -08:00
if ($options['eventListeners'] ?? false) {
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions) . $this->eventListener($options['eventListeners']);
}
2026-11-14 01:54:14 -08:00
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions);
}
/**
* @param array $options
2026-11-14 01:54:14 -08:00
*
* @return string
*/
public function eventListener(array $options) {
$options += [
'dynamicButton' => $this->getConfig('dynamicButton', false),
'log' => $this->getConfig('log', false),
'submitButtonId' => $this->getConfig('submitButtonId', 'submit-btn'),
];
$final = '<script>';
$final .= $this->_onTurnstileSuccess($options);
$final .= $this->onTurnstileError($options);
$final .= $this->onTurnstileExpired($options);
$final .= '</script>';
2026-11-14 01:54:14 -08:00
return $this->Html->scriptBlock($final);
}
2026-11-14 01:54:14 -08:00
/**
* @param array $options
2026-11-14 01:54:14 -08:00
*
* @return string
*/
protected function _onTurnstileSuccess(array $options = []): string {
$jsFunction = 'function onTurnstileSuccess(token) {';
if ($options['log'] ?? false) {
$jsFunction .= 'console.log("Turnstile success:", token);';
}
if ($options['dynamicButton'] ?? false) {
$jsFunction .= 'document.getElementById("'
. ($options['submitButtonId'] ?? 'submit-btn')
. '")?.disabled = false;';
}
$jsFunction .= '}';
2026-11-14 01:54:14 -08:00
return $jsFunction;
}
2026-11-14 01:54:14 -08:00
/**
* @param array $options
2026-11-14 01:54:14 -08:00
*
* @return string
*/
protected function onTurnstileError(array $options = []): string {
$jsFunction = 'function onTurnstileError(errorCode) {';
if ($options['log'] ?? false) {
$jsFunction .= 'console.error("Turnstile error:", errorCode);';
}
if ($options['dynamicButton'] ?? false) {
$jsFunction .= 'document.getElementById("'
. ($options['submitButtonId'] ?? 'submit-btn')
. '")?.disabled = true;';
}
$jsFunction .= '}';
2026-11-14 01:54:14 -08:00
return $jsFunction;
}
2026-11-14 01:54:14 -08:00
/**
* @param array $options
2026-11-14 01:54:14 -08:00
*
* @return string
*/
protected function onTurnstileExpired(array $options = []): string {
$jsFunction = 'function onTurnstileExpired(errorCode) {';
if ($options['log'] ?? false) {
$jsFunction .= 'console.warn("Turnstile token expired");';
}
if ($options['dynamicButton'] ?? false) {
$jsFunction .= 'document.getElementById("'
. ($options['submitButtonId'] ?? 'submit-btn')
. '")?.disabled = true;';
}
2026-11-14 01:54:14 -08:00
$jsFunction .= '}';
2026-11-14 01:54:14 -08:00
return $jsFunction;
}
2026-11-14 01:54:14 -08:00
}