PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.3.1
Defender Security – Malware Scanner, Login Security & Firewall v5.3.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 5.3.1, at assets/js/recaptcha_frontend.js

275 lines 8.5 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 var parameters = params ? params : { 'sitekey' : WPDEF.options.sitekey, 'theme' : WPDEF.options.theme, 'size' : WPDEF.options.size },
181 block = $( '#' + container ),
182 form = block.closest( 'form' );
183
184 // Callback function works only in frontend.
185 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
186 parameters['callback'] = function() {
187 form.find( 'button, input:submit' ).prop( 'disabled', false );
188 };
189 }
190
191 var grecaptcha_index = grecaptcha.render( container, parameters );
192 $( '#' + container ).data( 'grecaptcha_index', grecaptcha_index );
193 } else if ( 'v2_invisible' == grecaptcha_version ) {
194 var block = $( '#' + container ),
195 form = block.closest( 'form' ),
196 parameters = params ? params : { 'sitekey' : WPDEF.options.sitekey, 'size' : WPDEF.options.size, 'tabindex' : 9999, badge: 'inline' },
197 targetObject = false,
198 targetEvent = false;
199
200 if ( form.length ) {
201 form.find( 'button, input:submit' ).each( function() {
202 storeEvents( $( this ) );
203 } );
204
205 // Callback function works only in frontend.
206 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
207 parameters['callback'] = function( token ) {
208 form.off();
209 restoreEvents( form );
210 form.find( 'button, input:submit' ).off().each( function() {
211 restoreEvents( $( this ) );
212 } );
213 if ( targetObject && targetEvent ) {
214 targetObject.trigger( targetEvent );
215 }
216 form.find( 'button, input:submit' ).each( function() {
217 storeEvents( $( this ) );
218 } );
219 storeEvents( form );
220 storeOnSubmit( form, grecaptcha_index );
221 grecaptcha.reset( grecaptcha_index );
222 };
223 }
224
225 var grecaptcha_index = grecaptcha.render( container, parameters );
226 block.data( { 'grecaptcha_index' : grecaptcha_index } );
227
228 if ( ! $( 'body' ).hasClass( 'wp-admin' ) ) {
229 storeOnSubmit( form, grecaptcha_index );
230 }
231 }
232 }
233 };
234
235 $( function() {
236 var tryCounter = 0,
237 wpdef_timer = setInterval( function() {
238 if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) {
239 try {
240 WPDEF.prepare();
241 } catch ( e ) {
242 console.log( 'Unexpected error occurred: ', e );
243 }
244 clearInterval( wpdef_timer );
245 }
246 tryCounter++;
247 // Stop trying after 10 times.
248 if ( tryCounter >= 10 ) {
249 clearInterval( wpdef_timer );
250 }
251 }, 1000 );
252
253 function wpdef_prepare() {
254 if ( typeof Recaptcha != "undefined" || typeof grecaptcha != "undefined" ) {
255 try {
256 WPDEF.prepare();
257 } catch ( err ) {
258 console.log( err );
259 }
260 }
261 }
262
263 $( window ).on( 'load', wpdef_prepare );
264 } );
265
266 function get_id() {
267 var id = 'wpdef_recaptcha_' + Math.floor( Math.random() * 1000 );
268 if ( $( '#' + id ).length ) {
269 id = get_id();
270 } else {
271 return id;
272 }
273 }
274
275 } )( jQuery, WPDEF );