PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.5.4
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.5.4
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 -203 6.26 → 5.5.4 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,11 @@
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 - if ( $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ) {
32 + if ( ! self::should_show_captcha() ) {
33 + $image_name = 'captcha_not_setup';
34 + } elseif ( $active_captcha === 'recaptcha' && $frm_settings->re_type === 'v3' ) {
35 35 $image_name = 'recaptcha_v3';
36 36 } else {
37 37 $image_name = $active_captcha;
38 38 }
@@ -43,16 +43,13 @@
43 43 /**
44 44 * @return array
45 45 */
46 46 protected function field_settings_for_type() {
47 - $settings = FrmCaptchaFactory::get_settings_object();
48 47 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,
48 + 'required' => false,
49 + 'invalid' => true,
50 + 'captcha_size' => true,
51 + 'default' => false,
55 52 );
56 53 }
57 54
58 55 /**
@@ -77,78 +74,51 @@
77 74 );
78 75 }
79 76
80 77 /**
81 - * Replace the "for" attribute for captcha field so it matches the response ID.
78 + * Remove the "for" attribute for captcha
82 79 *
83 - * @param array $args
80 + * @param array $args
84 81 * @param string $html
85 82 *
86 83 * @return string
87 84 */
88 85 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 );
86 + $frm_settings = FrmAppHelper::get_settings();
87 + $replace_response = $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha-response' : 'h-captcha-response';
88 + $replaced_for = str_replace( ' for="field_[key]"', ' for="' . $replace_response . '"', $html );
89 +
90 + return $replaced_for;
91 91 }
92 92
93 - /**
94 - * @param array $args
95 - * @param array $shortcode_atts
96 - *
97 - * @return string
98 - */
99 93 public function front_field_input( $args, $shortcode_atts ) {
100 94 $frm_settings = FrmAppHelper::get_settings();
101 -
102 95 if ( ! self::should_show_captcha() ) {
103 96 return '';
104 97 }
105 98
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 - );
99 + $class_prefix = $this->class_prefix( $frm_settings );
100 + $captcha_class = $this->captcha_class( $frm_settings );
101 + $captcha_size = $this->captcha_size( $frm_settings );
102 + $allow_mutiple = $frm_settings->re_multi;
112 103
113 - if ( 'turnstile' === $frm_settings->active_captcha ) {
114 - $captcha_language = $this->get_captcha_language();
104 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
105 + $site_key = $frm_settings->pubkey;
106 + $recaptcha_options = '" data-size="' . esc_attr( $captcha_size ) . '" data-theme="' . esc_attr( $this->field['captcha_theme'] ) . '"';
107 + } else {
108 + $site_key = $frm_settings->hcaptcha_pubkey;
109 + }
115 110
116 - if ( $captcha_language ) {
117 - $div_attributes['data-language'] = $captcha_language;
118 - }
111 + $html = '<div id="' . esc_attr( $args['html_id'] ) . '" class="' . esc_attr( $class_prefix ) . $captcha_class . '" data-sitekey="' . esc_attr( $site_key ) . '"';
112 + $html .= ! empty( $recaptcha_options ) ? $recaptcha_options : '';
113 + if ( $captcha_size === 'invisible' && ! $allow_mutiple ) {
114 + $html .= ' data-callback="frmAfterRecaptcha"';
119 115 }
116 + $html .= '></div>';
120 117
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 118 return $html;
125 119 }
126 120
127 - /**
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 - * @return void
150 - */
151 121 protected function load_field_scripts( $args ) {
152 122 $api_js_url = $this->api_url();
153 123
154 124 wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
@@ -154,80 +124,42 @@
154 124 wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
155 125 wp_enqueue_script( 'captcha-api' );
156 126 }
157 127
158 - /**
159 - * Get the URL for the script JS that is loaded on the front end.
160 - *
161 - * @return string
162 - */
163 128 protected function api_url() {
164 129 $frm_settings = FrmAppHelper::get_settings();
165 - $active_mode = $frm_settings->active_captcha;
166 -
167 - if ( 'recaptcha' === $active_mode ) {
130 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
168 131 return $this->recaptcha_api_url( $frm_settings );
169 132 }
170 133
171 - if ( 'hcaptcha' === $active_mode ) {
172 - return $this->hcaptcha_api_url();
173 - }
174 -
175 - return $this->turnstile_api_url();
134 + return $this->hcaptcha_api_url();
176 135 }
177 136
178 - /**
179 - * @param FrmSettings $frm_settings
180 - *
181 - * @return string
182 - */
183 137 protected function recaptcha_api_url( $frm_settings ) {
184 138 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
185 139
186 - if ( $this->allow_multiple( $frm_settings ) ) {
140 + $allow_mutiple = $frm_settings->re_multi;
141 + if ( $allow_mutiple ) {
187 142 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
188 143 }
189 144
190 145 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
191 -
192 - if ( $lang ) {
146 + if ( ! empty( $lang ) ) {
193 147 $api_js_url .= '&hl=' . $lang;
194 148 }
195 149
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 150 $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
204 151
205 152 return $api_js_url;
206 153 }
207 154
208 - /**
209 - * @since 6.0
210 - *
211 - * @return string
212 - */
213 155 protected function hcaptcha_api_url() {
214 156 $api_js_url = 'https://js.hcaptcha.com/1/api.js';
215 157
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 158 /**
227 159 * Allows updating hcaptcha js api url.
228 160 *
229 - * @since 6.0
161 + * @since x.x
230 162 *
231 163 * @param string $api_js_url
232 164 */
233 165 $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
@@ -234,73 +166,40 @@
234 166
235 167 return $api_js_url;
236 168 }
237 169
238 - /**
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';
170 + protected function class_prefix( $frm_settings ) {
171 + if ( $this->allow_multiple( $frm_settings ) && $frm_settings->active_captcha === 'recaptcha' ) {
172 + $class_prefix = 'frm-';
173 + } else {
174 + $class_prefix = '';
175 + }
245 176
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 );
177 + return $class_prefix;
178 + }
254 179
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;
180 + protected function captcha_class( $frm_settings ) {
181 + return $frm_settings->active_captcha === 'recaptcha' ? 'g-recaptcha' : 'h-captcha';
264 182 }
265 183
266 - /**
267 - * @param FrmSettings $frm_settings
268 - *
269 - * @return string
270 - *
271 - * @psalm-return ''|'frm-'
272 - */
273 - protected function class_prefix( $frm_settings ) {
274 - return FrmCaptchaFactory::get_settings_object()->get_class_prefix( $this->allow_multiple( $frm_settings ) );
184 + protected function allow_multiple( $frm_settings ) {
185 + return $frm_settings->re_multi;
275 186 }
276 187
277 188 /**
278 - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility.
279 - *
280 189 * @return string
281 - *
282 - * @psalm-return 'g-recaptcha'|'h-captcha'
283 190 */
284 - protected function captcha_class( $frm_settings ) {
285 - $settings = FrmCaptchaFactory::get_settings_object();
286 - return $settings->get_element_class_name();
191 + protected function captcha_size( $frm_settings ) {
192 + if ( in_array( $frm_settings->re_type, array( 'invisible', 'v3' ), true ) ) {
193 + return 'invisible';
194 + }
195 + // for reverse compatibility
196 + return $this->field['captcha_size'] === 'default' ? 'normal' : $this->field['captcha_size'];
287 197 }
288 198
289 199 /**
290 - * @param FrmSettings $frm_settings
291 - *
292 - * @return bool
293 - */
294 - protected function allow_multiple( $frm_settings ) {
295 - return $frm_settings->re_multi;
296 - }
297 -
298 - /**
299 200 * @since 4.07
300 - *
301 201 * @param array $args
302 - *
303 202 * @return array
304 203 */
305 204 protected function validate_against_api( $args ) {
306 205 $errors = array();
@@ -334,9 +233,8 @@
334 233
335 234 if ( isset( $response['success'] ) && ! $response['success'] ) {
336 235 // What happens when the CAPTCHA was entered incorrectly
337 236 $invalid_message = FrmField::get_option( $this->field, 'invalid' );
338 -
339 237 if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
340 238 $invalid_message = '';
341 239 }
342 240 $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
@@ -346,20 +244,16 @@
346 244 }
347 245
348 246 /**
349 247 * @param float $score
350 - *
351 248 * @return void
352 249 */
353 250 private function set_score( $score ) {
354 251 global $frm_vars;
355 -
356 252 if ( ! isset( $frm_vars['captcha_scores'] ) ) {
357 253 $frm_vars['captcha_scores'] = array();
358 254 }
359 -
360 255 $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id'];
361 -
362 256 if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) {
363 257 $frm_vars['captcha_scores'][ $form_id ] = $score;
364 258 }
365 259 }
@@ -365,9 +259,8 @@
365 259 }
366 260
367 261 /**
368 262 * @param array $args
369 - *
370 263 * @return array
371 264 */
372 265 public function validate( $args ) {
373 266 if ( ! $this->should_validate() ) {
@@ -373,11 +266,11 @@
373 266 if ( ! $this->should_validate() ) {
374 267 return array();
375 268 }
376 269
377 - $missing_token = ! self::post_data_includes_token();
378 -
379 - if ( $missing_token ) {
270 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
271 + if ( ! isset( $_POST['g-recaptcha-response'] ) && ! isset( $_POST['h-captcha-response'] ) ) {
272 + // There was no captcha submitted.
380 273 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
381 274 }
382 275
383 276 return $this->validate_against_api( $args );
@@ -383,36 +276,22 @@
383 276 return $this->validate_against_api( $args );
384 277 }
385 278
386 279 /**
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 280 * @since 4.07
401 - *
402 281 * @return bool
403 282 */
404 283 public static function should_show_captcha() {
405 - $settings = FrmCaptchaFactory::get_settings_object();
406 - return $settings->has_pubkey();
284 + $frm_settings = FrmAppHelper::get_settings();
285 + if ( $frm_settings->active_captcha === 'recaptcha' ) {
286 + return ! empty( $frm_settings->pubkey );
287 + }
288 +
289 + return ! empty( $frm_settings->hcaptcha_pubkey );
407 290 }
408 291
409 - /**
410 - * @return bool
411 - */
412 292 protected function should_validate() {
413 293 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
414 -
415 294 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
416 295 return false;
417 296 }
418 297
@@ -419,16 +298,12 @@
419 298 // don't require the captcha if it shouldn't be shown
420 299 return self::should_show_captcha();
421 300 }
422 301
423 - /**
424 - * @param FrmSettings $frm_settings
425 - *
426 - * @return array|WP_Error
427 - */
428 302 protected function send_api_check( $frm_settings ) {
429 - $captcha_settings = FrmCaptchaFactory::get_settings_object();
430 - $arg_array = array(
303 + $captcha_settings = new FrmFieldCaptchaSettings( $frm_settings );
304 +
305 + $arg_array = array(
431 306 'body' => array(
432 307 'secret' => $captcha_settings->secret,
433 308 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
434 309 'remoteip' => FrmAppHelper::get_ip_address(),
@@ -440,9 +315,9 @@
440 315
441 316 /**
442 317 * Updates field name in page builder to the currently activated captcha if it is set to the default.
443 318 *
444 - * @since 6.0
319 + * @since x.x
445 320 *
446 321 * @param array $values
447 322 *
448 323 * @return array $values
@@ -449,23 +324,12 @@
449 324 */
450 325 public static function update_field_name( $values ) {
451 326 if ( $values['type'] === 'captcha' ) {
452 327 $name = $values['name'];
453 -
454 328 if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
455 - $values['name'] = __( 'Captcha', 'formidable' );
329 + $values['name'] = __( 'CAPTCHA', 'formidable' );
456 330 }
457 331 }
458 332
459 333 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 334 }
471 335 }