Advertisement
Guest User

Untitled

a guest
Nov 20th, 2018
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. /**reCAPTCHA*/
  2.  
  3. function ava_custom_recaptcha(){
  4. ?>
  5. <script type="text/javascript">
  6. var key = '6Lf4y3kUAAAAAOrIE2rFAeKbetABnNvC4VKtbb3L';
  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 style='display: inline-block' data-callback='onloadCallback' 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.  
  23. createCaptcha();
  24.  
  25. var onloadCallback = function() {
  26. grecaptcha.render('reCAPTCHAV3', {
  27. 'sitekey' : key,
  28. 'callback' : 'onSuccessfullCallback'
  29. });
  30. };
  31.  
  32. var onSuccessfullCallback = function(success) {
  33. captcha.attr( 'data-capkey', grecaptcha.getResponse() );
  34. console.log(captcha.data('capkey'));
  35. onVerifyCallback(captcha.data('capkey'));
  36. };
  37.  
  38. var onVerifyCallback = function( gcaptchakey ) {
  39. jQuery.ajax({
  40. type: "POST",
  41. url: avia_framework_globals.ajaxurl,
  42. data: {
  43. recaptcha: gcaptchakey,
  44. action: 'avia_ajax_recaptcha'
  45. },
  46. success: function(response) {
  47. console.log('success', response);
  48. if(response == 'success') {
  49. form.attr('action', window.location.href.replace("#", ""));
  50. button.removeAttr('disabled');
  51. button.css("display", 'block');
  52. jQuery('.answer').remove();
  53. }
  54. },
  55. error: function() {
  56. console.log('error');
  57. },
  58. complete: function() {
  59. console.log('complete');
  60. }
  61. });
  62. }
  63. </script>
  64. <?php
  65. }
  66. add_action('wp_footer', 'ava_custom_recaptcha');
  67.  
  68. add_action( 'wp_ajax_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  69. add_action( 'wp_ajax_nopriv_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  70.  
  71. function avia_ajax_recaptcha()
  72. {
  73. $gcaptcha = $_POST['recaptcha'];
  74.  
  75. if( isset( $gcaptcha ) ) {
  76. $secret = '6Lf4y3kUAAAAAKBEcJ3Gmuzzy9ZAShTzmYWXd2uX';
  77.  
  78. $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$gcaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
  79. $response = json_decode( $verify, true);
  80.  
  81. if( $response['success'] ) {
  82. echo 'success';
  83. } else {
  84. echo 'error';
  85. }
  86. } else {
  87. echo 'invalid captcha';
  88. }
  89.  
  90. die();
  91. }
  92.  
  93. if( ! function_exists( 'avia_register_reCAPTCHAV3_scripts' ) )
  94. {
  95. if( ! is_admin() ){
  96. add_action( 'wp_enqueue_scripts', 'avia_register_reCAPTCHAV3_scripts' );
  97. }
  98.  
  99. function avia_register_reCAPTCHAV3_scripts()
  100. {
  101. $key = '6Lf4y3kUAAAAAOrIE2rFAeKbetABnNvC4VKtbb3L';
  102. $prefix = is_ssl() ? "https" : "http";
  103. $api_url = $prefix.'://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit';
  104.  
  105. wp_register_script( 'avia-google-reCAPTCHAV3-api', $api_url, array( 'avia-default' ), '1.0.0', false);
  106. wp_enqueue_script( 'avia-google-reCAPTCHAV3-api' );
  107. }
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement