Skip to content

Instantly share code, notes, and snippets.

@wpmudev-sls
Last active May 9, 2024 21:40
Show Gist options
  • Save wpmudev-sls/f80723bae931cac432e84884edf3705c to your computer and use it in GitHub Desktop.
Save wpmudev-sls/f80723bae931cac432e84884edf3705c to your computer and use it in GitHub Desktop.
[Defender Pro] reCaptcha compatibility with Woffice theme
<?php
/**
* Plugin Name: [Defender Pro] reCaptcha compatibility with Woffice theme
* Description: Adds a custom reCaptcha integration to the Woffice's Login template
* Author: Anderson Salas @ WPMUDEV
* Task: SLS-6127
* 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( 'WOFFICE_CORE_ENABLED' ) ) {
return; // Woffice theme 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( 'template_include', function( $template ) use ( $recaptcha, $configure ) {
if ( false !== strpos( $template, 'themes/woffice/page-templates/login.php' ) ) {
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 ) ) {
add_filter( 'login_form_middle', function( $html, $args ) use ( $markup ) {
$html .= $markup;
return $html;
}, 10, 2 );
}
});
}
return $template;
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment