You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

53 lines
1.2 KiB

  1. ( function() {
  2. document.addEventListener( 'DOMContentLoaded', function( event ) {
  3. wpcf7_recaptcha.execute = function( action ) {
  4. grecaptcha.execute(
  5. wpcf7_recaptcha.sitekey,
  6. { action: action }
  7. ).then( function( token ) {
  8. var event = new CustomEvent( 'wpcf7grecaptchaexecuted', {
  9. detail: {
  10. action: action,
  11. token: token,
  12. },
  13. } );
  14. document.dispatchEvent( event );
  15. } );
  16. };
  17. wpcf7_recaptcha.execute_on_homepage = function() {
  18. wpcf7_recaptcha.execute( wpcf7_recaptcha.actions[ 'homepage' ] );
  19. };
  20. wpcf7_recaptcha.execute_on_contactform = function() {
  21. wpcf7_recaptcha.execute( wpcf7_recaptcha.actions[ 'contactform' ] );
  22. };
  23. grecaptcha.ready(
  24. wpcf7_recaptcha.execute_on_homepage
  25. );
  26. document.addEventListener( 'change',
  27. wpcf7_recaptcha.execute_on_contactform
  28. );
  29. document.addEventListener( 'wpcf7submit',
  30. wpcf7_recaptcha.execute_on_homepage
  31. );
  32. } );
  33. document.addEventListener( 'wpcf7grecaptchaexecuted', function( event ) {
  34. var fields = document.querySelectorAll(
  35. "form.wpcf7-form input[name='_wpcf7_recaptcha_response']"
  36. );
  37. for ( var i = 0; i < fields.length; i++ ) {
  38. var field = fields[ i ];
  39. field.setAttribute( 'value', event.detail.token );
  40. }
  41. } );
  42. } )();