Last active
August 17, 2024 04:21
-
-
Save wpmudev-sls/b272e5b23ff23935e26f1f7a3c15964e to your computer and use it in GitHub Desktop.
[Defender Pro] reCaptcha compatibility with Profile Builder/Profile Builder Pro login form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [Defender Pro] reCaptcha compatibility with Profile Builder/Profile Builder Pro login form | |
* Description: Adds a custom reCaptcha integration to the Profile Builder/Profile Builder Pro login form | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: SLS-6396 | |
* 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( 'PROFILE_BUILDER_VERSION' ) ) { | |
return; // Profile Builder 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( 'wppb_login_form_before_content_output', function( $output, $args ) use( $recaptcha, $configure ) { | |
if ( isset( $GLOBALS['_wds_pb_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_pb_recaptcha'] = true; | |
return $output; | |
}, 10, 2 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment