'; break; case 'recaptcha_v2': echo '
'; break; case 'recaptcha_v3': echo ''; break; } } /** * Verify the token sent with a form. * * @param string $form Form. * @param array $data Request data. * @return bool */ public static function verify( $form, $data ) { if ( ! self::enabled_for( $form ) ) { return true; } /** * Short-circuit captcha verification (return a boolean). * * @param null|bool $result Null to verify normally. * @param string $form Form. * @param array $data Request data. */ $pre = apply_filters( 'logvia_pre_verify_captcha', null, $form, $data ); if ( null !== $pre ) { return (bool) $pre; } $provider = self::provider(); $field = 'turnstile' === $provider ? 'cf-turnstile-response' : 'g-recaptcha-response'; $token = isset( $data[ $field ] ) ? sanitize_text_field( wp_unslash( $data[ $field ] ) ) : ''; if ( '' === $token ) { return false; } // Server-to-server answer check; nothing is loaded in the browser. $url = 'turnstile' === $provider ? 'https://challenges.cloudflare.com/turnstile/v0/siteverify' : 'https://www.google.com/recaptcha/api/siteverify'; // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent $response = wp_remote_post( $url, array( 'timeout' => 10, 'body' => array( 'secret' => trim( (string) Logvia_Settings::get( 'captcha_secret' ) ), 'response' => $token, 'remoteip' => Logvia_Rate_Limiter::ip(), ), ) ); if ( is_wp_error( $response ) ) { /** * Whether to let visitors through when the captcha service cannot be reached. * * @param bool $allow Allow. */ return (bool) apply_filters( 'logvia_captcha_fail_open', false ); } $body = json_decode( wp_remote_retrieve_body( $response ), true ); if ( empty( $body['success'] ) ) { return false; } if ( 'recaptcha_v3' === $provider ) { $score = isset( $body['score'] ) ? (float) $body['score'] : 0.0; $min = (float) Logvia_Settings::get( 'recaptcha_threshold' ); if ( $score < $min ) { return false; } if ( isset( $body['action'] ) && 'logvia_' . $form !== $body['action'] ) { return false; } } return true; } }