From d6f0eccae7ece8026618a19f2a1554b3a9b6b345 Mon Sep 17 00:00:00 2001 From: j Date: Wed, 18 Jun 2025 13:53:40 +1000 Subject: [PATCH] Add GPT generated code. Reasonably untested. --- src/funcs/recaptcha.php | 96 +++++++++++++++++++++++++++++++++++++++++ src/funcs/settings.php | 3 ++ src/fwp-recaptcha.php | 5 +-- 3 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 src/funcs/recaptcha.php 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';