-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-widget.js
More file actions
60 lines (54 loc) · 1.97 KB
/
Copy pathadmin-widget.js
File metadata and controls
60 lines (54 loc) · 1.97 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
jQuery(document).ready(function($) {
function evaluateCode() {
const code = $('#wp-http-eval-code').val();
const resultDiv = $('#wp-http-eval-result');
if (!code) return;
resultDiv.hide().empty();
$.ajax({
url: wpHttpEval.ajaxUrl + '?action=wp_http_eval&_ajax_nonce=' + wpHttpEval.nonce,
type: 'POST',
data: code,
contentType: 'text/plain',
processData: false,
beforeSend: function() {
$('#wp-http-eval-submit').prop('disabled', true).text('Evaluating...');
},
success: function(response) {
if (response.success) {
// Format the result properly for display
let formattedResult;
if (typeof response.result === 'object' && response.result !== null) {
formattedResult = JSON.stringify(response.result, null, 2);
} else {
formattedResult = String(response.result);
}
resultDiv.html('<strong>Result:</strong><pre>' + $('<div>').text(formattedResult).html() + '</pre>');
} else {
// Handle WordPress error format
const errorMessage = response.errors
? Object.values(response.errors)[0][0]
: response.data?.message || 'Unknown error';
resultDiv.html('<strong>Error:</strong> ' + errorMessage);
}
},
error: function(xhr) {
const response = xhr.responseJSON || {};
const errorMessage = response.errors
? Object.values(response.errors)[0][0]
: response.data?.message || 'Unknown error occurred';
resultDiv.html('<strong>Error:</strong> ' + errorMessage);
},
complete: function() {
$('#wp-http-eval-submit').prop('disabled', false).text('Evaluate');
resultDiv.show();
}
});
}
$('#wp-http-eval-submit').click(evaluateCode);
$('#wp-http-eval-code').on('keydown', function(e) {
if (e.ctrlKey && e.key === 'Enter') {
evaluateCode();
e.preventDefault();
}
});
});