PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.7
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.7
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/fields/FrmFieldCaptcha.php +80 -161 6.276.7 View file →
@@ -9,9 +9,8 @@
9 9 class FrmFieldCaptcha extends FrmFieldType {
10 10
11 11 /**
12 12 * @var string
13 - *
14 13 * @since 3.0
15 14 */
16 15 protected $type = 'captcha';
17 16
@@ -29,10 +28,15 @@
29 28 */
30 29 public static function get_captcha_image_name() {
31 30 $frm_settings = FrmAppHelper::get_settings();
32 31 $active_captcha = $frm_settings->active_captcha;
32 + if ( $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ) {
33 + $image_name = 'recaptcha_v3';
34 + } else {
35 + $image_name = $active_captcha;
36 + }
33 37
34 - return $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ? 'recaptcha_v3' : $active_captcha;
38 + return $image_name;
35 39 }
36 40
37 41 /**
38 42 * @return array
@@ -37,16 +41,13 @@
37 41 /**
38 42 * @return array
39 43 */
40 44 protected function field_settings_for_type() {
41 - $settings = FrmCaptchaFactory::get_settings_object();
42 45 return array(
43 - 'required' => false,
44 - 'invalid' => true,
45 - 'captcha_size' => $settings->should_show_captcha_size(),
46 - 'captcha_theme' => $settings->should_show_captcha_theme(),
47 - 'captcha_theme_auto_option' => $settings->should_show_captcha_theme_auto_option(),
48 - 'default' => false,
46 + 'required' => false,
47 + 'invalid' => true,
48 + 'captcha_size' => true,
49 + 'default' => false,
49 50 );
50 51 }
51 52
52 53 /**
@@ -71,74 +72,55 @@
71 72 );
72 73 }
73 74
74 75 /**
75 - * Replace the "for" attribute for captcha field so it matches the response ID.
76 + * Remove the "for" attribute for captcha
76 77 *
77 - * @param array $args
78 + * @param array $args
78 79 * @param string $html
79 80 *
80 81 * @return string
81 82 */
82 83 protected function before_replace_html_shortcodes( $args, $html ) {
83 - $settings = FrmCaptchaFactory::get_settings_object();
84 - return str_replace( ' for="field_[key]"', ' for="' . esc_attr( $settings->token_field ) . '"', $html );
84 + $frm_settings = FrmAppHelper::get_settings();
85 + $replace_response = $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha-response' : 'h-captcha-response';
86 + $replaced_for = str_replace( ' for="field_[key]"', ' for="' . $replace_response . '"', $html );
87 +
88 + return $replaced_for;
85 89 }
86 90
87 91 /**
88 - * @param array $args
89 - * @param array $shortcode_atts
90 - *
91 92 * @return string
92 93 */
93 94 public function front_field_input( $args, $shortcode_atts ) {
95 + $frm_settings = FrmAppHelper::get_settings();
94 96 if ( ! self::should_show_captcha() ) {
95 97 return '';
96 98 }
97 99
98 - $frm_settings = FrmAppHelper::get_settings();
99 - $settings = FrmCaptchaFactory::get_settings_object();
100 - $div_attributes = array(
101 - 'id' => $args['html_id'],
102 - 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
103 - 'data-sitekey' => $settings->get_pubkey(),
104 - );
100 + $class_prefix = $this->class_prefix( $frm_settings );
101 + $captcha_class = $this->captcha_class( $frm_settings );
102 + $captcha_size = $this->captcha_size( $frm_settings );
103 + $allow_mutiple = $frm_settings->re_multi;
105 104
106 - if ( 'turnstile' === $frm_settings->active_captcha ) {
107 - $captcha_language = $this->get_captcha_language();
105 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
106 + $site_key = $frm_settings->pubkey;
107 + $recaptcha_options = ' data-size="' . esc_attr( $captcha_size ) . '" data-theme="' . esc_attr( $this->field['captcha_theme'] ) . '"';
108 + } else {
109 + $site_key = $frm_settings->hcaptcha_pubkey;
110 + }
108 111
109 - if ( $captcha_language ) {
110 - $div_attributes['data-language'] = $captcha_language;
111 - }
112 + $html = '<div id="' . esc_attr( $args['html_id'] ) . '" class="' . esc_attr( $class_prefix ) . $captcha_class . '" data-sitekey="' . esc_attr( $site_key ) . '"';
113 + $html .= ! empty( $recaptcha_options ) ? $recaptcha_options : '';
114 + if ( $captcha_size === 'invisible' && ! $allow_mutiple ) {
115 + $html .= ' data-callback="frmAfterRecaptcha"';
112 116 }
117 + $html .= '></div>';
113 118
114 - $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field );
115 -
116 - return '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
119 + return $html;
117 120 }
118 121
119 122 /**
120 - * @since 6.25
121 - *
122 - * @return string
123 - */
124 - private function get_captcha_language() {
125 - /**
126 - * Allows updating the captcha language.
127 - *
128 - * @since 6.25
129 - *
130 - * @param string $lang
131 - * @param array $field
132 - */
133 - return apply_filters( 'frm_captcha_lang', get_bloginfo( 'language' ), $this->field );
134 - }
135 -
136 - /**
137 - * Load the captcha script.
138 - *
139 - * @param array $args
140 - *
141 123 * @return void
142 124 */
143 125 protected function load_field_scripts( $args ) {
144 126 $api_js_url = $this->api_url();
@@ -146,73 +128,41 @@
146 128 wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
147 129 wp_enqueue_script( 'captcha-api' );
148 130 }
149 131
150 - /**
151 - * Get the URL for the script JS that is loaded on the front end.
152 - *
153 - * @return string
154 - */
155 132 protected function api_url() {
156 133 $frm_settings = FrmAppHelper::get_settings();
157 - $active_mode = $frm_settings->active_captcha;
158 -
159 - if ( 'recaptcha' === $active_mode ) {
134 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
160 135 return $this->recaptcha_api_url( $frm_settings );
161 136 }
162 137
163 - if ( 'hcaptcha' === $active_mode ) {
164 - return $this->hcaptcha_api_url();
165 - }
166 -
167 - return $this->turnstile_api_url();
138 + return $this->hcaptcha_api_url();
168 139 }
169 140
170 141 /**
171 142 * @param FrmSettings $frm_settings
172 - *
173 - * @return string
174 143 */
175 144 protected function recaptcha_api_url( $frm_settings ) {
176 145 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
177 146
178 - if ( $this->allow_multiple( $frm_settings ) ) {
147 + $allow_mutiple = $frm_settings->re_multi;
148 + if ( $allow_mutiple ) {
179 149 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
180 150 }
181 151
182 152 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
183 -
184 - if ( $lang ) {
153 + if ( ! empty( $lang ) ) {
185 154 $api_js_url .= '&hl=' . $lang;
186 155 }
187 156
188 - // Since this URL initially ends with ? and we never use add_query_arg, remove the extra
189 - // & that appears immediately after the ?
190 - $api_js_url = str_replace( '?&', '?', $api_js_url );
157 + $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
191 158
192 - /**
193 - * @param string $api_js_url
194 - */
195 - return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
159 + return $api_js_url;
196 160 }
197 161
198 - /**
199 - * @since 6.0
200 - *
201 - * @return string
202 - */
203 162 protected function hcaptcha_api_url() {
204 163 $api_js_url = 'https://js.hcaptcha.com/1/api.js';
205 - $lang = $this->get_captcha_language();
206 164
207 - if ( $lang ) {
208 - // Language might be in the format of en-US, fr-FR, etc. In that case, we need to extract the first part to comply with the hcaptcha api request format.
209 - $lang_parts = explode( '-', $lang );
210 - $api_js_url .= '?hl=' . $lang_parts[0];
211 - }
212 -
213 - $api_js_url = add_query_arg( 'onload', 'frmHcaptcha', $api_js_url );
214 -
215 165 /**
216 166 * Allows updating hcaptcha js api url.
217 167 *
218 168 * @since 6.0
@@ -218,35 +168,11 @@
218 168 * @since 6.0
219 169 *
220 170 * @param string $api_js_url
221 171 */
222 - return apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
223 - }
172 + $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
224 173
225 - /**
226 - * @since 6.8.4
227 - *
228 - * @return string
229 - */
230 - protected function turnstile_api_url() {
231 - $api_js_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=frmTurnstile&render=explicit';
232 -
233 - /**
234 - * Allows updating hcaptcha js api url.
235 - *
236 - * @since 6.8.4
237 - *
238 - * @param string $api_js_url
239 - */
240 - $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url );
241 -
242 - // Prevent render=explicit from happening twice in case someone patched
243 - // the double rendering issue using the frm_turnstile_js_url hook.
244 - return str_replace(
245 - '&render=explicit&render=explicit',
246 - '&render=explicit',
247 - $api_js_url
248 - );
174 + return $api_js_url;
249 175 }
250 176
251 177 /**
252 178 * @param FrmSettings $frm_settings
@@ -255,13 +181,19 @@
255 181 *
256 182 * @psalm-return ''|'frm-'
257 183 */
258 184 protected function class_prefix( $frm_settings ) {
259 - return FrmCaptchaFactory::get_settings_object()->get_class_prefix( $this->allow_multiple( $frm_settings ) );
185 + if ( $this->allow_multiple( $frm_settings ) && $frm_settings->active_captcha === 'recaptcha' ) {
186 + $class_prefix = 'frm-';
187 + } else {
188 + $class_prefix = '';
189 + }
190 +
191 + return $class_prefix;
260 192 }
261 193
262 194 /**
263 - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility.
195 + * @param FrmSettings $frm_settings
264 196 *
265 197 * @return string
266 198 *
267 199 * @psalm-return 'g-recaptcha'|'h-captcha'
@@ -266,32 +198,37 @@
266 198 *
267 199 * @psalm-return 'g-recaptcha'|'h-captcha'
268 200 */
269 201 protected function captcha_class( $frm_settings ) {
270 - $settings = FrmCaptchaFactory::get_settings_object();
271 - return $settings->get_element_class_name();
202 + return $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha' : 'h-captcha';
272 203 }
273 204
205 + protected function allow_multiple( $frm_settings ) {
206 + return $frm_settings->re_multi;
207 + }
208 +
274 209 /**
210 + * @return string
211 + *
275 212 * @param FrmSettings $frm_settings
276 - *
277 - * @return bool
278 213 */
279 - protected function allow_multiple( $frm_settings ) {
280 - return $frm_settings->re_multi;
214 + protected function captcha_size( $frm_settings ) {
215 + if ( in_array( $frm_settings->re_type, array( 'invisible', 'v3' ), true ) ) {
216 + return 'invisible';
217 + }
218 + // for reverse compatibility
219 + return $this->field['captcha_size'] === 'default' ? 'normal' : $this->field['captcha_size'];
281 220 }
282 221
283 222 /**
284 223 * @since 4.07
285 - *
286 224 * @param array $args
287 - *
288 225 * @return array
289 226 */
290 227 protected function validate_against_api( $args ) {
291 228 $errors = array();
292 229 $frm_settings = FrmAppHelper::get_settings();
293 - $resp = $this->send_api_check();
230 + $resp = $this->send_api_check( $frm_settings );
294 231 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
295 232
296 233 if ( is_wp_error( $resp ) ) {
297 234 $error_string = $resp->get_error_message();
@@ -319,14 +256,12 @@
319 256
320 257 if ( isset( $response['success'] ) && ! $response['success'] ) {
321 258 // What happens when the CAPTCHA was entered incorrectly
322 259 $invalid_message = FrmField::get_option( $this->field, 'invalid' );
323 -
324 260 if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
325 261 $invalid_message = '';
326 262 }
327 -
328 - $errors[ 'field' . $args['id'] ] = $invalid_message === '' ? $frm_settings->re_msg : $invalid_message;
263 + $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
329 264 }
330 265
331 266 return $errors;
332 267 }
@@ -332,20 +267,16 @@
332 267 }
333 268
334 269 /**
335 270 * @param float $score
336 - *
337 271 * @return void
338 272 */
339 273 private function set_score( $score ) {
340 274 global $frm_vars;
341 -
342 275 if ( ! isset( $frm_vars['captcha_scores'] ) ) {
343 276 $frm_vars['captcha_scores'] = array();
344 277 }
345 -
346 278 $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
347 -
348 279 if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
349 280 $frm_vars['captcha_scores'][ $form_id ] = $score;
350 281 }
351 282 }
@@ -351,9 +282,8 @@
351 282 }
352 283
353 284 /**
354 285 * @param array $args
355 - *
356 286 * @return array
357 287 */
358 288 public function validate( $args ) {
359 289 if ( ! $this->should_validate() ) {
@@ -359,11 +289,11 @@
359 289 if ( ! $this->should_validate() ) {
360 290 return array();
361 291 }
362 292
363 - $missing_token = ! self::post_data_includes_token();
364 -
365 - if ( $missing_token ) {
293 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
294 + if ( ! isset( $_POST['g-recaptcha-response'] ) && ! isset( $_POST['h-captcha-response'] ) ) {
295 + // There was no captcha submitted.
366 296 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
367 297 }
368 298
369 299 return $this->validate_against_api( $args );
@@ -369,28 +299,18 @@
369 299 return $this->validate_against_api( $args );
370 300 }
371 301
372 302 /**
373 - * @since 6.8.4
374 - *
375 - * @return bool
376 - */
377 - protected static function post_data_includes_token() {
378 - $settings = FrmCaptchaFactory::get_settings_object();
379 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
380 - return ! empty( $_POST[ $settings->token_field ] );
381 - }
382 -
383 - /**
384 - * Check if the active captcha type's public key is set.
385 - *
386 303 * @since 4.07
387 - *
388 304 * @return bool
389 305 */
390 306 public static function should_show_captcha() {
391 - $settings = FrmCaptchaFactory::get_settings_object();
392 - return $settings->has_pubkey();
307 + $frm_settings = FrmAppHelper::get_settings();
308 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
309 + return ! empty( $frm_settings->pubkey );
310 + }
311 +
312 + return ! empty( $frm_settings->hcaptcha_pubkey );
393 313 }
394 314
395 315 /**
396 316 * @return bool
@@ -396,9 +316,8 @@
396 316 * @return bool
397 317 */
398 318 protected function should_validate() {
399 319 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
400 -
401 320 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
402 321 return false;
403 322 }
404 323
@@ -406,13 +325,14 @@
406 325 return self::should_show_captcha();
407 326 }
408 327
409 328 /**
410 - * @return array|WP_Error
329 + * @param FrmSettings $frm_settings
411 330 */
412 - protected function send_api_check() {
413 - $captcha_settings = FrmCaptchaFactory::get_settings_object();
414 - $arg_array = array(
331 + protected function send_api_check( $frm_settings ) {
332 + $captcha_settings = new FrmFieldCaptchaSettings( $frm_settings );
333 +
334 + $arg_array = array(
415 335 'body' => array(
416 336 'secret' => $captcha_settings->secret,
417 337 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
418 338 'remoteip' => FrmAppHelper::get_ip_address(),
@@ -433,9 +353,8 @@
433 353 */
434 354 public static function update_field_name( $values ) {
435 355 if ( $values['type'] === 'captcha' ) {
436 356 $name = $values['name'];
437 -
438 357 if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
439 358 $values['name'] = __( 'Captcha', 'formidable' );
440 359 }
441 360 }