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