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