-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_debugger.php
216 lines (178 loc) · 4.85 KB
/
code_debugger.php
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
<?php
/*
Plugin Name: Code Debugget
Plugin URI: http://www.andrespina.com/
Description: Enable debug code
Version: 1.0
Author: neoslink
Author URI: http://www.andrespina.com
License: GPL
*/
// If this file is called directly, abort.
defined('ABSPATH') or die();
class CodeDebugger {
/**
* Variable for singleton
* @var CodeDebugger
*/
private static $instance;
function __construct() {
define( 'CDEBUG_URL', plugin_dir_url( __FILE__ ) );
define( 'CDEBUG_PATH', plugin_dir_path( __FILE__ ) );
define( 'CDEBUG_URL_INCLUDE', CDEBUG_URL . 'includes/' );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
}
/**
* Initialize the singleton
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
self::$instance = new self;
}
return self::$instance;
}
/**
* This method add the admin_menu
*/
public function admin_menu() {
add_menu_page( 'Code Debugger', 'Code Debugger', 'manage_options', 'code-debugger', array( $this, 'page' ), 'dashicons-editor-code' );
}
/**
* This methos render page
*/
public function page() {
$this->register_style_and_javascript();
$content = '';
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ) {
$content = $this->clear_content( $_POST['textarea-debug'] );
}
echo '<div class="wrap">';
echo '<form action="#" method="POST">';
echo '<h1>Code Debugger</h1>';
echo '<div class="postbox">';
echo '<div class="inside">';
echo '<textarea id="textarea-debug" name="textarea-debug" class="large-text code" rows="10" cols="20">' . $content . '</textarea>';
echo '</div>';
echo '</div>';
echo '<input type="submit" value="Execute code" class="button button-primary">';
echo '</form>';
echo '</div>';
$this->execute();
}
/**
* This method execute de code
*/
public function execute() {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$title = 'Result execute';
try {
$content = $this->clear_content($_POST['textarea-debug']);
print '<div class="wrap">';
print '<h2>' . $title . '</h2>';
print '<div class="postbox">';
print '<div class="inside">';
eval($content);
print '</div>';
print '</div>';
print '</div>';
}
catch(Exception $e) {
echo '<div class="wrap">';
echo '<h2>' . $title . '</h2>';
echo '<div class="postbox">';
echo '<div class="inside">';
echo '<div class="error">';
echo $e->getMessage();
echo '</div>';
echo '</div>';
echo '</div>';
echo '</div>';
}
}
}
/**
* This method clear the content text debugger
*
* @param [string] $text [text code]
* @return [string] [clear text code]
*/
public function clear_content($text) {
$clear = array(
'find' => array(
"\'",
'\"'
),
'replace' => array(
"'",
'"'
)
);
$clear = apply_filters( 'cdebug_items_for_replace' ,$clear );
return str_replace( $clear['find'], $clear['replace'], $text );
}
/**
* This method add the style and javascript
*/
public function register_style_and_javascript() {
$items = array(
'cdebug_codemirror_style' => array(
'type' => 'style',
'file' => CDEBUG_URL_INCLUDE . 'codemirror/lib/codemirror.css'
),
'cdebug_codemirror_js' => array(
'type' => 'script',
'file' => CDEBUG_URL_INCLUDE . 'codemirror/lib/codemirror.js'
),
'cdebug_codemirror_addon_matchbrackets' => array(
'type' => 'script',
'file' => CDEBUG_URL_INCLUDE . 'codemirror/addon/edit/matchbrackets.js'
),
'cdebug_codemirror_mode_clike' => array(
'type' => 'script',
'file' => CDEBUG_URL_INCLUDE . 'codemirror/mode/clike/clike.js'
),
'cdebug_codemirror_mode_php' => array(
'type' => 'script',
'file' => CDEBUG_URL_INCLUDE . 'codemirror/mode/php/php.js'
),
'cdebug_own_js' => array(
'type' => 'script',
'file' => CDEBUG_URL_INCLUDE . 'own/code_debugger.js'
),
'cdebug_own_css' => array(
'type' => 'style',
'file' => CDEBUG_URL_INCLUDE . 'own/code_debugger.css'
),
);
foreach ($items as $key => $item) {
$funcname = array(
'register' => 'wp_register_' . $item['type'],
'enqueue' => 'wp_enqueue_' . $item['type']
);
$funcname['register']($key, $item['file'] );
$funcname['enqueue']($key);
}
}
}
/**
* Function code debugger
*
* @param [Any] $var [varible to debug]
* @param string $title [optional title for the current debug]
*/
function cdebug( $var, $title = '' ) {
include_once CDEBUG_PATH . 'includes/krumo/class.krumo.php';
echo '<div class="cdebug-result">';
if ( $title != '' ) {
echo '<div class="cdebug-result-head">' . $title . '</div>';
}
krumo($var);
echo '</div>';
}
/**
* Invoke plugin
*/
function GetCodeDebugger() {
return CodeDebugger::instance();
}
GetCodeDebugger();