| 1 |
var charitable_reCAPTCHA_onload = function() { |
| 2 |
|
| 3 |
( function( $ ) { |
| 4 |
var $body = $( 'body' ); |
| 5 |
var $recaptcha = $( '.charitable-recaptcha' ); |
| 6 |
var input = $recaptcha.parent().find( '[name=charitable_grecaptcha_token]')[0]; |
| 7 |
var form = input.form; |
| 8 |
var recaptcha_id; |
| 9 |
|
| 10 |
/** |
| 11 |
* For donation form submissions, execute reCAPTCHA as part of the |
| 12 |
* validation process, before firing off the rest of the donation |
| 13 |
* form processing. |
| 14 |
*/ |
| 15 |
var donation_form_handler = function() { |
| 16 |
var helper, |
| 17 |
executed = false; |
| 18 |
|
| 19 |
recaptcha_id = grecaptcha.render( $recaptcha[0], { |
| 20 |
'sitekey' : CHARITABLE_RECAPTCHA.site_key, |
| 21 |
'callback' : function( token ) { |
| 22 |
input.setAttribute( 'value', token ); |
| 23 |
helper.remove_pending_process_by_name( 'recaptcha' ); |
| 24 |
}, |
| 25 |
'error-callback' : function() { |
| 26 |
helper.add_error( CHARITABLE_RECAPTCHA.error_message ); |
| 27 |
helper.remove_pending_process_by_name( 'recaptcha' ); |
| 28 |
}, |
| 29 |
'expired-callback' : function() { |
| 30 |
input.setAttribute( 'value', null ); |
| 31 |
helper.remove_pending_process_by_name( 'recaptcha' ); |
| 32 |
}, |
| 33 |
'size' : 'invisible', |
| 34 |
'isolated' : true, |
| 35 |
} ); |
| 36 |
|
| 37 |
$body.on( 'charitable:form:validate', function( event, target ) { |
| 38 |
helper = target; |
| 39 |
|
| 40 |
if ( helper.errors.length === 0 ) { |
| 41 |
if ( executed ) { |
| 42 |
grecaptcha.reset( recaptcha_id ); |
| 43 |
} |
| 44 |
|
| 45 |
helper.add_pending_process( 'recaptcha' ); |
| 46 |
|
| 47 |
grecaptcha.execute( recaptcha_id ); |
| 48 |
|
| 49 |
executed = true; |
| 50 |
} |
| 51 |
} ); |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* For regular form submissions (not the donation form), execute reCAPTCHA |
| 56 |
* when the form is submitted. |
| 57 |
* */ |
| 58 |
var default_form_handler = function() { |
| 59 |
var submitting = false; |
| 60 |
|
| 61 |
recaptcha_id = grecaptcha.render( $recaptcha[0], { |
| 62 |
'sitekey' : CHARITABLE_RECAPTCHA.site_key, |
| 63 |
'callback' : function( token ) { |
| 64 |
input.setAttribute( 'value', token ); |
| 65 |
form.submit(); |
| 66 |
}, |
| 67 |
'error-callback' : function() { |
| 68 |
if ( submitting ) { |
| 69 |
alert( CHARITABLE_RECAPTCHA.error_message ); |
| 70 |
submitting = false; |
| 71 |
} |
| 72 |
}, |
| 73 |
'size' : 'invisible', |
| 74 |
'isolated' : true, |
| 75 |
} ); |
| 76 |
|
| 77 |
form.onsubmit = function() { |
| 78 |
submitting = true; |
| 79 |
grecaptcha.execute( recaptcha_id ); |
| 80 |
return false; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
if ( form.classList.contains( 'charitable-donation-form' ) ) { |
| 85 |
donation_form_handler(); |
| 86 |
} else { |
| 87 |
default_form_handler(); |
| 88 |
} |
| 89 |
})( jQuery ); |
| 90 |
} |
| 91 |
|
| 92 |
|