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