Last active
April 30, 2025 03:23
-
-
Save wpmudev-sls/da2350d04716ad8f93c5d0d8db175232 to your computer and use it in GitHub Desktop.
[Defender Pro] reCaptcha compatibility with WP Affiliate Manager plugin
This file contains hidden or 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 WP Affiliate Manager plugin | |
* Description: Adds a custom reCaptcha integration to the WP Affiliate Manager login shortcode | |
* Author: Anderson Salas @ WPMUDEV | |
* Task: DEF-3195 | |
* 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( 'WPAM_VERSION' ) ) { | |
return; // WP Affiliate Manager 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 ( 'AffiliatesLogin' !== $tag || isset( $GLOBALS['_wds_wpam_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_wpam_recaptcha'] = true; | |
return $output; | |
}, 10, 4 ); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment