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 +64 -171 6.266.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,9 +28,8 @@
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;
33 -
34 32 if ( $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ) {
35 33 $image_name = 'recaptcha_v3';
36 34 } else {
37 35 $image_name = $active_captcha;
@@ -43,16 +41,13 @@
43 41 /**
44 42 * @return array
45 43 */
46 44 protected function field_settings_for_type() {
47 - $settings = FrmCaptchaFactory::get_settings_object();
48 45 return array(
49 - 'required' => false,
50 - 'invalid' => true,
51 - 'captcha_size' => $settings->should_show_captcha_size(),
52 - 'captcha_theme' => $settings->should_show_captcha_theme(),
53 - 'captcha_theme_auto_option' => $settings->should_show_captcha_theme_auto_option(),
54 - 'default' => false,
46 + 'required' => false,
47 + 'invalid' => true,
48 + 'captcha_size' => true,
49 + 'default' => false,
55 50 );
56 51 }
57 52
58 53 /**
@@ -77,76 +72,55 @@
77 72 );
78 73 }
79 74
80 75 /**
81 - * Replace the "for" attribute for captcha field so it matches the response ID.
76 + * Remove the "for" attribute for captcha
82 77 *
83 - * @param array $args
78 + * @param array $args
84 79 * @param string $html
85 80 *
86 81 * @return string
87 82 */
88 83 protected function before_replace_html_shortcodes( $args, $html ) {
89 - $settings = FrmCaptchaFactory::get_settings_object();
90 - 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;
91 89 }
92 90
93 91 /**
94 - * @param array $args
95 - * @param array $shortcode_atts
96 - *
97 92 * @return string
98 93 */
99 94 public function front_field_input( $args, $shortcode_atts ) {
100 95 $frm_settings = FrmAppHelper::get_settings();
101 -
102 96 if ( ! self::should_show_captcha() ) {
103 97 return '';
104 98 }
105 99
106 - $settings = FrmCaptchaFactory::get_settings_object();
107 - $div_attributes = array(
108 - 'id' => $args['html_id'],
109 - 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
110 - 'data-sitekey' => $settings->get_pubkey(),
111 - );
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;
112 104
113 - if ( 'turnstile' === $frm_settings->active_captcha ) {
114 - $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 + }
115 111
116 - if ( $captcha_language ) {
117 - $div_attributes['data-language'] = $captcha_language;
118 - }
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"';
119 116 }
117 + $html .= '></div>';
120 118
121 - $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field );
122 - $html = '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
123 -
124 119 return $html;
125 120 }
126 121
127 122 /**
128 - * @since 6.25
129 - *
130 - * @return string
131 - */
132 - private function get_captcha_language() {
133 - /**
134 - * Allows updating the captcha language.
135 - *
136 - * @since 6.25
137 - *
138 - * @param string $lang
139 - * @param array $field
140 - */
141 - return apply_filters( 'frm_captcha_lang', get_bloginfo( 'language' ), $this->field );
142 - }
143 -
144 - /**
145 - * Load the captcha script.
146 - *
147 - * @param array $args
148 - *
149 123 * @return void
150 124 */
151 125 protected function load_field_scripts( $args ) {
152 126 $api_js_url = $this->api_url();
@@ -154,76 +128,41 @@
154 128 wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
155 129 wp_enqueue_script( 'captcha-api' );
156 130 }
157 131
158 - /**
159 - * Get the URL for the script JS that is loaded on the front end.
160 - *
161 - * @return string
162 - */
163 132 protected function api_url() {
164 133 $frm_settings = FrmAppHelper::get_settings();
165 - $active_mode = $frm_settings->active_captcha;
166 -
167 - if ( 'recaptcha' === $active_mode ) {
134 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
168 135 return $this->recaptcha_api_url( $frm_settings );
169 136 }
170 137
171 - if ( 'hcaptcha' === $active_mode ) {
172 - return $this->hcaptcha_api_url();
173 - }
174 -
175 - return $this->turnstile_api_url();
138 + return $this->hcaptcha_api_url();
176 139 }
177 140
178 141 /**
179 142 * @param FrmSettings $frm_settings
180 - *
181 - * @return string
182 143 */
183 144 protected function recaptcha_api_url( $frm_settings ) {
184 145 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
185 146
186 - if ( $this->allow_multiple( $frm_settings ) ) {
147 + $allow_mutiple = $frm_settings->re_multi;
148 + if ( $allow_mutiple ) {
187 149 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
188 150 }
189 151
190 152 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
191 -
192 - if ( $lang ) {
153 + if ( ! empty( $lang ) ) {
193 154 $api_js_url .= '&hl=' . $lang;
194 155 }
195 156
196 - // Since this URL initially ends with ? and we never use add_query_arg, remove the extra
197 - // & that appears immediately after the ?
198 - $api_js_url = str_replace( '?&', '?', $api_js_url );
199 -
200 - /**
201 - * @param string $api_js_url
202 - */
203 157 $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
204 158
205 159 return $api_js_url;
206 160 }
207 161
208 - /**
209 - * @since 6.0
210 - *
211 - * @return string
212 - */
213 162 protected function hcaptcha_api_url() {
214 163 $api_js_url = 'https://js.hcaptcha.com/1/api.js';
215 164
216 - $lang = $this->get_captcha_language();
217 -
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 165 /**
227 166 * Allows updating hcaptcha js api url.
228 167 *
229 168 * @since 6.0
@@ -235,36 +174,8 @@
235 174 return $api_js_url;
236 175 }
237 176
238 177 /**
239 - * @since 6.8.4
240 - *
241 - * @return string
242 - */
243 - protected function turnstile_api_url() {
244 - $api_js_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=frmTurnstile&render=explicit';
245 -
246 - /**
247 - * Allows updating hcaptcha js api url.
248 - *
249 - * @since 6.8.4
250 - *
251 - * @param string $api_js_url
252 - */
253 - $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url );
254 -
255 - // Prevent render=explicit from happening twice in case someone patched
256 - // the double rendering issue using the frm_turnstile_js_url hook.
257 - $api_js_url = str_replace(
258 - '&render=explicit&render=explicit',
259 - '&render=explicit',
260 - $api_js_url
261 - );
262 -
263 - return $api_js_url;
264 - }
265 -
266 - /**
267 178 * @param FrmSettings $frm_settings
268 179 *
269 180 * @return string
270 181 *
@@ -270,13 +181,19 @@
270 181 *
271 182 * @psalm-return ''|'frm-'
272 183 */
273 184 protected function class_prefix( $frm_settings ) {
274 - 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;
275 192 }
276 193
277 194 /**
278 - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility.
195 + * @param FrmSettings $frm_settings
279 196 *
280 197 * @return string
281 198 *
282 199 * @psalm-return 'g-recaptcha'|'h-captcha'
@@ -281,26 +198,31 @@
281 198 *
282 199 * @psalm-return 'g-recaptcha'|'h-captcha'
283 200 */
284 201 protected function captcha_class( $frm_settings ) {
285 - $settings = FrmCaptchaFactory::get_settings_object();
286 - return $settings->get_element_class_name();
202 + return $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha' : 'h-captcha';
287 203 }
288 204
205 + protected function allow_multiple( $frm_settings ) {
206 + return $frm_settings->re_multi;
207 + }
208 +
289 209 /**
210 + * @return string
211 + *
290 212 * @param FrmSettings $frm_settings
291 - *
292 - * @return bool
293 213 */
294 - protected function allow_multiple( $frm_settings ) {
295 - 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'];
296 220 }
297 221
298 222 /**
299 223 * @since 4.07
300 - *
301 224 * @param array $args
302 - *
303 225 * @return array
304 226 */
305 227 protected function validate_against_api( $args ) {
306 228 $errors = array();
@@ -334,9 +256,8 @@
334 256
335 257 if ( isset( $response['success'] ) && ! $response['success'] ) {
336 258 // What happens when the CAPTCHA was entered incorrectly
337 259 $invalid_message = FrmField::get_option( $this->field, 'invalid' );
338 -
339 260 if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
340 261 $invalid_message = '';
341 262 }
342 263 $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
@@ -346,20 +267,16 @@
346 267 }
347 268
348 269 /**
349 270 * @param float $score
350 - *
351 271 * @return void
352 272 */
353 273 private function set_score( $score ) {
354 274 global $frm_vars;
355 -
356 275 if ( ! isset( $frm_vars['captcha_scores'] ) ) {
357 276 $frm_vars['captcha_scores'] = array();
358 277 }
359 -
360 278 $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
361 -
362 279 if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
363 280 $frm_vars['captcha_scores'][ $form_id ] = $score;
364 281 }
365 282 }
@@ -365,9 +282,8 @@
365 282 }
366 283
367 284 /**
368 285 * @param array $args
369 - *
370 286 * @return array
371 287 */
372 288 public function validate( $args ) {
373 289 if ( ! $this->should_validate() ) {
@@ -373,11 +289,11 @@
373 289 if ( ! $this->should_validate() ) {
374 290 return array();
375 291 }
376 292
377 - $missing_token = ! self::post_data_includes_token();
378 -
379 - 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.
380 296 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
381 297 }
382 298
383 299 return $this->validate_against_api( $args );
@@ -383,28 +299,18 @@
383 299 return $this->validate_against_api( $args );
384 300 }
385 301
386 302 /**
387 - * @since 6.8.4
388 - *
389 - * @return bool
390 - */
391 - protected static function post_data_includes_token() {
392 - $settings = FrmCaptchaFactory::get_settings_object();
393 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
394 - return ! empty( $_POST[ $settings->token_field ] );
395 - }
396 -
397 - /**
398 - * Check if the active captcha type's public key is set.
399 - *
400 303 * @since 4.07
401 - *
402 304 * @return bool
403 305 */
404 306 public static function should_show_captcha() {
405 - $settings = FrmCaptchaFactory::get_settings_object();
406 - 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 );
407 313 }
408 314
409 315 /**
410 316 * @return bool
@@ -410,9 +316,8 @@
410 316 * @return bool
411 317 */
412 318 protected function should_validate() {
413 319 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
414 -
415 320 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
416 321 return false;
417 322 }
418 323
@@ -421,14 +326,13 @@
421 326 }
422 327
423 328 /**
424 329 * @param FrmSettings $frm_settings
425 - *
426 - * @return array|WP_Error
427 330 */
428 331 protected function send_api_check( $frm_settings ) {
429 - $captcha_settings = FrmCaptchaFactory::get_settings_object();
430 - $arg_array = array(
332 + $captcha_settings = new FrmFieldCaptchaSettings( $frm_settings );
333 +
334 + $arg_array = array(
431 335 'body' => array(
432 336 'secret' => $captcha_settings->secret,
433 337 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
434 338 'remoteip' => FrmAppHelper::get_ip_address(),
@@ -449,9 +353,8 @@
449 353 */
450 354 public static function update_field_name( $values ) {
451 355 if ( $values['type'] === 'captcha' ) {
452 356 $name = $values['name'];
453 -
454 357 if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
455 358 $values['name'] = __( 'Captcha', 'formidable' );
456 359 }
457 360 }
@@ -456,16 +359,6 @@
456 359 }
457 360 }
458 361
459 362 return $values;
460 - }
461 -
462 - /**
463 - * @param FrmSettings $frm_settings
464 - *
465 - * @return string
466 - */
467 - protected function captcha_size( $frm_settings ) {
468 - _deprecated_function( __METHOD__, '6.8.4' );
469 - return 'normal';
470 363 }
471 364 }