← All changes
|
app/Services/FormBuilder/Components/Recaptcha.php
+132
-36
3.6.51
→
6.2.15
View file →
| @@ -1,51 +1,147 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace FluentForm\App\Services\FormBuilder\Components; |
| 4 | 4 | |
| 5 | +defined('ABSPATH') or die; | |
| 6 | + | |
| 7 | +use FluentForm\Framework\Helpers\ArrayHelper; | |
| 8 | + | |
| 5 | 9 | class Recaptcha extends BaseComponent |
| 6 | 10 | { |
| 7 | - /** | |
| 8 | - * Compile and echo the html element | |
| 9 | - * @param array $data [element data] | |
| 10 | - * @param stdClass $form [Form Object] | |
| 11 | - * @return viod | |
| 12 | - */ | |
| 13 | - public function compile($data, $form) | |
| 14 | - { | |
| 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 | + { | |
| 15 | 21 | $elementName = $data['element']; |
| 16 | - $data = apply_filters('fluenform_rendering_field_data_'.$elementName, $data, $form); | |
| 17 | 22 | |
| 18 | - wp_enqueue_script( | |
| 19 | - 'google-recaptcha', | |
| 20 | - 'https://www.google.com/recaptcha/api.js', | |
| 21 | - array(), | |
| 22 | - FLUENTFORM_VERSION, | |
| 23 | - true | |
| 24 | - ); | |
| 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 | + ); | |
| 25 | 33 | |
| 26 | - $key = get_option('_fluentform_reCaptcha_details'); | |
| 27 | - if($key && isset($key['siteKey'])) { | |
| 28 | - $siteKey = $key['siteKey']; | |
| 29 | - } else { | |
| 30 | - $siteKey = ''; | |
| 31 | - } | |
| 34 | + $data = apply_filters('fluentform/rendering_field_data_' . $elementName, $data, $form); | |
| 32 | 35 | |
| 33 | - $recaptchaBlock = "<div | |
| 34 | - data-sitekey='{$siteKey}' | |
| 35 | - id='fluentform-recaptcha-{$form->id}' | |
| 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}' | |
| 36 | 116 | class='ff-el-recaptcha g-recaptcha' |
| 37 | 117 | data-callback='fluentFormrecaptchaSuccessCallback'></div>"; |
| 38 | 118 | |
| 39 | - $label = ''; | |
| 40 | - if (!empty($data['settings']['label'])) { | |
| 41 | - $label = "<div class='ff-el-input--label'><label>{$data['settings']['label']}</label></div>"; | |
| 42 | - } | |
| 43 | - | |
| 44 | - $el = "<div class='ff-el-input--content'><div data-fluent_id='".$form->id."' name='g-recaptcha-response'>{$recaptchaBlock}</div></div>"; | |
| 45 | - $atts = $this->buildAttributes( | |
| 46 | - \FluentForm\Framework\Helpers\ArrayHelper::except($data['attributes'], 'name') | |
| 47 | - ); | |
| 48 | - $html = "<div class='ff-el-group' {$atts}>{$label}{$el}</div>"; | |
| 49 | - echo apply_filters('fluenform_rendering_field_html_'.$elementName, $html, $data, $form); | |
| 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); | |
| 50 | 146 | } |
| 51 | 147 | } |