Plugin Directory

Changeset 3338594


Ignore:
Timestamp:
08/03/2025 07:26:07 PM (8 months ago)
Author:
gswebdev
Message:

Fixed the captcha bug in the login forms

Location:
codenitive-captcha
Files:
16 added
4 edited

Legend:

Unmodified
Added
Removed
  • codenitive-captcha/trunk/codenitive-captcha.php

    r3334736 r3338594  
    44* Plugin URI:  https://wordpress.org/plugins/codenitive-captcha
    55* Description: Enhance your website’s security by integrating CAPTCHA verification into essential WordPress, WooCommerce, Contact form 7 (cf7) forms. This plugin helps prevent spam, bots, and unauthorized access by adding CAPTCHA challenges to key areas such as login, registration, password reset, checkout, and more. With built-in support for Google reCAPTCHA (v2), this plugin provides a seamless way to protect both the WordPress core and WooCommerce without disrupting the user experience.
    6 * Version: 1.0.4
     6* Version: 1.0.5
    77* Requires at least: 5.6
    88* Requires PHP:      7.4
  • codenitive-captcha/trunk/includes/class-captcha-config.php

    r3334966 r3338594  
    176176            ), $url );
    177177
    178             \wp_enqueue_script( 'codenitcaptcha-recaptcha-js', CODENITCAPTCHA_PLUGIN_DIR_URL . 'assets/js/scripts.js', array(), '1.0.9', true );
     178            \wp_enqueue_script( 'codenitcaptcha-recaptcha-js', CODENITCAPTCHA_PLUGIN_DIR_URL . 'assets/js/scripts.js', array(), CODENITCAPTCHA_VERSION, true );
    179179            \wp_enqueue_script( 'google-recaptcha', $url, array( 'codenitcaptcha-recaptcha-js' ), CODENITCAPTCHA_VERSION, true );
    180180
    181             wp_localize_script('codenitcaptcha-recaptcha-js', 'CodenitCaptchaData', [
     181            \wp_localize_script('codenitcaptcha-recaptcha-js', 'CodenitCaptchaData', [
    182182                'siteKey' => $this->get_site_key_v2(),
    183183            ]);
     
    191191        switch ( $message ) {
    192192            case 'captcha_required':
    193                 $output = __( 'The CAPTCHA was incorrect. Please try again.', 'codenitive-captcha' );
     193                $output = __( 'The CAPTCHA is required. Please try again.', 'codenitive-captcha' );
    194194                break;
    195195            case 'captcha_invalid':
    196                 $output = __( 'The CAPTCHA was incorrect. Please try again.', 'codenitive-captcha' );
     196                $output = __( 'The CAPTCHA was invalid. Please try again.', 'codenitive-captcha' );
    197197                break;
    198198            case 'nonce_invalid':
     
    243243        }
    244244
    245         if(isset($_POST['g-recaptcha-response'])){
    246            
    247             $response = \sanitize_text_field( \wp_unslash( $_POST['g-recaptcha-response'] ) );
    248 
    249             if (empty($response)) {
    250                 return array(
    251                     'status' => 'error',
    252                     'message' => 'captcha_required'
    253                 );
     245        //error_log(print_r($_POST['g-recaptcha-response'], true));
     246       
     247        if (!isset($_POST['g-recaptcha-response'])) {
     248            return array(
     249                'status' => 'error',
     250                'message' => 'captcha_required'
     251            );
     252        }
     253       
     254        $response = sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) );
     255       
     256        if (empty($response)) {
     257            return array(
     258                'status' => 'error',
     259                'message' => 'captcha_required'
     260            );
     261        }
     262
     263        $remoteip = '';
     264
     265        if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
     266            // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
     267            $remoteip = filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP );
     268
     269            // If it's not a valid IP, fall back to empty string
     270            if ( false === $remoteip ) {
     271                $remoteip = '';
    254272            }
    255 
    256             $remoteip = '';
    257 
    258             if ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
    259                 // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash
    260                 $remoteip = \filter_var( $_SERVER['REMOTE_ADDR'], FILTER_VALIDATE_IP );
    261 
    262                 // If it's not a valid IP, fall back to empty string
    263                 if ( false === $remoteip ) {
    264                     $remoteip = '';
    265                 }
    266             }
    267 
    268             $verify = \wp_remote_post('https://www.google.com/recaptcha/api/siteverify', [
    269                 'body' => [
    270                     'secret' => $secret,
    271                     'response' => $response,
    272                     'remoteip' => $remoteip
    273                 ]
    274             ]);
    275 
    276             if (\is_wp_error($verify)) {
    277                 return array(
    278                     'status' => 'error',
    279                     'message' => 'verify_invalid'
    280                 );
    281             }
    282 
    283             $result = \json_decode(\wp_remote_retrieve_body($verify));
    284 
    285             if (empty($result->success)) {
    286                 return array(
    287                     'status' => 'error',
    288                     'message' => 'captcha_invalid'
    289                 );
    290             }
     273        }
     274
     275        $verify = wp_remote_post('https://www.google.com/recaptcha/api/siteverify', [
     276            'body' => [
     277                'secret' => $secret,
     278                'response' => $response,
     279                'remoteip' => $remoteip
     280            ]
     281        ]);
     282
     283        if (is_wp_error($verify)) {
     284            return array(
     285                'status' => 'error',
     286                'message' => 'verify_invalid'
     287            );
     288        }
     289
     290        $result = json_decode(wp_remote_retrieve_body($verify));
     291       
     292        //error_log(print_r($result, true));
     293       
     294        if (empty($result->success)) {
     295            return array(
     296                'status' => 'error',
     297                'message' => 'captcha_invalid'
     298            );
    291299        }
    292300    }
  • codenitive-captcha/trunk/includes/class-forms.php

    r3334966 r3338594  
    3939            if ( $this->config->get_wcc_login() == 1 ) {
    4040                \add_action('woocommerce_login_form', array($this, 'display_captcha'), 30);
    41                 \add_filter('woocommerce_process_login_errors', array($this, 'validate_login_captcha'), 10, 3);
     41                \add_filter('woocommerce_process_login_errors', array($this, 'validate_login_captcha'), 30, 3);
    4242            }
    4343            if ( $this->config->get_wcc_checkout() == 1 ) {
     
    5858        if ( $this->config->get_wp_login() == 1 ) {
    5959            \add_action('login_form', array($this, 'display_captcha'), 20);
     60            \add_action('login_form', array($this, 'wp_login_hidden_field'), 21);
    6061            \add_action('authenticate', array($this, 'validate_wplogin_captcha'), 21, 3);
    6162        }
     
    7778    public function captcha_style(){
    7879        // Register your own empty CSS file (optional) or attach to one you know is enqueued
    79         \wp_register_style('codenitcaptcha-style', false, array(), '1.0.4');
     80        \wp_register_style('codenitcaptcha-style', false, array(), '1.0.5');
    8081        \wp_enqueue_style('codenitcaptcha-style');
    8182
     
    8687    public function captcha_checkout_script(){
    8788        if( \function_exists('is_checkout') && \is_checkout()) {
    88             \wp_register_script( 'codenitcaptcha-script-checkout', CODENITCAPTCHA_PLUGIN_DIR_ASSETS_URL.'js/checkout.js', array(), 0.00002, true );
     89            \wp_register_script( 'codenitcaptcha-script-checkout', CODENITCAPTCHA_PLUGIN_DIR_ASSETS_URL.'js/checkout.js', array(), CODENITCAPTCHA_VERSION, true );
    8990            \wp_enqueue_script( 'codenitcaptcha-script-checkout' );
    9091            \wp_localize_script( 'codenitcaptcha-script-checkout', 'codenitcaptcha_captcha_obj', array(
     
    100101    public function wp_forgot_password_hidden_field() {
    101102        echo '<input type="hidden" name="wp_forget" value="wp">';
     103    }
     104
     105    public function wp_login_hidden_field(){
     106        echo '<input type="hidden" name="codenit_wp_login" value="codenit-wp-login">';
    102107    }
    103108
     
    113118                $captcha = '<div class="g-recaptcha codenitcaptcha-recaptcha" data-sitekey="' . esc_attr($this->config->get_site_key_v2()) . '"></div>';
    114119            }
    115 
    116             echo \wp_kses_post( \wp_nonce_field( 'codenitcaptcha_action', 'codenitcaptcha_nonce' ));
     120           
     121            $nonce = \wp_create_nonce('codenitcaptcha_action');
     122            echo '<input type="hidden" name="codenitcaptcha_nonce" value="'.esc_attr( $nonce ).'" />';
     123
     124            //echo \wp_kses_post( \wp_nonce_field( 'codenitcaptcha_action', 'codenitcaptcha_nonce' ));
    117125            echo \wp_kses_post( $captcha );
    118126
     
    128136
    129137    public function validate_wplogin_captcha($user, $username, $password) {
    130         $response = $this->config->verify_captcha();
    131         if (isset($response['status']) && $response['status'] === 'error') {
    132             return new \WP_Error('captcha_invalid', $this->config->messages($response['message']));
     138        if(isset($_POST['codenit_wp_login'])){
     139            $response = $this->config->verify_captcha();
     140            if (isset($response['status']) && $response['status'] === 'error') {
     141                return new \WP_Error('captcha_invalid', $this->config->messages($response['message']));
     142            }
    133143        }
    134144        return $user;
  • codenitive-captcha/trunk/readme.txt

    r3334738 r3338594  
    55Tested up to: 6.8.2
    66Requires PHP: 7.4
    7 Stable tag: 1.0.4
     7Stable tag: 1.0.5
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    124124* Add reCAPTCHA security for Contact form 7 (cf7)
    125125
     126= 1.0.5 =
     127* Fix login captcha
     128
    126129== Upgrade Notice ==
    127130
     
    138141* Add reCAPTCHA security for Contact form 7 (cf7)
    139142
     143= 1.0.5 =
     144* Fix login captcha
     145
    140146== Feedback ==
    141147
Note: See TracChangeset for help on using the changeset viewer.