Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active June 6, 2024 23:32
Show Gist options
  • Save wpmudev-sls/06bd3ac5b5d6d672f33028c453dd5dfe to your computer and use it in GitHub Desktop.
Save wpmudev-sls/06bd3ac5b5d6d672f33028c453dd5dfe to your computer and use it in GitHub Desktop.
[Defender Pro] reCaptcha compatibility with Easy Digital Downloads plugin
<?php
/**
* Plugin Name: [Defender Pro] reCaptcha compatibility with Easy Digital Downloads plugin
* Description: Adds a custom reCaptcha integration to the Easy Digital Downloads login shortcode
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-6194
* Author URI: https://premium.wpmudev.org
* License: GPLv2 or later
*/
add_action( 'plugins_loaded', function() {
if ( ! defined( 'DEFENDER_VERSION' ) ) {
return; // Defender is not installed/enabled.
}
if ( ! defined( 'EDD_PLUGIN_FILE' ) ) {
return; // Easy Digital Downloads plugin is not installed/enabled.
}
$recaptcha = wd_di()->get( \WP_Defender\Controller\Recaptcha::class );
$recaptcha_model = wd_di()->get( \WP_Defender\Model\Setting\Recaptcha::class );
if ( ! $recaptcha_model->is_active() ) {
return; // reCaptcha module is not active.
}
$configure = function( $recaptcha ) {
$method = new ReflectionMethod( $recaptcha, 'declare_variables' );
$method->setAccessible( true );
$method->invoke( $recaptcha );
};
add_filter( 'do_shortcode_tag', function( $output, $tag, $attr, $m ) use( $recaptcha, $configure ) {
if ( 'edd_login' !== $tag || isset( $GLOBALS['_wds_edd_recaptcha'] ) ) {
return $output;
}
$configure( $recaptcha );
add_action( 'wp_enqueue_scripts', function() use ( $recaptcha ) {
$recaptcha->add_scripts();
});
$method = new ReflectionMethod( $recaptcha, 'display_recaptcha' );
$method->setAccessible( true );
ob_start();
echo $method->invoke( $recaptcha );
$markup = ob_get_clean();
if ( ! empty( $markup ) ) {
$output = str_ireplace( '</form>', $markup . '</form>', $output );
}
$GLOBALS['_wds_edd_recaptcha'] = true;
return $output;
}, 10, 4 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment