script.js
300 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 | // add attribute disable to the submit |
| 115 | if ( 'v2' === gglcptch.options.version && gglcptch.options.disable ) { |
| 116 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 117 | if ( 'setupform' === $( '#' + container ).closest( 'form' ).attr( 'id' ) ) { |
| 118 | setTimeout( |
| 119 | function(){ |
| 120 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 121 | }, |
| 122 | 200 |
| 123 | ); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | function storeEvents( el ) { |
| 128 | var target = el, |
| 129 | events = $._data( el.get( 0 ), 'events' ); |
| 130 | /* restoring events */ |
| 131 | if ( typeof events != 'undefined' ) { |
| 132 | var storedEvents = {}; |
| 133 | $.extend( true, storedEvents, events ); |
| 134 | target.off(); |
| 135 | target.data( 'storedEvents', storedEvents ); |
| 136 | } |
| 137 | /* storing and removing onclick action */ |
| 138 | if ( 'undefined' != typeof target.attr( 'onclick' ) ) { |
| 139 | target.attr( 'gglcptch-onclick', target.attr( 'onclick') ); |
| 140 | target.removeAttr( 'onclick' ); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | function restoreEvents( el ) { |
| 145 | var target = el, |
| 146 | events = target.data( 'storedEvents' ); |
| 147 | /* restoring events */ |
| 148 | if ( typeof events != 'undefined' ) { |
| 149 | for ( var event in events ) { |
| 150 | for ( var i = 0; i < events[event].length; i++ ) { |
| 151 | target.on( event, events[event][i] ); |
| 152 | } |
| 153 | } |
| 154 | } |
| 155 | /* reset stored events */ |
| 156 | target.removeData( 'storedEvents' ); |
| 157 | /* restoring onclick action */ |
| 158 | if ( 'undefined' != typeof target.attr( 'gglcptch-onclick' ) ) { |
| 159 | target.attr( 'onclick', target.attr( 'gglcptch-onclick' ) ); |
| 160 | target.removeAttr( 'gglcptch-onclick' ); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | function storeOnSubmit( form, gglcptch_index ) { |
| 165 | form.on( 'submit', function( e ) { |
| 166 | if ( '' == form.find( '.g-recaptcha-response' ).val() ) { |
| 167 | e.preventDefault(); |
| 168 | e.stopImmediatePropagation(); |
| 169 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 170 | targetEvent = e.type; |
| 171 | grecaptcha.execute( gglcptch_index ); |
| 172 | } |
| 173 | } ).find( 'input:submit, button' ).on( 'click', function( e ) { |
| 174 | if ( '' == form.find( '.g-recaptcha-response' ).val() ) { |
| 175 | e.preventDefault(); |
| 176 | e.stopImmediatePropagation(); |
| 177 | targetObject = $( e.target || e.srcElement || e.targetObject ); |
| 178 | targetEvent = e.type; |
| 179 | grecaptcha.execute( gglcptch_index ); |
| 180 | } |
| 181 | } ); |
| 182 | } |
| 183 | |
| 184 | var gglcptch_version = gglcptch.options.version; |
| 185 | |
| 186 | if ( 'v2' == gglcptch_version ) { |
| 187 | if ( $( '#' + container ).parent().width() <= 300 && $( '#' + container ).parent().width() != 0 || $( window ).width() < 400 ) { |
| 188 | var size = 'compact'; |
| 189 | } else { |
| 190 | var size = 'normal'; |
| 191 | } |
| 192 | var parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'theme' : gglcptch.options.theme, 'size' : size }, |
| 193 | block = $( '#' + container ), |
| 194 | form = block.closest( 'form' ); |
| 195 | |
| 196 | /* Callback function works only in frontend */ |
| 197 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 198 | parameters['callback'] = function() { |
| 199 | form.find( 'button, input:submit' ).prop( 'disabled', false ); |
| 200 | }; |
| 201 | } |
| 202 | |
| 203 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 204 | $( '#' + container ).data( 'gglcptch_index', gglcptch_index ); |
| 205 | } else if ( 'invisible' == gglcptch_version ) { |
| 206 | var block = $( '#' + container ), |
| 207 | form = block.closest( 'form' ), |
| 208 | parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'size' : 'invisible', 'tabindex' : 9999 }, |
| 209 | targetObject = false, |
| 210 | targetEvent = false; |
| 211 | |
| 212 | if ( form.length ) { |
| 213 | storeEvents( form ); |
| 214 | form.find( 'button, input:submit' ).each( function() { |
| 215 | storeEvents( $( this ) ); |
| 216 | } ); |
| 217 | |
| 218 | /* Callback function works only in frontend */ |
| 219 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 220 | parameters['callback'] = function( token ) { |
| 221 | form.off(); |
| 222 | restoreEvents( form ); |
| 223 | form.find( 'button, input:submit' ).off().each( function() { |
| 224 | restoreEvents( $( this ) ); |
| 225 | } ); |
| 226 | if ( targetObject && targetEvent ) { |
| 227 | targetObject.trigger( targetEvent ); |
| 228 | } |
| 229 | form.find( 'button, input:submit' ).each( function() { |
| 230 | storeEvents( $( this ) ); |
| 231 | } ); |
| 232 | storeEvents( form ); |
| 233 | storeOnSubmit( form, gglcptch_index ); |
| 234 | grecaptcha.reset( gglcptch_index ); |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 239 | block.data( { 'gglcptch_index' : gglcptch_index } ); |
| 240 | |
| 241 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 242 | storeOnSubmit( form, gglcptch_index ); |
| 243 | } |
| 244 | } |
| 245 | } |
| 246 | }; |
| 247 | |
| 248 | $( document ).ready( function() { |
| 249 | var tryCounter = 0, |
| 250 | /* launching timer so that the function keeps trying to display the reCAPTCHA again and again until google js api is loaded */ |
| 251 | gglcptch_timer = setInterval( function() { |
| 252 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 253 | try { |
| 254 | gglcptch.prepare(); |
| 255 | } catch ( e ) { |
| 256 | console.log( 'Unexpected error occurred: ', e ); |
| 257 | } |
| 258 | clearInterval( gglcptch_timer ); |
| 259 | } |
| 260 | tryCounter++; |
| 261 | /* Stop trying after 10 times */ |
| 262 | if ( tryCounter >= 10 ) { |
| 263 | clearInterval( gglcptch_timer ); |
| 264 | } |
| 265 | }, 1000 ); |
| 266 | |
| 267 | function gglcptch_prepare() { |
| 268 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 269 | try { |
| 270 | gglcptch.prepare(); |
| 271 | } catch ( err ) { |
| 272 | console.log( err ); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | $( window ).on( 'load', gglcptch_prepare ); |
| 278 | |
| 279 | $( '.woocommerce' ).on( 'click', '.woocommerce-tabs', gglcptch_prepare ); |
| 280 | |
| 281 | $( '#recaptcha_widget_div' ).on( 'input paste change', '#recaptcha_response_field', cleanError ); |
| 282 | } ); |
| 283 | |
| 284 | function cleanError() { |
| 285 | $error = $( this ).parents( '#recaptcha_widget_div' ).next( '#gglcptch_error' ); |
| 286 | if ( $error.length ) { |
| 287 | $error.remove(); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | function get_id() { |
| 292 | var id = 'gglcptch_recaptcha_' + Math.floor( Math.random() * 1000 ); |
| 293 | if ( $( '#' + id ).length ) { |
| 294 | id = get_id(); |
| 295 | } else { |
| 296 | return id; |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | } )( jQuery, gglcptch ); |