Skip to content

Commit

Permalink
fix login
Browse files Browse the repository at this point in the history
  • Loading branch information
mcguffin committed Oct 15, 2017
1 parent 17b9f04 commit 4fea0dd
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 44 deletions.
47 changes: 25 additions & 22 deletions inc/class-wp_recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private function __construct() {

if ( $this->has_api_key() ) {

add_action('init' , array(&$this,'init') , 9 );
add_action('plugins_loaded' , array(&$this,'plugins_loaded'), 9 );
add_action('init' , array( $this,'init') , 9 );
add_action('plugins_loaded' , array( $this,'plugins_loaded'), 9 );

}

Expand Down Expand Up @@ -137,47 +137,50 @@ function init() {
//*/
add_action('pre_comment_on_post',array($this,'recaptcha_check_or_die'));

add_action( 'print_comments_recaptcha' , array( &$this , 'print_recaptcha_html' ) );
add_filter( 'comments_recaptcha_html' , array( &$this , 'recaptcha_html' ) );
add_action( 'print_comments_recaptcha' , array( $this , 'print_recaptcha_html' ) );
add_filter( 'comments_recaptcha_html' , array( $this , 'recaptcha_html' ) );
}
if ( $this->get_option('recaptcha_enable_signup') ) {
$this->captcha_instance();
// buddypress suuport.
if ( function_exists('buddypress') ) {
add_action('bp_account_details_fields',array($this,'print_recaptcha_html'));
add_action('bp_signup_pre_validate',array(&$this,'recaptcha_check_or_die'),99 );
add_action('bp_account_details_fields',array( $this,'print_recaptcha_html'));
add_action('bp_signup_pre_validate',array( $this,'recaptcha_check_or_die'),99 );
} else {
add_action('register_form',array($this,'print_recaptcha_html'));
add_filter('registration_errors',array(&$this,'registration_errors'));
add_action('register_form',array( $this,'print_recaptcha_html'));
add_filter('registration_errors',array( $this,'registration_errors'));
}
if ( is_multisite() ) {
add_action( 'signup_extra_fields' , array($this,'print_recaptcha_html'));
add_filter('wpmu_validate_user_signup',array(&$this,'wpmu_validate_user_signup'));
add_filter('wpmu_validate_user_signup',array( $this,'wpmu_validate_user_signup'));
}
add_filter( 'signup_recaptcha_html' , array( &$this , 'recaptcha_html' ) );
add_filter( 'signup_recaptcha_html' , array( $this , 'recaptcha_html' ) );

}
if ( $this->get_option('recaptcha_enable_login') ) {
add_action('login_form',array(&$this,'print_recaptcha_html'));
add_filter('wp_authenticate_user',array(&$this,'deny_login'),99 );
add_filter( 'login_recaptcha_html' , array( &$this , 'recaptcha_html' ) );
$this->captcha_instance();
add_action( 'login_form', array( $this, 'print_recaptcha_html' ) );
add_filter( 'wp_authenticate_user', array( $this, 'deny_login'), 99 );
add_filter( 'login_recaptcha_html', array( $this , 'recaptcha_html' ) );
}
if ( $this->get_option('recaptcha_enable_lostpw') ) {
add_action('lostpassword_form' , array($this,'print_recaptcha_html') );
$this->captcha_instance();
add_action('lostpassword_form' , array( $this, 'print_recaptcha_html') );
//*
add_action('lostpassword_post' , array(&$this,'recaptcha_check_or_die') , 99 );
add_action('lostpassword_post' , array( $this, 'recaptcha_check_or_die') , 99 );
/*/ // switch this when pull request accepted and included in official WC release.
add_filter('allow_password_reset' , array(&$this,'wp_error') );
add_filter('allow_password_reset' , array( $this,'wp_error') );
//*/
add_filter( 'lostpassword_recaptcha_html' , array( &$this , 'recaptcha_html' ) );
add_filter( 'lostpassword_recaptcha_html' , array( $this, 'recaptcha_html' ) );
}
if ( 'WPLANG' === $this->get_option( 'recaptcha_language' ) )
add_filter( 'wp_recaptcha_language' , array( &$this,'recaptcha_wplang' ) , 5 );
add_filter( 'wp_recaptcha_language' , array( $this, 'recaptcha_wplang' ) , 5 );

add_action( 'recaptcha_print' , array( &$this , 'print_recaptcha_html' ) );
add_filter( 'recaptcha_error' , array( &$this , 'wp_error' ) );
add_filter( 'recaptcha_html' , array( &$this , 'recaptcha_html' ) );
add_action( 'recaptcha_print' , array( $this, 'print_recaptcha_html' ) );
add_filter( 'recaptcha_error' , array( $this, 'wp_error' ) );
add_filter( 'recaptcha_html' , array( $this, 'recaptcha_html' ) );
}
add_filter( 'recaptcha_valid' , array( &$this , 'recaptcha_check' ) );
add_filter( 'recaptcha_valid' , array( $this , 'recaptcha_check' ) );
}

