-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathrun.js
More file actions
254 lines (232 loc) · 7.1 KB
/
run.js
File metadata and controls
254 lines (232 loc) · 7.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Global variables.
var test_version, only_active, skip_list, timer;
jQuery( document ).ready(function($) {
// Check the status immediately to reflect if tests are running.
checkStatus();
$( '#developermode' ).change(function() {
if ( $(this).is( ':checked' ) ) {
$( '#developerMode' ).show();
$( '#wpe-pcc-standardMode' ).hide();
} else {
$( '#developerMode' ).hide();
$( '#wpe-pcc-standardMode' ).show();
}
});
$( '#downloadReport' ).on( 'click', function() {
download( $( '#testResults' ).val(), 'report.txt', 'text/plain' );
return false;
});
$( document ).on( 'click', '.wpe-pcc-alert-details', function() {
// Get the textarea with is on the same (dom) level.
var textarea = $( this ).siblings( 'textarea' );
textarea.toggleClass( 'hide' );
return false;
});
$( '#runButton' ).on( 'click', function() {
// Unselect button so it's not highlighted.
$( '#runButton' ).blur();
// Show the ajax spinner.
$( '.wpe-pcc-spinner' ).show();
// Empty the results textarea.
resetDisplay();
test_version = $( 'input[name=phptest_version]:checked' ).val();
only_active = $( 'input[name=active_plugins]:checked' ).val();
skip_list = $( 'textarea[name=skip_list]' ).val();
var data = {
'action': 'wpephpcompat_start_test',
'test_version': test_version,
'only_active': only_active,
'skip_list': skip_list,
'startScan': 1
};
$( '.wpe-pcc-test-version' ).text(test_version);
// Start the test!
jQuery.post( ajaxurl, data ).always(function() {
// Start timer to check scan status.
checkStatus();
});
});
$( '#cleanupButton' ).on( 'click', function() {
clearTimeout( timer );
jQuery.get( ajaxurl, { 'action': 'wpephpcompat_clean_up' }, function() {
resetDisplay();
checkStatus();
});
});
});
function startTimer() {
// Requeue the checkStatus call.
timer = setTimeout(function() {
checkStatus();
}, 5000);
}
/**
* Check the scan status and display results if scan is done.
*/
function checkStatus() {
var data = {
'action': 'wpephpcompat_check_status'
};
var obj;
jQuery.post( ajaxurl, data, function( obj ) {
// TODO: Without jQuery migrate an empty response can throw a JSON parse error.
// So we should do the parsing manually.
if ( !obj ) {
startTimer();
return;
}
/*
* Status false: the test is not running and has not been run yet
* Status 1: the test is currently running
* Status 0: the test as completed but is not currently running
*/
if ( false === obj.results ) {
jQuery( '#runButton' ).val( window.wpephpcompat.run );
} else {
jQuery( '#runButton' ).val( window.wpephpcompat.rerun );
}
if ( '1' === obj.status ) {
jQuery( '.wpe-pcc-spinner' ).show();
} else {
jQuery( '.wpe-pcc-spinner' ).hide();
}
if ( '0' !== obj.results ) {
if( false !== obj.results ) {
test_version = obj.version;
displayReport( obj.results );
}
jQuery( '#wpe-pcc-progress-count' ).hide();
} else {
// Display the current plugin count.
if ( obj.total ) {
jQuery( '#wpe-pcc-progress-count' ).show();
jQuery( '#wpe-pcc-progress-count' ).text( '(' + ( obj.total - obj.count + 1 ) + ' of ' + obj.total + ')' );
}
// Display the object being scanned.
jQuery( '#wpe-progress-active' ).html( '<strong>Now scanning:</strong> ' + obj.activeJob );
startTimer();
}
}, 'json' ).fail(function ( xhr, status, error )
{
// Server responded correctly, but the response wasn't valid.
if ( 200 === xhr.status ) {
alert( "Error: " + error + "\nResponse: " + xhr.responseText );
}
else { // Server didn't respond correctly.
alert( "Error: " + error + "\nStatus: " + xhr.status );
}
});
}
/**
* Clear previous results.
*/
function resetDisplay() {
jQuery( '#testResults' ).text('');
jQuery( '#wpe-pcc-standardMode' ).html('');
jQuery( '#wpe-pcc-progress-count' ).text('');
jQuery( '#wpe-progress-active' ).text('');
jQuery( '.wpe-pcc-download-report' ).hide();
jQuery( '.wpe-pcc-results' ).hide();
jQuery( '.wpe-pcc-information' ).hide();
}
/**
* Loop through a string and count the total matches.
* @param {RegExp} regex Regex to execute.
* @param {string} log String to loop through.
* @return {int} The total number of matches.
*/
function findAll( regex, log ) {
var m;
var count = 0;
while ( ( m = regex.exec( log ) ) !== null ) {
if ( m.index === regex.lastIndex ) {
regex.lastIndex++;
}
if ( parseInt( m[1] ) > 0 ) {
count += parseInt( m[1] );
}
}
return count;
}
/**
* Display the pretty report.
* @param {string} response Full test results.
*/
function displayReport( response ) {
// Clean up before displaying results.
resetDisplay();
var $ = jQuery;
var compatible = 1;
// Keep track of the number of failed plugins/themes.
var failedCount = 0;
var errorsRegex = /(\d*) ERRORS?/g;
var warningRegex = /(\d*) WARNINGS?/g;
var updateVersionRegex = /e: (.*?);/g;
var currentVersionRegex = /n: (.*?);/g;
// Grab and compile our template.
var source = $( '#result-template' ).html();
var template = Handlebars.compile( source );
$( '#testResults' ).text( response );
// Separate plugins/themes.
var plugins = response.replace( /^\s+|\s+$/g, '' ).split( window.wpephpcompat.name + ':' );
// Remove the first item, it's empty.
plugins.shift();
// Loop through them.
for ( var x in plugins ) {
var updateVersion;
var updateAvailable = 0;
var passed = 1;
var skipped = 0;
// Extract plugin/theme name.
var name = plugins[x].substring( 0, plugins[x].indexOf( '\n' ) );
// Extract results.
var log = plugins[x].substring( plugins[x].indexOf('\n'), plugins[x].length );
// Find number of errors and warnings.
var errors = findAll( errorsRegex, log );
var warnings = findAll( warningRegex, log );
// Check to see if there are any plugin/theme updates.
if ( updateVersionRegex.exec( log ) ) {
updateAvailable = 1;
}
// Update plugin and global compatibility flags.
if ( parseInt( errors ) > 0 ) {
compatible = 0;
passed = 0;
failedCount++;
}
// Trim whitespace and newlines from report.
log = log.replace( /^\s+|\s+$/g, '' );
if ( log.search('skipped') !== -1 ) {
skipped = 1;
}
// Use handlebars to build our template.
var context = {
plugin_name: name,
warnings: warnings,
errors: errors,
logs: log,
passed: passed,
skipped: skipped,
test_version: test_version,
updateAvailable: updateAvailable
};
var html = template( context );
$('#wpe-pcc-standardMode').append( html );
}
// Display global compatibility status.
if ( test_version == '7.0' && compatible ) {
// php 7 ready, and user tested version 7
jQuery( '.wpe-pcc-download-report' ).show();
jQuery( '.wpe-pcc-results' ).show();
} else if ( compatible ) {
jQuery( '.wpe-pcc-download-report' ).show();
jQuery( '.wpe-pcc-results' ).show();
jQuery( '.wpe-pcc-information-errors' ).show();
} else {
// Display scan stats.
jQuery( '.wpe-pcc-download-report' ).show();
$( '#wpe-pcc-standardMode' ).prepend( '<p>' + failedCount + ' ' + window.wpephpcompat.out_of + ' ' + plugins.length + ' ' + window.wpephpcompat.are_not + '.</p>' );
jQuery( '.wpe-pcc-information-errors' ).show();
jQuery( '.wpe-pcc-results' ).show();
}
}