diff --git a/inc/class-wp_recaptcha.php b/inc/class-wp_recaptcha.php index 711ecae..e2d6a0a 100755 --- a/inc/class-wp_recaptcha.php +++ b/inc/class-wp_recaptcha.php @@ -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 ); } @@ -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' ) ); } /** diff --git a/inc/class-wp_recaptcha_nocaptcha.php b/inc/class-wp_recaptcha_nocaptcha.php index f670555..878b6f5 100755 --- a/inc/class-wp_recaptcha_nocaptcha.php +++ b/inc/class-wp_recaptcha_nocaptcha.php @@ -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( @@ -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' ); + ?> + + ') + .attr( 'src', wp_recaptcha.recaptcha_url ) + .appendTo('body'); + } + window.wp_recaptcha_loaded = function(){ if ( ! grecaptcha ) { return; @@ -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 - $('') - .attr( 'src', wp_recaptcha.recaptcha_url ) - .appendTo('body'); + } else { + $(document).ready( init ); } - })(jQuery); diff --git a/js/wp-recaptcha.min.js b/js/wp-recaptcha.min.js index 227dff9..9b161b8 100644 --- a/js/wp-recaptcha.min.js +++ b/js/wp-recaptcha.min.js @@ -1,2 +1,2 @@ -!function(t){var e=t(".wp-recaptcha");window.wp_recaptcha_loaded=function(){grecaptcha&&e.each(function(e,a){var i,p=t(a).closest("form"),c={sitekey:wp_recaptcha.site_key,theme:t(a).attr("data-theme"),size:t(a).attr("data-size")},r=t(a).attr("data-callback");""!==r&&(c.callback=function(){p.find('[type="submit"]').prop("disabled",!1)},c.expiredCallback=function(){p.find('[type="submit"]').prop("disabled",!0)},p.find('[type="submit"]').prop("disabled",!0).length||p.append(''),"submit"==r&&p.find('[type="submit"]').length&&(i=setInterval(function(){p.find('[type="submit"]').prop("disabled")||(clearInterval(i),p.find('[type="submit"]').trigger("click"))},100))),grecaptcha.render(a,c)})},e.length&&t("') + .attr( 'src', wp_recaptcha.recaptcha_url ) + .appendTo('body'); + } + window.wp_recaptcha_loaded = function(){ if ( ! grecaptcha ) { return; @@ -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 - $('') - .attr( 'src', wp_recaptcha.recaptcha_url ) - .appendTo('body'); + } else { + $(document).ready( init ); } - })(jQuery);