Passed
Push — master ( cf9d65...a7feb4 )
by Chris
03:29
created

$(document).ready   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
/** 
2
 * Developer's Notice:
3
 * 
4
 * Note: JS in this file (and this file itself) is not garunteed backwards compatibility. JS can be added, changed or removed at any time without notice.
5
 * For more information see the `Backwards Compatibility Guidelines for Developers` section of the README.md file.
6
 */
7
/**
8
 * Handles:
9
 * - Copy to Clipboard functionality
10
 * - Dismissable Notices
11
 *
12
 * @since 6.0.0
13
 */
14
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];
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
22
		else
23
			dbjsError(errorMsg, url, lineNumber);
24
	};
25
26
	jQuery(document).ready( function(){
27
		for ( var err in errors )
0 ignored issues
show
Complexity introduced by
A for in loop automatically includes the property of any prototype object, consider checking the key using hasOwnProperty.

When iterating over the keys of an object, this includes not only the keys of the object, but also keys contained in the prototype of that object. It is generally a best practice to check for these keys specifically:

var someObject;
for (var key in someObject) {
    if ( ! someObject.hasOwnProperty(key)) {
        continue; // Skip keys from the prototype.
    }

    doSomethingWith(key);
}
Loading history...
28
			dbjsError( errors[err][0], errors[err][1], errors[err][2] );
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
29
30
	});
