153 lines
3.9 KiB
PHP
153 lines
3.9 KiB
PHP
<?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
|
|
{
|
|
/**
|
|
* Default configuration.
|
|
*
|
|
* @var array<string, mixed>
|
|
*/
|
|
protected array $_defaultConfig = [];
|
|
|
|
/**
|
|
* @var array|string[]
|
|
*/
|
|
protected array $helpers = ['Html'];
|
|
|
|
/**
|
|
* @param \Cake\View\View $view
|
|
* @param array<string, mixed> $config
|
|
*/
|
|
public function __construct(View $view, array $config = []) {
|
|
$config += (array)Configure::read('ContactUs.turnstile');
|
|
|
|
parent::__construct($view, $config);
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
*/
|
|
public function preConnect()
|
|
{
|
|
return '<link rel="preconnect" href="https://challenges.cloudflare.com" />';
|
|
}
|
|
|
|
/**
|
|
* @param array $options
|
|
*
|
|
* @return string|null
|
|
*/
|
|
public function script(array $options = [])
|
|
{
|
|
$options += [
|
|
'async' => true,
|
|
'defer' => true,
|
|
];
|
|
$scriptOptions = $options;
|
|
unset($scriptOptions['eventListeners']);
|
|
|
|
if ($options['eventListeners'] ?? false) {
|
|
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions) . $this->eventListener($options['eventListeners']);
|
|
}
|
|
return $this->Html->script('https://challenges.cloudflare.com/turnstile/v0/api.js', $scriptOptions);
|
|
}
|
|
|
|
/**
|
|
* @param $options
|
|
*
|
|
* @return string
|
|
*/
|
|
public function eventListener($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>';
|
|
|
|
return $final;
|
|
}
|
|
|
|
/**
|
|
* @param $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function _onTurnstileSuccess($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 .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
/**
|
|
* @param $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function onTurnstileError($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 .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
/**
|
|
* @param $options
|
|
*
|
|
* @return string
|
|
*/
|
|
protected function onTurnstileExpired($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;';
|
|
}
|
|
|
|
$jsFunction .= '}';
|
|
|
|
return $jsFunction;
|
|
}
|
|
|
|
}
|