Advertisement
Guest User

Untitled

a guest
Nov 15th, 2018
669
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. /**reCAPTCHA*/
  2.  
  3. function ava_custom_recaptcha(){
  4. ?>
  5. <script type="text/javascript">
  6. var key = 'PUBLIC KEY HERE';
  7. var form = jQuery(".avia_ajax_form");
  8. var button = jQuery(form).find(".button");
  9. var parent = button.parent(".form_element");
  10. var captcha = jQuery("<p data-sitekey='"+ key +"' id='reCAPTCHAV3'></p>");
  11. var answer = jQuery("<p class='answer'>You have to pass reCAPTCHA before you can send the form.</p>");
  12. var publickey = captcha.data('sitekey');
  13.  
  14. var createCaptcha = function() {
  15. button.attr("disabled", "disabled");
  16. button.css("display", 'none');
  17. form.attr('action', '');
  18. captcha.insertBefore(parent);
  19. answer.insertAfter(parent);
  20. };
  21.  
  22. createCaptcha();
  23.  
  24. grecaptcha.ready(function() {
  25. grecaptcha.execute(key, {action: 'homepage'}).then(function(token) {
  26. console.log(token);
  27. setTimeout( function() {
  28. onVerifyCallback(response);
  29. }, 100 );
  30. });
  31. });
  32.  
  33. var onVerifyCallback = function(captcharesp) {
  34. jQuery.ajax({
  35. type: "POST",
  36. url: avia_framework_globals.ajaxurl,
  37. data: {
  38. recaptcha: captcharesp,
  39. action: 'avia_ajax_recaptcha'
  40. },
  41. success: function(response) {
  42. console.log('success', response);
  43. if(response == 'success') {
  44. form.attr('action', window.location.href.replace("#", ""));
  45. button.removeAttr('disabled');
  46. button.css("display", 'block');
  47. jQuery('.answer').remove();
  48. }
  49. },
  50. error: function() {
  51. console.log('error');
  52. },
  53. complete: function() {
  54. console.log('complete');
  55. }
  56. });
  57. }
  58. </script>
  59. <?php
  60. }
  61. add_action('wp_footer', 'ava_custom_recaptcha');
  62.  
  63. add_action( 'wp_ajax_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  64. add_action( 'wp_ajax_nopriv_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  65.  
  66. function avia_ajax_recaptcha()
  67. {
  68. $gcaptcha = $_POST['recaptcha'];
  69.  
  70. if(isset($gcaptcha)):
  71.  
  72. $secret = '6LeB_HoUAAAAAH5gmXvzfIiGGK5TMSTR-eqVaYTM';
  73.  
  74. $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$gcaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
  75.  
  76. if($response['success']):
  77. echo 'success';
  78. else:
  79. echo 'error';
  80. endif;
  81. else:
  82. echo 'invalid captcha';
  83. endif;
  84. die();
  85. }
  86.  
  87. if( ! function_exists( 'avia_register_reCAPTCHAV2_scripts' ) )
  88. {
  89. if( ! is_admin() ){
  90. add_action( 'wp_enqueue_scripts', 'avia_register_reCAPTCHAV2_scripts' );
  91. }
  92.  
  93. function avia_register_reCAPTCHAV2_scripts()
  94. {
  95. $key = 'PUBLIC KEY HERE';
  96. $api_url = 'https://www.google.com/recaptcha/api.js?render='.$key;
  97.  
  98. wp_register_script( 'avia-google-reCAPTCHAV2-api', $api_url, false, '1.0.0', false);
  99. wp_enqueue_script( 'avia-google-reCAPTCHAV2-api' );
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement