script.js
260 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 | /* |
| 45 | * count the number of reCAPTCHA blocks in the form |
| 46 | */ |
| 47 | $( 'form' ).each( function() { |
| 48 | if ( $( this ).contents().find( 'iframe[title="recaptcha widget"]' ).length > 1 && ! $( this ).children( '.gglcptch_dublicate_error' ).length ) { |
| 49 | $( this ).prepend( '<div class="gglcptch_dublicate_error error" style="color: red;">' + gglcptch.options.error + '</div><br />\n' ); |
| 50 | } |
| 51 | } ); |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | gglcptch.display = function( container, params ) { |
| 56 | if ( typeof( container ) == 'undefined' || container == '' || typeof( gglcptch.options ) == 'undefined' ) { |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | if ( 'v2' === gglcptch.options.version && $( '#' + container ).parents( '.gglcptch_custom' ).length > 0 ) { |
| 61 | if ( $( '#' + container ).find( 'textarea#g-recaptcha-response' ).length == 0 ) { |
| 62 | setTimeout( |
| 63 | function(){ |
| 64 | $( '#' + container ).find( 'textarea#g-recaptcha-response' ).remove(); |
| 65 | }, |
| 66 | 1000 |
| 67 | ); |
| 68 | } else { |
| 69 | $( '#' + container ).find( 'textarea#g-recaptcha-response' ).remove(); |
| 70 | } |
| 71 | $( '.gglcptch.gglcptch_v2 noscript' ).remove(); |
| 72 | } |
| 73 | |
| 74 | // add attribute disable to the submit |
| 75 | if ( 'v2' === gglcptch.options.version && gglcptch.options.disable ) { |
| 76 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 77 | if ( 'setupform' === $( '#' + container ).closest( 'form' ).attr( 'id' ) ) { |
| 78 | setTimeout( |
| 79 | function(){ |
| 80 | $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true ); |
| 81 | }, |
| 82 | 200 |
| 83 | ); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | function storeEvents( el ) { |
| 88 | var target = el, |
| 89 | events = $._data( el.get( 0 ), 'events' ); |
| 90 | /* restoring events */ |
| 91 | if ( typeof events != 'undefined' ) { |
| 92 | var storedEvents = {}; |
| 93 | $.extend( true, storedEvents, events ); |
| 94 | target.off(); |
| 95 | target.data( 'storedEvents', storedEvents ); |
| 96 | } |
| 97 | /* storing and removing onclick action */ |
| 98 | if ( 'undefined' != typeof target.attr( 'onclick' ) ) { |
| 99 | target.attr( 'gglcptch-onclick', target.attr( 'onclick') ); |
| 100 | target.removeAttr( 'onclick' ); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | function restoreEvents( el ) { |
| 105 | var target = el, |
| 106 | events = target.data( 'storedEvents' ); |
| 107 | /* restoring events */ |
| 108 | if ( typeof events != 'undefined' ) { |
| 109 | for ( var event in events ) { |
| 110 | for ( var i = 0; i < events[event].length; i++ ) { |
| 111 | target.on( event, events[event][i] ); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | /* reset stored events */ |
| 116 | target.removeData( 'storedEvents' ); |
| 117 | /* restoring onclick action */ |
| 118 | if ( 'undefined' != typeof target.attr( 'gglcptch-onclick' ) ) { |
| 119 | target.attr( 'onclick', target.attr( 'gglcptch-onclick' ) ); |
| 120 | target.removeAttr( 'gglcptch-onclick' ); |
| 121 | } |
| 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 | |
| 146 | if ( 'v2' == gglcptch_version ) { |
| 147 | if ( $( '#' + container ).parent().width() <= 300 && $( '#' + container ).parent().width() != 0 || $( window ).width() < 400 ) { |
| 148 | var size = 'compact'; |
| 149 | } else { |
| 150 | var size = 'normal'; |
| 151 | } |
| 152 | var parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'theme' : gglcptch.options.theme, 'size' : size }, |
| 153 | block = $( '#' + container ), |
| 154 | form = block.closest( 'form' ); |
| 155 | |
| 156 | /* Callback function works only in frontend */ |
| 157 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 158 | parameters['callback'] = function() { |
| 159 | form.find( 'button, input:submit' ).prop( 'disabled', false ); |
| 160 | }; |
| 161 | } |
| 162 | |
| 163 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 164 | $( '#' + container ).data( 'gglcptch_index', gglcptch_index ); |
| 165 | } else if ( 'invisible' == gglcptch_version ) { |
| 166 | var block = $( '#' + container ), |
| 167 | form = block.closest( 'form' ), |
| 168 | parameters = params ? params : { 'sitekey' : gglcptch.options.sitekey, 'size' : 'invisible', 'tabindex' : 9999 }, |
| 169 | targetObject = false, |
| 170 | targetEvent = false; |
| 171 | |
| 172 | if ( form.length ) { |
| 173 | storeEvents( form ); |
| 174 | form.find( 'button, input:submit' ).each( function() { |
| 175 | storeEvents( $( this ) ); |
| 176 | } ); |
| 177 | |
| 178 | /* Callback function works only in frontend */ |
| 179 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 180 | parameters['callback'] = function( token ) { |
| 181 | form.off(); |
| 182 | restoreEvents( form ); |
| 183 | form.find( 'button, input:submit' ).off().each( function() { |
| 184 | restoreEvents( $( this ) ); |
| 185 | } ); |
| 186 | if ( targetObject && targetEvent ) { |
| 187 | targetObject.trigger( targetEvent ); |
| 188 | } |
| 189 | form.find( 'button, input:submit' ).each( function() { |
| 190 | storeEvents( $( this ) ); |
| 191 | } ); |
| 192 | storeEvents( form ); |
| 193 | storeOnSubmit( form, gglcptch_index ); |
| 194 | grecaptcha.reset( gglcptch_index ); |
| 195 | }; |
| 196 | } |
| 197 | |
| 198 | var gglcptch_index = grecaptcha.render( container, parameters ); |
| 199 | block.data( { 'gglcptch_index' : gglcptch_index } ); |
| 200 | |
| 201 | if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) { |
| 202 | storeOnSubmit( form, gglcptch_index ); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | }; |
| 207 | |
| 208 | $( document ).ready( function() { |
| 209 | var tryCounter = 0, |
| 210 | /* launching timer so that the function keeps trying to display the reCAPTCHA again and again until google js api is loaded */ |
| 211 | gglcptch_timer = setInterval( function() { |
| 212 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 213 | try { |
| 214 | gglcptch.prepare(); |
| 215 | } catch ( e ) { |
| 216 | console.log( 'Unexpected error occurred: ', e ); |
| 217 | } |
| 218 | clearInterval( gglcptch_timer ); |
| 219 | } |
| 220 | tryCounter++; |
| 221 | /* Stop trying after 10 times */ |
| 222 | if ( tryCounter >= 10 ) { |
| 223 | clearInterval( gglcptch_timer ); |
| 224 | } |
| 225 | }, 1000 ); |
| 226 | |
| 227 | function gglcptch_prepare() { |
| 228 | if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) { |
| 229 | try { |
| 230 | gglcptch.prepare(); |
| 231 | } catch ( err ) { |
| 232 | console.log( err ); |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | $( window ).on( 'load', gglcptch_prepare ); |
| 238 | |
| 239 | $( '.woocommerce' ).on( 'click', '.woocommerce-tabs', gglcptch_prepare ); |
| 240 | |
| 241 | $( '#recaptcha_widget_div' ).on( 'input paste change', '#recaptcha_response_field', cleanError ); |
| 242 | } ); |
| 243 | |
| 244 | function cleanError() { |
| 245 | $error = $( this ).parents( '#recaptcha_widget_div' ).next( '#gglcptch_error' ); |
| 246 | if ( $error.length ) { |
| 247 | $error.remove(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | function get_id() { |
| 252 | var id = 'gglcptch_recaptcha_' + Math.floor( Math.random() * 1000 ); |
| 253 | if ( $( '#' + id ).length ) { |
| 254 | id = get_id(); |
| 255 | } else { |
| 256 | return id; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | } )( jQuery, gglcptch ); |