Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**reCAPTCHA*/
- function ava_custom_recaptcha(){
- ?>
- <script type="text/javascript">
- var key = '6Lf4y3kUAAAAAOrIE2rFAeKbetABnNvC4VKtbb3L';
- var form = jQuery(".avia_ajax_form");
- var button = jQuery(form).find(".button");
- var parent = button.parent(".form_element");
- var captcha = jQuery("<p style='display: inline-block' data-callback='onloadCallback' data-sitekey='"+ key +"' id='reCAPTCHAV3'></p>");
- var answer = jQuery("<p class='answer'>You have to pass reCAPTCHA before you can send the form.</p>");
- var publickey = captcha.data('sitekey');
- var createCaptcha = function() {
- button.attr("disabled", "disabled");
- button.css("display", 'none');
- form.attr('action', '');
- captcha.insertBefore(parent);
- answer.insertAfter(parent);
- };
- createCaptcha();
- var onloadCallback = function() {
- grecaptcha.render('reCAPTCHAV3', {
- 'sitekey' : key,
- 'callback' : 'onSuccessfullCallback'
- });
- };
- var onSuccessfullCallback = function(success) {
- captcha.attr( 'data-capkey', grecaptcha.getResponse() );
- console.log(captcha.data('capkey'));
- onVerifyCallback(captcha.data('capkey'));
- };
- var onVerifyCallback = function( gcaptchakey ) {
- jQuery.ajax({
- type: "POST",
- url: avia_framework_globals.ajaxurl,
- data: {
- recaptcha: gcaptchakey,
- action: 'avia_ajax_recaptcha'
- },
- success: function(response) {
- console.log('success', response);
- if(response == 'success') {
- form.attr('action', window.location.href.replace("#", ""));
- button.removeAttr('disabled');
- button.css("display", 'block');
- jQuery('.answer').remove();
- }
- },
- error: function() {
- console.log('error');
- },
- complete: function() {
- console.log('complete');
- }
- });
- }
- </script>
- <?php
- }
- add_action('wp_footer', 'ava_custom_recaptcha');
- add_action( 'wp_ajax_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
- add_action( 'wp_ajax_nopriv_avia_ajax_recaptcha', 'avia_ajax_recaptcha' );
- function avia_ajax_recaptcha()
- {
- $gcaptcha = $_POST['recaptcha'];
- if( isset( $gcaptcha ) ) {
- $secret = '6Lf4y3kUAAAAAKBEcJ3Gmuzzy9ZAShTzmYWXd2uX';
- $verify = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret."&response=".$gcaptcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
- $response = json_decode( $verify, true);
- if( $response['success'] ) {
- echo 'success';
- } else {
- echo 'error';
- }
- } else {
- echo 'invalid captcha';
- }
- die();
- }
- if( ! function_exists( 'avia_register_reCAPTCHAV3_scripts' ) )
- {
- if( ! is_admin() ){
- add_action( 'wp_enqueue_scripts', 'avia_register_reCAPTCHAV3_scripts' );
- }
- function avia_register_reCAPTCHAV3_scripts()
- {
- $key = '6Lf4y3kUAAAAAOrIE2rFAeKbetABnNvC4VKtbb3L';
- $prefix = is_ssl() ? "https" : "http";
- $api_url = $prefix.'://www.google.com/recaptcha/api.js?onload=onloadCallback&render=explicit';
- wp_register_script( 'avia-google-reCAPTCHAV3-api', $api_url, array( 'avia-default' ), '1.0.0', false);
- wp_enqueue_script( 'avia-google-reCAPTCHAV3-api' );
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement