| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 |
|
| 5 |
class Recaptcha extends BaseComponent |
| 6 |
{ |
| 7 |
/** |
| 8 |
* Compile and echo the html element |
| 9 |
* |
| 10 |
* @param array $data [element data] |
| 11 |
* @param \stdClass $form [Form Object] |
| 12 |
* |
| 13 |
* @return void |
| 14 |
*/ |
| 15 |
public function compile($data, $form) |
| 16 |
{ |
| 17 |
$elementName = $data['element']; |
| 18 |
$data = apply_filters('fluentform_rendering_field_data_' . $elementName, $data, $form); |
| 19 |
|
| 20 |
$key = get_option('_fluentform_reCaptcha_details'); |
| 21 |
$apiVersion = 'v2_visible'; |
| 22 |
if ($key && isset($key['siteKey'])) { |
| 23 |
$siteKey = $key['siteKey']; |
| 24 |
} else { |
| 25 |
$siteKey = ''; |
| 26 |
} |
| 27 |
|
| 28 |
if (! $siteKey) { |
| 29 |
return false; |
| 30 |
} |
| 31 |
|
| 32 |
if (! empty($key['api_version'])) { |
| 33 |
$apiVersion = $key['api_version']; |
| 34 |
} |
| 35 |
|
| 36 |
if ('v3_invisible' == $apiVersion) { |
| 37 |
wp_enqueue_script( |
| 38 |
'google-recaptcha', |
| 39 |
'https://www.google.com/recaptcha/api.js?render=' . $siteKey, |
| 40 |
[], |
| 41 |
FLUENTFORM_VERSION, |
| 42 |
true |
| 43 |
); |
| 44 |
|
| 45 |
add_filter('fluentform_form_class', function ($formClass) { |
| 46 |
$formClass .= ' ff_has_v3_recptcha'; |
| 47 |
return $formClass; |
| 48 |
}); |
| 49 |
|
| 50 |
add_filter('fluent_form_html_attributes', function ($atts) use ($siteKey) { |
| 51 |
$atts['data-recptcha_key'] = $siteKey; |
| 52 |
return $atts; |
| 53 |
}); |
| 54 |
|
| 55 |
return; |
| 56 |
} |
| 57 |
|
| 58 |
wp_enqueue_script( |
| 59 |
'google-recaptcha', |
| 60 |
'https://www.google.com/recaptcha/api.js', |
| 61 |
[], |
| 62 |
FLUENTFORM_VERSION, |
| 63 |
true |
| 64 |
); |
| 65 |
|
| 66 |
$recaptchaBlock = "<div |
| 67 |
data-sitekey='" . esc_attr($siteKey) . "' |
| 68 |
id='fluentform-recaptcha-" . $form->id . "' |
| 69 |
class='ff-el-recaptcha g-recaptcha' |
| 70 |
data-callback='fluentFormrecaptchaSuccessCallback'></div>"; |
| 71 |
|
| 72 |
$label = ''; |
| 73 |
if (! empty($data['settings']['label'])) { |
| 74 |
$label = "<div class='ff-el-input--label'><label>" . fluentform_sanitize_html($data['settings']['label'], false) . '</label></div>'; |
| 75 |
} |
| 76 |
|
| 77 |
$containerClass = ''; |
| 78 |
if (! empty($data['settings']['label_placement'])) { |
| 79 |
$containerClass = 'ff-el-form-' . $data['settings']['label_placement']; |
| 80 |
} |
| 81 |
|
| 82 |
$el = "<div class='ff-el-input--content'><div data-fluent_id='" . $form->id . "' name='g-recaptcha-response'>{$recaptchaBlock}</div></div>"; |
| 83 |
|
| 84 |
$html = "<div class='ff-el-group " . esc_attr($containerClass) . "' >{$label}{$el}</div>"; |
| 85 |
|
| 86 |
$this->printContent('fluentform_rendering_field_html_' . $elementName, $html, $data, $form); |
| 87 |
} |
| 88 |
} |
| 89 |
|