| @@ -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; |
| @@ -77,9 +75,9 @@ | ||
| 77 | 75 | ); |
| 78 | 76 | } |
| 79 | 77 | |
| 80 | 78 | /** |
| 81 | - * Replace the "for" attribute for captcha field so it matches the response ID. | |
| 79 | + * Remove the "for" attribute for captcha | |
| 82 | 80 | * |
| 83 | 81 | * @param array $args |
| 84 | 82 | * @param string $html |
| 85 | 83 | * |
| @@ -85,21 +83,22 @@ | ||
| 85 | 83 | * |
| 86 | 84 | * @return string |
| 87 | 85 | */ |
| 88 | 86 | 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 ); | |
| 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; | |
| 91 | 92 | } |
| 92 | 93 | |
| 93 | 94 | /** |
| 94 | 95 | * @param array $args |
| 95 | 96 | * @param array $shortcode_atts |
| 96 | - * | |
| 97 | 97 | * @return string |
| 98 | 98 | */ |
| 99 | 99 | public function front_field_input( $args, $shortcode_atts ) { |
| 100 | 100 | $frm_settings = FrmAppHelper::get_settings(); |
| 101 | - | |
| 102 | 101 | if ( ! self::should_show_captcha() ) { |
| 103 | 102 | return ''; |
| 104 | 103 | } |
| 105 | 104 | |
| @@ -108,17 +107,8 @@ | ||
| 108 | 107 | 'id' => $args['html_id'], |
| 109 | 108 | 'class' => $this->class_prefix( $frm_settings ) . $this->captcha_class( $frm_settings ), |
| 110 | 109 | 'data-sitekey' => $settings->get_pubkey(), |
| 111 | 110 | ); |
| 112 | - | |
| 113 | - if ( 'turnstile' === $frm_settings->active_captcha ) { | |
| 114 | - $captcha_language = $this->get_captcha_language(); | |
| 115 | - | |
| 116 | - if ( $captcha_language ) { | |
| 117 | - $div_attributes['data-language'] = $captcha_language; | |
| 118 | - } | |
| 119 | - } | |
| 120 | - | |
| 121 | 111 | $div_attributes = $settings->add_front_end_element_attributes( $div_attributes, $this->field ); |
| 122 | 112 | $html = '<div ' . FrmAppHelper::array_to_html_params( $div_attributes ) . '></div>'; |
| 123 | 113 | |
| 124 | 114 | return $html; |
| @@ -124,29 +114,8 @@ | ||
| 124 | 114 | return $html; |
| 125 | 115 | } |
| 126 | 116 | |
| 127 | 117 | /** |
| 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 | 118 | * @return void |
| 150 | 119 | */ |
| 151 | 120 | protected function load_field_scripts( $args ) { |
| 152 | 121 | $api_js_url = $this->api_url(); |
| @@ -176,28 +145,23 @@ | ||
| 176 | 145 | } |
| 177 | 146 | |
| 178 | 147 | /** |
| 179 | 148 | * @param FrmSettings $frm_settings |
| 180 | - * | |
| 181 | 149 | * @return string |
| 182 | 150 | */ |
| 183 | 151 | protected function recaptcha_api_url( $frm_settings ) { |
| 184 | 152 | $api_js_url = 'https://www.google.com/recaptcha/api.js?'; |
| 185 | 153 | |
| 186 | - if ( $this->allow_multiple( $frm_settings ) ) { | |
| 154 | + $allow_multiple = $frm_settings->re_multi; | |
| 155 | + if ( $allow_multiple ) { | |
| 187 | 156 | $api_js_url .= '&onload=frmRecaptcha&render=explicit'; |
| 188 | 157 | } |
| 189 | 158 | |
| 190 | 159 | $lang = apply_filters( 'frm_recaptcha_lang', $frm_settings->re_lang, $this->field ); |
| 191 | - | |
| 192 | 160 | if ( $lang ) { |
| 193 | 161 | $api_js_url .= '&hl=' . $lang; |
| 194 | 162 | } |
| 195 | 163 | |
| 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 | 164 | /** |
| 201 | 165 | * @param string $api_js_url |
| 202 | 166 | */ |
| 203 | 167 | $api_js_url = apply_filters( 'frm_recaptcha_js_url', $api_js_url ); |
| @@ -212,18 +176,8 @@ | ||
| 212 | 176 | */ |
| 213 | 177 | protected function hcaptcha_api_url() { |
| 214 | 178 | $api_js_url = 'https://js.hcaptcha.com/1/api.js'; |
| 215 | 179 | |
| 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 | 180 | /** |
| 227 | 181 | * Allows updating hcaptcha js api url. |
| 228 | 182 | * |
| 229 | 183 | * @since 6.0 |
| @@ -270,13 +224,19 @@ | ||
| 270 | 224 | * |
| 271 | 225 | * @psalm-return ''|'frm-' |
| 272 | 226 | */ |
| 273 | 227 | protected function class_prefix( $frm_settings ) { |
| 274 | - 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; | |
| 275 | 235 | } |
| 276 | 236 | |
| 277 | 237 | /** |
| 278 | - * @param FrmSettings $frm_settings This isn't used anymore. It's only there for backwards compatibility. | |
| 238 | + * @param FrmSettings $frm_settings | |
| 279 | 239 | * |
| 280 | 240 | * @return string |
| 281 | 241 | * |
| 282 | 242 | * @psalm-return 'g-recaptcha'|'h-captcha' |
| @@ -285,13 +245,8 @@ | ||
| 285 | 245 | $settings = FrmCaptchaFactory::get_settings_object(); |
| 286 | 246 | return $settings->get_element_class_name(); |
| 287 | 247 | } |
| 288 | 248 | |
| 289 | - /** | |
| 290 | - * @param FrmSettings $frm_settings | |
| 291 | - * | |
| 292 | - * @return bool | |
| 293 | - */ | |
| 294 | 249 | protected function allow_multiple( $frm_settings ) { |
| 295 | 250 | return $frm_settings->re_multi; |
| 296 | 251 | } |
| 297 | 252 | |
| @@ -296,11 +251,9 @@ | ||
| 296 | 251 | } |
| 297 | 252 | |
| 298 | 253 | /** |
| 299 | 254 | * @since 4.07 |
| 300 | - * | |
| 301 | 255 | * @param array $args |
| 302 | - * | |
| 303 | 256 | * @return array |
| 304 | 257 | */ |
| 305 | 258 | protected function validate_against_api( $args ) { |
| 306 | 259 | $errors = array(); |
| @@ -334,9 +287,8 @@ | ||
| 334 | 287 | |
| 335 | 288 | if ( isset( $response['success'] ) && ! $response['success'] ) { |
| 336 | 289 | // What happens when the CAPTCHA was entered incorrectly |
| 337 | 290 | $invalid_message = FrmField::get_option( $this->field, 'invalid' ); |
| 338 | - | |
| 339 | 291 | if ( $invalid_message === __( 'The reCAPTCHA was not entered correctly', 'formidable' ) ) { |
| 340 | 292 | $invalid_message = ''; |
| 341 | 293 | } |
| 342 | 294 | $errors[ 'field' . $args['id'] ] = ( $invalid_message === '' ? $frm_settings->re_msg : $invalid_message ); |
| @@ -346,20 +298,16 @@ | ||
| 346 | 298 | } |
| 347 | 299 | |
| 348 | 300 | /** |
| 349 | 301 | * @param float $score |
| 350 | - * | |
| 351 | 302 | * @return void |
| 352 | 303 | */ |
| 353 | 304 | private function set_score( $score ) { |
| 354 | 305 | global $frm_vars; |
| 355 | - | |
| 356 | 306 | if ( ! isset( $frm_vars['captcha_scores'] ) ) { |
| 357 | 307 | $frm_vars['captcha_scores'] = array(); |
| 358 | 308 | } |
| 359 | - | |
| 360 | 309 | $form_id = is_object( $this->field ) ? $this->field->form_id : $this->field['form_id']; |
| 361 | - | |
| 362 | 310 | if ( ! isset( $frm_vars['captcha_scores'][ $form_id ] ) ) { |
| 363 | 311 | $frm_vars['captcha_scores'][ $form_id ] = $score; |
| 364 | 312 | } |
| 365 | 313 | } |
| @@ -365,9 +313,8 @@ | ||
| 365 | 313 | } |
| 366 | 314 | |
| 367 | 315 | /** |
| 368 | 316 | * @param array $args |
| 369 | - * | |
| 370 | 317 | * @return array |
| 371 | 318 | */ |
| 372 | 319 | public function validate( $args ) { |
| 373 | 320 | if ( ! $this->should_validate() ) { |
| @@ -374,9 +321,8 @@ | ||
| 374 | 321 | return array(); |
| 375 | 322 | } |
| 376 | 323 | |
| 377 | 324 | $missing_token = ! self::post_data_includes_token(); |
| 378 | - | |
| 379 | 325 | if ( $missing_token ) { |
| 380 | 326 | return array( 'field' . $args['id'] => __( 'The captcha is missing from this form', 'formidable' ) ); |
| 381 | 327 | } |
| 382 | 328 | |
| @@ -410,9 +356,8 @@ | ||
| 410 | 356 | * @return bool |
| 411 | 357 | */ |
| 412 | 358 | protected function should_validate() { |
| 413 | 359 | $is_hidden_field = apply_filters( 'frm_is_field_hidden', false, $this->field, wp_unslash( $_POST ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 414 | - | |
| 415 | 360 | if ( FrmAppHelper::is_admin() || $is_hidden_field ) { |
| 416 | 361 | return false; |
| 417 | 362 | } |
| 418 | 363 | |
| @@ -421,10 +366,8 @@ | ||
| 421 | 366 | } |
| 422 | 367 | |
| 423 | 368 | /** |
| 424 | 369 | * @param FrmSettings $frm_settings |
| 425 | - * | |
| 426 | - * @return array|WP_Error | |
| 427 | 370 | */ |
| 428 | 371 | protected function send_api_check( $frm_settings ) { |
| 429 | 372 | $captcha_settings = FrmCaptchaFactory::get_settings_object(); |
| 430 | 373 | $arg_array = array( |
| @@ -449,9 +392,8 @@ | ||
| 449 | 392 | */ |
| 450 | 393 | public static function update_field_name( $values ) { |
| 451 | 394 | if ( $values['type'] === 'captcha' ) { |
| 452 | 395 | $name = $values['name']; |
| 453 | - | |
| 454 | 396 | if ( in_array( $name, array( __( 'reCAPTCHA', 'formidable' ), __( 'hCaptcha', 'formidable' ) ), true ) ) { |
| 455 | 397 | $values['name'] = __( 'Captcha', 'formidable' ); |
| 456 | 398 | } |
| 457 | 399 | } |
| @@ -460,9 +402,8 @@ | ||
| 460 | 402 | } |
| 461 | 403 | |
| 462 | 404 | /** |
| 463 | 405 | * @param FrmSettings $frm_settings |
| 464 | - * | |
| 465 | 406 | * @return string |
| 466 | 407 | */ |
| 467 | 408 | protected function captcha_size( $frm_settings ) { |
| 468 | 409 | _deprecated_function( __METHOD__, '6.8.4' ); |