PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / assets / js / recaptcha_frontend.js

recaptcha_frontend.js in Defender Security – Malware Scanner, Login Security & Firewall 3.0.1, at assets/js/recaptcha_frontend.js

282 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 ( function( $, WPDEF ) {
2 WPDEF = WPDEF || {};
3
4 WPDEF.prepare = function() {
5 // Display reCaptcha for plugin`s block. Also check if elements exists when loaded via lazy loading.
6 var tryReCaptchaCounter = 0,
7 wpdefRecaptchaTimer = setInterval( function() {
8 if ( $( '.wpdef_recaptcha_v2_checkbox, .wpdef_recaptcha_v2_invisible' ).length > 0 ) {
9 $( '.wpdef_recaptcha_v2_checkbox, .wpdef_recaptcha_v2_invisible' ).each( function() {
10 var container = $( this ).find( '.wpdef_recaptcha' );
11
12 if (
13 container.is( ':empty' ) &&
14 ( WPDEF.vars.visibility || $( this ).is( ':visible' ) === $( this ).is( ':not(:hidden)' ) )
15 ) {
16 var containerId = container.attr( 'id' );
17 WPDEF.display( containerId );
18
19 // disable input field in noscript
20 $( this ).find( 'noscript #g-recaptcha-response' ).prop( 'disabled', true );
21 }
22 } );
23
24 clearInterval( wpdefRecaptchaTimer );
25 }
26 tryReCaptchaCounter++;
27 // Stop trying after 20 times.
28 if ( tryReCaptchaCounter >= 20 ) {
29 clearInterval( wpdefRecaptchaTimer );
30 }
31 }, 1000 );
32
33 if ( 'v3_recaptcha' == WPDEF.options.version ) {
34 grecaptcha.ready( function() {
35 grecaptcha.execute( WPDEF.options.sitekey, {action: 'WPDEF_reCaptcha'}).then(function( token ) {
36 document.querySelectorAll( "#g-recaptcha-response" ).forEach( function ( elem ) { elem.value = token } );
37 });
38 });
39 }
40
41 /*
42 * Display google reCaptcha for others blocks. It's necessary because
43 * we have disabled the connection to Google reCaptcha API from other plugins.
44 */
45 if ( 'v2_checkbox' == WPDEF.options.version || 'v2_invisible' == WPDEF.options.version ) {
46
47 $( '.g-recaptcha' ).each( function() {
48 // reCAPTCHA will be generated into the empty block only.
49 if ( $( this ).html() === '' && $( this ).text() === '' ) {
50
51 // Get element`s ID.
52 var container = $( this ).attr( 'id' );
53
54 if ( typeof container == 'undefined' ) {
55 container = get_id();
56 $( this ).attr( 'id', container );
57 }
58
59 // Get reCaptcha parameters.
60 var sitekey = $( this ).attr( 'data-sitekey' ),
61 theme = $( this ).attr( 'data-theme' ),
62 lang = $( this ).attr( 'data-lang' ),
63 size = $( this ).attr( 'data-size' ),
64 type = $( this ).attr( 'data-type' ),
65 tabindex = $( this ).attr( 'data-tabindex' ),
66 callback = $( this ).attr( 'data-callback' ),
67 ex_call = $( this ).attr( 'data-expired-callback' ),
68 stoken = $( this ).attr( 'data-stoken' ),
69 params = [];
70
71 params['sitekey'] = sitekey ? sitekey : WPDEF.options.sitekey;
72 if ( !! theme ) {
73 params['theme'] = theme;
74 }
75 if ( !! lang ) {
76 params['lang'] = lang;
77 }
78 if ( !! size ) {
79 params['size'] = size;
80 }
81 if ( !! type ) {
82 params['type'] = type;
83 }
84 if ( !! tabindex ) {
85 params['tabindex'] = tabindex;
86 }
87 if ( !! callback ) {
88 params['callback'] = callback;
89 }
90 if ( !! ex_call ) {
91 params['expired-callback'] = ex_call;
92 }
93 if ( !! stoken ) {
94 params['stoken'] = stoken;
95 }
96
97 WPDEF.display( container, params );
98 }
99 } );
100
101 // Count the number of reCAPTCHA blocks in the form.
102 $( 'form' ).each( function() {
103 if ( $( this ).contents().find( 'iframe[title="recaptcha widget"]' ).length > 1 && ! $( this ).children( '.grecaptcha_dublicate_error' ).length ) {
104 $( this ).prepend( '<div class="grecaptcha_dublicate_error error" style="color: red;">' + WPDEF.options.error + '</div><br />\n' );
105 }
106 } );
107 }
108 };
109
110 WPDEF.display = function( container, params ) {
111 if ( typeof( container ) == 'undefined' || container == '' || typeof( WPDEF.options ) == 'undefined' ) {
112 return;
113 }
114
115 // Add attribute disable to the submit.
116 if ( 'v2_checkbox' === WPDEF.options.version && WPDEF.options.disable ) {
117 $( '#' + container ).closest( 'form' ).find( 'input:submit, button' ).prop( 'disabled', true );
118 }
119
120 function storeEvents( el ) {
121 var target = el,
122 events = $._data( el.get( 0 ), 'events' );
123 // Restoring events.
124 if ( typeof events != 'undefined' ) {
125 var storedEvents = {};
126 $.extend( true, storedEvents, events );
127 target.off();
128 target.data( 'storedEvents', storedEvents );
129 }
130 // Storing and removing onclick action.
131 if ( 'undefined' != typeof target.attr( 'onclick' ) ) {
132 target.attr( 'wpdef-onclick', target.attr( 'onclick') );
133 target.removeAttr( 'onclick' );
134 }
135 }
136
137 function restoreEvents( el ) {
138 var target = el,
139 events = target.data( 'storedEvents' );
140 // Restoring events.
141 if ( typeof events != 'undefined' ) {
142 for ( var event in events ) {
143 for ( var i = 0; i < events[event].length; i++ ) {
144 target.on( event, events[event][i] );
145 }
146 }
147 }
148 // Reset stored events.
149 target.removeData( 'storedEvents' );
150 // Restoring onclick action.
151 if ( 'undefined' != typeof target.attr( 'wpdef-onclick' ) ) {
152 target.attr( 'onclick', target.attr( 'wpdef-onclick' ) );
153 target.removeAttr( 'wpdef-onclick' );
154 }
155 }
156
157 function storeOnSubmit( form, grecaptcha_index ) {
158 form.on( 'submit', function( e ) {
159 if ( '' == form.find( '.g-recaptcha-response' ).val() ) {
160 e.preventDefault();
161 e.stopImmediatePropagation();
162 targetObject = $( e.target || e.srcElement || e.targetObject );
163 targetEvent = e.type;
164 grecaptcha.execute( grecaptcha_index );
165 }
166 } ).find( 'input:submit, button' ).on( 'click', function( e ) {
167 if ( '' == form.find( '.g-recaptcha-response' ).val() ) {
168 e.preventDefault();
169 e.stopImmediatePropagation();
170 targetObject = $( e.target || e.srcElement || e.targetObject );
171 targetEvent = e.type;
172 grecaptcha.execute( grecaptcha_index );
173 }
174 } );
175 }
176
177 var grecaptcha_version = WPDEF.options.version;
178
179 if ( 'v2_checkbox' == grecaptcha_version ) {
180 if ( $( '#' + container ).parent().width() <= 300 && $( '#' + container ).parent().width() != 0 || $( window ).width() < 400 ) {
181 var size = 'compact';
182 } else {
183 var size = 'normal';
184 }
185 var parameters = params ? params : { 'sitekey' : WPDEF.options.sitekey, 'theme' : WPDEF.options.theme, 'size' : size },
186 block = $( '#' + container ),
187 form = block.closest( 'form' );
188
189 // Callback function works only in frontend.
190 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
191 parameters['callback'] = function() {
192 form.find( 'button, input:submit' ).prop( 'disabled', false );
193 };
194 }
195
196 var grecaptcha_index = grecaptcha.render( container, parameters );
197 $( '#' + container ).data( 'grecaptcha_index', grecaptcha_index );
198 } else if ( 'v2_invisible' == grecaptcha_version ) {
199 var block = $( '#' + container ),
200 form = block.closest( 'form' ),
201 parameters = params ? params : { 'sitekey' : WPDEF.options.sitekey, 'size' : WPDEF.options.size, 'tabindex' : 9999, badge: 'inline' },
202 targetObject = false,
203 targetEvent = false;
204
205 if ( form.length ) {
206 storeEvents( form );
207 form.find( 'button, input:submit' ).each( function() {
208 storeEvents( $( this ) );
209 } );
210
211 // Callback function works only in frontend.
212 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
213 parameters['callback'] = function( token ) {
214 form.off();
215 restoreEvents( form );
216 form.find( 'button, input:submit' ).off().each( function() {
217 restoreEvents( $( this ) );
218 } );
219 if ( targetObject && targetEvent ) {
220 targetObject.trigger( targetEvent );
221 }
222 form.find( 'button, input:submit' ).each( function() {
223 storeEvents( $( this ) );
224 } );
225 storeEvents( form );
226 storeOnSubmit( form, grecaptcha_index );
227 grecaptcha.reset( grecaptcha_index );
228 };
229 }
230
231 var grecaptcha_index = grecaptcha.render( container, parameters );
232 block.data( { 'grecaptcha_index' : grecaptcha_index } );
233
234 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
235 storeOnSubmit( form, grecaptcha_index );
236 }
237 }
238 }
239 };
240
241 $( function() {
242 var tryCounter = 0,
243 wpdef_timer = setInterval( function() {
244 if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) {
245 try {
246 WPDEF.prepare();
247 } catch ( e ) {
248 console.log( 'Unexpected error occurred: ', e );
249 }
250 clearInterval( wpdef_timer );
251 }
252 tryCounter++;
253 // Stop trying after 10 times.
254 if ( tryCounter >= 10 ) {
255 clearInterval( wpdef_timer );
256 }
257 }, 1000 );
258
259 function wpdef_prepare() {
260 if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) {
261 try {
262 WPDEF.prepare();
263 } catch ( err ) {
264 console.log( err );
265 }
266 }
267 }
268
269 $( window ).on( 'load', wpdef_prepare );
270 } );
271
272 function get_id() {
273 var id = 'wpdef_recaptcha_' + Math.floor( Math.random() * 1000 );
274 if ( $( '#' + id ).length ) {
275 id = get_id();
276 } else {
277 return id;
278 }
279 }
280
281 } )( jQuery, WPDEF );
282