script.js
314 lines
| 1 | ( function( $, gglcptch ) { |
| 2 | gglcptch = gglcptch || {}; |
| 3 | |
| 4 | gglcptch.prepare = function() { |
| 5 | /* |
| 6 | * display reCaptcha for plugin`s block |
| 7 | */ |
| 8 | $( '.gglcptch_v2, .gglcptch_invisible' ).each( function() { |
| 9 | |
| 10 | var container = $( this ).find( '.gglcptch_recaptcha' ); |
| 11 | |
| 12 | if ( |
| 13 | container.is( ':empty' ) && |
| 14 | ( gglcptch.vars.visibility || $( this ).is( ':visible' ) === $( this ).is( ':not(:hidden)' ) ) |
| 15 | ) { |
| 16 | var containerId = container.attr( 'id' ); |
| 17 | gglcptch.display( containerId ); |
| 18 | } |
| 19 | } ); |
| 20 | |
| 21 | if ( 'v3' == gglcptch.options.version ) { |
| 22 | grecaptcha.ready( function() { |
| 23 | grecaptcha.execute( gglcptch.options.sitekey, {action: 'BWS_reCaptcha'}).then(function( token ) { |
| 24 | document.querySelectorAll( "#g-recaptcha-response" ).forEach( function ( elem ) { elem.value = token } ); |
| 25 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 26 | var gglcptchTimeout = setTimeout( function(){ |
| 27 | $( '.gglcptch_error_text' ).each( function(){ |
| 28 | $( this ).show(); |
| 29 | clearTimeout( gglcptchTimeout ); |
| 30 | }); |
| 31 | }, 180000 ); |
| 32 | } |
| 33 | }); |
| 34 | }); |
| 35 | } |
| 36 | |
| 37 | /* |
| 38 | * display reCaptcha for others blocks |
| 39 | * this part is neccessary because |
| 40 | * we have disabled the connection to Google reCaptcha API from other plugins |
| 41 | * via plugin`s php-functionality |
| 42 | */ |
| 43 | if ( 'v2' == gglcptch.options.version || 'invisible' == gglcptch.options.version ) { |
| 44 | $( '.g-recaptcha' ).each( function() { |
| 45 | /* reCAPTCHA will be generated into the empty block only */ |
| 46 | if ( $( this ).html() === '' && $( this ).text() === '' ) { |
| 47 | |
| 48 | /* get element`s ID */ |
| 49 | var container = $( this ).attr( 'id' ); |
| 50 | |
| 51 | if ( typeof container == 'undefined' ) { |
| 52 | container = get_id(); |
| 53 | $( this ).attr( 'id', container ); |
| 54 | } |
| 55 | |
| 56 | /* get reCapatcha parameters */ |
| 57 | var sitekey = $( this ).attr( 'data-sitekey' ), |
| 58 | theme = $( this ).attr( 'data-theme' ), |
| 59 | lang = $( this ).attr( 'data-lang' ), |
| 60 | size = $( this ).attr( 'data-size' ), |
| 61 | type = $( this ).attr( 'data-type' ), |
| 62 | tabindex = $( this ).attr( 'data-tabindex' ), |
| 63 | callback = $( this ).attr( 'data-callback' ), |
| 64 | ex_call = $( this ).attr( 'data-expired-callback' ), |
| 65 | stoken = $( this ).attr( 'data-stoken' ), |
| 66 | params = []; |
| 67 | |
| 68 | params['sitekey'] = sitekey ? sitekey : gglcptch.options.sitekey; |
| 69 | if ( !! theme ) { |
| 70 | params['theme'] = theme; |
| 71 | } |
| 72 | if ( !! lang ) { |
| 73 | params['lang'] = lang; |
| 74 | } |
| 75 | if ( !! size ) { |
| 76 | params['size'] = size; |
| 77 | } |
| 78 | if ( !! type ) { |
| 79 | params['type'] = type; |
| 80 | } |
| 81 | if ( !! tabindex ) { |
| 82 | params['tabindex'] = tabindex; |
| 83 | } |
| 84 | if ( !! callback ) { |
| 85 | params['callback'] = callback; |
| 86 | } |
| 87 | if ( !! ex_call ) { |
| 88 | params['expired-callback'] = ex_call; |
| 89 | } |
| 90 | if ( !! stoken ) { |
| 91 | params['stoken'] = stoken; |
| 92 | } |
| 93 | |
| 94 | gglcptch.display( container, params ); |
| 95 | } |
| 96 | } ); |
| 97 | |
| 98 | /* |
| 99 | * count the number of reCAPTCHA blocks in the form |
| 100 | */ |
| 101 | $( 'form' ).each( function() { |
| 102 | if ( $( this ).contents().find( 'iframe[title="recaptcha widget"]' ).length > 1 && ! $( this ).children( '.gglcptch_dublicate_error' ).length ) { |
| 103 | $( this ).prepend( '<div class="gglcptch_dublicate_error error" style="color: red;">' + gglcptch.options.error + '</div><br />\n' ); |
| 104 | } |
| 105 | } ); |
| 106 | } |
| 107 | }; |
| 108 | |
| 109 | gglcptch.display = function( container, params ) { |
| 110 | if ( typeof( container ) == 'undefined' || container == '' || typeof( gglcptch.options ) == 'undefined' ) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | if ( 'v2' === gglcptch.options.version && $( '#' + container ).parents( '.gglcptch_custom' ).length > 0 ) { |
| 115 | if ( $( '#' + container ).find( 'textarea#g-recaptcha-response' ).length == 0 ) { |
| 116 | setTimeout( |
| 117 | function(){ |
| 118 | $( '#' + container ).find( 'textarea#g-recaptcha-response' ).remove(); |
| 119 | }, |
| 120 | 1000 |
| 121 | ); |
| 122 | } else { |
| 123 | $( '#' + container ).find( 'textarea#g-recaptcha-response' ).remove(); |
| 124 | } |
| 125 | $( '.gglcptch.gglcptch_v2 noscript' ).remove(); |
| 126 | } |
| 127 | |
| 128 | // add attribute disable to the submit |
| 129 | if ( 'v2' === gglcptch.options.version && gglcptch.options.disable ) { |
| 130 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 131 | if ( 'setupform' === $( '#' + container ).closest( 'form' ).attr( 'id' ) ) { |
| 132 | setTimeout( |
| 133 | function(){ |
| 134 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 135 | }, |
| 136 | 200 |
| 137 | ); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | function storeEvents( el ) { |
| 142 | var target = el, |
| 143 | events = $._data( el.get( 0 ), 'events' ); |
| 144 | /* restoring events */ |
| 145 | if ( typeof events != 'undefined' ) { |
| 146 | var storedEvents = {}; |
| 147 | $.extend( true, storedEvents, events ); |
| 148 | target.off(); |
| 149 | target.data( 'storedEvents', storedEvents ); |
| 150 | } |
| 151 | /* storing and removing onclick action */ |
| 152 | if ( 'undefined' != typeof target.attr( 'onclick' ) ) { |
| 153 | target.attr( 'gglcptch-onclick', target.attr( 'onclick') ); |
| 154 | target.removeAttr( 'onclick' ); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | function restoreEvents( el ) { |
| 159 | var target = el, |
| 160 | events = target.data( 'storedEvents' ); |
| 161 | /* restoring events */ |
| 162 | if ( typeof events != 'undefined' ) { |
| 163 | for ( var event in events ) { |
| 164 | for ( var i = 0; i < events[event].length; i++ ) { |
| 165 | target.on( event, events[event][i] ); |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | /* reset stored events */ |
| 170 | target.removeData( 'storedEvents' ); |
| 171 | /* restoring onclick action */ |
| 172 | if ( 'undefined' != typeof target.attr( 'gglcptch-onclick' ) ) { |
| 173 | target.attr( 'onclick', target.attr( 'gglcptch-onclick' ) ); |
| 174 | target.removeAttr( 'gglcptch-onclick' ); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | function storeOnSubmit( form, gglcptch_index ) { |
| 179 | form.on( 'submit', function( e ) { |
| 180 | if ( '' == form.find( '.g-recaptcha-response' ).val() ) { |
| 181 | e.preventDefault(); |
| 182 | e.stopImmediatePropagation(); |
| 183 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 184 | targetEvent = e.type; |
| 185 | grecaptcha.execute( gglcptch_index ); |
| 186 | } |
| 187 | } ).find( 'input:submit, button' ).on( 'click', function( e ) { |
| 188 | if ( '' == form.find( '.g-recaptcha-response' ).val() ) { |
| 189 | e.preventDefault(); |
| 190 | e.stopImmediatePropagation(); |
| 191 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 192 | targetEvent = e.type; |
| 193 | grecaptcha.execute( gglcptch_index ); |
| 194 | } |
| 195 | } ); |
| 196 | } |
| 197 | |
| 198 | var gglcptch_version = gglcptch.options.version; |
| 199 | |
| 200 | if ( 'v2' == gglcptch_version ) { |
| 201 | if ( $( '#' + container ).parent().width() <= 300 && $( '#' + container ).parent().width() != 0 || $( window ).width() < 400 ) { |
| 202 | var size = 'compact'; |
| 203 | } else { |
| 204 | var size = 'normal'; |
| 205 | } |
| 206 | var parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'theme' : gglcptch.options.theme, 'size' : size }, |
| 207 | block = $( '#' + container ), |
| 208 | form = block.closest( 'form' ); |
| 209 | |
| 210 | /* Callback function works only in frontend */ |
| 211 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 212 | parameters['callback'] = function() { |
| 213 | form.find( 'button, input:submit' ).prop( 'disabled', false ); |
| 214 | }; |
| 215 | } |
| 216 | |
| 217 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 218 | $( '#' + container ).data( 'gglcptch_index', gglcptch_index ); |
| 219 | } else if ( 'invisible' == gglcptch_version ) { |
| 220 | var block = $( '#' + container ), |
| 221 | form = block.closest( 'form' ), |
| 222 | parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'size' : 'invisible', 'tabindex' : 9999 }, |
| 223 | targetObject = false, |
| 224 | targetEvent = false; |
| 225 | |
| 226 | if ( form.length ) { |
| 227 | storeEvents( form ); |
| 228 | form.find( 'button, input:submit' ).each( function() { |
| 229 | storeEvents( $( this ) ); |
| 230 | } ); |
| 231 | |
| 232 | /* Callback function works only in frontend */ |
| 233 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 234 | parameters['callback'] = function( token ) { |
| 235 | form.off(); |
| 236 | restoreEvents( form ); |
| 237 | form.find( 'button, input:submit' ).off().each( function() { |
| 238 | restoreEvents( $( this ) ); |
| 239 | } ); |
| 240 | if ( targetObject && targetEvent ) { |
| 241 | targetObject.trigger( targetEvent ); |
| 242 | } |
| 243 | form.find( 'button, input:submit' ).each( function() { |
| 244 | storeEvents( $( this ) ); |
| 245 | } ); |
| 246 | storeEvents( form ); |
| 247 | storeOnSubmit( form, gglcptch_index ); |
| 248 | grecaptcha.reset( gglcptch_index ); |
| 249 | }; |
| 250 | } |
| 251 | |
| 252 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 253 | block.data( { 'gglcptch_index' : gglcptch_index } ); |
| 254 | |
| 255 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 256 | storeOnSubmit( form, gglcptch_index ); |
| 257 | } |
| 258 | } |
| 259 | } |
| 260 | }; |
| 261 | |
| 262 | $( document ).ready( function() { |
| 263 | var tryCounter = 0, |
| 264 | /* launching timer so that the function keeps trying to display the reCAPTCHA again and again until google js api is loaded */ |
| 265 | gglcptch_timer = setInterval( function() { |
| 266 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 267 | try { |
| 268 | gglcptch.prepare(); |
| 269 | } catch ( e ) { |
| 270 | console.log( 'Unexpected error occurred: ', e ); |
| 271 | } |
| 272 | clearInterval( gglcptch_timer ); |
| 273 | } |
| 274 | tryCounter++; |
| 275 | /* Stop trying after 10 times */ |
| 276 | if ( tryCounter >= 10 ) { |
| 277 | clearInterval( gglcptch_timer ); |
| 278 | } |
| 279 | }, 1000 ); |
| 280 | |
| 281 | function gglcptch_prepare() { |
| 282 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 283 | try { |
| 284 | gglcptch.prepare(); |
| 285 | } catch ( err ) { |
| 286 | console.log( err ); |
| 287 | } |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | $( window ).on( 'load', gglcptch_prepare ); |
| 292 | |
| 293 | $( '.woocommerce' ).on( 'click', '.woocommerce-tabs', gglcptch_prepare ); |
| 294 | |
| 295 | $( '#recaptcha_widget_div' ).on( 'input paste change', '#recaptcha_response_field', cleanError ); |
| 296 | } ); |
| 297 | |
| 298 | function cleanError() { |
| 299 | $error = $( this ).parents( '#recaptcha_widget_div' ).next( '#gglcptch_error' ); |
| 300 | if ( $error.length ) { |
| 301 | $error.remove(); |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | function get_id() { |
| 306 | var id = 'gglcptch_recaptcha_' + Math.floor( Math.random() * 1000 ); |
| 307 | if ( $( '#' + id ).length ) { |
| 308 | id = get_id(); |
| 309 | } else { |
| 310 | return id; |
| 311 | } |
| 312 | } |
| 313 | |
| 314 | } )( jQuery, gglcptch ); |