| 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 Recaptcha 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 |
|
| 23 |
$data = apply_filters_deprecated( |
| 24 |
'fluentform_rendering_field_data_' . $elementName, |
| 25 |
[ |
| 26 |
$data, |
| 27 |
$form |
| 28 |
], |
| 29 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 30 |
'fluentform/rendering_field_data_' . $elementName, |
| 31 |
'Use fluentform/rendering_field_data_' . $elementName . ' instead of fluentform_rendering_field_data_' . $elementName |
| 32 |
); |
| 33 |
|
| 34 |
$data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); |
| 35 |
|
| 36 |
$key = get_option('_fluentform_reCaptcha_details'); |
| 37 |
$apiVersion = 'v2_visible'; |
| 38 |
if ($key && isset($key['siteKey'])) { |
| 39 |
$siteKey = $key['siteKey']; |
| 40 |
} else { |
| 41 |
$siteKey = ''; |
| 42 |
} |
| 43 |
|
| 44 |
if (! $siteKey) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
if (! empty($key['api_version'])) { |
| 49 |
$apiVersion = $key['api_version']; |
| 50 |
} |
| 51 |
|
| 52 |
if ('v3_invisible' == $apiVersion) { |
| 53 |
if (!wp_script_is('google-recaptcha')) { |
| 54 |
$apiUrl = 'https://www.google.com/recaptcha/api.js?render=' . $siteKey; |
| 55 |
|
| 56 |
$locale = apply_filters('fluentform/recaptcha_lang', ''); |
| 57 |
if ($locale) { |
| 58 |
$apiUrl .= '&hl=' . $locale; |
| 59 |
} |
| 60 |
|
| 61 |
wp_enqueue_script( |
| 62 |
'google-recaptcha', |
| 63 |
$apiUrl, |
| 64 |
[], |
| 65 |
FLUENTFORM_VERSION, |
| 66 |
true |
| 67 |
); |
| 68 |
} |
| 69 |
|
| 70 |
add_filter('fluentform/form_class', function ($formClass) { |
| 71 |
$formClass .= ' ff_has_v3_recptcha'; |
| 72 |
return $formClass; |
| 73 |
}); |
| 74 |
|
| 75 |
add_filter('fluentform/html_attributes', function ($atts) use ($siteKey) { |
| 76 |
$atts['data-recptcha_key'] = $siteKey; |
| 77 |
return $atts; |
| 78 |
}); |
| 79 |
|
| 80 |
$shouldRenderBadge = ArrayHelper::get($data, 'settings.render_recaptcha_v3_badge', false); |
| 81 |
|
| 82 |
if (!$shouldRenderBadge) { |
| 83 |
// Add CSS to hide reCAPTCHA badge |
| 84 |
add_action('wp_footer', function() { |
| 85 |
echo "<style> |
| 86 |
.grecaptcha-badge { |
| 87 |
visibility: hidden; |
| 88 |
} |
| 89 |
</style>"; |
| 90 |
}); |
| 91 |
} |
| 92 |
|
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
if (!wp_script_is('google-recaptcha')) { |
| 97 |
$apiUrl = 'https://www.google.com/recaptcha/api.js?render=explicit'; |
| 98 |
|
| 99 |
$locale = apply_filters('fluentform/recaptcha_lang', ''); |
| 100 |
if ($locale) { |
| 101 |
$apiUrl .= '&hl=' . $locale; |
| 102 |
} |
| 103 |
|
| 104 |
wp_enqueue_script( |
| 105 |
'google-recaptcha', |
| 106 |
$apiUrl, |
| 107 |
[], |
| 108 |
FLUENTFORM_VERSION, |
| 109 |
true |
| 110 |
); |
| 111 |
} |
| 112 |
|
| 113 |
$recaptchaBlock = "<div |
| 114 |
data-sitekey='" . esc_attr($siteKey) . "' |
| 115 |
id='fluentform-recaptcha-{$form->id}-{$form->instance_index}' |
| 116 |
class='ff-el-recaptcha g-recaptcha' |
| 117 |
data-callback='fluentFormrecaptchaSuccessCallback'></div>"; |
| 118 |
|
| 119 |
$label = ''; |
| 120 |
if (! empty($data['settings']['label'])) { |
| 121 |
$label = "<div class='ff-el-input--label'><label>" . fluentform_sanitize_html($data['settings']['label']) . '</label></div>'; |
| 122 |
} |
| 123 |
|
| 124 |
$containerClass = ''; |
| 125 |
if (! empty($data['settings']['label_placement'])) { |
| 126 |
$containerClass = 'ff-el-form-' . $data['settings']['label_placement']; |
| 127 |
} |
| 128 |
|
| 129 |
$el = "<div class='ff-el-input--content'><div data-fluent_id='" . $form->id . "' name='g-recaptcha-response'>{$recaptchaBlock}</div></div>"; |
| 130 |
|
| 131 |
$html = "<div class='ff-el-group " . esc_attr($containerClass) . "' >{$label}{$el}</div>"; |
| 132 |
|
| 133 |
$html = apply_filters_deprecated( |
| 134 |
'fluentform_rendering_field_html_' . $elementName, |
| 135 |
[ |
| 136 |
$html, |
| 137 |
$data, |
| 138 |
$form |
| 139 |
], |
| 140 |
FLUENTFORM_FRAMEWORK_UPGRADE, |
| 141 |
'fluentform/rendering_field_html_' . $elementName, |
| 142 |
'Use fluentform/rendering_field_html_' . $elementName . ' instead of fluentform_rendering_field_html_' . $elementName |
| 143 |
); |
| 144 |
|
| 145 |
$this->printContent('fluentform/rendering_field_html_' . $elementName, $html, $data, $form); |
| 146 |
} |
| 147 |
} |
| 148 |
|