| @@ -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,44 +107,15 @@ | ||
| 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 | 121 | $api_js_url = $this->api_url(); |
| @@ -168,32 +145,29 @@ | ||
| 168 | 145 | } |
| 169 | 146 | |
| 170 | 147 | /** |
| 171 | 148 | * @param FrmSettings $frm_settings |
| 172 | - * | |
| 173 | 149 | * @return string |
| 174 | 150 | */ |
| 175 | 151 | protected function recaptcha_api_url( $frm_settings ) { |
| 176 | 152 | $api_js_url = 'https://www.google.com/recaptcha/api.js?'; |
| 177 | 153 | |
| 178 | - if ( $this->allow_multiple( $frm_settings ) ) { | |
| 154 | + $allow_multiple = $frm_settings->re_multi; | |
| 155 | + if ( $allow_multiple ) { | |
| 179 | 156 | $api_js_url .= '&onload=frmRecaptcha&render=explicit'; |
| 180 | 157 | } |
| 181 | 158 | |
| 182 | 159 | $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field ); |
| 183 | - | |
| 184 | 160 | if ( $lang ) { |
| 185 | 161 | $api_js_url .= '&hl=' . $lang; |
| 186 | 162 | } |
| 187 | 163 | |
| 188 | - // Since this URL initially ends with ? and we never use add_query_arg, remove the extra | |
| 189 | - // & that appears immediately after the ? | |
| 190 | - $api_js_url = str_replace( '?&', '?', $api_js_url ); | |
| 191 | - | |
| 192 | 164 | /** |
| 193 | 165 | * @param string $api_js_url |
| 194 | 166 | */ |
| 195 | - 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; | |
| 196 | 170 | } |
| 197 | 171 | |
| 198 | 172 | /** |
| 199 | 173 | * @since 6.0 |
| @@ -201,18 +175,9 @@ | ||
| 201 | 175 | * @return string |
| 202 | 176 | */ |
| 203 | 177 | protected function hcaptcha_api_url() { |
| 204 | 178 | $api_js_url = 'https://js.hcaptcha.com/1/api.js'; |
| 205 | - $lang = $this->get_captcha_language(); | |
| 206 | 179 | |
| 207 | - if ( $lang ) { | |
| 208 | - // 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. | |
| 209 | - $lang_parts = explode( '-', $lang ); | |
| 210 | - $api_js_url .= '?hl=' . $lang_parts[0]; | |
| 211 | - } | |
| 212 | - | |
| 213 | - $api_js_url = add_query_arg( 'onload', 'frmHcaptcha', $api_js_url ); | |
| 214 | - | |
| 215 | 180 | /** |
| 216 | 181 | * Allows updating hcaptcha js api url. |
| 217 | 182 | * |
| 218 | 183 | * @since 6.0 |
| @@ -218,9 +183,11 @@ | ||
| 218 | 183 | * @since 6.0 |
| 219 | 184 | * |
| 220 | 185 | * @param string $api_js_url |
| 221 | 186 | */ |
| 222 | - 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; | |
| 223 | 190 | } |
| 224 | 191 | |
| 225 | 192 | /** |
| 226 | 193 | * @since 6.8.4 |
| @@ -240,13 +207,15 @@ | ||
| 240 | 207 | $api_js_url = apply_filters( 'frm_turnstile_js_url', $api_js_url ); |
| 241 | 208 | |
| 242 | 209 | // Prevent render=explicit from happening twice in case someone patched |
| 243 | 210 | // the double rendering issue using the frm_turnstile_js_url hook. |
| 244 | - return str_replace( | |
| 211 | + $api_js_url = str_replace( | |
| 245 | 212 | '&render=explicit&render=explicit', |
| 246 | 213 | '&render=explicit', |
| 247 | 214 | $api_js_url |
| 248 | 215 | ); |
| 216 | + | |
| 217 | + return $api_js_url; | |
| 249 | 218 | } |
| 250 | 219 | |
| 251 | 220 | /** |
| 252 | 221 | * @param FrmSettings $frm_settings |
| @@ -255,13 +224,19 @@ | ||
| 255 | 224 | * |
| 256 | 225 | * @psalm-return ''|'frm-' |
| 257 | 226 | */ |
| 258 | 227 | protected function class_prefix( $frm_settings ) { |
| 259 | - 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; | |
| 260 | 235 | } |
| 261 | 236 | |
| 262 | 237 | /** |
| 263 | - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility. | |
| 238 | + * @param FrmSettings $frm_settings | |
| 264 | 239 | * |
| 265 | 240 | * @return string |
| 266 | 241 | * |
| 267 | 242 | * @psalm-return 'g-recaptcha'|'h-captcha' |
| @@ -270,13 +245,8 @@ | ||
| 270 | 245 | $settings = FrmCaptchaFactory::get_settings_object(); |
| 271 | 246 | return $settings->get_element_class_name(); |
| 272 | 247 | } |
| 273 | 248 | |
| 274 | - /** | |
| 275 | - * @param FrmSettings $frm_settings | |
| 276 | - * | |
| 277 | - * @return bool | |
| 278 | - */ | |
| 279 | 249 | protected function allow_multiple( $frm_settings ) { |
| 280 | 250 | return $frm_settings->re_multi; |
| 281 | 251 | } |
| 282 | 252 | |
| @@ -281,17 +251,15 @@ | ||
| 281 | 251 | } |
| 282 | 252 | |
| 283 | 253 | /** |
| 284 | 254 | * @since 4.07 |
| 285 | - * | |
| 286 | 255 | * @param array $args |
| 287 | - * | |
| 288 | 256 | * @return array |
| 289 | 257 | */ |
| 290 | 258 | protected function validate_against_api( $args ) { |
| 291 | 259 | $errors = array(); |
| 292 | 260 | $frm_settings = FrmAppHelper::get_settings(); |
| 293 | - $resp = $this->send_api_check(); | |
| 261 | + $resp = $this->send_api_check( $frm_settings ); | |
| 294 | 262 | $response = json_decode( wp_remote_retrieve_body( $resp ), true ); |
| 295 | 263 | |
| 296 | 264 | if ( is_wp_error( $resp ) ) { |
| 297 | 265 | $error_string = $resp->get_error_message(); |
| @@ -319,14 +287,12 @@ | ||
| 319 | 287 | |
| 320 | 288 | if ( isset( $response['success'] ) && ! $response['success'] ) { |
| 321 | 289 | // What happens when the CAPTCHA was entered incorrectly |
| 322 | 290 | $invalid_message = FrmField::get_option( $this->field, 'invalid' ); |
| 323 | - | |
| 324 | 291 | if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) { |
| 325 | 292 | $invalid_message = ''; |
| 326 | 293 | } |
| 327 | - | |
| 328 | - $errors[ 'field' . $args['id'] ] = $invalid_message === '' ? $frm_settings->re_msg : $invalid_message; | |
| 294 | + $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message ); | |
| 329 | 295 | } |
| 330 | 296 | |
| 331 | 297 | return $errors; |
| 332 | 298 | } |
| @@ -332,20 +298,16 @@ | ||
| 332 | 298 | } |
| 333 | 299 | |
| 334 | 300 | /** |
| 335 | 301 | * @param float $score |
| 336 | - * | |
| 337 | 302 | * @return void |
| 338 | 303 | */ |
| 339 | 304 | private function set_score( $score ) { |
| 340 | 305 | global $frm_vars; |
| 341 | - | |
| 342 | 306 | if ( ! isset( $frm_vars['captcha_scores'] ) ) { |
| 343 | 307 | $frm_vars['captcha_scores'] = array(); |
| 344 | 308 | } |
| 345 | - | |
| 346 | 309 | $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id']; |
| 347 | - | |
| 348 | 310 | if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) { |
| 349 | 311 | $frm_vars['captcha_scores'][ $form_id ] = $score; |
| 350 | 312 | } |
| 351 | 313 | } |
| @@ -351,9 +313,8 @@ | ||
| 351 | 313 | } |
| 352 | 314 | |
| 353 | 315 | /** |
| 354 | 316 | * @param array $args |
| 355 | - * | |
| 356 | 317 | * @return array |
| 357 | 318 | */ |
| 358 | 319 | public function validate( $args ) { |
| 359 | 320 | if ( ! $this->should_validate() ) { |
| @@ -360,9 +321,8 @@ | ||
| 360 | 321 | return array(); |
| 361 | 322 | } |
| 362 | 323 | |
| 363 | 324 | $missing_token = ! self::post_data_includes_token(); |
| 364 | - | |
| 365 | 325 | if ( $missing_token ) { |
| 366 | 326 | return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) ); |
| 367 | 327 | } |
| 368 | 328 | |
| @@ -396,9 +356,8 @@ | ||
| 396 | 356 | * @return bool |
| 397 | 357 | */ |
| 398 | 358 | protected function should_validate() { |
| 399 | 359 | $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 400 | - | |
| 401 | 360 | if ( FrmAppHelper::is_admin() || $is_hidden_field ) { |
| 402 | 361 | return false; |
| 403 | 362 | } |
| 404 | 363 | |
| @@ -406,11 +365,11 @@ | ||
| 406 | 365 | return self::should_show_captcha(); |
| 407 | 366 | } |
| 408 | 367 | |
| 409 | 368 | /** |
| 410 | - * @return array|WP_Error | |
| 369 | + * @param FrmSettings $frm_settings | |
| 411 | 370 | */ |
| 412 | - protected function send_api_check() { | |
| 371 | + protected function send_api_check( $frm_settings ) { | |
| 413 | 372 | $captcha_settings = FrmCaptchaFactory::get_settings_object(); |
| 414 | 373 | $arg_array = array( |
| 415 | 374 | 'body' => array( |
| 416 | 375 | 'secret' => $captcha_settings->secret, |
| @@ -433,9 +392,8 @@ | ||
| 433 | 392 | */ |
| 434 | 393 | public static function update_field_name( $values ) { |
| 435 | 394 | if ( $values['type'] === 'captcha' ) { |
| 436 | 395 | $name = $values['name']; |
| 437 | - | |
| 438 | 396 | if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) { |
| 439 | 397 | $values['name'] = __( 'Captcha', 'formidable' ); |
| 440 | 398 | } |
| 441 | 399 | } |
| @@ -440,6 +398,15 @@ | ||
| 440 | 398 | } |
| 441 | 399 | } |
| 442 | 400 | |
| 443 | 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'; | |
| 444 | 411 | } |
| 445 | 412 | } |