| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
defined('ABSPATH') or die; |
| 6 |
|
| 7 |
use FluentForm\Framework\Helpers\ArrayHelper; |
| 8 |
|
| 9 |
class Turnstile extends BaseComponent |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Compile and echo the html element |
| 13 |
* |
| 14 |
* @param array $data [element data] |
| 15 |
* @param \stdClass $form [Form Object] |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function compile($data, $form) |
| 20 |
{ |
| 21 |
$elementName = $data['element']; |
| 22 |
$data = apply_filters_deprecated( |
| 23 |
'fluentform_rendering_field_data_' . $elementName, |
| 24 |
[ |
| 25 |
$data, |
| 26 |
$form |
| 27 |
], |
| 28 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 29 |
'fluentform/rendering_field_data_' . $elementName, |
| 30 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 31 |
); |
| 32 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 33 |
|
| 34 |
$turnstile = get_option('_fluentform_turnstile_details'); |
| 35 |
$siteKey = ArrayHelper::get($turnstile, 'siteKey'); |
| 36 |
|
| 37 |
if (! $siteKey) { |
| 38 |
return false; |
| 39 |
} |
| 40 |
|
| 41 |
add_filter('fluentform/html_attributes', function ($atts) use ($siteKey) { |
| 42 |
$atts['data-turnstile_key'] = $siteKey; |
| 43 |
return $atts; |
| 44 |
}); |
| 45 |
|
| 46 |
if (!wp_script_is('turnstile')) { |
| 47 |
$apiUrl = 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit'; |
| 48 |
|
| 49 |
$locale = apply_filters('fluentform/turnstile_lang', ''); |
| 50 |
if ($locale) { |
| 51 |
$apiUrl .= '&language=' . $locale; |
| 52 |
} |
| 53 |
|
| 54 |
wp_enqueue_script( |
| 55 |
'turnstile', |
| 56 |
'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit', // phpcs:ignore PluginCheck.CodeAnalysis.EnqueuedResourceOffloading.OffloadedContent -- Cloudflare Turnstile requires loading from their CDN for CAPTCHA functionality |
| 57 |
[], |
| 58 |
FLUENTFORM_VERSION, |
| 59 |
true |
| 60 |
); |
| 61 |
|
| 62 |
// for WP Rocket compatibility |
| 63 |
wp_script_add_data('turnstile', 'data-cfasync', 'false'); |
| 64 |
} |
| 65 |
|
| 66 |
$appearance = esc_attr(ArrayHelper::get($turnstile, 'appearance', 'always')); |
| 67 |
|
| 68 |
if ('yes' == ArrayHelper::get($turnstile, 'invisible')) { |
| 69 |
$appearance = 'interaction-only'; |
| 70 |
} |
| 71 |
|
| 72 |
$turnstileBlock = "<div |
| 73 |
data-sitekey='" . esc_attr($siteKey) . "' |
| 74 |
data-theme='" . esc_attr(ArrayHelper::get($turnstile, 'theme', 'auto')) . "' |
| 75 |
id='fluentform-turnstile-{$form->id}-{$form->instance_index}' |
| 76 |
class='ff-el-turnstile cf-turnstile' |
| 77 |
data-appearance='" . $appearance . "'></div>"; |
| 78 |
|
| 79 |
$label = ''; |
| 80 |
if (! empty($data['settings']['label'])) { |
| 81 |
$label = "<div class='ff-el-input--label'><label>" . fluentform_sanitize_html($data['settings']['label']) . '</label></div>'; |
| 82 |
} |
| 83 |
|
| 84 |
$containerClass = ''; |
| 85 |
if (! empty($data['settings']['label_placement'])) { |
| 86 |
$containerClass = 'ff-el-form-' . $data['settings']['label_placement']; |
| 87 |
} |
| 88 |
|
| 89 |
$el = "<div class='ff-el-input--content'><div data-fluent_id='" . $form->id . "' name='cf-turnstile-response'>{$turnstileBlock}</div></div>"; |
| 90 |
|
| 91 |
$html = "<div class='ff-el-group " . esc_attr($containerClass) . "' >{$label}{$el}</div>"; |
| 92 |
if ($appearance == 'interaction-only') { |
| 93 |
$html = str_replace("<div class='ff-el-group ' >", "<div class='ff-el-group ' style='margin-bottom: 0;'>", $html); |
| 94 |
} |
| 95 |
|
| 96 |
$html = apply_filters_deprecated( |
| 97 |
'fluentform_rendering_field_html_' . $elementName, |
| 98 |
[ |
| 99 |
$html, |
| 100 |
$data, |
| 101 |
$form |
| 102 |
], |
| 103 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 104 |
'fluentform/rendering_field_html_' . $elementName, |
| 105 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 106 |
); |
| 107 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 108 |
} |
| 109 |
} |
| 110 |
|