Total Complexity | 163 |
Complexity/F | 1.92 |
Lines of Code | 1107 |
Function Count | 85 |
Duplicated Lines | 225 |
Ratio | 20.33 % |
Changes | 3 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like assets/js/admin.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** |
||
15 | (function() { |
||
16 | var list, dbjsError, |
||
17 | errors = []; |
||
18 | |||
19 | window.onerror = function( errorMsg, url, lineNumber ) { |
||
20 | if ( ! document.getElementById( 'monsterinsights-ublock-origin-error' ) ) |
||
21 | errors[ errors.length ] = [errorMsg, url, lineNumber]; |
||
|
|||
22 | else |
||
23 | dbjsError(errorMsg, url, lineNumber); |
||
24 | }; |
||
25 | |||
26 | jQuery(document).ready( function(){ |
||
27 | for ( var err in errors ) |
||
28 | dbjsError( errors[err][0], errors[err][1], errors[err][2] ); |
||
29 | |||
30 | }); |
||
31 | |||
32 | dbjsError = function( errorMsg, url, lineNumber ) { |
||
33 | |||
34 | var errorLine, place, button, tab; |
||
35 | |||
36 | |||
37 | if ( !list ) |
||
38 | list = document.getElementById( 'monsterinsights-ublock-origin-error' ); |
||
39 | |||
40 | if (!list ) |
||
41 | return; // threw way too early... @todo cache these? |
||
42 | |||
43 | errorLine = document.createElement( 'li' ); |
||
44 | errorLine.className = 'debug-bar-js-error'; |
||
45 | errorLine.textContent = errorMsg + ' on '; |
||
46 | place = document.createElement( 'span' ); |
||
47 | place.textContent = url + ' line ' + lineNumber; |
||
48 | errorLine.appendChild( place ); |
||
49 | list.appendChild( errorLine ); |
||
50 | |||
51 | }; |
||
52 | |||
53 | })(); |
||
54 | |||
55 | jQuery( document ).ready( function( $ ) { |
||
56 | // Disable function |
||
57 | jQuery.fn.extend({ |
||
58 | disable: function(state) { |
||
59 | return this.each(function() { |
||
60 | this.disabled = state; |
||
61 | }); |
||
62 | } |
||
63 | }); |
||
64 | |||
65 | jQuery("#screen-meta-links").prependTo("#monsterinsights-header-temp"); |
||
66 | jQuery("#screen-meta").prependTo("#monsterinsights-header-temp"); |
||
67 | |||
68 | // Tooltips |
||
69 | jQuery('.monsterinsights-help-tip').tooltip({ |
||
70 | content: function() { |
||
71 | return jQuery(this).prop('title'); |
||
72 | }, |
||
73 | tooltipClass: 'monsterinsights-ui-tooltip', |
||
74 | position: { |
||
75 | my: 'center top', |
||
76 | at: 'center bottom+10', |
||
77 | collision: 'flipfit', |
||
78 | }, |
||
79 | hide: { |
||
80 | duration: 200, |
||
81 | }, |
||
82 | show: { |
||
83 | duration: 200, |
||
84 | }, |
||
85 | }); |
||
86 | |||
87 | // Reports Tooltips |
||
88 | jQuery("body").tooltip({ |
||
89 | selector: '.monsterinsights-reports-uright-tooltip', |
||
90 | items: "[data-tooltip-title], [data-tooltip-description]", |
||
91 | content: function() { |
||
92 | return '<div class="monsterinsights-reports-tooltip-title">' + jQuery(this).data("tooltip-title") + '</div>' + |
||
93 | '<div class="monsterinsights-reports-tooltip-content">' + jQuery(this).data("tooltip-description") + '</div>'; |
||
94 | }, |
||
95 | tooltipClass: 'monsterinsights-reports-ui-tooltip', |
||
96 | position: { my: "right-10 top", at: "left top", collision: "flipfit" }, |
||
97 | hide: {duration: 200}, |
||
98 | show: {duration: 200}, |
||
99 | }); |
||
100 | |||
101 | /** |
||
102 | * Copy to Clipboard |
||
103 | */ |
||
104 | if ( typeof Clipboard !== 'undefined' ) { |
||
105 | var monsterinsights_clipboard = new Clipboard( '.monsterinsights-copy-to-clipboard' ); |
||
106 | jQuery( document ).on( 'click', '.monsterinsights-copy-to-clipboard', function( e ) { |
||
107 | e.preventDefault(); |
||
108 | } ); |
||
109 | |||
110 | function fallbackMessage(action){ |
||
111 | var actionMsg='';var actionKey=(action==='cut'?'X':'C'); |
||
112 | if (/iPhone|iPad/i.test(navigator.userAgent ) ) { |
||
113 | actionMsg='No support :('; |
||
114 | } else if (/Mac/i.test(navigator.userAgent ) ) { |
||
115 | actionMsg='Press ⌘-'+ actionKey+' to '+ action; |
||
116 | } else { |
||
117 | actionMsg='Press Ctrl-'+ actionKey+' to '+ action; |
||
118 | } |
||
119 | return actionMsg; |
||
120 | } |
||
121 | monsterinsights_clipboard.on('success',function(e){ |
||
122 | e.trigger.textContent = monsterinsights_admin.copied; |
||
123 | window.setTimeout(function() { |
||
124 | e.trigger.textContent = monsterinsights_admin.copytoclip; |
||
125 | }, 2000); |
||
126 | }); |
||
127 | monsterinsights_clipboard.on('error',function(e){ |
||
128 | e.trigger.textContent = fallbackMessage(e.action); |
||
129 | }); |
||
130 | } |
||
131 | |||
132 | function modelMatcher(params, data) { |
||
133 | data.parentText = data.parentText || ""; |
||
134 | |||
135 | // Always return the object if there is nothing to compare |
||
136 | if (jQuery.trim(params.term) === '') { |
||
137 | return data; |
||
138 | } |
||
139 | |||
140 | // Do a recursive check for options with children |
||
141 | if (data.children && data.children.length > 0) { |
||
142 | // Clone the data object if there are children |
||
143 | // This is required as we modify the object to remove any non-matches |
||
144 | var match = $.extend(true, {}, data); |
||
145 | |||
146 | // Check each child of the option |
||
147 | for (var c = data.children.length - 1; c >= 0; c--) { |
||
148 | var child = data.children[c]; |
||
149 | child.parentText += data.parentText + " " + data.text; |
||
150 | |||
151 | var matches = modelMatcher(params, child); |
||
152 | |||
153 | // If there wasn't a match, remove the object in the array |
||
154 | if (matches == null) { |
||
155 | match.children.splice(c, 1); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | // If any children matched, return the new object |
||
160 | if (match.children.length > 0) { |
||
161 | return match; |
||
162 | } |
||
163 | |||
164 | // If there were no matching children, check just the plain object |
||
165 | return modelMatcher(params, match); |
||
166 | } |
||
167 | |||
168 | // If the typed-in term matches the text of this term, or the text from any |
||
169 | // parent term, then it's a match. |
||
170 | var original = (data.parentText + ' ' + data.text).toUpperCase(); |
||
171 | var term = params.term.toUpperCase(); |
||
172 | |||
173 | // Check if the text contains the term |
||
174 | if (original.indexOf(term) > -1) { |
||
175 | return data; |
||
176 | } |
||
177 | |||
178 | // If it doesn't contain the term, don't return anything |
||
179 | return null; |
||
180 | } |
||
181 | |||
182 | |||
183 | // Setup Select2 |
||
184 | jQuery('.monsterinsights-select300').select300(); |
||
185 | |||
186 | var fields_changed = false; |
||
187 | jQuery(document).on('change', '#monsterinsights-settings :input', function(){ |
||
188 | fields_changed = true; |
||
189 | }); |
||
190 | |||
191 | jQuery(document).on('click', 'a:not(.monsterinsights-settings-click-excluded)', function( e ){ |
||
192 | |||
193 | if ( fields_changed ) { |
||
194 | var answer = confirm( monsterinsights_admin.settings_changed_confirm ); |
||
195 | if ( answer ){ |
||
196 | fields_changed = false; |
||
197 | return true; |
||
198 | } else { |
||
199 | View Code Duplication | e.preventDefault(); |
|
200 | return false; |
||
201 | } |
||
202 | } |
||
203 | }); |
||
204 | |||
205 | |||
206 | // Auth Actions |
||
207 | // Auth and Reauth |
||
208 | jQuery('#monsterinsights-google-authenticate-submit').on( "click", function( e ) { |
||
209 | e.preventDefault(); |
||
210 | swal({ |
||
211 | type: 'info', |
||
212 | title: monsterinsights_admin.redirect_loading_title_text, |
||
213 | text: monsterinsights_admin.redirect_loading_text_text, |
||
214 | allowOutsideClick: false, |
||
215 | allowEscapeKey: false, |
||
216 | allowEnterKey: false, |
||
217 | onOpen: function () { |
||
218 | swal.showLoading(); |
||
219 | } |
||
220 | }).catch(swal.noop); |
||
221 | var data = { |
||
222 | 'action': 'monsterinsights_maybe_authenticate', |
||
223 | 'nonce': monsterinsights_admin.admin_nonce, |
||
224 | 'isnetwork': monsterinsights_admin.isnetwork |
||
225 | }; |
||
226 | jQuery.post(ajaxurl, data, function( response ) { |
||
227 | if ( response.success ) { |
||
228 | window.location = response.data.redirect; |
||
229 | } else { |
||
230 | swal({ |
||
231 | type: 'error', |
||
232 | title: monsterinsights_admin.redirect_loading_error_title, |
||
233 | text: response.data.message, |
||
234 | confirmButtonText: monsterinsights_admin.ok_text, |
||
235 | }).catch(swal.noop); |
||
236 | } |
||
237 | }).fail( function(xhr, textStatus, errorThrown) { |
||
238 | var message = jQuery(xhr.responseText).text(); |
||
239 | message = message.substring(0, message.indexOf("Call Stack")); |
||
240 | swal({ |
||
241 | View Code Duplication | type: 'error', |
|
242 | title: monsterinsights_admin.redirect_loading_error_title, |
||
243 | text: message, |
||
244 | confirmButtonText: monsterinsights_admin.ok_text, |
||
245 | }).catch(swal.noop); |
||
246 | }); |
||
247 | }); |
||
248 | |||
249 | // Reauth |
||
250 | jQuery('#monsterinsights-google-reauthenticate-submit').on( "click", function( e ) { |
||
251 | e.preventDefault(); |
||
252 | swal({ |
||
253 | type: 'info', |
||
254 | title: monsterinsights_admin.redirect_loading_title_text, |
||
255 | text: monsterinsights_admin.redirect_loading_text_text, |
||
256 | allowOutsideClick: false, |
||
257 | allowEscapeKey: false, |
||
258 | allowEnterKey: false, |
||
259 | onOpen: function () { |
||
260 | swal.showLoading(); |
||
261 | } |
||
262 | }).catch(swal.noop); |
||
263 | var data = { |
||
264 | 'action': 'monsterinsights_maybe_reauthenticate', |
||
265 | 'nonce': monsterinsights_admin.admin_nonce, |
||
266 | 'isnetwork': monsterinsights_admin.isnetwork |
||
267 | }; |
||
268 | jQuery.post(ajaxurl, data, function( response ) { |
||
269 | if ( response.success ) { |
||
270 | window.location = response.data.redirect; |
||
271 | } else { |
||
272 | swal({ |
||
273 | type: 'error', |
||
274 | title: monsterinsights_admin.redirect_loading_error_title, |
||
275 | text: response.data.message, |
||
276 | confirmButtonText: monsterinsights_admin.ok_text, |
||
277 | }).catch(swal.noop); |
||
278 | } |
||
279 | }).fail( function(xhr, textStatus, errorThrown) { |
||
280 | var message = jQuery(xhr.responseText).text(); |
||
281 | message = message.substring(0, message.indexOf("Call Stack")); |
||
282 | swal({ |
||
283 | type: 'error', |
||
284 | title: monsterinsights_admin.redirect_loading_error_title, |
||
285 | text: message, |
||
286 | confirmButtonText: monsterinsights_admin.ok_text, |
||
287 | }).catch(swal.noop); |
||
288 | }); |
||
289 | }); |
||
290 | |||
291 | // Verify |
||
292 | jQuery('#monsterinsights-google-verify-submit').on( "click", function( e ) { |
||
293 | e.preventDefault(); |
||
294 | swal({ |
||
295 | type: 'info', |
||
296 | title: monsterinsights_admin.verify_loading_title_text, |
||
297 | text: monsterinsights_admin.verify_loading_text_text, |
||
298 | allowOutsideClick: false, |
||
299 | allowEscapeKey: false, |
||
300 | allowEnterKey: false, |
||
301 | onOpen: function () { |
||
302 | swal.showLoading(); |
||
303 | } |
||
304 | }).catch(swal.noop); |
||
305 | var data = { |
||
306 | 'action': 'monsterinsights_maybe_verify', |
||
307 | 'nonce': monsterinsights_admin.admin_nonce, |
||
308 | 'isnetwork': monsterinsights_admin.isnetwork |
||
309 | }; |
||
310 | jQuery.post(ajaxurl, data, function( response ) { |
||
311 | if ( response.success ) { |
||
312 | swal({ |
||
313 | type: 'success', |
||
314 | title: monsterinsights_admin.verify_success_title_text, |
||
315 | text: monsterinsights_admin.verify_success_text_text, |
||
316 | confirmButtonText: monsterinsights_admin.ok_text, |
||
317 | }).catch(swal.noop); |
||
318 | } else { |
||
319 | swal({ |
||
320 | type: 'error', |
||
321 | title: monsterinsights_admin.verify_loading_error_title, |
||
322 | text: response.data.message, |
||
323 | confirmButtonText: monsterinsights_admin.ok_text, |
||
324 | }).catch(swal.noop); |
||
325 | } |
||
326 | }).fail( function(xhr, textStatus, errorThrown) { |
||
327 | var message = jQuery(xhr.responseText).text(); |
||
328 | message = message.substring(0, message.indexOf("Call Stack")); |
||
329 | swal({ |
||
330 | type: 'error', |
||
331 | title: monsterinsights_admin.verify_loading_error_title, |
||
332 | text: message, |
||
333 | confirmButtonText: monsterinsights_admin.ok_text, |
||
334 | }).catch(swal.noop); |
||
335 | }); |
||
336 | }); |
||
337 | |||
338 | // Delete |
||
339 | jQuery(document).on('click','#monsterinsights-google-deauthenticate-submit', function( e ){ |
||
340 | e.preventDefault(); |
||
341 | monsterinsights_delete_auth( $(this), false ); |
||
342 | }); |
||
343 | |||
344 | // Force Delete |
||
345 | jQuery(document).on('click','#monsterinsights-google-force-deauthenticate-submit', function( e ){ |
||
346 | e.preventDefault(); |
||
347 | monsterinsights_delete_auth( $(this), true ); |
||
348 | }); |
||
349 | |||
350 | function monsterinsights_delete_auth( buttonObject, force ) { |
||
351 | swal({ |
||
352 | type: 'info', |
||
353 | title: monsterinsights_admin.deauth_loading_title_text, |
||
354 | text: monsterinsights_admin.deauth_loading_text_text, |
||
355 | allowOutsideClick: false, |
||
356 | allowEscapeKey: false, |
||
357 | allowEnterKey: false, |
||
358 | onOpen: function () { |
||
359 | swal.showLoading(); |
||
360 | } |
||
361 | }).catch(swal.noop); |
||
362 | var data = { |
||
363 | 'action': 'monsterinsights_maybe_delete', |
||
364 | 'nonce': monsterinsights_admin.admin_nonce, |
||
365 | 'isnetwork': monsterinsights_admin.isnetwork, |
||
366 | 'forcedelete' : force.toString(), |
||
367 | }; |
||
368 | jQuery.post(ajaxurl, data, function( response ) { |
||
369 | if ( response.success ) { |
||
370 | swal({ |
||
371 | type: 'success', |
||
372 | title: monsterinsights_admin.deauth_success_title_text, |
||
373 | text: monsterinsights_admin.deauth_success_text_text, |
||
374 | confirmButtonText: monsterinsights_admin.ok_text, |
||
375 | allowOutsideClick: false, |
||
376 | allowEscapeKey: false, |
||
377 | allowEnterKey: false, |
||
378 | }).then(function () { |
||
379 | location.reload(); |
||
380 | }).catch(swal.noop); |
||
381 | } else { |
||
382 | if ( ! force ) { |
||
383 | // Replace name, ID, value |
||
384 | buttonObject.attr('name', 'monsterinsights-google-force-deauthenticate-submit' ); |
||
385 | buttonObject.attr('id', 'monsterinsights-google-force-deauthenticate-submit' ); |
||
386 | buttonObject.attr('value', monsterinsights_admin.force_deauth_button_text ); |
||
387 | } |
||
388 | swal({ |
||
389 | type: 'error', |
||
390 | title: monsterinsights_admin.deauth_loading_error_title, |
||
391 | text: response.data.message, |
||
392 | confirmButtonText: monsterinsights_admin.ok_text, |
||
393 | }).catch(swal.noop); |
||
394 | } |
||
395 | }).fail( function(xhr, textStatus, errorThrown) { |
||
396 | var message = jQuery(xhr.responseText).text(); |
||
397 | message = message.substring(0, message.indexOf("Call Stack")); |
||
398 | swal({ |
||
399 | type: 'error', |
||
400 | title: monsterinsights_admin.deauth_loading_error_title, |
||
401 | text: message, |
||
402 | confirmButtonText: monsterinsights_admin.ok_text, |
||
403 | }).catch(swal.noop); |
||
404 | }); |
||
405 | }; |
||
406 | |||
407 | // Tools JS |
||
408 | jQuery('#monsterinsights-url-builder input').keyup(monsterinsights_update_campaign_url); |
||
409 | jQuery('#monsterinsights-url-builder input').click(monsterinsights_update_campaign_url); |
||
410 | |||
411 | function monsterinsights_update_campaign_url() { |
||
412 | var domain = jQuery('#monsterinsights-url-builer-domain').val().trim(); |
||
413 | var source = jQuery('#monsterinsights-url-builer-source').val().trim(); |
||
414 | var medium = jQuery('#monsterinsights-url-builer-medium').val().trim(); |
||
415 | var term = jQuery('#monsterinsights-url-builer-term').val().trim(); |
||
416 | var content = jQuery('#monsterinsights-url-builer-content').val().trim(); |
||
417 | var name = jQuery('#monsterinsights-url-builer-name').val().trim(); |
||
418 | var fragment = jQuery('#monsterinsights-url-builer-fragment').is(':checked'); |
||
419 | var file = domain.substring(domain.lastIndexOf("/") + 1); |
||
420 | |||
421 | if ( fragment && file.length > 0 && file.indexOf('#') > -1 ) { |
||
422 | // If we're going to use hash, but there's already a hash, use & |
||
423 | fragment = '&'; |
||
424 | } else if ( ! fragment && file.length > 0 && file.indexOf('?') > -1 ) { |
||
425 | // If we're going to use ?, but there's already one of those, use & |
||
426 | fragment = '&'; |
||
427 | } else { |
||
428 | // The attachment we want to use doesn't exist yet, use requested (? or #) |
||
429 | fragment = fragment ? '#' : '?'; |
||
430 | } |
||
431 | |||
432 | var html = domain + fragment + 'utm_source=' + encodeURIComponent(source); |
||
433 | |||
434 | if (medium) { |
||
435 | html = html + '&utm_medium=' + encodeURIComponent(medium); |
||
436 | } |
||
437 | if (name) { |
||
438 | html = html + '&utm_campaign=' + encodeURIComponent(name); |
||
439 | } |
||
440 | if (term) { |
||
441 | html = html + '&utm_term=' + encodeURIComponent(term); |
||
442 | } |
||
443 | if (content) { |
||
444 | html = html + '&utm_content=' + encodeURIComponent(content); |
||
445 | } |
||
446 | |||
447 | |||
448 | if ( domain && source ) { |
||
449 | jQuery('#monsterinsights-url-builer-url').html(html.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")); |
||
450 | } else { |
||
451 | jQuery('#monsterinsights-url-builer-url').html(''); |
||
452 | } |
||
453 | } |
||
454 | |||
455 | jQuery( document ).on( 'click', '#monsterinsights-shorten-url', function( e ) { |
||
456 | e.preventDefault(); |
||
457 | jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.working ); |
||
458 | var url = decodeURIComponent( jQuery('#monsterinsights-url-builer-url').val() ); |
||
459 | var data = { |
||
460 | 'action': 'monsterinsights_get_shortlink', |
||
461 | 'url' : url, |
||
462 | 'nonce': monsterinsights_admin.admin_nonce, |
||
463 | |||
464 | }; |
||
465 | jQuery.post(ajaxurl, data, function(response) { |
||
466 | jQuery('#monsterinsights-url-builer-url').html(response.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")); |
||
467 | jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shortened ); |
||
468 | window.setTimeout(function() { |
||
469 | jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shorten ); |
||
470 | }, 2000); |
||
471 | }).fail( function(xhr, textStatus, errorThrown) { |
||
472 | jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.failed ); |
||
473 | window.setTimeout(function() { |
||
474 | jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shorten ); |
||
475 | }, 2000); |
||
476 | }); |
||
477 | } ); |
||
478 | |||
479 | // Addons JS |
||
480 | // Addon background color |
||
481 | if ( jQuery( "#monsterinsights-addons" ).length !== 0 ) { |
||
482 | jQuery( "#wpbody").css("background-color", "#f1f1f1"); |
||
483 | jQuery( "body").css("background-color", "#f1f1f1"); |
||
484 | jQuery( "#wpfooter").css("background-color", "#f1f1f1"); |
||
485 | jQuery( "#wpbody-content").css("padding-bottom", "0px"); |
||
486 | } |
||
487 | |||
488 | // Addons Search |
||
489 | var addon_search_timeout; |
||
490 | jQuery( 'form#add-on-search input#add-on-searchbox' ).on( 'keyup', function() { |
||
491 | |||
492 | // Clear timeout |
||
493 | clearTimeout( addon_search_timeout ); |
||
494 | |||
495 | // Get the search input, heading, results and cancel elements |
||
496 | var search = jQuery( this ), |
||
497 | search_terms = jQuery( search ).val().toLowerCase(), |
||
498 | search_heading = jQuery( search ).data( 'heading' ), |
||
499 | search_results = jQuery( search ).data( 'results' ), |
||
500 | search_cancel = jQuery( search ).data( 'cancel' ); |
||
501 | |||
502 | // Show the Spinner |
||
503 | jQuery( 'form#add-on-search .spinner' ).css( 'visibility', 'visible' ); |
||
504 | |||
505 | // If the search terms is less than 3 characters, show all Addons |
||
506 | if ( search_terms.length < 3 ) { |
||
507 | jQuery( 'div.monsterinsights-addon' ).fadeIn( 'fast', function() { |
||
508 | // Hide the Spinner |
||
509 | jQuery( 'form#add-on-search .spinner' ).css( 'visibility', 'hidden' ); |
||
510 | } ); |
||
511 | return; |
||
512 | } |
||
513 | |||
514 | // Iterate through the Addons, showing or hiding them depending on whether they |
||
515 | // match the given search terms. |
||
516 | jQuery( 'div.monsterinsights-addon' ).each( function() { |
||
517 | if ( jQuery( 'h3.monsterinsights-addon-title', jQuery( this ) ).text().toLowerCase().search( search_terms ) >= 0 ) { |
||
518 | // This Addon's title does match the search terms |
||
519 | // Show |
||
520 | jQuery( this ).fadeIn(); |
||
521 | } else { |
||
522 | // This Addon's title does not match the search terms |
||
523 | // Hide |
||
524 | jQuery( this ).fadeOut(); |
||
525 | } |
||
526 | } ); |
||
527 | |||
528 | // Hide the Spinner |
||
529 | jQuery( 'form#add-on-search .spinner' ).css( 'visibility', 'hidden' ); |
||
530 | |||
531 | } ); |
||
532 | |||
533 | // Addons Sorting |
||
534 | var monsterinsights_addons_licensed_sorting = new List( 'monsterinsights-addons-licensed', { |
||
535 | valueNames: [ 'monsterinsights-addon-title' ] |
||
536 | } ); |
||
537 | var monsterinsights_addons_unlicensed_sorting = new List( 'monsterinsights-addons-unlicensed', { |
||
538 | valueNames: [ 'monsterinsights-addon-title' ] |
||
539 | } ); |
||
540 | jQuery( 'select#monsterinsights-filter-select' ).on( 'change', function() { |
||
541 | if ( typeof monsterinsights_addons_licensed_sorting.sort !== 'undefined' ) { |
||
542 | monsterinsights_addons_licensed_sorting.sort( 'monsterinsights-addon-title', { |
||
543 | order: jQuery( this ).val(), |
||
544 | } ); |
||
545 | } |
||
546 | if ( typeof monsterinsights_addons_unlicensed_sorting.sort !== 'undefined' ) { |
||
547 | monsterinsights_addons_unlicensed_sorting.sort( 'monsterinsights-addon-title', { |
||
548 | order: jQuery( this ).val(), |
||
549 | } ); |
||
550 | } |
||
551 | } ); |
||
552 | |||
553 | // Re-enable install button if user clicks on it, needs creds but tries to install another addon instead. |
||
554 | jQuery('#monsterinsights-addons').on('click.refreshInstallAddon', '.monsterinsights-addon-action-button', function(e) { |
||
555 | var el = jQuery(this); |
||
556 | var buttons = jQuery('#monsterinsights-addons').find('.monsterinsights-addon-action-button'); |
||
557 | $.each(buttons, function(i, element) { |
||
558 | if ( el == element ) { |
||
559 | return true; |
||
560 | } |
||
561 | |||
562 | monsterinsightsAddonRefresh(element); |
||
563 | }); |
||
564 | }); |
||
565 | |||
566 | // Activate Addon |
||
567 | jQuery('#monsterinsights-addons').on('click.activateAddon', '.monsterinsights-activate-addon', function(e) { |
||
568 | e.preventDefault(); |
||
569 | var $this = jQuery(this); |
||
570 | |||
571 | // Remove any leftover error messages, output an icon and get the plugin basename that needs to be activated. |
||
572 | jQuery('.monsterinsights-addon-error').remove(); |
||
573 | jQuery(this).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.activating); |
||
574 | jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'}); |
||
575 | var button = jQuery(this); |
||
576 | var plugin = jQuery(this).attr('rel'); |
||
577 | var el = jQuery(this).parent().parent(); |
||
578 | var message = jQuery(this).parent().parent().find('.addon-status'); |
||
579 | |||
580 | // Process the Ajax to perform the activation. |
||
581 | var opts = { |
||
582 | url: ajaxurl, |
||
583 | type: 'post', |
||
584 | async: true, |
||
585 | cache: false, |
||
586 | dataType: 'json', |
||
587 | data: { |
||
588 | action: 'monsterinsights_activate_addon', |
||
589 | nonce: monsterinsights_admin.activate_nonce, |
||
590 | plugin: plugin, |
||
591 | isnetwork: monsterinsights_admin.isnetwork |
||
592 | }, |
||
593 | success: function(response) { |
||
594 | // If there is a WP Error instance, output it here and quit the script. |
||
595 | if ( response && true !== response ) { |
||
596 | jQuery(el).slideDown('normal', function() { |
||
597 | jQuery(this).after('<div class="monsterinsights-addon-error"><strong>' + response.error + '</strong></div>'); |
||
598 | $this.next().hide(); |
||
599 | jQuery('.monsterinsights-addon-error').delay(3000).slideUp(); |
||
600 | }); |
||
601 | return; |
||
602 | } |
||
603 | |||
604 | // The Ajax request was successful, so let's update the output. |
||
605 | if ( monsterinsights_admin.isnetwork ) { |
||
606 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.networkdeactivate).removeClass('monsterinsights-activate-addon').addClass('monsterinsights-deactivate-addon'); |
||
607 | } else { |
||
608 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.deactivate).removeClass('monsterinsights-activate-addon').addClass('monsterinsights-deactivate-addon'); |
||
609 | } |
||
610 | |||
611 | jQuery(message).text(monsterinsights_admin.active); |
||
612 | // Trick here to wrap a span around he last word of the status |
||
613 | var heading = jQuery(message), word_array, last_word, first_part; |
||
614 | |||
615 | word_array = heading.html().split(/\s+/); // split on spaces |
||
616 | last_word = word_array.pop(); // pop the last word |
||
617 | first_part = word_array.join(' '); // rejoin the first words together |
||
618 | |||
619 | heading.html([first_part, ' <span>', last_word, '</span>'].join('')); |
||
620 | // Proceed with CSS changes |
||
621 | jQuery(el).removeClass('monsterinsights-addon-inactive').addClass('monsterinsights-addon-active'); |
||
622 | $this.next().hide(); |
||
623 | }, |
||
624 | View Code Duplication | error: function(xhr, textStatus ,e) { |
|
625 | $this.next().hide(); |
||
626 | return; |
||
627 | } |
||
628 | }; |
||
629 | $.ajax(opts); |
||
630 | }); |
||
631 | |||
632 | // Deactivate Addon |
||
633 | jQuery('#monsterinsights-addons').on('click.deactivateAddon', '.monsterinsights-deactivate-addon', function(e) { |
||
634 | e.preventDefault(); |
||
635 | var $this = jQuery(this); |
||
636 | |||
637 | // Remove any leftover error messages, output an icon and get the plugin basename that needs to be activated. |
||
638 | jQuery('.monsterinsights-addon-error').remove(); |
||
639 | jQuery(this).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.deactivating); |
||
640 | jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'}); |
||
641 | var button = jQuery(this); |
||
642 | var plugin = jQuery(this).attr('rel'); |
||
643 | var el = jQuery(this).parent().parent(); |
||
644 | var message = jQuery(this).parent().parent().find('.addon-status'); |
||
645 | |||
646 | // Process the Ajax to perform the activation. |
||
647 | var opts = { |
||
648 | url: ajaxurl, |
||
649 | type: 'post', |
||
650 | async: true, |
||
651 | cache: false, |
||
652 | dataType: 'json', |
||
653 | data: { |
||
654 | action: 'monsterinsights_deactivate_addon', |
||
655 | nonce: monsterinsights_admin.deactivate_nonce, |
||
656 | plugin: plugin, |
||
657 | isnetwork: monsterinsights_admin.isnetwork |
||
658 | }, |
||
659 | success: function(response) { |
||
660 | // If there is a WP Error instance, output it here and quit the script. |
||
661 | if ( response && true !== response ) { |
||
662 | jQuery(el).slideDown('normal', function() { |
||
663 | jQuery(this).after('<div class="monsterinsights-addon-error"><strong>' + response.error + '</strong></div>'); |
||
664 | $this.next().hide(); |
||
665 | jQuery('.monsterinsights-addon-error').delay(3000).slideUp(); |
||
666 | }); |
||
667 | return; |
||
668 | } |
||
669 | |||
670 | // The Ajax request was successful, so let's update the output. |
||
671 | if ( monsterinsights_admin.isnetwork ) { |
||
672 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.networkactivate).removeClass('monsterinsights-deactivate-addon').addClass('monsterinsights-activate-addon'); |
||
673 | } else { |
||
674 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.activate).removeClass('monsterinsights-deactivate-addon').addClass('monsterinsights-activate-addon'); |
||
675 | } |
||
676 | |||
677 | jQuery(message).text(monsterinsights_admin.inactive); |
||
678 | |||
679 | // Trick here to wrap a span around he last word of the status |
||
680 | var heading = jQuery(message), word_array, last_word, first_part; |
||
681 | |||
682 | word_array = heading.html().split(/\s+/); // split on spaces |
||
683 | last_word = word_array.pop(); // pop the last word |
||
684 | first_part = word_array.join(' '); // rejoin the first words together |
||
685 | |||
686 | heading.html([first_part, ' <span>', last_word, '</span>'].join('')); |
||
687 | // Proceed with CSS changes |
||
688 | jQuery(el).removeClass('monsterinsights-addon-active').addClass('monsterinsights-addon-inactive'); |
||
689 | $this.next().hide(); |
||
690 | }, |
||
691 | error: function(xhr, textStatus ,e) { |
||
692 | $this.next().hide(); |
||
693 | return; |
||
694 | } |
||
695 | }; |
||
696 | $.ajax(opts); |
||
697 | }); |
||
698 | |||
699 | // Install Addon |
||
700 | jQuery('#monsterinsights-addons').on('click.installAddon', '.monsterinsights-install-addon', function(e) { |
||
701 | e.preventDefault(); |
||
702 | var $this = jQuery(this); |
||
703 | |||
704 | // Remove any leftover error messages, output an icon and get the plugin basename that needs to be activated. |
||
705 | jQuery('.monsterinsights-addon-error').remove(); |
||
706 | jQuery(this).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.installing); |
||
707 | jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'}); |
||
708 | var button = jQuery(this); |
||
709 | var plugin = jQuery(this).attr('rel'); |
||
710 | var el = jQuery(this).parent().parent(); |
||
711 | var message = jQuery(this).parent().parent().find('.addon-status'); |
||
712 | |||
713 | // Process the Ajax to perform the activation. |
||
714 | var opts = { |
||
715 | url: ajaxurl, |
||
716 | type: 'post', |
||
717 | async: true, |
||
718 | cache: false, |
||
719 | dataType: 'json', |
||
720 | data: { |
||
721 | action: 'monsterinsights_install_addon', |
||
722 | nonce: monsterinsights_admin.install_nonce, |
||
723 | plugin: plugin |
||
724 | }, |
||
725 | success: function(response) { |
||
726 | // If there is a WP Error instance, output it here and quit the script. |
||
727 | if ( response.error ) { |
||
728 | jQuery(el).slideDown('normal', function() { |
||
729 | jQuery(button).parent().parent().after('<div class="monsterinsights-addon-error"><div class="xinterior"><p><strong>' + response.error + '</strong></p></div></div>'); |
||
730 | jQuery(button).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.install); |
||
731 | $this.next().hide(); |
||
732 | jQuery('.monsterinsights-addon-error').delay(4000).slideUp(); |
||
733 | }); |
||
734 | return; |
||
735 | } |
||
736 | |||
737 | // If we need more credentials, output the form sent back to us. |
||
738 | if ( response.form ) { |
||
739 | View Code Duplication | // Display the form to gather the users credentials. |
|
740 | jQuery(el).slideDown('normal', function() { |
||
741 | jQuery(this).after('<div class="monsterinsights-addon-error">' + response.form + '</div>'); |
||
742 | $this.next().hide(); |
||
743 | }); |
||
744 | |||
745 | // Add a disabled attribute the install button if the creds are needed. |
||
746 | jQuery(button).attr('disabled', true); |
||
747 | |||
748 | jQuery('#monsterinsights-addons').on('click.installCredsAddon', '#upgrade', function(e) { |
||
749 | // Prevent the default action, let the user know we are attempting to install again and go with it. |
||
750 | e.preventDefault(); |
||
751 | $this.next().hide(); |
||
752 | jQuery(this).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.installing); |
||
753 | jQuery(this).next().css({'display' : 'inline-block', 'margin-top' : '0px'}); |
||
754 | |||
755 | // Now let's make another Ajax request once the user has submitted their credentials. |
||
756 | var hostname = jQuery(this).parent().parent().find('#hostname').val(); |
||
757 | var username = jQuery(this).parent().parent().find('#username').val(); |
||
758 | var password = jQuery(this).parent().parent().find('#password').val(); |
||
759 | var proceed = jQuery(this); |
||
760 | var connect = jQuery(this).parent().parent().parent().parent(); |
||
761 | var cred_opts = { |
||
762 | url: ajaxurl, |
||
763 | type: 'post', |
||
764 | async: true, |
||
765 | cache: false, |
||
766 | dataType: 'json', |
||
767 | data: { |
||
768 | action: 'monsterinsights_install_addon', |
||
769 | nonce: monsterinsights_admin.install_nonce, |
||
770 | plugin: plugin, |
||
771 | hostname: hostname, |
||
772 | username: username, |
||
773 | password: password |
||
774 | }, |
||
775 | success: function(response) { |
||
776 | // If there is a WP Error instance, output it here and quit the script. |
||
777 | if ( response.error ) { |
||
778 | jQuery(el).slideDown('normal', function() { |
||
779 | jQuery(button).parent().parent().after('<div class="monsterinsights-addon-error"><strong>' + response.error + '</strong></div>'); |
||
780 | jQuery(button).html('<i class="monsterinsights-cloud-download"></i> ' + monsterinsights_admin.install); |
||
781 | $this.next().hide(); |
||
782 | jQuery('.monsterinsights-addon-error').delay(4000).slideUp(); |
||
783 | }); |
||
784 | return; |
||
785 | } |
||
786 | |||
787 | if ( response.form ) { |
||
788 | $this.next().hide(); |
||
789 | jQuery('.monsterinsights-inline-error').remove(); |
||
790 | jQuery(proceed).val(monsterinsights_admin.proceed); |
||
791 | jQuery(proceed).after('<span class="monsterinsights-inline-error">' + monsterinsights_admin.connect_error + '</span>'); |
||
792 | return; |
||
793 | } |
||
794 | |||
795 | // The Ajax request was successful, so let's update the output. |
||
796 | jQuery(connect).remove(); |
||
797 | jQuery(button).show(); |
||
798 | |||
799 | if ( monsterinsights_admin.isnetwork ) { |
||
800 | jQuery(button).text(monsterinsights_admin.networkactivate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon'); |
||
801 | } else { |
||
802 | jQuery(button).text(monsterinsights_admin.activate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon'); |
||
803 | } |
||
804 | |||
805 | jQuery(button).attr('rel', response.plugin); |
||
806 | jQuery(button).removeAttr('disabled'); |
||
807 | jQuery(message).text(monsterinsights_admin.inactive); |
||
808 | |||
809 | // Trick here to wrap a span around he last word of the status |
||
810 | var heading = jQuery(message), word_array, last_word, first_part; |
||
811 | |||
812 | word_array = heading.html().split(/\s+/); // split on spaces |
||
813 | last_word = word_array.pop(); // pop the last word |
||
814 | first_part = word_array.join(' '); // rejoin the first words together |
||
815 | |||
816 | heading.html([first_part, ' <span>', last_word, '</span>'].join('')); |
||
817 | // Proceed with CSS changes |
||
818 | jQuery(el).removeClass('monsterinsights-addon-not-installed').addClass('monsterinsights-addon-inactive'); |
||
819 | $this.next().hide(); |
||
820 | }, |
||
821 | error: function(xhr, textStatus ,e) { |
||
822 | $this.next().hide(); |
||
823 | return; |
||
824 | } |
||
825 | }; |
||
826 | $.ajax(cred_opts); |
||
827 | }); |
||
828 | |||
829 | // No need to move further if we need to enter our creds. |
||
830 | return; |
||
831 | } |
||
832 | |||
833 | // The Ajax request was successful, so let's update the output. |
||
834 | if ( monsterinsights_admin.isnetwork ) { |
||
835 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.networkactivate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon'); |
||
836 | } else { |
||
837 | jQuery(button).html('<i class="monsterinsights-toggle-on"></i> ' + monsterinsights_admin.activate).removeClass('monsterinsights-install-addon').addClass('monsterinsights-activate-addon'); |
||
838 | } |
||
839 | jQuery(button).attr('rel', response.plugin); |
||
840 | jQuery(message).text(monsterinsights_admin.inactive); |
||
841 | |||
842 | // Trick here to wrap a span around he last word of the status |
||
843 | var heading = jQuery(message), word_array, last_word, first_part; |
||
844 | |||
845 | word_array = heading.html().split(/\s+/); // split on spaces |
||
846 | last_word = word_array.pop(); // pop the last word |
||
847 | first_part = word_array.join(' '); // rejoin the first words together |
||
848 | |||
849 | heading.html([first_part, ' <span>', last_word, '</span>'].join('')); |
||
850 | // Proceed with CSS changes |
||
851 | jQuery(el).removeClass('monsterinsights-addon-not-installed').addClass('monsterinsights-addon-inactive'); |
||
852 | $this.next().hide(); |
||
853 | }, |
||
854 | error: function(xhr, textStatus ,e) { |
||
855 | $this.next().hide(); |
||
856 | return; |
||
857 | } |
||
858 | }; |
||
859 | $.ajax(opts); |
||
860 | }); |
||
861 | |||
862 | // Function to clear any disabled buttons and extra text if the user needs to add creds but instead tries to install a different addon. |
||
863 | function monsterinsightsAddonRefresh(element) { |
||
864 | if ( jQuery(element).attr('disabled') ) { |
||
865 | jQuery(element).removeAttr('disabled'); |
||
866 | } |
||
867 | |||
868 | if ( jQuery(element).parent().parent().hasClass('monsterinsights-addon-not-installed') ) { |
||
869 | jQuery(element).text( monsterinsights_admin.install ); |
||
870 | } |
||
871 | } |
||
872 | |||
873 | jQuery(document).ready(function($) { |
||
874 | monsterinsights_equalheight2column(); |
||
875 | }); |
||
876 | |||
877 | |||
878 | jQuery(document).on('click', ".monsterinsights-reports-show-selector-group > .btn", function( e ){ |
||
879 | e.preventDefault(); |
||
880 | var id = jQuery(this).attr("data-tid"); |
||
881 | jQuery(this).addClass("active").disable(true).siblings().removeClass("active").disable(false); |
||
882 | if ( jQuery(this).hasClass("ten") ) { |
||
883 | jQuery("#" + id + " .monsterinsights-reports-pages-list > .monsterinsights-listing-table-row").slice(10,50).hide(); |
||
884 | jQuery("#" + id + " .monsterinsights-reports-pages-list > .monsterinsights-listing-table-row").slice(0,10).show(); |
||
885 | } else if ( jQuery(this).hasClass("twentyfive") ) { |
||
886 | jQuery("#" + id + " .monsterinsights-reports-pages-list > .monsterinsights-listing-table-row").slice(25,50).hide(); |
||
887 | jQuery("#" + id + " .monsterinsights-reports-pages-list > .monsterinsights-listing-table-row").slice(0,25).show(); |
||
888 | } else if ( jQuery(this).hasClass("fifty") ) { |
||
889 | jQuery("#" + id + " .monsterinsights-reports-pages-list > .monsterinsights-listing-table-row").slice(0,50).show(); |
||
890 | } |
||
891 | }); |
||
892 | |||
893 | /** |
||
894 | * Handles tabbed interfaces within MonsterInsights: |
||
895 | * - Settings Page |
||
896 | * - Reports Page |
||
897 | * - Tools Page |
||
898 | */ |
||
899 | /* @todo: remove this comment, convert other comments to multiline (reduction safe), and namespace all variables (reduction safe) */ |
||
900 | // Reports graph tabs |
||
901 | jQuery(document).on('click', '.monsterinsights-tabbed-nav > .monsterinsights-tabbed-nav-tab-title', function( e ){ |
||
902 | e.preventDefault(); |
||
903 | var tabname = jQuery(this).attr("data-tab"); |
||
904 | jQuery(this).addClass("active").siblings().removeClass("active"); |
||
905 | jQuery('.monsterinsights-tabbed-nav-panel').hide(); |
||
906 | jQuery('.monsterinsights-tabbed-nav-panel.' + tabname ).show(); |
||
907 | }); |
||
908 | |||
909 | jQuery( function() { |
||
910 | MonsterInsightsTriggerTabs( true ); |
||
911 | }); |
||
912 | |||
913 | jQuery( window ).on( "hashchange", function( e ) { |
||
914 | e.preventDefault(); |
||
915 | MonsterInsightsTriggerTabs( false ); |
||
916 | }); |
||
917 | |||
918 | function MonsterInsightsTriggerTabs( init ) { |
||
919 | var window_hash = window.location.hash; |
||
920 | var current_tab = ''; |
||
921 | var tab_nav = '.monsterinsights-main-nav-container'; |
||
922 | var tabs_section = '.monsterinsights-main-nav-tabs'; |
||
923 | |||
924 | var current_sub_tab = ''; |
||
925 | var sub_tabs_nav = '.monsterinsights-sub-nav-container'; |
||
926 | var sub_tabs_section = '.monsterinsights-sub-nav-tabs'; |
||
927 | var current_sub_tab_div = ''; |
||
928 | |||
929 | // If there's no hash, then we're on the default, which the page will auto load first tab + subtab as active |
||
930 | if ( window_hash.indexOf( '#' ) > -1 ) { |
||
931 | if ( window_hash.indexOf( '?' ) < 1 ) { |
||
932 | // No ?, but there is a # |
||
933 | current_tab = window_hash; |
||
934 | var firstchildclick = jQuery( sub_tabs_nav ); |
||
935 | |||
936 | // If there's no subtab defined, let's see if the page has subtabs, and if so select the first one. |
||
937 | if ( "0" in firstchildclick && "firstElementChild" in firstchildclick[0] && "hash" in firstchildclick[0].firstElementChild ) { |
||
938 | current_sub_tab = firstchildclick[0].firstElementChild.hash; |
||
939 | current_sub_tab_div = '#' + ( firstchildclick[0].firstElementChild.hash ).split( '?' )[1]; |
||
940 | } |
||
941 | } else { |
||
942 | // ? and a # |
||
943 | var tab_split = window_hash.split( '?' ); |
||
944 | current_tab = tab_split[0]; |
||
945 | current_sub_tab = window_hash; |
||
946 | current_sub_tab_div = '#' + tab_split[1]; |
||
947 | } |
||
948 | |||
949 | // @todo: if the tab doesn't exist, we should fallback to finding the first tab and opening that |
||
950 | // If we fallback, we should clear the sub_tab so we ensure we land on the first subtab of the new |
||
951 | // tab, if that pages has subtabs. |
||
952 | |||
953 | jQuery( tab_nav ).find( '.monsterinsights-active' ).removeClass( 'monsterinsights-active' ); |
||
954 | jQuery( tabs_section ).find( '.monsterinsights-active' ).removeClass( 'monsterinsights-active' ); |
||
955 | jQuery( sub_tabs_nav ).find( '.monsterinsights-active' ).removeClass( 'monsterinsights-active' ); |
||
956 | jQuery( sub_tabs_section ).find( '.monsterinsights-active' ).removeClass( 'monsterinsights-active' ); |
||
957 | |||
958 | jQuery( tab_nav ).find( 'a[href="' + current_tab + '"]' ).addClass( 'monsterinsights-active' ); |
||
959 | jQuery( tabs_section ).find( current_tab ).addClass( 'monsterinsights-active' ); |
||
960 | |||
961 | // Check to make sure the subtab given in the url exists, and then open it. |
||
962 | if ( jQuery( sub_tabs_nav ).find( 'a[href="' + current_sub_tab + '"]' ).length == 1 ) { |
||
963 | jQuery( sub_tabs_nav ).find( 'a[href="' + current_sub_tab + '"]' ).addClass( 'monsterinsights-active' ); |
||
964 | jQuery( sub_tabs_section ).find( current_sub_tab_div ).addClass( 'monsterinsights-active' ); |
||
965 | } else { |
||
966 | // If the subtab given in the URL doesn't exist, let's see if the page has subtabs, and if so select the first one. |
||
967 | var firstchildclick = jQuery( sub_tabs_nav ); |
||
968 | if ( "0" in firstchildclick && "firstElementChild" in firstchildclick[0] && "hash" in firstchildclick[0].firstElementChild ) { |
||
969 | jQuery( sub_tabs_nav ).find( 'a[href="#' + (firstchildclick[0].firstElementChild.hash).split( '?' )[1] + '"]' ).addClass( 'monsterinsights-active' ); |
||
970 | jQuery( sub_tabs_section ).find( '#' + (firstchildclick[0].firstElementChild.hash).split( '?' )[1] ).addClass( 'monsterinsights-active' ); |
||
971 | } |
||
972 | } |
||
973 | |||
974 | if ( jQuery('.monsterinsights-main-nav-tabs .monsterinsights-main-nav-tab:not(".monsterinsights-active") .monsterinsights-tab-settings-notices .monsterinsights-notice' ).length > 0 ) { |
||
975 | jQuery('.monsterinsights-main-nav-tabs .monsterinsights-main-nav-tab:not(".monsterinsights-active") .monsterinsights-tab-settings-notices .monsterinsights-notice' ).remove(); |
||
976 | } |
||
977 | |||
978 | if ( jQuery('.monsterinsights-sub-nav-tabs .monsterinsights-sub-nav-tab:not("' + current_sub_tab_div + '") .monsterinsights-subtab-settings-notices .monsterinsights-notice' ).length > 0 ) { |
||
979 | jQuery('.monsterinsights-sub-nav-tabs .monsterinsights-sub-nav-tab:not("' + current_sub_tab_div + '") .monsterinsights-subtab-settings-notices .monsterinsights-notice' ).remove(); |
||
980 | } |
||
981 | |||
982 | if ( current_tab !== '#monsterinsights-main-tab-tracking' ) { |
||
983 | if ( jQuery('.monsterinsights-sub-nav-tabs .monsterinsights-sub-nav-tab .monsterinsights-subtab-settings-notices .monsterinsights-notice' ).length > 0 ) { |
||
984 | jQuery('.monsterinsights-sub-nav-tabs .monsterinsights-sub-nav-tab .monsterinsights-subtab-settings-notices .monsterinsights-notice' ).remove(); |
||
985 | } |
||
986 | } |
||
987 | // Is the window taller than the #adminmenuwrap? |
||
988 | if (jQuery(window).height() > jQuery("#adminmenuwrap").height()) { |
||
989 | // ...if so, make the #adminmenuwrap fixed |
||
990 | jQuery('#adminmenuwrap').css('position', 'fixed'); |
||
991 | |||
992 | } else { |
||
993 | //...otherwise, leave it relative |
||
994 | jQuery('#adminmenuwrap').css('position', 'relative'); |
||
995 | |||
996 | } |
||
997 | } else if ( init ) { |
||
998 | // If we have a default open, else open one |
||
999 | if ( jQuery(tab_nav + " .monsterinsights-active").length > 0 ){ |
||
1000 | return; |
||
1001 | } |
||
1002 | jQuery(tab_nav).find('a:first').addClass( 'monsterinsights-active' ); |
||
1003 | jQuery( tabs_section ).find('div:first').addClass( 'monsterinsights-active' ); |
||
1004 | jQuery(sub_tabs_nav).find('a:first').addClass( 'monsterinsights-active' ); |
||
1005 | jQuery( sub_tabs_section ).find('div:first').addClass( 'monsterinsights-active' ); |
||
1006 | } |
||
1007 | } |
||
1008 | }); |
||
1009 | |||
1010 | function monsterinsights_equalheight2column(){ |
||
1011 | jQuery('.monsterinsights-reports-2-column-container').each(function(i, elem) { |
||
1012 | jQuery(elem) |
||
1013 | .find('.monsterinsights-reports-data-table-tbody') // Only children of this row |
||
1014 | .matchHeight({byRow: false}); // Row detection gets confused so disable it |
||
1015 | jQuery(elem) |
||
1016 | .find('.monsterinsights-reports-2-column-panel') // Only children of this row |
||
1017 | .matchHeight({byRow: true}); // Row detection gets confused so disable it |
||
1018 | }); |
||
1019 | } |
||
1020 | |||
1021 | function monsterinsights_show_manual( ){ |
||
1022 | document.getElementById("monsterinsights-google-ua-box").className = ""; |
||
1023 | } |
||
1024 | |||
1025 | var uorigindetected = 'no'; |
||
1026 | |||
1027 | // Reports: |
||
1028 | // Thanks ChartJS for making us have to do this nonsense. |
||
1029 | |||
1030 | // A huge Thanks ChartJS for making us have to do this nonsense. Why ChartJS can't just fix non-initalization on hidden elements (or at least |
||
1031 | // give a generic action to re-fire initialization for in-view charts generically is beyond me) |
||
1032 | jQuery(document).ready(function($) { |
||
1033 | var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver; |
||
1034 | |||
1035 | jQuery.fn.attrchange = function(callback) { |
||
1036 | if (MutationObserver) { |
||
1037 | var options = { |
||
1038 | subtree: false, |
||
1039 | attributes: true, |
||
1040 | attributeName: "class", |
||
1041 | }; |
||
1042 | |||
1043 | var observer = new MutationObserver(function(mutations) { |
||
1044 | mutations.forEach(function(e) { |
||
1045 | callback.call(e.target, e.attributeName); |
||
1046 | }); |
||
1047 | }); |
||
1048 | |||
1049 | return this.each(function() { |
||
1050 | observer.observe(this, options); |
||
1051 | }); |
||
1052 | } |
||
1053 | }; |
||
1054 | |||
1055 | jQuery('#monsterinsights-reports-page-main-nav .monsterinsights-main-nav-item.monsterinsights-nav-item').attrchange(function(attrName) { |
||
1056 | if ( attrName != 'class' ){ |
||
1057 | return; |
||
1058 | } |
||
1059 | |||
1060 | // Blur report shown |
||
1061 | jQuery( "#monsterinsights-reports-pages" ).addClass( "monsterinsights-mega-blur" ); |
||
1062 | |||
1063 | // Which report? |
||
1064 | var reportname = jQuery("#monsterinsights-reports-pages").find( "div.monsterinsights-main-nav-tab.monsterinsights-active" ).attr("id").replace("monsterinsights-main-tab-", "" ); |
||
1065 | var reportid = jQuery("#monsterinsights-reports-pages").find( "div.monsterinsights-main-nav-tab.monsterinsights-active" ).attr("id"); |
||
1066 | var start = moment( moment().subtract(30, 'days') ).utc().format('YYYY-MM-DD'); |
||
1067 | var end = moment( moment().subtract( 1, 'days' ) ).utc().format('YYYY-MM-DD'); |
||
1068 | |||
1069 | swal({ |
||
1070 | type: 'info', |
||
1071 | title: monsterinsights_admin.refresh_report_title, |
||
1072 | text: monsterinsights_admin.refresh_report_text, |
||
1073 | allowOutsideClick: false, |
||
1074 | allowEscapeKey: false, |
||
1075 | allowEnterKey: false, |
||
1076 | onOpen: function () { |
||
1077 | swal.showLoading(); |
||
1078 | |||
1079 | var data = { |
||
1080 | 'action' : 'monsterinsights_refresh_reports', |
||
1081 | 'security' : monsterinsights_admin.admin_nonce, |
||
1082 | 'isnetwork': monsterinsights_admin.isnetwork, |
||
1083 | 'start' : start, |
||
1084 | 'end' : end, |
||
1085 | 'report' : reportname, |
||
1086 | }; |
||
1087 | |||
1088 | jQuery.post(ajaxurl, data, function( response ) { |
||
1089 | |||
1090 | if ( response.success && response.data.html ) { |
||
1091 | // Insert new data here |
||
1092 | jQuery("#monsterinsights-main-tab-" + reportname + " > .monsterinsights-reports-wrap").html( response.data.html ); |
||
1093 | |||
1094 | // Resize divs |
||
1095 | monsterinsights_equalheight2column(); |
||
1096 | swal.close(); |
||
1097 | } else { |
||
1098 | swal({ |
||
1099 | type: 'error', |
||
1100 | title: monsterinsights_admin.refresh_report_failure_title, |
||
1101 | text: response.data.message, |
||
1102 | }).catch(swal.noop); |
||
1103 | } |
||
1104 | }).then(function (result) { |
||
1105 | // Unblur reports |
||
1106 | jQuery( "#monsterinsights-reports-pages" ).removeClass( "monsterinsights-mega-blur" ); |
||
1107 | }).fail( function(xhr, textStatus, errorThrown) { |
||
1108 | var message = jQuery(xhr.responseText).text(); |
||
1109 | message = message.substring(0, message.indexOf("Call Stack")); |
||
1110 | swal({ |
||
1111 | type: 'error', |
||
1112 | title: monsterinsights_admin.refresh_report_failure_title, |
||
1113 | text: message, |
||
1114 | }).catch(swal.noop); |
||
1115 | // Unblur reports |
||
1116 | jQuery( "#monsterinsights-reports-pages" ).removeClass( "monsterinsights-mega-blur" ); |
||
1117 | }); |
||
1118 | } |
||
1119 | }); |
||
1120 | }); |
||
1121 | }); |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.