| 1 |
var charitable_hcaptcha_onload = function() { |
| 2 |
( function( $ ) { |
| 3 |
var $captcha = $( '.charitable-hcaptcha' ), |
| 4 |
input = $captcha.parent().find( '[name=hcaptcha_token]' )[0], |
| 5 |
form = input.form, |
| 6 |
captcha_id, |
| 7 |
helper; |
| 8 |
|
| 9 |
/** |
| 10 |
* For donation form submissions, execute hcaptcha as part of the |
| 11 |
* validation process, before firing off the rest of the donation |
| 12 |
* form processing. |
| 13 |
*/ |
| 14 |
var donation_form_handler = function() { |
| 15 |
var helper; |
| 16 |
|
| 17 |
captcha_id = hcaptcha.render( $captcha.attr( 'id' ), { |
| 18 |
'size': 'invisible', |
| 19 |
'callback': function( token ) { |
| 20 |
input.setAttribute( 'value', token ); |
| 21 |
helper.remove_pending_process_by_name( 'hcaptcha' ); |
| 22 |
} |
| 23 |
} ); |
| 24 |
|
| 25 |
$( 'body' ).on( 'charitable:form:validate', function( event, target ) { |
| 26 |
helper = target; |
| 27 |
|
| 28 |
if ( helper.errors.length === 0 ) { |
| 29 |
helper.add_pending_process( 'hcaptcha' ); |
| 30 |
hcaptcha.execute( captcha_id ); |
| 31 |
} |
| 32 |
} ); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* For regular form submissions (not the donation form), execute hcaptcha |
| 37 |
* when the form is submitted. |
| 38 |
*/ |
| 39 |
var default_form_handler = function() { |
| 40 |
captcha_id = hcaptcha.render( $captcha.attr( 'id' ), { |
| 41 |
'size': 'invisible', |
| 42 |
'callback': function( token ) { |
| 43 |
input.setAttribute( 'value', token ); |
| 44 |
form.submit(); |
| 45 |
} |
| 46 |
} ); |
| 47 |
|
| 48 |
form.onsubmit = function() { |
| 49 |
hcaptcha.execute( captcha_id ); |
| 50 |
return false; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if ( form.classList.contains( 'charitable-donation-form' ) ) { |
| 55 |
donation_form_handler(); |
| 56 |
} else { |
| 57 |
default_form_handler(); |
| 58 |
} |
| 59 |
})( jQuery ); |
| 60 |
} |
| 61 |
|
| 62 |
|