31
32
	dbjsError = function( errorMsg, url, lineNumber ) {
33
34
		var errorLine, place, button, tab;
0 ignored issues
show
Unused Code introduced by
The variable button seems to be never used. Consider removing it.
Loading history...
Unused Code introduced by
The variable tab seems to be never used. Consider removing it.
Loading history...
35
36
37
		if ( !list )
38
			list = document.getElementById( 'monsterinsights-ublock-origin-error' );
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
39
40
		if (!list )
41
			return; // threw way too early... @todo cache these?
0 ignored issues
show
Coding Style Best Practice introduced by
Curly braces around statements make for more readable code and help prevent bugs when you add further statements.

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 (a > 0)
    b = 42;

If you or someone else later decides to put another statement in, only the first statement will be executed.

if (a > 0)
    console.log("a > 0");
    b = 42;

In this case the statement b = 42 will always be executed, while the logging statement will be executed conditionally.

if (a > 0) {
    console.log("a > 0");
    b = 42;
}

ensures that the proper code will be executed conditionally no matter how many statements are added or removed.

Loading history...
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' ) {
0 ignored issues
show
Bug introduced by
The variable Clipboard seems to be never declared. If this is a global, consider adding a /** global: Clipboard */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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){
0 ignored issues
show
Bug introduced by
The function fallbackMessage is declared conditionally. This is not supported by all runtimes. Consider moving it to root scope or using var fallbackMessage = function() { /* ... */ }; instead.
Loading history...
111
			var actionMsg='';var actionKey=(action==='cut'?'X':'C');
112
			if (/iPhone|iPad/i.test(navigator.userAgent ) ) {
0 ignored issues
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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;
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
123
			window.setTimeout(function() {
124
				e.trigger.textContent = monsterinsights_admin.copytoclip;
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ) { 
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if fields_changed is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
194
				var answer = confirm( monsterinsights_admin.settings_changed_confirm );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
195
				if ( answer ){
196
				   fields_changed = false;
197
				   return true;
198
				} else {
0 ignored issues
show
Comprehensibility introduced by
else is not necessary here since all if branches return, consider removing it to reduce nesting and make code more readable.
Loading history...
199 View Code Duplication
					e.preventDefault();
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
213
				  text: monsterinsights_admin.redirect_loading_text_text,
214
				  allowOutsideClick: false,
215
				  allowEscapeKey: false,
216
				  allowEnterKey: false,
217
				  onOpen: function () {
218
					swal.showLoading();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
219
				  }
220
				}).catch(swal.noop);
221
				var data = { 
222
					'action': 'monsterinsights_maybe_authenticate', 
223
					'nonce':  monsterinsights_admin.admin_nonce,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
224
					'isnetwork': monsterinsights_admin.isnetwork
225
				};
226
				jQuery.post(ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
233
							  text: response.data.message,
234
							  confirmButtonText: monsterinsights_admin.ok_text,
235
						  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
236
					}
237
				}).fail( function(xhr, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
238
					var message = jQuery(xhr.responseText).text();
239
					message = message.substring(0, message.indexOf("Call Stack"));
240
					swal({
241 View Code Duplication
						type: 'error',
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
242
						  title: monsterinsights_admin.redirect_loading_error_title,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
243
						  text: message,
244
						  confirmButtonText: monsterinsights_admin.ok_text,
245
					  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
255
				  text: monsterinsights_admin.redirect_loading_text_text,
256
				  allowOutsideClick: false,
257
				  allowEscapeKey: false,
258
				  allowEnterKey: false,
259
				  onOpen: function () {
260
					swal.showLoading();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
261
				  }
262
				}).catch(swal.noop);
263
				var data = { 
264
					'action': 'monsterinsights_maybe_reauthenticate',
265
					'nonce':  monsterinsights_admin.admin_nonce,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
266
					'isnetwork': monsterinsights_admin.isnetwork
267
				};
268
				jQuery.post(ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
275
							  text: response.data.message,
276
							  confirmButtonText: monsterinsights_admin.ok_text,
277
						  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
278
					}
279
				}).fail( function(xhr, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
285
						  text: message,
286
						  confirmButtonText: monsterinsights_admin.ok_text,
287
					  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
297
				  text: monsterinsights_admin.verify_loading_text_text,
298
				  allowOutsideClick: false,
299
				  allowEscapeKey: false,
300
				  allowEnterKey: false,
301
				  onOpen: function () {
302
					swal.showLoading();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
303
				  }
304
				}).catch(swal.noop);
305
				var data = { 
306
					'action': 'monsterinsights_maybe_verify',
307
					'nonce':  monsterinsights_admin.admin_nonce,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
308
					'isnetwork': monsterinsights_admin.isnetwork
309
				};
310
				jQuery.post(ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
311
				if ( response.success ) {
312
					swal({
313
						type: 'success',
314
						  title: monsterinsights_admin.verify_success_title_text,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
315
						  text: monsterinsights_admin.verify_success_text_text,
316
						  confirmButtonText: monsterinsights_admin.ok_text,
317
					  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
332
						  text: message,
333
						  confirmButtonText: monsterinsights_admin.ok_text,
334
					  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
354
				  text: monsterinsights_admin.deauth_loading_text_text,
355
				  allowOutsideClick: false,
356
				  allowEscapeKey: false,
357
				  allowEnterKey: false,
358
				  onOpen: function () {
359
					swal.showLoading();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
360
				  }
361
				}).catch(swal.noop);
362
				var data = { 
363
					'action': 'monsterinsights_maybe_delete', 
364
					'nonce':  monsterinsights_admin.admin_nonce,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
365
					'isnetwork': monsterinsights_admin.isnetwork,
366
					'forcedelete' : force.toString(),
367
				};
368
				jQuery.post(ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
369
				if ( response.success ) {
370
					swal({
371
						type: 'success',
372
						  title: monsterinsights_admin.deauth_success_title_text,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
401
						text: message,
402
						confirmButtonText: monsterinsights_admin.ok_text,
403
					}).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"));
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 );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
466
				jQuery('#monsterinsights-url-builer-url').html(response.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;"));
467
				jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shortened );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
468
				window.setTimeout(function() {
469
					jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shorten );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
470
				}, 2000);
471
			}).fail( function(xhr, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter xhr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
472
				jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.failed );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
473
				window.setTimeout(function() {
474
					jQuery("#monsterinsights-shorten-url").text( monsterinsights_admin.shorten );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable addon_search_timeout seems to be never initialized.
Loading history...
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' ),
0 ignored issues
show
Unused Code introduced by
The variable search_heading seems to be never used. Consider removing it.
Loading history...
499
					search_results  = jQuery( search ).data( 'results' ),
0 ignored issues
show
Unused Code introduced by
The variable search_results seems to be never used. Consider removing it.
Loading history...
500
					search_cancel   = jQuery( search ).data( 'cancel' );
0 ignored issues
show
Unused Code introduced by
The variable search_cancel seems to be never used. Consider removing it.
Loading history...
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', {
0 ignored issues
show
Bug introduced by
The variable List seems to be never declared. If this is a global, consider adding a /** global: List */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter xhr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
625
						$this.next().hide();
626
						return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter xhr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
692
						$this.next().hide();
693
						return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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.
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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);
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter xhr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
822
										$this.next().hide();
823
										return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
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 ) {
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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) {
0 ignored issues
show
Unused Code introduced by
The parameter e is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter xhr is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
855
						$this.next().hide();
856
						return;
0 ignored issues
show
Unused Code introduced by
This return has no effect and can be removed.
Loading history...
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 );
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
870
				}
871
			}
