script.js
376 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, .gglcptch_invisible' ).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 ( 'v2' == gglcptch.options.version || 'invisible' == gglcptch.options.version ) { |
| 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 | function storeEvents( el ) { |
| 97 | var target = el, |
| 98 | events = $._data( el.get(0), 'events' ); |
| 99 | /* restoring events */ |
| 100 | if ( typeof events != 'undefined' ) { |
| 101 | var storedEvents = {}; |
| 102 | $.extend( true, storedEvents, events ); |
| 103 | target.off(); |
| 104 | target.data('storedEvents', storedEvents ); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | function restoreEvents( el ) { |
| 109 | var target = el, |
| 110 | events = target.data('storedEvents'); |
| 111 | /* restoring events */ |
| 112 | if ( typeof events != 'undefined' ) { |
| 113 | for ( var event in events ) { |
| 114 | for ( var i = 0; i < events[event].length; i++ ) { |
| 115 | target.on( event, events[event][i] ); |
| 116 | } |
| 117 | } |
| 118 | } |
| 119 | /* reset stored events */ |
| 120 | target.removeData( 'storedEvents' ); |
| 121 | } |
| 122 | |
| 123 | function storeOnSubmit( form, gglcptch_index ) { |
| 124 | form.on( 'submit', function( e ) { |
| 125 | if ( '' == form.find('.g-recaptcha-response').val() ) { |
| 126 | e.preventDefault(); |
| 127 | e.stopImmediatePropagation(); |
| 128 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 129 | targetEvent = e.type; |
| 130 | grecaptcha.execute( gglcptch_index ); |
| 131 | } |
| 132 | } ).find( 'input:submit, button' ).on( 'click', function( e ) { |
| 133 | if ( '' == form.find('.g-recaptcha-response').val() ) { |
| 134 | e.preventDefault(); |
| 135 | e.stopImmediatePropagation(); |
| 136 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 137 | targetEvent = e.type; |
| 138 | grecaptcha.execute( gglcptch_index ); |
| 139 | } |
| 140 | } ); |
| 141 | } |
| 142 | |
| 143 | var gglcptch_version = gglcptch.options.version; |
| 144 | v1_add_to_last_element = v1_add_to_last_element || false; |
| 145 | |
| 146 | if ( 'v1' == gglcptch_version ) { |
| 147 | if ( Recaptcha.widget == null || v1_add_to_last_element == true ) { |
| 148 | Recaptcha.create( gglcptch.options.sitekey, container, { 'theme' : gglcptch.options.theme } ); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | if ( 'v2' == gglcptch_version ) { |
| 153 | var parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'theme' : gglcptch.options.theme }, |
| 154 | gglcptch_index = grecaptcha.render( container, parameters ); |
| 155 | $( '#' + container ).data( 'gglcptch_index', gglcptch_index ); |
| 156 | } |
| 157 | |
| 158 | if ( 'invisible' == gglcptch_version ) { |
| 159 | var block = $( '#' + container ), |
| 160 | form = block.closest( 'form' ), |
| 161 | parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'size' : 'invisible' }, |
| 162 | targetObject = false, |
| 163 | targetEvent = false; |
| 164 | |
| 165 | if ( form.length ) { |
| 166 | storeEvents( form ); |
| 167 | form.find( 'button, input:submit' ).each( function() { |
| 168 | storeEvents( $( this ) ); |
| 169 | } ); |
| 170 | |
| 171 | /* Callback function works only in frontend */ |
| 172 | if ( 'gglcptch_admin_settings_page' != form.attr( 'id' ) ) { |
| 173 | parameters['callback'] = function( token ) { |
| 174 | form.off(); |
| 175 | restoreEvents( form ); |
| 176 | form.find( 'button, input:submit' ).off().each( function() { |
| 177 | restoreEvents( $( this ) ); |
| 178 | } ); |
| 179 | if ( targetObject && targetEvent ) { |
| 180 | targetObject.trigger( targetEvent ); |
| 181 | } |
| 182 | form.find( 'button, input:submit' ).each( function() { |
| 183 | storeEvents( $( this ) ); |
| 184 | } ); |
| 185 | storeEvents( form ); |
| 186 | storeOnSubmit( form, gglcptch_index ); |
| 187 | grecaptcha.reset( gglcptch_index ); |
| 188 | }; |
| 189 | } |
| 190 | |
| 191 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 192 | block.data( { 'gglcptch_index' : gglcptch_index } ); |
| 193 | |
| 194 | if ( 'gglcptch_admin_settings_page' != form.attr( 'id' ) ) { |
| 195 | storeOnSubmit( form, gglcptch_index ); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | }; |
| 200 | |
| 201 | $( document ).ready( function() { |
| 202 | var tryCounter = 0, |
| 203 | /* launching timer so that the function keeps trying to display the reCAPTCHA again and again until google js api is loaded */ |
| 204 | gglcptch_timer = setInterval( function() { |
| 205 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 206 | try { |
| 207 | gglcptch.prepare(); |
| 208 | } catch ( e ) { |
| 209 | console.log( 'Unexpected error occurred: ', e ); |
| 210 | } |
| 211 | clearInterval( gglcptch_timer ); |
| 212 | } |
| 213 | tryCounter++; |
| 214 | /* Stop trying after 10 times */ |
| 215 | if ( tryCounter >= 10 ) { |
| 216 | clearInterval( gglcptch_timer ); |
| 217 | } |
| 218 | }, 1000 ); |
| 219 | |
| 220 | function gglcptch_prepare() { |
| 221 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 222 | try { |
| 223 | gglcptch.prepare(); |
| 224 | } catch ( err ) { |
| 225 | console.log( err ); |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | $( window ).on( 'load', gglcptch_prepare ); |
| 231 | |
| 232 | $( '.woocommerce' ).on( 'click', '.woocommerce-tabs', gglcptch_prepare ); |
| 233 | |
| 234 | $( '#recaptcha_widget_div' ).on( 'input paste change', '#recaptcha_response_field', cleanError ); |
| 235 | |
| 236 | $( 'form' ).not( '[name="loginform"], [name="registerform"], [name="lostpasswordform"], #setupform, .cntctfrmpr_contact_form, .cntctfrm_contact_form, #commentform, #gglcptch_admin_settings_page, #crrntl-user-info' + gglcptch.vars.excluded_forms ).submit( function( e ) { |
| 237 | var $form = $( this ), |
| 238 | $gglcptch = $form.find( '.gglcptch' ), |
| 239 | $captcha = $gglcptch.filter( '.gglcptch_v1' ).find( '.gglcptch_recaptcha:visible' ), |
| 240 | $captcha_v2 = $gglcptch.filter( '.gglcptch_v2' ).find( '.gglcptch_recaptcha:visible' ); |
| 241 | $captcha_invisible = $gglcptch.filter( '.gglcptch_invisible' ).find( '.gglcptch_recaptcha:visible' ); |
| 242 | if ( $captcha.length ) { |
| 243 | if ( $gglcptch.find( 'input[name="gglcptch_test_enable_js_field"]:hidden' ).length == 0 ) { |
| 244 | $gglcptch.append( '<input type="hidden" value="' + gglcptch.vars.nonce + '" name="gglcptch_test_enable_js_field" />' ); |
| 245 | } |
| 246 | $.ajax( { |
| 247 | async : false, |
| 248 | cache : false, |
| 249 | type : 'POST', |
| 250 | url : gglcptch.vars.ajaxurl, |
| 251 | headers : { |
| 252 | 'Content-Type' : 'application/x-www-form-urlencoded' |
| 253 | }, |
| 254 | data : { |
| 255 | action: 'gglcptch_captcha_check', |
| 256 | recaptcha_challenge_field : $( '#recaptcha_challenge_field' ).val(), |
| 257 | recaptcha_response_field : $( '#recaptcha_response_field' ).val() |
| 258 | }, |
| 259 | success: function( data ) { |
| 260 | if ( data == 'error' ) { |
| 261 | if ( $captcha.next( '#gglcptch_error' ).length == 0 ) { |
| 262 | $captcha.after( '<label id="gglcptch_error">' + gglcptch.vars.error_msg + '</label>' ); |
| 263 | } |
| 264 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 265 | e.preventDefault ? e.preventDefault() : (e.returnValue = false); |
| 266 | return false; |
| 267 | } |
| 268 | }, |
| 269 | error: function( request, status, error ) { |
| 270 | if ( $captcha.next( '#gglcptch_error' ).length == 0 ) { |
| 271 | $captcha.after( '<label id="gglcptch_error">' + request.status + ' ' + error + '</label>' ); |
| 272 | } |
| 273 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 274 | e.preventDefault ? e.preventDefault() : ( e.returnValue = false ); |
| 275 | return false; |
| 276 | } |
| 277 | } ); |
| 278 | $( '#recaptcha_reload' ).trigger( 'click' ); |
| 279 | } else if ( $captcha_v2.length ) { |
| 280 | if ( $gglcptch.find( 'input[name="gglcptch_test_enable_js_field"]:hidden' ).length == 0 ) { |
| 281 | $gglcptch.append( '<input type="hidden" value="' + gglcptch.vars.nonce + '" name="gglcptch_test_enable_js_field" />' ); |
| 282 | } |
| 283 | $.ajax( { |
| 284 | async : false, |
| 285 | cache : false, |
| 286 | type : 'POST', |
| 287 | url : gglcptch.vars.ajaxurl, |
| 288 | headers : { |
| 289 | 'Content-Type' : 'application/x-www-form-urlencoded' |
| 290 | }, |
| 291 | data : { |
| 292 | action: 'gglcptch_captcha_check', |
| 293 | 'g-recaptcha-response' : $form.find( '.g-recaptcha-response' ).val() |
| 294 | }, |
| 295 | success: function( data ) { |
| 296 | if ( data == 'error' ) { |
| 297 | if ( $captcha_v2.next( '#gglcptch_error' ).length == 0 ) { |
| 298 | $captcha_v2.after( '<label id="gglcptch_error">' + gglcptch.vars.error_msg + '</label>' ); |
| 299 | $( "#gglcptch_error" ).fadeOut( 15000, function() { |
| 300 | $( "#gglcptch_error" ).remove(); |
| 301 | } ); |
| 302 | $( 'html, body' ).animate( { scrollTop: $captcha_v2.offset().top - 50 }, 500); |
| 303 | } |
| 304 | grecaptcha.reset( $captcha_v2.data( 'gglcptch_index' ) ); |
| 305 | e.preventDefault ? e.preventDefault() : ( e.returnValue = false ); |
| 306 | return false; |
| 307 | } |
| 308 | }, |
| 309 | error: function( request, status, error ) { |
| 310 | if ( $captcha_v2.next( '#gglcptch_error' ).length == 0 ) { |
| 311 | $captcha_v2.after( '<label id="gglcptch_error">' + request.status + ' ' + error + '</label>' ); |
| 312 | } |
| 313 | grecaptcha.reset( $captcha_v2.data( 'gglcptch_index' ) ); |
| 314 | e.preventDefault ? e.preventDefault() : ( e.returnValue = false ); |
| 315 | return false; |
| 316 | } |
| 317 | } ); |
| 318 | } else if ( $captcha_invisible.length ) { |
| 319 | if ( $gglcptch.find( 'input[name="gglcptch_test_enable_js_field"]:hidden' ).length == 0 ) { |
| 320 | $gglcptch.append( '<input type="hidden" value="' + gglcptch.vars.nonce + '" name="gglcptch_test_enable_js_field" />' ); |
| 321 | } |
| 322 | $.ajax( { |
| 323 | async : false, |
| 324 | cache : false, |
| 325 | type : 'POST', |
| 326 | url : gglcptch.vars.ajaxurl, |
| 327 | headers : { |
| 328 | 'Content-Type' : 'application/x-www-form-urlencoded' |
| 329 | }, |
| 330 | data : { |
| 331 | action: 'gglcptch_captcha_check', |
| 332 | 'g-recaptcha-response' : $form.find( '.g-recaptcha-response' ).val() |
| 333 | }, |
| 334 | success: function( data ) { |
| 335 | if ( data == 'error' ) { |
| 336 | if ( $captcha_invisible.next( '#gglcptch_error' ).length == 0 ) { |
| 337 | $captcha_invisible.after( '<label id="gglcptch_error">' + gglcptch.vars.error_msg + '</label>' ); |
| 338 | $( "#gglcptch_error" ).fadeOut( 15000, function() { |
| 339 | $( "#gglcptch_error" ).remove(); |
| 340 | } ); |
| 341 | $( 'html, body' ).animate( { scrollTop: $captcha_invisible.offset().top - 50 }, 500); |
| 342 | } |
| 343 | grecaptcha.reset( $captcha_invisible.data( 'gglcptch_index' ) ); |
| 344 | e.preventDefault ? e.preventDefault() : ( e.returnValue = false ); |
| 345 | return false; |
| 346 | } |
| 347 | }, |
| 348 | error: function( request, status, error ) { |
| 349 | if ( $captcha_invisible.next( '#gglcptch_error' ).length == 0 ) { |
| 350 | $captcha_invisible.after( '<label id="gglcptch_error">' + request.status + ' ' + error + '</label>' ); |
| 351 | } |
| 352 | grecaptcha.reset( $captcha_invisible.data( 'gglcptch_index' ) ); |
| 353 | e.preventDefault ? e.preventDefault() : ( e.returnValue = false ); |
| 354 | return false; |
| 355 | } |
| 356 | } ); |
| 357 | } |
| 358 | } ); |
| 359 | } ); |
| 360 | |
| 361 | function cleanError() { |
| 362 | $error = $( this ).parents( '#recaptcha_widget_div' ).next( '#gglcptch_error' ); |
| 363 | if ( $error.length ) { |
| 364 | $error.remove(); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | function get_id() { |
| 369 | var id = 'gglcptch_recaptcha_' + Math.floor( Math.random() * 1000 ); |
| 370 | if ( $( '#' + id ).length ) { |
| 371 | id = get_id(); |
| 372 | } else { |
| 373 | return id; |
| 374 | } |
| 375 | } |
| 376 | } )( jQuery, gglcptch ); |