PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.2.02
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.2.02
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 +59 -251 6.25.15.2.02 View file →
@@ -21,36 +21,16 @@
21 21 return FrmAppHelper::plugin_path() . '/classes/views/frm-fields/back-end/field-captcha.php';
22 22 }
23 23
24 24 /**
25 - * Returns the image name for a captcha.
26 - *
27 - * @return string
28 - */
29 - public static function get_captcha_image_name() {
30 - $frm_settings = FrmAppHelper::get_settings();
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 - }
37 -
38 - return $image_name;
39 - }
40 -
41 - /**
42 25 * @return array
43 26 */
44 27 protected function field_settings_for_type() {
45 - $settings = FrmCaptchaFactory::get_settings_object();
46 28 return array(
47 - 'required' => false,
48 - 'invalid' => true,
49 - 'captcha_size' => $settings->should_show_captcha_size(),
50 - 'captcha_theme' => $settings->should_show_captcha_theme(),
51 - 'captcha_theme_auto_option' => $settings->should_show_captcha_theme_auto_option(),
52 - 'default' => false,
29 + 'required' => false,
30 + 'invalid' => true,
31 + 'captcha_size' => true,
32 + 'default' => false,
53 33 );
54 34 }
55 35
56 36 /**
@@ -75,209 +55,91 @@
75 55 );
76 56 }
77 57
78 58 /**
79 - * Replace the "for" attribute for captcha field so it matches the response ID.
59 + * Remove the "for" attribute for captcha
80 60 *
81 - * @param array $args
61 + * @param array $args
82 62 * @param string $html
83 63 *
84 64 * @return string
85 65 */
86 66 protected function before_replace_html_shortcodes( $args, $html ) {
87 - $settings = FrmCaptchaFactory::get_settings_object();
88 - return str_replace( ' for="field_[key]"', ' for="' . esc_attr( $settings->token_field ) . '"', $html );
67 + return str_replace( ' for="field_[key]"', ' for="g-recaptcha-response"', $html );
89 68 }
90 69
91 - /**
92 - * @param array $args
93 - * @param array $shortcode_atts
94 - * @return string
95 - */
96 70 public function front_field_input( $args, $shortcode_atts ) {
97 71 $frm_settings = FrmAppHelper::get_settings();
98 - if ( ! self::should_show_captcha() ) {
72 + if ( empty( $frm_settings->pubkey ) ) {
99 73 return '';
100 74 }
101 75
102 - $settings = FrmCaptchaFactory::get_settings_object();
103 - $div_attributes = array(
104 - 'id' => $args['html_id'],
105 - 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ),
106 - 'data-sitekey' => $settings->get_pubkey(),
107 - );
108 - if ( 'turnstile' === $frm_settings->active_captcha ) {
109 - $captcha_language = $this->get_captcha_language();
110 - if ( $captcha_language ) {
111 - $div_attributes['data-language'] = $captcha_language;
112 - }
76 + $class_prefix = $this->class_prefix();
77 + $captcha_size = $this->captcha_size();
78 + $allow_mutiple = $frm_settings->re_multi;
79 +
80 + $html = '<div id="' . esc_attr( $args['html_id'] ) . '" class="' . esc_attr( $class_prefix ) . 'g-recaptcha" data-sitekey="' . esc_attr( $frm_settings->pubkey ) . '" data-size="' . esc_attr( $captcha_size ) . '" data-theme="' . esc_attr( $this->field['captcha_theme'] ) . '"';
81 + if ( $captcha_size == 'invisible' && ! $allow_mutiple ) {
82 + $html .= ' data-callback="frmAfterRecaptcha"';
113 83 }
114 - $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field );
115 - $html = '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>';
84 + $html .= '></div>';
116 85
117 86 return $html;
118 87 }
119 88
120 - /**
121 - * @since 6.25
122 - *
123 - * @return string
124 - */
125 - private function get_captcha_language() {
126 - /**
127 - * Allows updating the captcha language.
128 - *
129 - * @since 6.25
130 - *
131 - * @param string $lang
132 - * @param array $field
133 - */
134 - return apply_filters( 'frm_captcha_lang', get_bloginfo( 'language' ), $this->field );
135 - }
136 -
137 - /**
138 - * @return void
139 - */
140 89 protected function load_field_scripts( $args ) {
141 90 $api_js_url = $this->api_url();
142 91
143 - wp_register_script( 'captcha-api', $api_js_url, array( 'formidable' ), '3', true );
144 - wp_enqueue_script( 'captcha-api' );
92 + wp_register_script( 'recaptcha-api', $api_js_url, array( 'formidable' ), '3', true );
93 + wp_enqueue_script( 'recaptcha-api' );
145 94 }
146 95
147 - /**
148 - * Get the URL for the script JS that is loaded on the front end.
149 - *
150 - * @return string
151 - */
152 96 protected function api_url() {
153 - $frm_settings = FrmAppHelper::get_settings();
154 - $active_mode = $frm_settings->active_captcha;
155 -
156 - if ( 'recaptcha' === $active_mode ) {
157 - return $this->recaptcha_api_url( $frm_settings );
158 - }
159 -
160 - if ( 'hcaptcha' === $active_mode ) {
161 - return $this->hcaptcha_api_url();
162 - }
163 -
164 - return $this->turnstile_api_url();
165 - }
166 -
167 - /**
168 - * @param FrmSettings $frm_settings
169 - * @return string
170 - */
171 - protected function recaptcha_api_url( $frm_settings ) {
172 97 $api_js_url = 'https://www.google.com/recaptcha/api.js?';
173 98
174 - if ( $this->allow_multiple( $frm_settings ) ) {
99 + $frm_settings = FrmAppHelper::get_settings();
100 + $allow_mutiple = $frm_settings->re_multi;
101 + if ( $allow_mutiple ) {
175 102 $api_js_url .= '&onload=frmRecaptcha&render=explicit';
176 103 }
177 104
178 105 $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field );
179 - if ( $lang ) {
106 + if ( ! empty( $lang ) ) {
180 107 $api_js_url .= '&hl=' . $lang;
181 108 }
182 109
183 - // Since this URL initially ends with ? and we never use add_query_arg, remove the extra
184 - // & that appears immediately after the ?
185 - $api_js_url = str_replace( '?&', '?', $api_js_url );
186 -
187 - /**
188 - * @param string $api_js_url
189 - */
190 - $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url );
191 -
192 - return $api_js_url;
110 + return apply_filters( 'frm_recaptcha_js_url', $api_js_url );
193 111 }
194 112
195 - /**
196 - * @since 6.0
197 - *
198 - * @return string
199 - */
200 - protected function hcaptcha_api_url() {
201 - $api_js_url = 'https://js.hcaptcha.com/1/api.js';
202 -
203 - $lang = $this->get_captcha_language();
204 - if ( $lang ) {
205 - // 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.
206 - $lang_parts = explode( '-', $lang );
207 - $api_js_url .= '?hl=' . $lang_parts[0];
113 + protected function class_prefix() {
114 + if ( $this->allow_multiple() ) {
115 + $class_prefix = 'frm-';
116 + } else {
117 + $class_prefix = '';
208 118 }
209 119
210 - $api_js_url = add_query_arg( 'onload', 'frmHcaptcha', $api_js_url );
211 -
212 - /**
213 - * Allows updating hcaptcha js api url.
214 - *
215 - * @since 6.0
216 - *
217 - * @param string $api_js_url
218 - */
219 - $api_js_url = apply_filters( 'frm_hcaptcha_js_url', $api_js_url );
220 -
221 - return $api_js_url;
120 + return $class_prefix;
222 121 }
223 122
224 - /**
225 - * @since 6.8.4
226 - *
227 - * @return string
228 - */
229 - protected function turnstile_api_url() {
230 - $api_js_url = 'https://challenges.cloudflare.com/turnstile/v0/api.js?onload=frmTurnstile&render=explicit';
123 + protected function allow_multiple() {
124 + $frm_settings = FrmAppHelper::get_settings();
231 125
232 - /**
233 - * Allows updating hcaptcha js api url.
234 - *
235 - * @since 6.8.4
236 - *
237 - * @param string $api_js_url
238 - */
239 - $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url );
240 -
241 - // Prevent render=explicit from happening twice in case someone patched
242 - // the double rendering issue using the frm_turnstile_js_url hook.
243 - $api_js_url = str_replace(
244 - '&render=explicit&render=explicit',
245 - '&render=explicit',
246 - $api_js_url
247 - );
248 -
249 - return $api_js_url;
126 + return $frm_settings->re_multi;
250 127 }
251 128
252 129 /**
253 - * @param FrmSettings $frm_settings
254 - *
255 130 * @return string
256 - *
257 - * @psalm-return ''|'frm-'
258 131 */
259 - protected function class_prefix( $frm_settings ) {
260 - return FrmCaptchaFactory::get_settings_object()->get_class_prefix( $this->allow_multiple( $frm_settings ) );
132 + protected function captcha_size() {
133 + $frm_settings = FrmAppHelper::get_settings();
134 + if ( in_array( $frm_settings->re_type, array( 'invisible', 'v3' ), true ) ) {
135 + return 'invisible';
136 + }
137 + // for reverse compatibility
138 + return $this->field['captcha_size'] === 'default' ? 'normal' : $this->field['captcha_size'];
261 139 }
262 140
263 141 /**
264 - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility.
265 - *
266 - * @return string
267 - *
268 - * @psalm-return 'g-recaptcha'|'h-captcha'
269 - */
270 - protected function captcha_class( $frm_settings ) {
271 - $settings = FrmCaptchaFactory::get_settings_object();
272 - return $settings->get_element_class_name();
273 - }
274 -
275 - protected function allow_multiple( $frm_settings ) {
276 - return $frm_settings->re_multi;
277 - }
278 -
279 - /**
280 142 * @since 4.07
281 143 * @param array $args
282 144 * @return array
283 145 */
@@ -288,9 +150,9 @@
288 150 $response = json_decode( wp_remote_retrieve_body( $resp ), true );
289 151
290 152 if ( is_wp_error( $resp ) ) {
291 153 $error_string = $resp->get_error_message();
292 - $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your captcha', 'formidable' );
154 + $errors[ 'field' . $args['id'] ] = __( 'There was a problem verifying your recaptcha', 'formidable' );
293 155 $errors[ 'field' . $args['id'] ] .= ' ' . $error_string;
294 156 return $errors;
295 157 }
296 158
@@ -297,28 +159,23 @@
297 159 if ( ! is_array( $response ) ) {
298 160 return $errors;
299 161 }
300 162
301 - if ( $frm_settings->active_captcha === 'recaptcha' ) {
302 - if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
303 - $threshold = floatval( $frm_settings->re_threshold );
304 - $score = floatval( $response['score'] );
163 + if ( 'v3' === $frm_settings->re_type && array_key_exists( 'score', $response ) ) {
164 + $threshold = floatval( $frm_settings->re_threshold );
165 + $score = floatval( $response['score'] );
305 166
306 - $this->set_score( $score );
167 + $this->set_score( $score );
307 168
308 - if ( $score < $threshold ) {
309 - $response['success'] = false;
310 - }
169 + if ( $score < $threshold ) {
170 + $response['success'] = false;
311 171 }
312 172 }
313 173
314 174 if ( isset( $response['success'] ) && ! $response['success'] ) {
315 175 // What happens when the CAPTCHA was entered incorrectly
316 - $invalid_message = FrmField::get_option( $this->field, 'invalid' );
317 - if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) {
318 - $invalid_message = '';
319 - }
320 - $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message );
176 + $invalid_message = FrmField::get_option( $this->field, 'invalid' );
177 + $errors[ 'field' . $args['id'] ] = ( $invalid_message == '' ? $frm_settings->re_msg : $invalid_message );
321 178 }
322 179
323 180 return $errors;
324 181 }
@@ -346,10 +203,11 @@
346 203 if ( ! $this->should_validate() ) {
347 204 return array();
348 205 }
349 206
350 - $missing_token = ! self::post_data_includes_token();
351 - if ( $missing_token ) {
207 + // phpcs:ignore WordPress.Security.NonceVerification.Missing
208 + if ( ! isset( $_POST['g-recaptcha-response'] ) ) {
209 + // There was no captcha submitted.
352 210 return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) );
353 211 }
354 212
355 213 return $this->validate_against_api( $args );
@@ -355,33 +213,16 @@
355 213 return $this->validate_against_api( $args );
356 214 }
357 215
358 216 /**
359 - * @since 6.8.4
360 - *
361 - * @return bool
362 - */
363 - protected static function post_data_includes_token() {
364 - $settings = FrmCaptchaFactory::get_settings_object();
365 - // phpcs:ignore WordPress.Security.NonceVerification.Missing
366 - return ! empty( $_POST[ $settings->token_field ] );
367 - }
368 -
369 - /**
370 - * Check if the active captcha type's public key is set.
371 - *
372 217 * @since 4.07
373 - *
374 218 * @return bool
375 219 */
376 - public static function should_show_captcha() {
377 - $settings = FrmCaptchaFactory::get_settings_object();
378 - return $settings->has_pubkey();
220 + private function should_show_captcha() {
221 + $frm_settings = FrmAppHelper::get_settings();
222 + return ! empty( $frm_settings->pubkey );
379 223 }
380 224
381 - /**
382 - * @return bool
383 - */
384 225 protected function should_validate() {
385 226 $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing
386 227 if ( FrmAppHelper::is_admin() || $is_hidden_field ) {
387 228 return false;
@@ -387,52 +228,19 @@
387 228 return false;
388 229 }
389 230
390 231 // don't require the captcha if it shouldn't be shown
391 - return self::should_show_captcha();
232 + return $this->should_show_captcha();
392 233 }
393 234
394 - /**
395 - * @param FrmSettings $frm_settings
396 - */
397 235 protected function send_api_check( $frm_settings ) {
398 - $captcha_settings = FrmCaptchaFactory::get_settings_object();
399 - $arg_array = array(
236 + $arg_array = array(
400 237 'body' => array(
401 - 'secret' => $captcha_settings->secret,
402 - 'response' => FrmAppHelper::get_param( $captcha_settings->token_field, '', 'post', 'sanitize_text_field' ),
238 + 'secret' => $frm_settings->privkey,
239 + 'response' => FrmAppHelper::get_param( 'g-recaptcha-response', '', 'post', 'sanitize_text_field' ),
403 240 'remoteip' => FrmAppHelper::get_ip_address(),
404 241 ),
405 242 );
406 243
407 - return wp_remote_post( $captcha_settings->endpoint, $arg_array );
408 - }
409 -
410 - /**
411 - * Updates field name in page builder to the currently activated captcha if it is set to the default.
412 - *
413 - * @since 6.0
414 - *
415 - * @param array $values
416 - *
417 - * @return array $values
418 - */
419 - public static function update_field_name( $values ) {
420 - if ( $values['type'] === 'captcha' ) {
421 - $name = $values['name'];
422 - if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) {
423 - $values['name'] = __( 'Captcha', 'formidable' );
424 - }
425 - }
426 -
427 - return $values;
428 - }
429 -
430 - /**
431 - * @param FrmSettings $frm_settings
432 - * @return string
433 - */
434 - protected function captcha_size( $frm_settings ) {
435 - _deprecated_function( __METHOD__, '6.8.4' );
436 - return 'normal';
244 + return wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', $arg_array );
437 245 }
438 246 }