872
873
			jQuery(document).ready(function($) {
0 ignored issues
show
Unused Code introduced by
The parameter $ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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 );
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable firstchildclick already seems to be declared on line 934. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
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($) {
0 ignored issues
show
Unused Code introduced by
The parameter $ is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
1033
	var MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
1034
1035
	jQuery.fn.attrchange = function(callback) {
1036
		if (MutationObserver) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if MutationObserver is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
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");
0 ignored issues
show
Unused Code introduced by
The variable reportid seems to be never used. Consider removing it.
Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1072
		  text: monsterinsights_admin.refresh_report_text,
1073
		  allowOutsideClick: false,
1074
		  allowEscapeKey: false,
1075
		  allowEnterKey: false,
1076
		  onOpen: function () {
1077
			swal.showLoading();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1078
1079
			var data = { 
1080
				'action'   : 'monsterinsights_refresh_reports', 
1081
				'security' :  monsterinsights_admin.admin_nonce,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1082
				'isnetwork':  monsterinsights_admin.isnetwork,
1083
				'start'    :  start,
1084
				'end'      :  end,
1085
				'report'   :  reportname,
1086
			};
1087
			
1088
			jQuery.post(ajaxurl, data, function( response ) {
0 ignored issues
show
Bug introduced by
The variable ajaxurl seems to be never declared. If this is a global, consider adding a /** global: ajaxurl */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
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();
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1097
				} else {
1098
					swal({
1099
						type: 'error',
1100
						  title: monsterinsights_admin.refresh_report_failure_title,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1101
						  text: response.data.message,
1102
					  }).catch(swal.noop);
1103
				}
1104
			}).then(function (result) {
0 ignored issues
show
Unused Code introduced by
The parameter result is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
1105
				// Unblur reports
1106
				jQuery( "#monsterinsights-reports-pages" ).removeClass( "monsterinsights-mega-blur" );
1107
			}).fail( function(xhr, textStatus, errorThrown) {
0 ignored issues
show
Unused Code introduced by
The parameter textStatus is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
Unused Code introduced by
The parameter errorThrown is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
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,
0 ignored issues
show
Bug introduced by
The variable monsterinsights_admin seems to be never declared. If this is a global, consider adding a /** global: monsterinsights_admin */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1113
					  text: message,
1114
				  }).catch(swal.noop);
0 ignored issues
show
Bug introduced by
The variable swal seems to be never declared. If this is a global, consider adding a /** global: swal */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
1115
				// Unblur reports
1116
				jQuery( "#monsterinsights-reports-pages" ).removeClass( "monsterinsights-mega-blur" );
1117
			});
1118
		  }
1119
		});
1120
	});
1121
});