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