| 1 |
<?php |
| 2 |
/** |
| 3 |
* JS code to manage queue. |
| 4 |
*/ |
| 5 |
|
| 6 |
?> |
| 7 |
// Hide object text box |
| 8 |
var queueOriginEl = document.getElementById('ao-ccss-queue' ); |
| 9 |
if (queueOriginEl) { |
| 10 |
queueOriginEl.style.display = 'none'; |
| 11 |
|
| 12 |
// Get queue object and call table renderer |
| 13 |
jQuery(document).ready(function() { |
| 14 |
// Instance and parse queue object |
| 15 |
var aoCssQueueRaw = document.getElementById('ao-ccss-queue').value; |
| 16 |
var aoCssQueue = aoCssQueueRaw.indexOf('{"') === 0 ? |
| 17 |
JSON.parse(aoCssQueueRaw) : |
| 18 |
""; |
| 19 |
var aoCssQueueLog = aoCssQueue === "" ? |
| 20 |
"empty" : |
| 21 |
aoCssQueue; |
| 22 |
<?php |
| 23 |
if ( $ao_ccss_debug ) { |
| 24 |
echo "console.log( 'Queue Object:', aoCssQueueLog );\n"; |
| 25 |
} |
| 26 |
?> |
| 27 |
|
| 28 |
// hook up "remove all jobs" button to the JS action. |
| 29 |
jQuery("#removeAllJobs").click(function(){removeAllJobs();}); |
| 30 |
|
| 31 |
// Render queue table |
| 32 |
drawQueueTable(aoCssQueue); |
| 33 |
|
| 34 |
// Make queue table sortable if there are any elements |
| 35 |
var queueBodyEl = jQuery('#queue > tr').length; |
| 36 |
if (queueBodyEl > 0) { |
| 37 |
jQuery('#queue-tbl').tablesorter({ |
| 38 |
sortList: [[0,0]], |
| 39 |
headers: {6: {sorter: false}} |
| 40 |
}); |
| 41 |
} |
| 42 |
|
| 43 |
// unhide queuerunner button conditionally (we don't want people running the queue continuously) and attach event to it. |
| 44 |
if (queueBodyEl > 4 || ( queueBodyEl > 0 && jQuery('#rules > tr').length < 1 ) ) { |
| 45 |
jQuery('#queuerunner-container').show(); |
| 46 |
jQuery("#queuerunner").click(function(){queuerunner();}); |
| 47 |
} |
| 48 |
}); |
| 49 |
} |
| 50 |
|
| 51 |
// Render the queue in a table |
| 52 |
function drawQueueTable(queue) { |
| 53 |
jQuery('#queue').empty(); |
| 54 |
rowNumber=0; |
| 55 |
jQuery.each(queue, function(path, keys) { |
| 56 |
// Prepare commom job values |
| 57 |
ljid = keys.ljid; |
| 58 |
targetArr = keys.rtarget.split('|' ); |
| 59 |
target = targetArr[1]; |
| 60 |
type = keys.ptype; |
| 61 |
ctime = EpochToDate(keys.jctime); |
| 62 |
rbtn = false; |
| 63 |
dbtn = false; |
| 64 |
hbtn = false; |
| 65 |
|
| 66 |
// don't list jobs that don't have a type, they are irrelevant and this also avoids "type.replace is not a function". |
| 67 |
if ( type == false ) { |
| 68 |
return; |
| 69 |
} |
| 70 |
|
| 71 |
// Prepare job statuses |
| 72 |
if (keys.jqstat === 'NEW') { |
| 73 |
// Status: NEW (N, sort order 1) |
| 74 |
status = '<span class="hidden">1</span>N'; |
| 75 |
statusClass = 'new'; |
| 76 |
title = '<?php _e( 'New', 'autoptimize' ); ?> (' + ljid + ')'; |
| 77 |
buttons = '<?php _e( 'None', 'autoptimize' ); ?>'; |
| 78 |
} else if (keys.jqstat === 'JOB_QUEUED' || keys.jqstat === 'JOB_ONGOING') { |
| 79 |
// Status: PENDING (P, sort order 2) |
| 80 |
status = '<span class="hidden">2</span>P'; |
| 81 |
statusClass = 'pending'; |
| 82 |
title = '<?php _e( 'PENDING', 'autoptimize' ); ?> (' + ljid + ')'; |
| 83 |
buttons = '<?php _e( 'None', 'autoptimize' ); ?>'; |
| 84 |
} else if (keys.jqstat === 'JOB_DONE' && keys.jrstat === 'GOOD' && (keys.jvstat === 'WARN' || keys.jvstat === 'BAD')) { |
| 85 |
// Status: REVIEW (R, sort order 5) |
| 86 |
status = '<span class="hidden">5</span>R'; |
| 87 |
statusClass = 'review'; |
| 88 |
title = "<?php _e( 'REVIEW', 'autoptimize' ); ?> (" + ljid + ")\n<?php _e( 'Info from criticalcss.com:', 'autoptimize' ); ?>\n<?php _e( '- Job ID: ', 'autoptimize' ); ?>" + keys.jid + "\n<?php _e( '- Status: ', 'autoptimize' ); ?>" + keys.jqstat + "\n<?php _e( '- Result: ', 'autoptimize' ); ?>" + keys.jrstat + "\n<?php _e( '- Validation: ', 'autoptimize' ); ?>" + keys.jvstat; |
| 89 |
buttons = '<span class="button-secondary" id="' + ljid + '_remove" title="<?php _e( 'Delete Job', 'autoptimize' ); ?>"><span class="dashicons dashicons-trash"></span></span>'; |
| 90 |
dbtn = true; |
| 91 |
} else if (keys.jqstat === 'JOB_DONE') { |
| 92 |
// Status: DONE (D, sort order 6) |
| 93 |
status = '<span class="hidden">6</span>D'; |
| 94 |
statusClass = 'done'; |
| 95 |
title = '<?php _e( 'DONE', 'autoptimize' ); ?> (' + ljid + ')'; |
| 96 |
buttons = '<span class="button-secondary" id="' + ljid + '_remove" title="<?php _e( 'Delete Job', 'autoptimize' ); ?>"><span class="dashicons dashicons-trash"></span></span>'; |
| 97 |
dbtn = true; |
| 98 |
} else if (keys.jqstat === 'JOB_FAILED' || keys.jqstat === 'STATUS_JOB_BAD' || keys.jqstat === 'INVALID_JWT_TOKEN' || keys.jqstat === 'NO_CSS' || keys.jqstat === 'NO_RESPONSE') { |
| 99 |
// Status: ERROR (E, sort order 4) |
| 100 |
status = '<span class="hidden">4</span>E'; |
| 101 |
statusClass = 'error'; |
| 102 |
title = "<?php _e( 'ERROR', 'autoptimize' ); ?> (" + ljid + ")\n<?php _e( 'Info from criticalcss.com:', 'autoptimize' ); ?>\n<?php _e( '- Job ID: ', 'autoptimize' ); ?>" + keys.jid + "\n<?php _e( '- Status: ', 'autoptimize' ); ?>" + keys.jqstat + "\n<?php _e( '- Result: ', 'autoptimize' ); ?>" + keys.jrstat + "\n<?php _e( '- Validation: ', 'autoptimize' ); ?>" + keys.jvstat; |
| 103 |
buttons = '<span class="button-secondary" id="' + ljid + '_retry" title="<?php _e( 'Retry Job', 'autoptimize' ); ?>"><span class="dashicons dashicons-update"></span></span><span class="button-secondary to-right" id="' + ljid + '_remove" title="<?php _e( 'Delete Job', 'autoptimize' ); ?>"><span class="dashicons dashicons-trash"></span></span><span class="button-secondary to-right" id="' + ljid + '_help" title="<?php _e( 'Get Help', 'autoptimize' ); ?>"><span class="dashicons dashicons-sos"></span></span>'; |
| 104 |
rbtn = true; |
| 105 |
dbtn = true; |
| 106 |
hbtn = true; |
| 107 |
} else { |
| 108 |
// Status: UNKNOWN (U, sort order 5) |
| 109 |
status = '<span class="hidden">5</span>U'; |
| 110 |
statusClass = 'unknown'; |
| 111 |
title = "<?php _e( 'UNKNOWN', 'autoptimize' ); ?> (" + ljid + ")\n<?php _e( 'Info from criticalcss.com:', 'autoptimize' ); ?>\n<?php _e( '- Job ID: ', 'autoptimize' ); ?>" + keys.jid + "\n<?php _e( '- Status: ', 'autoptimize' ); ?>" + keys.jqstat + "\n<?php _e( '- Result: ', 'autoptimize' ); ?>" + keys.jrstat + "\n<?php _e( '- Validation: ', 'autoptimize' ); ?>" + keys.jvstat; |
| 112 |
buttons = '<span class="button-secondary" id="' + ljid + '_remove" title="<?php _e( 'Delete Job', 'autoptimize' ); ?>"><span class="dashicons dashicons-trash"></span></span><span class="button-secondary to-right" id="' + ljid + '_help" title="<?php _e( 'Get Help', 'autoptimize' ); ?>"><span class="dashicons dashicons-sos"></span></span>'; |
| 113 |
dbtn = true; |
| 114 |
hbtn = true; |
| 115 |
} |
| 116 |
|
| 117 |
// Prepare job finish time |
| 118 |
if (keys.jftime === null) { |
| 119 |
ftime = '<?php _e( 'N/A', 'autoptimize' ); ?>'; |
| 120 |
} else { |
| 121 |
ftime = EpochToDate(keys.jftime); |
| 122 |
} |
| 123 |
|
| 124 |
// Append job entry |
| 125 |
jQuery("#queue").append("<tr id='" + ljid + "' class='job " + statusClass + "'><td class='status'><span class='badge " + statusClass + "' title='<?php _e( 'Job status is ', 'autoptimize' ); ?>" + title + "'>" + status + "</span></td><td>" + target.replace(/(woo_|template_|custom_post_|edd_|bp_|bbp_)/,'') + "</td><td>" + path + "</td><td>" + type.replace(/(woo_|template_|custom_post_|edd_|bp_|bbp_)/,'') + "</td><td>" + ctime + "</td><td>" + ftime + "</td><td class='btn'>" + buttons + "</td></tr>"); |
| 126 |
|
| 127 |
// Attach button actions |
| 128 |
if (rbtn) { |
| 129 |
jQuery('#' + ljid + '_retry').click(function(){retryJob(queue, this.id, path);}); |
| 130 |
} |
| 131 |
if (dbtn) { |
| 132 |
jQuery('#' + ljid + '_remove').click(function(){delJob(queue, this.id, path);}); |
| 133 |
} |
| 134 |
if (hbtn) { |
| 135 |
jQuery('#' + ljid + '_help').click(function(){jid=this.id.split('_' );window.open('https://criticalcss.com/faq?aoid=' + jid[0], '_blank' );}); |
| 136 |
} |
| 137 |
}); |
| 138 |
} |
| 139 |
|
| 140 |
// Delete a job from the queue |
| 141 |
function delJob(queue, jid, jpath) { |
| 142 |
jid = jid.split('_' ); |
| 143 |
jQuery('#queue-confirm-rm').dialog({ |
| 144 |
resizable: false, |
| 145 |
height: 180, |
| 146 |
modal: true, |
| 147 |
buttons: { |
| 148 |
"<?php _e( 'Delete', 'autoptimize' ); ?>": function() { |
| 149 |
delete queue[jpath]; |
| 150 |
updateQueue(queue); |
| 151 |
jQuery(this).dialog('close' ); |
| 152 |
}, |
| 153 |
"<?php _e( 'Cancel', 'autoptimize' ); ?>": function() { |
| 154 |
jQuery(this).dialog('close' ); |
| 155 |
} |
| 156 |
} |
| 157 |
}); |
| 158 |
} |
| 159 |
|
| 160 |
function removeAllJobs() { |
| 161 |
jQuery( "#queue-confirm-rm-all" ).dialog({ |
| 162 |
resizable: false, |
| 163 |
height:235, |
| 164 |
modal: true, |
| 165 |
buttons: { |
| 166 |
"<?php _e( 'Delete all jobs?', 'autoptimize' ); ?>": function() { |
| 167 |
queue=[]; |
| 168 |
updateQueue(queue); |
| 169 |
jQuery( this ).dialog( "close" ); |
| 170 |
}, |
| 171 |
"<?php _e( 'Cancel', 'autoptimize' ); ?>": function() { |
| 172 |
jQuery( this ).dialog( "close" ); |
| 173 |
} |
| 174 |
} |
| 175 |
}); |
| 176 |
} |
| 177 |
|
| 178 |
// Retry jobs with error |
| 179 |
function retryJob(queue, jid, jpath) { |
| 180 |
jid = jid.split('_' ); |
| 181 |
jQuery('#queue-confirm-retry').dialog({ |
| 182 |
resizable: false, |
| 183 |
height: 180, |
| 184 |
modal: true, |
| 185 |
buttons: { |
| 186 |
"<?php _e( 'Retry', 'autoptimize' ); ?>": function() { |
| 187 |
<?php |
| 188 |
if ( $ao_ccss_debug ) { |
| 189 |
echo "console.log( 'SHOULD retry job:', jid[0], jpath );\n"; |
| 190 |
} |
| 191 |
?> |
| 192 |
queue[jpath].jid = null; |
| 193 |
queue[jpath].jqstat = 'NEW'; |
| 194 |
queue[jpath].jrstat = null; |
| 195 |
queue[jpath].jvstat = null; |
| 196 |
queue[jpath].jctime = (new Date).getTime() / 1000; |
| 197 |
queue[jpath].jftime = null; |
| 198 |
updateQueue(queue); |
| 199 |
jQuery(this).dialog('close' ); |
| 200 |
}, |
| 201 |
"<?php _e( 'Cancel', 'autoptimize' ); ?>": function() { |
| 202 |
jQuery(this).dialog('close' ); |
| 203 |
} |
| 204 |
} |
| 205 |
}); |
| 206 |
} |
| 207 |
|
| 208 |
// Refresh queue |
| 209 |
function updateQueue(queue) { |
| 210 |
document.getElementById('ao-ccss-queue').value=JSON.stringify(queue); |
| 211 |
drawQueueTable(queue); |
| 212 |
jQuery('#unSavedWarning').show(); |
| 213 |
document.getElementById('ao_title_and_button').scrollIntoView(); |
| 214 |
<?php |
| 215 |
if ( $ao_ccss_debug ) { |
| 216 |
echo "console.log('Updated Queue Object:', queue);\n"; |
| 217 |
} |
| 218 |
?> |
| 219 |
} |
| 220 |
|
| 221 |
// Run the queue manually (in case of cron issues/ impatient users). |
| 222 |
function queuerunner() { |
| 223 |
var data = { |
| 224 |
'action': 'ao_ccss_queuerunner', |
| 225 |
'ao_ccss_queuerunner_nonce': '<?php echo wp_create_nonce( 'ao_ccss_queuerunner_nonce' ); ?>', |
| 226 |
}; |
| 227 |
|
| 228 |
jQuery.post(ajaxurl, data, function(response) { |
| 229 |
response_array=JSON.parse(response); |
| 230 |
if (response_array['code'] == 200) { |
| 231 |
displayNotice( '<?php _e( 'Queue processed, reloading page.', 'autoptimize' ); ?>', 'success' ) |
| 232 |
setTimeout(window.location.reload.bind(window.location), 1.5*1000); |
| 233 |
} else if ( response_array['code'] == 302 ) { |
| 234 |
displayNotice( '<?php _e( 'The queue is locked, retry in a couple of minutes. If this problem persists and the queue is not moving at all remove the <code>wp-content/uploads/ao_ccss/queue.lock</code> file.', 'autoptimize' ); ?>', 'warning' ) |
| 235 |
} else { |
| 236 |
displayNotice( '<?php _e( 'Could not process queue.', 'autoptimize' ); ?>', 'error' ) |
| 237 |
} |
| 238 |
}); |
| 239 |
} |
| 240 |
|
| 241 |
// Convert epoch to date for job times |
| 242 |
function EpochToDate(epoch) { |
| 243 |
if (epoch < 10000000000) |
| 244 |
epoch *= 1000; |
| 245 |
var epoch = epoch + (new Date().getTimezoneOffset() * -1); //for timeZone |
| 246 |
var sdate = new Date(epoch); |
| 247 |
var ldate = sdate.toLocaleString(); |
| 248 |
return ldate; |
| 249 |
} |
| 250 |
|