Advertisement
Guest User

test

a guest
Sep 4th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.43 KB | None | 0 0
  1. function ava_custom_recaptcha(){
  2.     ?>
  3.     <script type="text/javascript">
  4.         var form = jQuery(".avia_ajax_form");
  5.         var button = jQuery(form).find(".button");
  6.         var parent = button.parent(".form_element");
  7.         var captcha = jQuery("<p data-sitekey='PUBLIC KEY HERE' data-callback='onloadCallback' id='reCAPTCHAV2'></p>");
  8.         var answer = jQuery("<p class='answer'>You have to pass reCAPTCHA before you can send the form.</p>");
  9.         var publickey = captcha.data('sitekey');
  10.  
  11.         var createCaptcha = function() {
  12.             button.attr("disabled", "disabled");
  13.             button.css("display", 'none');
  14.             form.attr('action', '');
  15.             captcha.insertBefore(parent);
  16.             answer.insertAfter(parent);
  17.         };
  18.    
  19.         createCaptcha();
  20.    
  21.         var onloadCallback = function() {
  22.             grecaptcha.render('reCAPTCHAV2', {
  23.                 'sitekey' : publickey,
  24.                 'callback' : 'onSuccessfullCallback'
  25.             });
  26.         };
  27.    
  28.         var onSuccessfullCallback = function(success) {
  29.             window.location.hash = grecaptcha.getResponse();
  30.  
  31.             var response = window.location.hash.replace("#","");
  32.             console.log(response);
  33.             onVerifyCallback(response);
  34.  
  35.             window.location.hash = '';
  36.         };
  37.  
  38.         var onVerifyCallback = function(captcharesp) {
  39.             jQuery.ajax({
  40.                 type: "POST",
  41.                 url: avia_framework_globals.ajaxurl,
  42.                 data: {
  43.                     recaptcha: captcharesp,
  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. //now hook into wordpress ajax function to catch any ajax requests
  69. add_action( 'wp_ajax_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  70. add_action( 'wp_ajax_nopriv_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
  71.  
  72. function avia_ajax_recaptcha()
  73. {
  74.     $gcaptcha = $_POST['recaptcha'];
  75.  
  76.     if(isset($gcaptcha) && !empty($gcaptcha)):
  77.        
  78.         //your site secret key
  79.         $secret = 'SECRET KEY HERE';
  80.        
  81.         //get verify response data
  82.         $response = json_decode(file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$gcaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']), true);
  83.        
  84.         if($response['success']):
  85.             echo 'success';
  86.         else:
  87.             echo 'error';
  88.         endif;
  89.     else:
  90.         echo 'invalid captcha';
  91.     endif;
  92.     die();
  93. }
  94.  
  95. if(!function_exists('avia_register_reCAPTCHAV2_scripts'))
  96. {
  97.     if(!is_admin()){
  98.         add_action('wp_enqueue_scripts', 'avia_register_reCAPTCHAV2_scripts');
  99.     }
  100.  
  101.     function avia_register_reCAPTCHAV2_scripts()
  102.     {
  103.         $prefix  = is_ssl() ? "https" : "http";
  104.         $api_url = $prefix.'://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit';
  105.  
  106.         wp_register_script( 'avia-google-reCAPTCHAV2-api', $api_url, array('jquery'), NULL, true);
  107.         wp_enqueue_script(  'avia-google-reCAPTCHAV2-api' );
  108.     }
  109. }
  110.  
  111. function avf_recaCAPTCHAV2_defer_async($tag, $handle) {
  112.     if ( 'avia-google-reCAPTCHAV2-api' !== $handle ) return $tag;
  113.     return str_replace( ' src', ' defer="defer" async="async" src', $tag );
  114. }
  115. add_filter('script_loader_tag', 'avf_recaCAPTCHAV2_defer_async', 10, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement