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 +73 -55 6.25.1 → 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 - * Replace the "for" attribute for captcha field so it matches the response ID.
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,23 +83,33 @@
83 83 *
84 84 * @return string
85 85 */
86 86 protected function before_replace_html_shortcodes( $args, $html ) {
87 - $settings = FrmCaptchaFactory::get_settings_object();
88 - return str_replace( ' for="field_[key]"', ' for="' . esc_attr( $settings->token_field ) . '"', $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 + }
91 +
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 + );
89 98 }
90 99
91 100 /**
92 101 * @param array $args
93 102 * @param array $shortcode_atts
103 + *
94 104 * @return string
95 105 */
96 106 public function front_field_input( $args, $shortcode_atts ) {
97 - $frm_settings = FrmAppHelper::get_settings();
98 107 if ( ! self::should_show_captcha() ) {
99 108 return '';
100 109 }
101 110
111 + $frm_settings = FrmAppHelper::get_settings();
102 112 $settings = FrmCaptchaFactory::get_settings_object();
103 113 $div_attributes = array(
104 114 'id' => $args['html_id'],
105 115 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
@@ -104,18 +114,20 @@
104 114 'id' => $args['html_id'],
105 115 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
106 116 'data-sitekey' => $settings->get_pubkey(),
107 117 );
118 +
108 119 if ( 'turnstile' === $frm_settings->active_captcha ) {
109 120 $captcha_language = $this->get_captcha_language();
121 +
110 122 if ( $captcha_language ) {
111 123 $div_attributes['data-language'] = $captcha_language;
112 124 }
113 125 }
126 +
114 127 $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field );
115 - $html = '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
116 128
117 - return $html;
129 + return '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
118 130 }
119 131
120 132 /**
121 133 * @since 6.25
@@ -134,14 +146,16 @@
134 146 return apply_filters( 'frm_captcha_lang', get_bloginfo( 'language' ), $this->field );
135 147 }
136 148
137 149 /**
150 + * Load the captcha script.
151 + *
152 + * @param array $args
153 + *
138 154 * @return void
139 155 */
140 156 protected function load_field_scripts( $args ) {
141 - $api_js_url = $this->api_url();
142 -
143 - 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 );
144 158 wp_enqueue_script( 'captcha-api' );
145 159 }
146 160
147 161 /**
@@ -165,8 +179,9 @@
165 179 }
166 180
167 181 /**
168 182 * @param FrmSettings $frm_settings
183 + *
169 184 * @return string
170 185 */
171 186 protected function recaptcha_api_url( $frm_settings ) {
172 187 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
@@ -175,8 +190,9 @@
175 190 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
176 191 }
177 192
178 193 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
194 +
179 195 if ( $lang ) {
180 196 $api_js_url .= '&hl=' . $lang;
181 197 }
182 198
@@ -186,11 +202,9 @@
186 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];
@@ -215,11 +229,9 @@
215 229 * @since 6.0
216 230 *
217 231 * @param string $api_js_url
218 232 */
219 - $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
220 -
221 - return $api_js_url;
233 + return apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
222 234 }
223 235
224 236 /**
225 237 * @since 6.8.4
@@ -238,16 +250,14 @@
238 250 */
239 251 $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url );
240 252
241 253 // Prevent render=explicit from happening twice in case someone patched
242 - // the double rendering issue using the frm_turnstile_js_url hook.
243 - $api_js_url = str_replace(
254 + // The double rendering issue using the frm_turnstile_js_url hook.
255 + return str_replace(
244 256 '&render=explicit&render=explicit',
245 257 '&render=explicit',
246 258 $api_js_url
247 259 );
248 -
249 - return $api_js_url;
250 260 }
251 261
252 262 /**
253 263 * @param FrmSettings $frm_settings
@@ -271,8 +281,13 @@
271 281 $settings = FrmCaptchaFactory::get_settings_object();
272 282 return $settings->get_element_class_name();
273 283 }
274 284
285 + /**
286 + * @param FrmSettings $frm_settings
287 + *
288 + * @return bool
289 + */
275 290 protected function allow_multiple( $frm_settings ) {
276 291 return $frm_settings->re_multi;
277 292 }
278 293
@@ -277,15 +292,17 @@
277 292 }
278 293
279 294 /**
280 295 * @since 4.07
296 + *
281 297 * @param array $args
298 + *
282 299 * @return array
283 300 */
284 301 protected function validate_against_api( $args ) {
285 302 $errors = array();
286 303 $frm_settings = FrmAppHelper::get_settings();
287 - $resp = $this->send_api_check( $frm_settings );
304 + $resp = $this->send_api_check();
288 305 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
289 306
290 307 if ( is_wp_error( $resp ) ) {
291 308 $error_string = $resp->get_error_message();
@@ -297,43 +314,49 @@
297 314 if ( ! is_array( $response ) ) {
298 315 return $errors;
299 316 }
300 317
301 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
302 - if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
303 - $threshold = floatval( $frm_settings->re_threshold );
304 - $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'] );
305 321
306 - $this->set_score( $score );
322 + $this->set_score( $score );
307 323
308 - if ( $score < $threshold ) {
309 - $response['success'] = false;
310 - }
324 + if ( $score < $threshold ) {
325 + $response['success'] = false;
311 326 }
312 327 }
313 328
314 - if ( isset( $response['success'] ) && ! $response['success'] ) {
315 - // What happens when the CAPTCHA was entered incorrectly
316 - $invalid_message = FrmField::get_option( $this->field, 'invalid' );
317 - if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
318 - $invalid_message = '';
319 - }
320 - $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
329 + if ( ! isset( $response['success'] ) || $response['success'] ) {
330 + return $errors;
321 331 }
322 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 +
323 342 return $errors;
324 343 }
325 344
326 345 /**
327 346 * @param float $score
347 + *
328 348 * @return void
329 349 */
330 350 private function set_score( $score ) {
331 351 global $frm_vars;
352 +
332 353 if ( ! isset( $frm_vars['captcha_scores'] ) ) {
333 354 $frm_vars['captcha_scores'] = array();
334 355 }
356 +
335 357 $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
358 +
336 359 if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
337 360 $frm_vars['captcha_scores'][ $form_id ] = $score;
338 361 }
339 362 }
@@ -339,8 +362,9 @@
339 362 }
340 363
341 364 /**
342 365 * @param array $args
366 + *
343 367 * @return array
344 368 */
345 369 public function validate( $args ) {
346 370 if ( ! $this->should_validate() ) {
@@ -347,8 +371,9 @@
347 371 return array();
348 372 }
349 373
350 374 $missing_token = ! self::post_data_includes_token();
375 +
351 376 if ( $missing_token ) {
352 377 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
353 378 }
354 379
@@ -382,20 +407,21 @@
382 407 * @return bool
383 408 */
384 409 protected function should_validate() {
385 410 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
411 +
386 412 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
387 413 return false;
388 414 }
389 415
390 - // don't require the captcha if it shouldn't be shown
416 + // Don't require the captcha if it shouldn't be shown
391 417 return self::should_show_captcha();
392 418 }
393 419
394 420 /**
395 - * @param FrmSettings $frm_settings
421 + * @return array|WP_Error
396 422 */
397 - protected function send_api_check( $frm_settings ) {
423 + protected function send_api_check() {
398 424 $captcha_settings = FrmCaptchaFactory::get_settings_object();
399 425 $arg_array = array(
400 426 'body' => array(
401 427 'secret' => $captcha_settings->secret,
@@ -413,13 +439,14 @@
413 439 * @since 6.0
414 440 *
415 441 * @param array $values
416 442 *
417 - * @return array $values
443 + * @return array Values.
418 444 */
419 445 public static function update_field_name( $values ) {
420 446 if ( $values['type'] === 'captcha' ) {
421 447 $name = $values['name'];
448 +
422 449 if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
423 450 $values['name'] = __( 'Captcha', 'formidable' );
424 451 }
425 452 }
@@ -424,15 +451,6 @@
424 451 }
425 452 }
426 453
427 454 return $values;
428 - }
429 -
430 - /**
431 - * @param FrmSettings $frm_settings
432 - * @return string
433 - */
434 - protected function captcha_size( $frm_settings ) {
435 - _deprecated_function( __METHOD__, '6.8.4' );
436 - return 'normal';
437 455 }
438 456 }