/**
* FMA Enhanced Debug JavaScript
*
* Enhanced debugging capabilities for code editor
*
* @package Advanced File Manager
* @since 1.0.0
*/
(function($) {
'use strict';
// Debug panel object
var FMA_Debug = {
init: function() {
this.bindEvents();
this.createDebugPanel();
},
bindEvents: function() {
// Add debug button to code editor
$(document).on('click', '.fma-debug-toggle', this.toggleDebugPanel);
$(document).on('click', '.fma-debug-close', this.closeDebugPanel);
$(document).on('click', '.fma-debug-tab', this.switchTab);
$(document).on('click', '#validate-php-btn', this.validatePHP);
$(document).on('click', '#test-functions-btn', this.testExecFunctions);
},
createDebugPanel: function() {
var debugPanel = `
PHP Validation
Exec Functions
System Info
PHP Syntax Validation
⏳
Checking validation availability...
Validate Current Code
Exec Functions Status
Test All Functions
System Information
Loading system information...
`;
$('body').append(debugPanel);
this.loadSystemInfo();
},
toggleDebugPanel: function(e) {
e.preventDefault();
$('#fma-debug-panel').slideToggle();
},
closeDebugPanel: function() {
$('#fma-debug-panel').slideUp();
},
switchTab: function(e) {
e.preventDefault();
var tab = $(this).data('tab');
// Update tab buttons
$('.fma-debug-tab').removeClass('active');
$(this).addClass('active');
// Update tab content
$('.fma-debug-tab-content').hide();
$('#' + tab + '-tab').show();
},
validatePHP: function() {
var $btn = $(this);
var $results = $('#validation-results');
$btn.prop('disabled', true).text('Validating...');
$results.html('Validating PHP syntax...
');
// Get current editor content
var phpCode = '';
if (typeof editor !== 'undefined' && editor.getValue) {
phpCode = editor.getValue();
} else {
// Fallback to textarea
phpCode = $('textarea[name="content"]').val() || '';
}
if (!phpCode.trim()) {
$results.html('No PHP code to validate
');
$btn.prop('disabled', false).text('Validate Current Code');
return;
}
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'fma_validate_php',
php_code: phpCode,
filename: 'debug.php',
nonce: fmaDebug.nonce
},
success: function(response) {
FMA_Debug.displayValidationResults(response);
},
error: function(xhr, status, error) {
$results.html('Validation failed: ' + error + '
');
},
complete: function() {
$btn.prop('disabled', false).text('Validate Current Code');
}
});
},
displayValidationResults: function(response) {
var $results = $('#validation-results');
var html = '';
if (response.valid) {
html = '';
html += '✓ PHP syntax is valid ';
if (response.php_version) {
html += ' PHP Version: ' + response.php_version;
}
if (response.execution_time) {
html += ' Validation time: ' + response.execution_time + 'ms';
}
html += '
';
} else {
html = '';
html += '
✗ PHP syntax errors found ';
if (response.errors && response.errors.length > 0) {
html += '
';
response.errors.forEach(function(error) {
html += '';
html += 'Line ' + error.line + ': ';
html += error.message;
if (error.type) {
html += ' (' + error.type + ') ';
}
html += ' ';
});
html += ' ';
}
html += '
';
}
$results.html(html);
},
testExecFunctions: function() {
var $btn = $(this);
var $results = $('#functions-results');
$btn.prop('disabled', true).text('Testing...');
$results.html('Testing exec functions...
');
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'fma_test_exec_functions',
nonce: fmaDebug.nonce
},
success: function(response) {
FMA_Debug.displayFunctionResults(response.data);
},
error: function(xhr, status, error) {
$results.html('Test failed: ' + error + '
');
},
complete: function() {
$btn.prop('disabled', false).text('Test All Functions');
}
});
},
displayFunctionResults: function(results) {
var $results = $('#functions-results');
var html = '';
Object.keys(results).forEach(function(func) {
var result = results[func];
var statusClass = result.working ? 'working' : (result.available ? 'available' : 'unavailable');
var statusIcon = result.working ? '✓' : (result.available ? '⚠' : '✗');
var statusText = result.working ? 'Working' : (result.available ? 'Available' : 'Unavailable');
html += '
';
html += '
' + func + '
';
html += '
';
html += '' + statusIcon + ' ';
html += '' + statusText + ' ';
html += '
';
if (result.error) {
html += '
Error: ' + result.error + '
';
}
if (result.output) {
html += '
Output: ' + result.output + '
';
}
html += '
';
});
html += '
';
$results.html(html);
},
loadSystemInfo: function() {
$.ajax({
url: ajaxurl,
type: 'POST',
data: {
action: 'fma_get_debug_info',
nonce: fmaDebug.nonce
},
success: function(response) {
FMA_Debug.displaySystemInfo(response.data);
},
error: function(xhr, status, error) {
$('#system-info').html('Failed to load system info: ' + error + '
');
}
});
},
displaySystemInfo: function(info) {
var html = '';
html += 'PHP Version: ' + info.php_version + '
';
html += 'PHP Binary: ' + info.php_binary + '
';
if (info.php_path) {
html += 'PHP Path: ' + info.php_path + '
';
}
html += 'ExecWithFallback: ';
html += info.exec_with_fallback_available ?
'✓ Available ' :
'✗ Not Available ';
html += '
';
html += 'Validation Available: ';
html += info.validation_available ?
'✓ Yes ' :
'✗ No ';
html += '
';
// Update validation status
$('#validation-status').html(
info.validation_available ?
'✓ ' :
'✗ '
);
$('#validation-text').text(
info.validation_available ?
'PHP validation is available' :
'PHP validation is not available'
);
$('#system-info').html(html);
}
};
// Initialize when document is ready
$(document).ready(function() {
FMA_Debug.init();
});
// Add debug button to code editor when it loads
$(document).on('DOMNodeInserted', function(e) {
if ($(e.target).find('.CodeMirror').length > 0) {
if (!$('.fma-debug-toggle').length) {
var debugButton = '🐛 Debug ';
$(e.target).find('.CodeMirror').after(debugButton);
}
}
});
})(jQuery);