-
Notifications
You must be signed in to change notification settings - Fork 0
/
kb_debug.php
executable file
·350 lines (288 loc) · 8.75 KB
/
kb_debug.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
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/*
Plugin Name: KB_DEBUG
Plugin URI: http://kunal-b.in
Description: A swiss army knife for debugging wordpress plugins.
Version: 0.2-bleeding
Author: Kunal Bhalla.
Author URI: http://kunal-b.in
License: GPL2
Copyright 2010 Kunal Bhalla (email : bhalla.kunal@gmail.com)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Define KB_DISPLAY_HOOKS anywhere in your plugin/theme/code to show the
hooks at the end of execution. Define KB_DISPLAY_CONSTANTS anywhere to
show all constants defined.
Everything is pretty printed at shut down, to prevent irrevocable damage
(perhaps I exaggerate) to your theme. And to keep notices, warnings readable.
*/
/**
* The URL for the plugin.
* @global string KB_DEBUG_RELURL
*/
define( 'KB_DEBUG_RELURL', content_url() . "/mu-plugins" );
/**
* The main (and only) file for KB_Debug, divided into classes based on functionality.
* @package KB_Debug
* @author Kunal Bhalla
* @version 0.1
*/
/**
* Used to save debugging information logged by the user.
*/
class KB_Debug {
/**
* Store any errors, notices, warnings or debug information.
* @access private
* @var Array
*/
private $logged;
/**
* Constructor. Initializes values and assigns actions.
*/
public function __construct() {
$this->logged = Array();
add_action( 'shutdown', Array( &$this, 'display' ), 98 );
wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
}
/**
* Display all logged debugging information.
* @access public
*/
function display() {
echo "<div class = 'kb-debug-box' id = 'kb-debug'><div class = 'effect-border'>";
echo "<h2>Logged</h2>";
echo "<ul>";
foreach( $this->logged as $log ) {
echo "<li>";
echo "<h3>{$log->backtrace['line']}, {$log->backtrace['file']}</h3>";
foreach( $log->data as $data ) {
echo "<pre>"; print_r( $data ); echo "</pre>";
}
echo "</li>";
}
echo "</ul>";
echo "</div></div>";
}
/**
* Log array of arguments passed to it. Later pretty printed with a backtrace using print_r.
* Do not call this function directly.
* @access public
*/
function log( $arglist ) {
$log = new STDClass();
$log->data = $arglist;
$backtrace = debug_backtrace();
$log->backtrace = $backtrace[1];
$this->logged[] = $log;
}
}
/**
* Used to save errors and warnings information, if any.
* @package KB_Debug
*/
class KB_Debug_Errors {
/**
* Store any errors, notices, warnings or debug information.
* @access private
* @var Array
*/
private $logged;
/**
* Counters for filters available, filters used and gettexts.
* @access private
* @var Array
*/
private $counters;
/**
* Warning in case an existing error handler is removed.
* @access private
* @var String
*/
private $message;
/**
* Constructor. Initializes $counters and $logged.
*/
public function __construct( $display ) {
$this->logged = Array();
$this->counters = new STDClass();
$this->counters->errors = 0;
$this->counters->warnings = 0;
$this->counters->strict = 0;
$this->counters->notices = 0;
if ($display)
add_action( 'shutdown', Array( &$this, 'display' ), 99 );
$original_handler = set_error_handler( Array( &$this, 'log' ) );
$this->message = "";
if( $original_handler != NULL )
$this->message = "<p>Warning: An existing error handler was replaced." . print_r( $original_handler, true ) . "</p>";
wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
}
/**
* Toggles display of types of errors
*/
protected function toggleList() {
$output = <<<SCRIPT
<script type='text/javascript'>
(function($){
$(function(){
$('.kb-debug-error-toggle').click(function(){
$( '.kb-debug-' + $(this).first().attr('id').replace(/^kb-d-toggle-/, "") ).toggle();
})
$('.kb-debug-E_STRICT').toggle();
});
}(jQuery));
</script>
SCRIPT;
$output .= "Toggle —";
foreach( Array( 'E_ERROR', 'E_WARNING', 'E_NOTICE', 'E_STRICT', 'E_USER_NOTICE', 'E_USER_WARNING' ) as $type ) {
if( $type == "E_STRICT" ) $checked = ""; else $checked = "checked='checked'";
$output .= "<input type='checkbox' $checked class='kb-debug-error-toggle' id='kb-d-toggle-$type' /><label for='kb-d-toggle-$type'>$type</label>";
}
return $output;
}
/**
* Displays the logged data, if any.
* @access public
*/
public function display() {
echo "<div class = 'kb-debug-box'><div class = 'effect-border'>";
echo "<h2>Errors and Warnings</h2>";
echo $this->message;
echo $this->toggleList();
echo "<ul>";
foreach( $this->logged as $log ) {
$type = 'Strict';
switch ($log['no']) {
case E_ERROR: $type = 'E_ERROR'; break;
case E_WARNING: $type = 'E_WARNING'; break;
case E_NOTICE: $type = 'E_NOTICE'; break;
case E_STRICT: $type = 'E_STRICT'; break;
case E_USER_NOTICE: $type = 'E_USER_NOTICE'; break;
case E_USER_WARNING: $type = 'E_USER_WARNING'; break;
}
echo "<li class = 'kb-debug-{$type}'>";
echo "<h3>{$type}</h3>";
echo "<h4>{$log['line']}, {$log['file']}.</h4>";
echo "<p>{$log['str']}</p>";
echo "<h4>Context</h4>";
echo "<pre style = 'max-height: 100px;'>"; print_r($log['context']); echo "</pre>";
echo "</li>";
}
echo "</ul>";
echo "</div></div>";
}
/**
* Log all error based data.
* @access public
*/
public function log( $errno, $errstr, $errfile, $errline, $errcontext ) {
$this->logged[] = Array(
'no' => $errno,
'str' => $errstr,
'file' => $errfile,
'line' => $errline,
'context' => $errcontext
);
return 1;
}
}
/**
* Logs all hook information including arguments passed and functions run on each hook.
* @package KB_Debug
*/
class KB_Debug_Hooks {
/**
* Store any Hook information
* @access private
* @var Array
*/
private $logged;
/**
* Counters for filters available, filters used and gettexts.
* @access private
* @var Array
*/
private $counters;
/**
* Constructor. Initializes $counters and $logged.
*/
public function __construct() {
$this->logged = Array();
$this->counters = new STDClass();
$this->counters->actions = 0;
$this->counters->used = 0;
$this->counters->gettexts = 0;
add_action( 'all', Array( &$this, 'log' ) );
add_action( 'shutdown', Array( &$this, 'display' ), 100 );
wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
}
/**
* Displays the logged data, if any.
* @access public
*/
public function display() {
echo "<div class = 'kb-debug-box'><div class = 'effect-border'>";
echo "<h2>Hooks List (Total {$this->counters->actions}, {$this->counters->gettexts} gettext calls omitted)</h2>";
echo "<ul>";
foreach( $this->logged as $log ) {
echo "<li>";
echo "<h3>{$log['name']}</h3>";
echo "<h4>Arguments</h4>";
if ( !empty( $log['vars'] ) ) {
echo "<pre>";print_r( $log['vars'] );echo "</pre>";
} else echo "<pre>None.</pre>";
echo "<h4>Functions</h4>";
echo "<pre>";print_r( $log['funcs'] );echo "</pre>";
echo "</li>";
}
echo "</ul>";
echo "</div></div>";
}
/**
* Log all hook based data.
* @access public
*/
public function log( $args, $vars = '' ) {
global $wp_filter, $wp_query;
if ( $args != 'gettext' && $args != 'gettext_with_context' ) {
if ( array_key_exists( $args, $wp_filter ) ) {
$funcs = $wp_filter[$args];
ksort($funcs);
$this->counters->used++;
$this->logged[] = Array( 'vars' => $vars, 'funcs' => $funcs, 'name' => $args );
}
$this->counters->actions++;
} else $this->counters->gettexts++;
}
}
/** Enqueue the styling for kb_debug. */
wp_enqueue_style( 'kb-debug-css', KB_DEBUG_RELURL . "/kb-debug.css" );
/**
* Call this function to save any data. Records calling information via a stack trace.
*/
function kb_debug() {
static $kdb;
if ( !isset( $kdb ) ) $kdb = new KB_Debug();
$kdb->log( func_get_args() );
}
/**
* Check the $_GET variable and initialize classes accordingly.
*/
if (defined ('WP_DEBUG') && WP_DEBUG) {
wp_enqueue_script( 'jquery' );
/**
* Always log the errors, just don't show them if not called explicitly.
*/
new KB_Debug_Errors( isset( $_GET['KB_Debug_Errors'] ) || ( defined( 'KB_DEBUG' ) && KB_DEBUG ) );
if( isset( $_GET['KB_Debug_Hooks'] ) || ( defined( 'KB_DEBUG_HOOKS' ) && KB_DEBUG_HOOKS ) )
new KB_Debug_Hooks();
}