diff --git a/src/funcs/recaptcha.php b/src/funcs/recaptcha.php new file mode 100644 index 0000000..df19a60 --- /dev/null +++ b/src/funcs/recaptcha.php @@ -0,0 +1,96 @@ + [ + 'secret' => $secret_key, + 'response' => $token, + 'remoteip' => $_SERVER['REMOTE_ADDR'] ?? '', + ], + ]); + + if (is_wp_error($response)) return false; + + $data = json_decode(wp_remote_retrieve_body($response), true); + return !empty($data['success']) && $data['score'] >= 0.5; +} + +add_filter('authenticate', 'fwp_verify_recaptcha_on_login', 30, 3); +function fwp_verify_recaptcha_on_login($user, $username, $password) { + $enabled_forms = get_option('fwp_recaptcha_enabled_forms', []); + if (!in_array('login', $enabled_forms)) return $user; + + if (!isset($_POST['g-recaptcha-response']) || !fwp_verify_recaptcha_token($_POST['g-recaptcha-response'])) { + return new WP_Error('recaptcha_failed', __('reCAPTCHA verification failed.')); + } + + return $user; +} + +add_filter('registration_errors', 'fwp_verify_recaptcha_on_register', 10, 3); +function fwp_verify_recaptcha_on_register($errors, $sanitized_user_login, $user_email) { + $enabled_forms = get_option('fwp_recaptcha_enabled_forms', []); + if (!in_array('register', $enabled_forms)) return $errors; + + if (!isset($_POST['g-recaptcha-response']) || !fwp_verify_recaptcha_token($_POST['g-recaptcha-response'])) { + $errors->add('recaptcha_failed', __('reCAPTCHA verification failed.')); + } + + return $errors; +} + +add_action('pre_comment_on_post', 'fwp_verify_recaptcha_on_comment'); +function fwp_verify_recaptcha_on_comment($comment_post_ID) { + $enabled_forms = get_option('fwp_recaptcha_enabled_forms', []); + if (!in_array('comment', $enabled_forms)) return; + + if (!isset($_POST['g-recaptcha-response']) || !fwp_verify_recaptcha_token($_POST['g-recaptcha-response'])) { + wp_die(__('reCAPTCHA verification failed.'), __('Comment Blocked'), [ + 'back_link' => true + ]); + } +} + diff --git a/src/funcs/settings.php b/src/funcs/settings.php index 6cd4aa6..d14ff68 100644 --- a/src/funcs/settings.php +++ b/src/funcs/settings.php @@ -46,6 +46,9 @@ function fwp_render_recaptcha_settings_page() { ?>

FWP reCAPTCHA Settings

+

+ You can find more information about Google Recaptcha v3 in Googles Documentation. You can easily register new keys using the Recaptcha Admin Panels creation page or manage your keys on Googles Recaptcha management portal. +

diff --git a/src/fwp-recaptcha.php b/src/fwp-recaptcha.php index 455afa0..4fb4300 100644 --- a/src/fwp-recaptcha.php +++ b/src/fwp-recaptcha.php @@ -11,8 +11,5 @@ defined('ABSPATH') or exit; -// Load settings and admin menu require_once plugin_dir_path(__FILE__) . 'funcs/settings.php'; - -// Your future frontend logic (hook into forms) would go here - +require_once plugin_dir_path(__FILE__) . 'funcs/recaptcha.php';