'https://www.google.com/recaptcha/api.js',
'recaptcha_v3' => 'https://www.google.com/recaptcha/api.js?render=' . rawurlencode( (string) Settings::get( 'recaptcha_v3.site' ) ),
'hcaptcha' => 'https://js.hcaptcha.com/1/api.js',
'turnstile' => 'https://challenges.cloudflare.com/turnstile/v0/api.js', // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Captcha provider script must load from the provider; documented in readme.
);
if ( isset( $urls[ $type ] ) ) {
wp_enqueue_script( 'fwren-captcha-' . $type, $urls[ $type ], array(), null, array( 'in_footer' => true, 'strategy' => 'defer' ) ); // phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion
}
}
/** @return string Captcha widget markup. */
public static function captcha_markup( $form_id ) {
$type = self::captcha_type( $form_id );
$site = (string) Settings::get( $type . '.site' );
switch ( $type ) {
case 'recaptcha_v2':
return '
';
case 'recaptcha_v3':
return '';
case 'hcaptcha':
return '';
case 'turnstile':
return '';
}
return '';
}
/**
* Run every check.
*
* @return true|\WP_Error
*/
public static function check( $form_id, array $settings, array $data ) {
$sp = $settings['spam'];
$msg = __( 'Your submission could not be verified. Please reload the page and try again.', 'fieldwren' );
if ( ! empty( $sp['honeypot'] ) && '' !== (string) ( $data[ self::honeypot_name( $form_id ) ] ?? '' ) ) {
return new \WP_Error( 'fwren_honeypot', $msg );
}
if ( (int) $sp['time_trap'] > 0 ) {
$tok = explode( '.', (string) ( $data['_fwren_t'] ?? '' ) );
$valid = 2 === count( $tok ) && hash_equals( substr( hash_hmac( 'sha256', $tok[0], wp_salt( 'nonce' ) ), 0, 16 ), $tok[1] );
if ( ! $valid || time() - (int) $tok[0] < (int) $sp['time_trap'] ) {
return new \WP_Error( 'fwren_too_fast', $msg );
}
}
if ( ! empty( $sp['rate_limit']['enabled'] ) ) {
$key = 'fwren_rl_' . md5( Submission::ip() . '|' . $form_id );
$count = (int) get_transient( $key );
if ( $count >= max( 1, (int) $sp['rate_limit']['count'] ) ) {
return new \WP_Error( 'fwren_rate', __( 'Too many submissions. Please wait a few minutes and try again.', 'fieldwren' ) );
}
set_transient( $key, $count + 1, max( 1, (int) $sp['rate_limit']['minutes'] ) * MINUTE_IN_SECONDS );
}
$type = self::captcha_type( $form_id, $settings );
if ( 'none' !== $type ) {
$ok = self::verify_captcha( $type, $data );
if ( true !== $ok ) {
return new \WP_Error( 'fwren_captcha', __( 'Captcha verification failed. Please try again.', 'fieldwren' ) );
}
}
return apply_filters( 'fwren/spam_check', true, $form_id, $data );
}
/** @return bool Verify a captcha response with the provider. */
private static function verify_captcha( $type, array $data ) {
$endpoints = array(
'recaptcha_v2' => array( 'https://www.google.com/recaptcha/api/siteverify', 'g-recaptcha-response' ),
'recaptcha_v3' => array( 'https://www.google.com/recaptcha/api/siteverify', 'g-recaptcha-response' ),
'hcaptcha' => array( 'https://hcaptcha.com/siteverify', 'h-captcha-response' ),
'turnstile' => array( 'https://challenges.cloudflare.com/turnstile/v0/siteverify', 'cf-turnstile-response' ), // phpcs:ignore PluginCheck.CodeAnalysis.Offloading.OffloadedContent -- Captcha verification endpoint; documented in readme.
);
if ( ! isset( $endpoints[ $type ] ) ) {
return true;
}
$token = (string) ( $data[ $endpoints[ $type ][1] ] ?? '' );
if ( '' === $token ) {
return false;
}
$res = wp_remote_post(
$endpoints[ $type ][0],
array(
'timeout' => 10,
'body' => array(
'secret' => (string) Settings::get( $type . '.secret' ),
'response' => $token,
'remoteip' => Submission::ip(),
),
)
);
if ( is_wp_error( $res ) ) {
return apply_filters( 'fwren/captcha_fail_open', false, $res );
}
$body = json_decode( wp_remote_retrieve_body( $res ), true );
if ( empty( $body['success'] ) ) {
return false;
}
if ( 'recaptcha_v3' === $type && isset( $body['score'] ) && (float) $body['score'] < (float) Settings::get( 'recaptcha_v3.threshold', 0.5 ) ) {
return false;
}
return true;
}
/** @return bool Akismet verdict (entries flagged spam are stored but run no actions). */
public static function akismet_is_spam( $form_id, array $settings, array $values, array $schema ) {
if ( empty( $settings['spam']['akismet'] ) && ! Settings::get( 'akismet' ) ) {
return false;
}
if ( ! class_exists( '\Akismet' ) || ! method_exists( '\Akismet', 'http_post' ) || ! \Akismet::get_api_key() ) {
return false;
}
$email = '';
$name = '';
$text = array();
foreach ( $values as $k => $v ) {
$t = $schema['fields'][ $k ]['type'] ?? '';
if ( 'email' === $t && ! $email ) {
$email = $v;
} elseif ( 'name' === $t && ! $name ) {
$name = is_array( $v ) ? implode( ' ', $v ) : $v;
} elseif ( in_array( $t, array( 'textarea', 'text' ), true ) ) {
$text[] = (string) $v;
}
}
$q = array(
'blog' => home_url(),
'user_ip' => Submission::ip(),
'user_agent' => isset( $_SERVER['HTTP_USER_AGENT'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_USER_AGENT'] ) ) : '',
'referrer' => wp_get_referer(),
'comment_type' => 'contact-form',
'comment_author' => $name,
'comment_author_email' => $email,
'comment_content' => implode( "\n", $text ),
);
$res = \Akismet::http_post( http_build_query( $q ), 'comment-check' );
return isset( $res[1] ) && 'true' === trim( $res[1] );
}
}