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