/**
Expand Down
23 changes: 22 additions & 1 deletion inc/class-wp_recaptcha_nocaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,17 @@ private function __clone() {
* Prevent from creating more than one instance
*/
private function __construct() {

if ( did_action( 'wp_enqueue_scripts' ) ) {
$this->register_assets();
} else {
add_action( 'wp_enqueue_scripts', array( $this, 'register_assets') );
add_action( 'admin_enqueue_scripts', array( $this, 'register_assets') );
add_action( 'login_enqueue_scripts', array( $this, 'register_assets') );
}
}
public function register_assets() {
$is_login = current_filter() === 'login_enqueue_scripts';

$recaptcha_api_url = "https://www.google.com/recaptcha/api.js";
$recaptcha_api_url = add_query_arg( array(
Expand All @@ -124,11 +128,28 @@ public function register_assets() {
$recaptcha_api_url = add_query_arg( 'hl', $language_code, $recaptcha_api_url );
}
$suffix = WP_DEBUG ? '' : '.min';
wp_register_script( 'wp-recaptcha', plugins_url( "js/wp-recaptcha{$suffix}.js" , dirname(__FILE__)) , array( 'jquery' ), false, true );
wp_register_script( 'wp-recaptcha', plugins_url( "js/wp-recaptcha{$suffix}.js" , dirname(__FILE__)) , array( 'jquery' ), false, ! $is_login );
wp_localize_script( 'wp-recaptcha', 'wp_recaptcha', array(
'recaptcha_url' => $recaptcha_api_url,
'site_key' => WP_reCaptcha::instance()->get_option( 'recaptcha_publickey' ),
) );

if ( $is_login ) {
wp_enqueue_script( 'wp-recaptcha' );
?>
<style type="text/css">
.wp-recaptcha[data-size="normal"] {
width:304px;
margin-left:-15px;
margin-bottom:15px;
}
.wp-recaptcha[data-size="compact"] {
width:164px;
margin-bottom:15px;
}
</style>
<?php
}
}

public function get_supported_themes() {
Expand Down
46 changes: 40 additions & 6 deletions js/wp-recaptcha.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
(function($){
var $captchas = $('.wp-recaptcha');
var $captchas = $('.wp-recaptcha'),
$other_captchas = $('.g-recaptcha'),
loadedInterval;

function init() {
$other_captchas.removeClass('g-recaptcha');
$captchas = $('.wp-recaptcha').addClass('g-recaptcha');

$('<script></script>')
.attr( 'src', wp_recaptcha.recaptcha_url )
.appendTo('body');
}

window.wp_recaptcha_loaded = function(){
if ( ! grecaptcha ) {
return;
Expand Down Expand Up @@ -41,15 +53,37 @@

grecaptcha.render( el, opts );
});

$other_captchas.addClass('g-recaptcha');
}

function captchas_rendered() {
var rendered = true;
$other_captchas.each( function(i,el) {
rendered = captchas_rendered && $(el).html() !== '';
});
return rendered;
}

// load google recaptcha
if ( $captchas.length ) {

if ( $other_captchas.length || $('.nf-form-cont').length ) {
if ( 'undefined' !== typeof grecaptcha ) {
wp_recaptcha_loaded();
} else {
loadedInterval = setInterval(function(){
if ( 'undefined' !== typeof grecaptcha ) {
clearInterval( loadedInterval );
wp_recaptcha_loaded();
}
},333);
}
} else {
init();
}
// load recaptcha script
$('<script></script>')
.attr( 'src', wp_recaptcha.recaptcha_url )
.appendTo('body');
} else {
$(document).ready( init );
}


})(jQuery);
4 changes: 2 additions & 2 deletions js/wp-recaptcha.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 0 additions & 7 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,6 @@ running under his/her own domain name.


= Known Limitations =
- A No Captcha definitely requires client side JavaScript enabled. That's how it does its
sophisticated bot detection magic. There is no fallback. If your visitor does not have
JS enabled the captcha test will not let him through.

- As of version 4.3 CF7 comes with its own recaptcha. Both are supposed to work together.
I you want to keep the WP ReCaptcha functionality, e.g. if you want to hide the captcha
from known users, leave the integration in the CF7 settings unconfigured.

- In **WooCommerce** the reset password form can not be protected by a captcha. Woocommerce does
not fire any action in the lost password form, so there is no way for the plugin to hook in.
Expand Down
Loading

0 comments on commit 4fea0dd

Please sign in to comment.