script.js
215 lines
| 1 | ( function( $, gglcptch ) { |
| 2 | gglcptch = gglcptch || {}; |
| 3 | |
| 4 | gglcptch.prepare = function() { |
| 5 | /* |
| 6 | * display reCaptcha for plugin`s block |
| 7 | */ |
| 8 | $( '.gglcptch_v1, .gglcptch_v2' ).each( function() { |
| 9 | var container = $( this ).find( '.gglcptch_recaptcha' ); |
| 10 | if ( |
| 11 | container.is( ':empty' ) && |
| 12 | ( gglcptch.vars.visibility || $( this ).is( ':visible' ) === $( this ).is( ':not(:hidden)' ) ) |
| 13 | ) { |
| 14 | var containerId = container.attr( 'id' ); |
| 15 | gglcptch.display( containerId ); |
| 16 | } |
| 17 | } ); |
| 18 | |
| 19 | /* |
| 20 | * display reCaptcha for others blocks |
| 21 | * this part is neccessary because |
| 22 | * we have disabled the connection to Google reCaptcha API from other plugins |
| 23 | * via plugin`s php-functionality |
| 24 | */ |
| 25 | if ( gglcptch.options.version == 'v2' ) { |
| 26 | $( '.g-recaptcha' ).each( function() { |
| 27 | /* reCAPTCHA will be generated into the empty block only */ |
| 28 | if ( $( this ).html() === '' && $( this ).text() === '' ) { |
| 29 | |
| 30 | /* get element`s ID */ |
| 31 | var container = $( this ).attr( 'id' ); |
| 32 | |
| 33 | if ( typeof container == 'undefined' ) { |
| 34 | container = get_id(); |
| 35 | $( this ).attr( 'id', container ); |
| 36 | } |
| 37 | |
| 38 | /* get reCapatcha parameters */ |
| 39 | var sitekey = $( this ).attr( 'data-sitekey' ), |
| 40 | theme = $( this ).attr( 'data-theme' ), |
| 41 | lang = $( this ).attr( 'data-lang' ), |
| 42 | size = $( this ).attr( 'data-size' ), |
| 43 | type = $( this ).attr( 'data-type' ), |
| 44 | tabindex = $( this ).attr( 'data-tabindex' ), |
| 45 | callback = $( this ).attr( 'data-callback' ), |
| 46 | ex_call = $( this ).attr( 'data-expired-callback' ), |
| 47 | stoken = $( this ).attr( 'data-stoken' ), |
| 48 | params = []; |
| 49 | |
| 50 | params['sitekey'] = sitekey ? sitekey : gglcptch.options.sitekey; |
| 51 | if ( !! theme ) { |
| 52 | params['theme'] = theme; |
| 53 | } |
| 54 | if ( !! lang ) { |
| 55 | params['lang'] = lang; |
| 56 | } |
| 57 | if ( !! size ) { |
| 58 | params['size'] = size; |
| 59 | } |
| 60 | if ( !! type ) { |
| 61 | params['type'] = type; |
| 62 | } |
| 63 | if ( !! tabindex ) { |
| 64 | params['tabindex'] = tabindex; |
| 65 | } |
| 66 | if ( !! callback ) { |
| 67 | params['callback'] = callback; |
| 68 | } |
| 69 | if ( !! ex_call ) { |
| 70 | params['expired-callback'] = ex_call; |
| 71 | } |
| 72 | if ( !! stoken ) { |
| 73 | params['stoken'] = stoken; |
| 74 | } |
| 75 | |
| 76 | gglcptch.display( container, false, params ); |
| 77 | } |
| 78 | } ); |
| 79 | |
| 80 | /* |
| 81 | * count the number of reCAPTCHA blocks in the form |
| 82 | */ |
| 83 | $( 'form' ).each( function() { |
| 84 | if ( $( this ).contents().find( 'iframe[title="recaptcha widget"]' ).length > 1 && ! $( this ).children( '.gglcptch_dublicate_error' ).length ) { |
| 85 | $( this ).prepend( '<div class="gglcptch_dublicate_error error" style="color: red;">'+ gglcptch.options.error + '</div><br />\n' ); |
| 86 | } |
| 87 | } ); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | gglcptch.display = function( container, v1_add_to_last_element, params ) { |
| 92 | if ( typeof( container ) == 'undefined' || container == '' || typeof( gglcptch.options ) == 'undefined' ) { |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | var gglcptch_version = gglcptch.options.version; |
| 97 | v1_add_to_last_element = v1_add_to_last_element || false; |
| 98 | |
| 99 | if ( 'v1' == gglcptch_version ) { |
| 100 | if ( Recaptcha.widget == null || v1_add_to_last_element == true ) { |
| 101 | Recaptcha.create( gglcptch.options.sitekey, container, { 'theme' : gglcptch.options.theme } ); |
| 102 | } |
| 103 | } |
| 104 | if ( 'v2' == gglcptch_version ) { |
| 105 | var parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'theme' : gglcptch.options.theme }, |
| 106 | gglcptch_index = grecaptcha.render( container, parameters ); |
| 107 | $( '#' + container ).data( 'gglcptch_index', gglcptch_index ); |
| 108 | } |
| 109 | }; |
| 110 | |
| 111 | $( window ).on( 'load', gglcptch.prepare ); |
| 112 | |
| 113 | $( document ).ready( function() { |
| 114 | |
| 115 | $( '#recaptcha_widget_div' ).on( 'input paste change', '#recaptcha_response_field', cleanError ); |
| 116 | |
| 117 | $( 'form' ).not( '[name="loginform"], [name="registerform"], [name="lostpasswordform"], #setupform, .cntctfrmpr_contact_form, .cntctfrm_contact_form, #commentform, #gglcptch_admin_settings_page' ).submit( function( e ) { |
| 118 | var $form = $( this ), |
| 119 | $gglcptch = $form.find( '.gglcptch' ), |
| 120 | $captcha = $gglcptch.filter( '.gglcptch_v1' ).find( '.gglcptch_recaptcha:visible' ), |
| 121 | $captcha_v2 = $gglcptch.filter( '.gglcptch_v2' ).find( '.gglcptch_recaptcha:visible' ); |
| 122 | if ( $captcha.length ) { |
| 123 | if ( $gglcptch.find( 'input[name="gglcptch_test_enable_js_field"]:hidden' ).length == 0 ) { |
| 124 | $gglcptch.append( '<input type="hidden" value="' + gglcptch.vars.nonce + '" name="gglcptch_test_enable_js_field" />' ); |
| 125 | } |
| 126 | $.ajax( { |
| 127 | async : false, |
| 128 | cache : false, |
| 129 | type : 'POST', |
| 130 | url : gglcptch.vars.ajaxurl, |
| 131 | headers : { |
| 132 | 'Content-Type' : 'application/x-www-form-urlencoded' |
| 133 | }, |
| 134 | data : { |
| 135 | action: 'gglcptch_captcha_check', |
| 136 | recaptcha_challenge_field : $( '#recaptcha_challenge_field' ).val(), |
| 137 | recaptcha_response_field : $( '#recaptcha_response_field' ).val() |
| 138 | }, |
| 139 | success: function( data ) { |
| 140 | if ( data == 'error' ) { |
| 141 | if ( $captcha.next( '#gglcptch_error' ).length == 0 ) { |
| 142 | $captcha.after( '<label id="gglcptch_error">' + gglcptch.vars.error_msg + '</label>' ); |
| 143 | } |
| 144 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 145 | e.preventDefault ? e.preventDefault() : (e.returnValue = false); |
| 146 | return false; |
| 147 | } |
| 148 | }, |
| 149 | error: function( request, status, error ) { |
| 150 | if ( $captcha.next( '#gglcptch_error' ).length == 0 ) { |
| 151 | $captcha.after( '<label id="gglcptch_error">' + request.status + ' ' + error + '</label>' ); |
| 152 | } |
| 153 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 154 | e.preventDefault ? e.preventDefault() : (e.returnValue = false); |
| 155 | return false; |
| 156 | } |
| 157 | } ); |
| 158 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 159 | } else if ( $captcha_v2.length ) { |
| 160 | if ( $gglcptch.find( 'input[name="gglcptch_test_enable_js_field"]:hidden' ).length == 0 ) { |
| 161 | $gglcptch.append( '<input type="hidden" value="' + gglcptch.vars.nonce + '" name="gglcptch_test_enable_js_field" />' ); |
| 162 | } |
| 163 | $.ajax( { |
| 164 | async : false, |
| 165 | cache : false, |
| 166 | type : 'POST', |
| 167 | url : gglcptch.vars.ajaxurl, |
| 168 | headers : { |
| 169 | 'Content-Type' : 'application/x-www-form-urlencoded' |
| 170 | }, |
| 171 | data : { |
| 172 | action: 'gglcptch_captcha_check', |
| 173 | 'g-recaptcha-response' : $form.find( '.g-recaptcha-response' ).val() |
| 174 | }, |
| 175 | success: function( data ) { |
| 176 | if ( data == 'error' ) { |
| 177 | if ( $captcha_v2.next( '#gglcptch_error' ).length == 0 ) { |
| 178 | $captcha_v2.after( '<label id="gglcptch_error">' + gglcptch.vars.error_msg + '</label>' ); |
| 179 | $( "#gglcptch_error" ).fadeOut( 4000, function() { |
| 180 | $( "#gglcptch_error" ).remove(); |
| 181 | } ); |
| 182 | $( 'html, body' ).animate({ scrollTop: $captcha_v2.offset().top - 50 }, 500); |
| 183 | } |
| 184 | e.preventDefault ? e.preventDefault() : (e.returnValue = false); |
| 185 | return false; |
| 186 | } |
| 187 | }, |
| 188 | error: function( request, status, error ) { |
| 189 | if ( $captcha_v2.next( '#gglcptch_error' ).length == 0 ) { |
| 190 | $captcha_v2.after( '<label id="gglcptch_error">' + request.status + ' ' + error + '</label>' ); |
| 191 | } |
| 192 | e.preventDefault ? e.preventDefault() : (e.returnValue = false); |
| 193 | return false; |
| 194 | } |
| 195 | } ); |
| 196 | } |
| 197 | } ); |
| 198 | } ); |
| 199 | |
| 200 | function cleanError() { |
| 201 | $error = $( this ).parents( '#recaptcha_widget_div' ).next( '#gglcptch_error' ); |
| 202 | if ( $error.length ) { |
| 203 | $error.remove(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | function get_id() { |
| 208 | var id = 'gglcptch_recaptcha_' + Math.floor( Math.random() * 1000 ); |
| 209 | if ( $( '#' + id ).length ) { |
| 210 | id = get_id(); |
| 211 | } else { |
| 212 | return id; |
| 213 | } |
| 214 | } |
| 215 | } )( jQuery, gglcptch ); |