PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / trunk
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More vtrunk
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 +81 -67 6.25 → trunk View file →
@@ -9,8 +9,9 @@
9 9 class FrmFieldCaptcha extends FrmFieldType {
10 10
11 11 /**
12 12 * @var string
13 + *
13 14 * @since 3.0
14 15 */
15 16 protected $type = 'captcha';
16 17
@@ -28,15 +29,10 @@
28 29 */
29 30 public static function get_captcha_image_name() {
30 31 $frm_settings = FrmAppHelper::get_settings();
31 32 $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 - }
37 33
38 - return $image_name;
34 + return $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ? 'recaptcha_v3' : $active_captcha;
39 35 }
40 36
41 37 /**
42 38 * @return array
@@ -75,10 +71,14 @@
75 71 );
76 72 }
77 73
78 74 /**
79 - * Remove the "for" attribute for captcha
75 + * Modify the captcha field label so it is not orphaned.
80 76 *
77 + * The label is omitted when the label position is set to "none".
78 + * When the label is visible it is rendered as a <span> instead of a <label>,
79 + * since the captcha response input is inside an iframe and cannot be referenced.
80 + *
81 81 * @param array $args
82 82 * @param string $html
83 83 *
84 84 * @return string
@@ -83,26 +83,33 @@
83 83 *
84 84 * @return string
85 85 */
86 86 protected function before_replace_html_shortcodes( $args, $html ) {
87 - $frm_settings = FrmAppHelper::get_settings();
88 - $replace_response = $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha-response' : 'h-captcha-response';
89 - $replaced_for = str_replace( ' for="field_[key]"', ' for="' . $replace_response . '"', $html );
87 + if ( 'none' === FrmField::get_option( $this->field, 'label' ) ) {
88 + // Fully strip the label for a CAPTCHA if it is set to hidden.
89 + return preg_replace( '~\s*<label\b[^>]*for="field_\[key\]"[^>]*>.*?</label>\s*~s', '', $html );
90 + }
90 91
91 - return $replaced_for;
92 + // Convert a CAPTCHA label to a span to prevent an orphaned label issue in WAVE.
93 + return preg_replace(
94 + '~<label\b([^>]*?)\s*for="field_\[key\]"([^>]*?)>(.*?)</label>~s',
95 + '<span$1$2>$3</span>',
96 + $html
97 + );
92 98 }
93 99
94 100 /**
95 101 * @param array $args
96 102 * @param array $shortcode_atts
103 + *
97 104 * @return string
98 105 */
99 106 public function front_field_input( $args, $shortcode_atts ) {
100 - $frm_settings = FrmAppHelper::get_settings();
101 107 if ( ! self::should_show_captcha() ) {
102 108 return '';
103 109 }
104 110
111 + $frm_settings = FrmAppHelper::get_settings();
105 112 $settings = FrmCaptchaFactory::get_settings_object();
106 113 $div_attributes = array(
107 114 'id' => $args['html_id'],
108 115 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
@@ -107,18 +114,20 @@
107 114 'id' => $args['html_id'],
108 115 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
109 116 'data-sitekey' => $settings->get_pubkey(),
110 117 );
118 +
111 119 if ( 'turnstile' === $frm_settings->active_captcha ) {
112 120 $captcha_language = $this->get_captcha_language();
121 +
113 122 if ( $captcha_language ) {
114 123 $div_attributes['data-language'] = $captcha_language;
115 124 }
116 125 }
126 +
117 127 $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field );
118 - $html = '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
119 128
120 - return $html;
129 + return '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
121 130 }
122 131
123 132 /**
124 133 * @since 6.25
@@ -137,14 +146,16 @@
137 146 return apply_filters( 'frm_captcha_lang', get_bloginfo( 'language' ), $this->field );
138 147 }
139 148
140 149 /**
150 + * Load the captcha script.
151 + *
152 + * @param array $args
153 + *
141 154 * @return void
142 155 */
143 156 protected function load_field_scripts( $args ) {
144 - $api_js_url = $this->api_url();
145 -
146 - wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
157 + wp_register_script( 'captcha-api', $this->api_url(), array( 'formidable' ), '3', true );
147 158 wp_enqueue_script( 'captcha-api' );
148 159 }
149 160
150 161 /**
@@ -168,29 +179,32 @@
168 179 }
169 180
170 181 /**
171 182 * @param FrmSettings $frm_settings
183 + *
172 184 * @return string
173 185 */
174 186 protected function recaptcha_api_url( $frm_settings ) {
175 187 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
176 188
177 - $allow_multiple = $frm_settings->re_multi;
178 - if ( $allow_multiple ) {
189 + if ( $this->allow_multiple( $frm_settings ) ) {
179 190 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
180 191 }
181 192
182 193 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
194 +
183 195 if ( $lang ) {
184 196 $api_js_url .= '&hl=' . $lang;
185 197 }
186 198
199 + // Since this URL initially ends with ? and we never use add_query_arg, remove the extra
200 + // & that appears immediately after the ?
201 + $api_js_url = str_replace( '?&', '?', $api_js_url );
202 +
187 203 /**
188 204 * @param string $api_js_url
189 205 */
190 - $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
191 -
192 - return $api_js_url;
206 + return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
193 207 }
194 208
195 209 /**
196 210 * @since 6.0
@@ -198,10 +212,10 @@
198 212 * @return string
199 213 */
200 214 protected function hcaptcha_api_url() {
201 215 $api_js_url = 'https://js.hcaptcha.com/1/api.js';
216 + $lang = $this->get_captcha_language();
202 217
203 - $lang = $this->get_captcha_language();
204 218 if ( $lang ) {
205 219 // 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.
206 220 $lang_parts = explode( '-', $lang );
207 221 $api_js_url .= '?hl=' . $lang_parts[0];
@@ -206,8 +220,10 @@
206 220 $lang_parts = explode( '-', $lang );
207 221 $api_js_url .= '?hl=' . $lang_parts[0];
208 222 }
209 223
224 + $api_js_url = add_query_arg( 'onload', 'frmHcaptcha', $api_js_url );
225 +
210 226 /**
211 227 * Allows updating hcaptcha js api url.
212 228 *
213 229 * @since 6.0
@@ -213,11 +229,9 @@
213 229 * @since 6.0
214 230 *
215 231 * @param string $api_js_url
216 232 */
217 - $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
218 -
219 - return $api_js_url;
233 + return apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
220 234 }
221 235
222 236 /**
223 237 * @since 6.8.4
@@ -236,16 +250,14 @@
236 250 */
237 251 $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url );
238 252
239 253 // Prevent render=explicit from happening twice in case someone patched
240 - // the double rendering issue using the frm_turnstile_js_url hook.
241 - $api_js_url = str_replace(
254 + // The double rendering issue using the frm_turnstile_js_url hook.
255 + return str_replace(
242 256 '&render=explicit&render=explicit',
243 257 '&render=explicit',
244 258 $api_js_url
245 259 );
246 -
247 - return $api_js_url;
248 260 }
249 261
250 262 /**
251 263 * @param FrmSettings $frm_settings
@@ -254,19 +266,13 @@
254 266 *
255 267 * @psalm-return ''|'frm-'
256 268 */
257 269 protected function class_prefix( $frm_settings ) {
258 - if ( $this->allow_multiple( $frm_settings ) && $frm_settings->active_captcha === 'recaptcha' ) {
259 - $class_prefix = 'frm-';
260 - } else {
261 - $class_prefix = '';
262 - }
263 -
264 - return $class_prefix;
270 + return FrmCaptchaFactory::get_settings_object()->get_class_prefix( $this->allow_multiple( $frm_settings ) );
265 271 }
266 272
267 273 /**
268 - * @param FrmSettings $frm_settings
274 + * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility.
269 275 *
270 276 * @return string
271 277 *
272 278 * @psalm-return 'g-recaptcha'|'h-captcha'
@@ -275,8 +281,13 @@
275 281 $settings = FrmCaptchaFactory::get_settings_object();
276 282 return $settings->get_element_class_name();
277 283 }
278 284
285 + /**
286 + * @param FrmSettings $frm_settings
287 + *
288 + * @return bool
289 + */
279 290 protected function allow_multiple( $frm_settings ) {
280 291 return $frm_settings->re_multi;
281 292 }
282 293
@@ -281,15 +292,17 @@
281 292 }
282 293
283 294 /**
284 295 * @since 4.07
296 + *
285 297 * @param array $args
298 + *
286 299 * @return array
287 300 */
288 301 protected function validate_against_api( $args ) {
289 302 $errors = array();
290 303 $frm_settings = FrmAppHelper::get_settings();
291 - $resp = $this->send_api_check( $frm_settings );
304 + $resp = $this->send_api_check();
292 305 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
293 306
294 307 if ( is_wp_error( $resp ) ) {
295 308 $error_string = $resp->get_error_message();
@@ -301,43 +314,49 @@
301 314 if ( ! is_array( $response ) ) {
302 315 return $errors;
303 316 }
304 317
305 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
306 - if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
307 - $threshold = floatval( $frm_settings->re_threshold );
308 - $score = floatval( $response['score'] );
318 + if ( $frm_settings->active_captcha === 'recaptcha' && 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
319 + $threshold = floatval( $frm_settings->re_threshold );
320 + $score = floatval( $response['score'] );
309 321
310 - $this->set_score( $score );
322 + $this->set_score( $score );
311 323
312 - if ( $score < $threshold ) {
313 - $response['success'] = false;
314 - }
324 + if ( $score < $threshold ) {
325 + $response['success'] = false;
315 326 }
316 327 }
317 328
318 - if ( isset( $response['success'] ) && ! $response['success'] ) {
319 - // What happens when the CAPTCHA was entered incorrectly
320 - $invalid_message = FrmField::get_option( $this->field, 'invalid' );
321 - if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
322 - $invalid_message = '';
323 - }
324 - $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
329 + if ( ! isset( $response['success'] ) || $response['success'] ) {
330 + return $errors;
325 331 }
326 332
333 + // What happens when the CAPTCHA was entered incorrectly
334 + $invalid_message = FrmField::get_option( $this->field, 'invalid' );
335 +
336 + if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
337 + $invalid_message = '';
338 + }
339 +
340 + $errors[ 'field' . $args['id'] ] = $invalid_message === '' ? $frm_settings->re_msg : $invalid_message;
341 +
327 342 return $errors;
328 343 }
329 344
330 345 /**
331 346 * @param float $score
347 + *
332 348 * @return void
333 349 */
334 350 private function set_score( $score ) {
335 351 global $frm_vars;
352 +
336 353 if ( ! isset( $frm_vars['captcha_scores'] ) ) {
337 354 $frm_vars['captcha_scores'] = array();
338 355 }
356 +
339 357 $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
358 +
340 359 if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
341 360 $frm_vars['captcha_scores'][ $form_id ] = $score;
342 361 }
343 362 }
@@ -343,8 +362,9 @@
343 362 }
344 363
345 364 /**
346 365 * @param array $args
366 + *
347 367 * @return array
348 368 */
349 369 public function validate( $args ) {
350 370 if ( ! $this->should_validate() ) {
@@ -351,8 +371,9 @@
351 371 return array();
352 372 }
353 373
354 374 $missing_token = ! self::post_data_includes_token();
375 +
355 376 if ( $missing_token ) {
356 377 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
357 378 }
358 379
@@ -386,20 +407,21 @@
386 407 * @return bool
387 408 */
388 409 protected function should_validate() {
389 410 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
411 +
390 412 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
391 413 return false;
392 414 }
393 415
394 - // don't require the captcha if it shouldn't be shown
416 + // Don't require the captcha if it shouldn't be shown
395 417 return self::should_show_captcha();
396 418 }
397 419
398 420 /**
399 - * @param FrmSettings $frm_settings
421 + * @return array|WP_Error
400 422 */
401 - protected function send_api_check( $frm_settings ) {
423 + protected function send_api_check() {
402 424 $captcha_settings = FrmCaptchaFactory::get_settings_object();
403 425 $arg_array = array(
404 426 'body' => array(
405 427 'secret' => $captcha_settings->secret,
@@ -417,13 +439,14 @@
417 439 * @since 6.0
418 440 *
419 441 * @param array $values
420 442 *
421 - * @return array $values
443 + * @return array Values.
422 444 */
423 445 public static function update_field_name( $values ) {
424 446 if ( $values['type'] === 'captcha' ) {
425 447 $name = $values['name'];
448 +
426 449 if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
427 450 $values['name'] = __( 'Captcha', 'formidable' );
428 451 }
429 452 }
@@ -428,15 +451,6 @@
428 451 }
429 452 }
430 453
431 454 return $values;
432 - }
433 -
434 - /**
435 - * @param FrmSettings $frm_settings
436 - * @return string
437 - */
438 - protected function captcha_size( $frm_settings ) {
439 - _deprecated_function( __METHOD__, '6.8.4' );
440 - return 'normal';
441 455 }
442 456 }