hCaptcha / hcaptcha-wordpress-plugin

hCaptcha.com support for Wordpress (Plugin)
GNU General Public License v2.0
80 stars 32 forks source link

Integration request: Essential Addons for Elementor #329

Closed orlitzky closed 7 months ago

orlitzky commented 7 months ago

Essential Addons for Elementor (lite) provides its own login/registration widget.

Even with Elementor/WP integration enabled, bots can abuse the extra widget to register junk accounts. It would be nice if hCaptcha supported it out-of-the-box, but it may require a little help from the https://github.com/WPDevelopers team. I was able to hack something together myself, but as you can see, I was not able to find a way to hook into the form errors, so instead it's using die(). Obviously not ideal.

<?php

defined('ABSPATH') or die('No direct access!');

use HCaptcha\Helpers\HCaptcha;

const EAELHC_ACTION = 'hcaptcha_registration';
const EAELHC_NONCE = 'hcaptcha_registration_nonce';

function eaelhc_add_hcaptcha($lr) {
  /* Adapted from the hCaptcha plugin's WP\Register::add_captcha()
     method. */
  $args = [
    'action' => EAELHC_ACTION,
    'name'   => EAELHC_NONCE,
    'id'     => [
       'source'  => 'eael_hcaptcha',
       'form_id' => 'eael_login_register',
    ],
    'size' => 'invisible',
  ];

  HCaptcha::form_display($args);
}

function eaelhc_verify_hcaptcha() {
  /* Adapted from the hCaptcha plugin's WP\Register::verify()
     method. */
  $wpe = new WP_Error();
  $error_message = hcaptcha_verify_post(EAELHC_NONCE, EAELHC_ACTION);
  HCaptcha::add_error_message($wpe, $error_message);

  /* There's no way to return an error here, we just have to die() */
  empty($wpe->errors) or die("hCaptcha verification failed!");
}

/* Insert the hCaptcha field after the password box */
add_action('eael/login-register/after-password-field',
           'eaelhc_add_hcaptcha');

/* Verify the hCaptcha field before we register the user */
add_action('eael/login-register/before-register',
           'eaelhc_verify_hcaptcha');

?>
kagg-design commented 7 months ago

Thank you, @orlitzky, I will try to add this integration soon.

kagg-design commented 7 months ago

I have added integration for Essential Addons for Elementor - for the Login/Register form.

V4.0.0 of our plugin will be released in days.

orlitzky commented 7 months ago

That was fast, thank you!