Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active May 7, 2024 03:20
Show Gist options
  • Save wpmudev-sls/a244e7a5bf9deb21330491cffc4bd610 to your computer and use it in GitHub Desktop.
Save wpmudev-sls/a244e7a5bf9deb21330491cffc4bd610 to your computer and use it in GitHub Desktop.
[Defender Pro] reCaptcha compatibility with Minimog theme
<?php
/**
* Plugin Name: [Defender Pro] reCaptcha compatibility with Minimog theme
* Description: Adds a custom reCaptcha integration to the Minimog's Login/Register pop-up
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-6095
* 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( 'MINIMOG_THEME_VERSION' ) ) {
return; // Minimog theme is not installed/enabled.
}
if ( wp_doing_cron() ) {
return;
}
if ( is_user_logged_in() ) {
return;
}
$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 );
};
if ( ! wp_doing_ajax() ) {
add_action( 'wp_enqueue_scripts', function() use ( $recaptcha, $configure ) {
$recaptcha->add_scripts();
$configure( $recaptcha );
$method = new ReflectionMethod( $recaptcha, 'display_recaptcha' );
$method->setAccessible( true );
ob_start();
echo $method->invoke( $recaptcha );
$markup = ob_get_clean();
if ( ! empty( $markup ) ) {
$GLOBALS['_wd_recaptcha'] = $markup;
}
});
add_action( 'minimog/modal_user_login/before_form_submit', function() {
if ( ! empty( $GLOBALS['_wd_recaptcha'] ) ) {
echo $GLOBALS['_wd_recaptcha'];
}
});
add_action( 'minimog/modal_user_register/before_form_submit', function() {
if ( ! empty( $GLOBALS['_wd_recaptcha'] ) ) {
echo $GLOBALS['_wd_recaptcha'];
}
});
} else {
$configure( $recaptcha );
}
add_filter( 'minimog/user_login/errors', function( $errors ) use ( $recaptcha ) {
$recaptcha->validate_captcha_field_on_login( $errors );
return $errors;
});
add_filter( 'minimog/user_register/errors', function( $errors, $username, $email ) use ( $recaptcha ) {
$recaptcha->validate_captcha_field_on_registration( $errors );
return $errors;
}, 10, 3 );
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment