-
Notifications
You must be signed in to change notification settings - Fork 17
/
debug_objects.php
executable file
·395 lines (335 loc) · 12.6 KB
/
debug_objects.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
/**
* @package Debug Objects
* @author Frank Bültge
*/
/**
* Plugin Name: Debug Objects
* Plugin URI: http://bueltge.de/debug-objects-wordpress-plugin/966/
* Text Domain: debug_objects
* Domain Path: /languages
* Description: List filter and action-hooks, cache data, defined constants, qieries, included scripts and styles, php and memory informations and return of conditional tags only for admins; for debug, informations or learning purposes. Setting output in the settings of the plugin and use output via setting or url-param '<code>debug</code>' or set a cookie via url param '<code>debugcookie</code>' in days
* Version: 2.1.7
* License: GPLv3
* Author: Frank Bültge
* Author URI: http://bueltge.de/
* Last Change: 04/26/2012
*/
// error_reporting(E_ALL);
// avoid direct calls to this file, because now WP core and framework has been used.
if ( ! function_exists( 'add_filter' ) ) {
echo "Hi there! I'm just a part of plugin, not much I can do when called directly.";
exit;
}
if ( ! class_exists( 'Debug_Objects' ) ) {
// include plugin on hook
add_action( 'plugins_loaded', array( 'Debug_Objects', 'get_object' ) );
register_activation_hook( __FILE__, array( 'Debug_Objects', 'on_activation' ) );
class Debug_Objects {
static private $classobj = NULL;
// table for page hooks
public static $table = 'hook_list';
// var for tab array
public static $tabs = array();
// string vor save in DB
public static $option_string = 'debug_objects';
// plugin basename
public static $plugin;
// included classes on default; without user settings
public static $by_settings = array( 'Wrap' );
// exlude class for central include
public static $exclude_class = array( 'Backend', 'Frontend' );
/**
* Handler for the action 'init'. Instantiates this class.
*
* @access public
* @since 2.0.0
* @return $classobj
*/
public function get_object() {
if ( NULL === self :: $classobj ) {
self :: $classobj = new self;
}
return self :: $classobj;
}
/**
* Init other methods via hook; install settings and capabilities
*
* @since 2.0.0
* @return void
*/
public function __construct() {
// define table
self :: $table = $GLOBALS['wpdb'] -> base_prefix . self::$table;
self :: $plugin = plugin_basename( __FILE__ );
// add and remove settings, the table for the plugin
register_deactivation_hook( __FILE__, array( __CLASS__, 'on_deactivation' ) );
register_uninstall_hook( __FILE__, array( 'Debug_Objects', 'on_deactivation' ) );
// Include settings
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class-settings.php';
self :: init_classes();
}
/**
* Include classes
* Use filter string 'debug_objects_classes' for include custom classes
*
* @since 2.0.0
* @return void
*/
private function init_classes() {
if ( is_multisite() && is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
$options = get_site_option( self :: $option_string );
else
$options = get_option( self :: $option_string );
if ( ( isset( $options['frontend'] ) && '1' === $options['frontend'] ) ||
( isset( $options['backend'] ) && '1' === $options['backend'] ) )
$view = TRUE;
else
$view = FALSE;
// exclude options from include classes
foreach ( self :: $exclude_class as $exclude_class ) {
if ( isset( $options[ strtolower( $exclude_class ) ] ) )
unset( $options[ strtolower( $exclude_class ) ] );
}
if ( ! empty( $options ) ) {
foreach ( $options as $class => $check ) {
if ( '1' === $check )
self :: $by_settings[] = ucwords( $class );
}
}
$classes = apply_filters( 'debug_objects_classes', self :: $by_settings );
self::set_cookie_control();
if ( $view || self::debug_control()
) {
foreach ( $classes as $key => $require )
require_once dirname( __FILE__ ) . DIRECTORY_SEPARATOR . 'inc/class-' . strtolower( $require ) . '.php';
foreach ( $classes as $class )
add_action( 'init', array( 'Debug_Objects_' . $class, 'init' ) );
}
}
/**
* Check for url param to view output
*
* @access public
* @since 2.0.1
* @return $debug boolean
*/
public function debug_control() {
// Debug via _GET Param on URL
if ( ! isset( $_GET['debug'] ) )
$debug = FALSE;
else
$debug = TRUE;
if ( ! $debug )
$debug = self::get_cookie_control( $debug );
return (bool) $debug;
}
/**
* Check for cookie to view output
*
* @access public
* @since 2.0.1
* @return $debug boolean
*/
public function get_cookie_control( $debug ) {
if ( ! isset( $_COOKIE[self::get_plugin_data() . '_cookie'] ) )
return FALSE;
if ( 'Debug_Objects_True' === $_COOKIE[self::get_plugin_data() . '_cookie'] )
$debug = TRUE;
return (bool) $debug;
}
/**
* Init cookie and control the live time
*
* @access public
* @since 2.0.1
* @return void
*/
public function set_cookie_control() {
if ( ! isset( $_GET['debugcookie'] ) )
return;
if ( $_GET['debugcookie'] ) {
$cookie_live = time() + 60 * 60 * 24 * intval( $_GET['debugcookie'] ); // days
setcookie( $this->get_plugin_data() . '_cookie', 'Debug_Objects_True', $cookie_live, COOKIEPATH, COOKIE_DOMAIN );
}
if ( 0 == intval( $_GET['debugcookie'] ) )
setcookie( $this->get_plugin_data() . '_cookie', '', time() - 3600, COOKIEPATH, COOKIE_DOMAIN );
}
/**
* Return plugin comment data
*
* @since 2.0.0
* @access public
* @param $value string, default = 'TextDomain'
* Name, PluginURI, Version, Description, Author, AuthorURI, TextDomain, DomainPath, Network, Title
* @return string
*/
public static function get_plugin_data( $value = 'TextDomain', $echo = FALSE ) {
if ( ! function_exists( 'get_plugin_data' ) )
require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
$plugin_data = get_plugin_data( __FILE__ );
$plugin_value = $plugin_data[$value];
if ( $echo )
echo $plugin_value;
else
return $plugin_value;
}
/**
* Return plugin basename of plugin
*
* @since 2.0.0
* @return string
*/
public static function get_plugin_string() {
return self :: $plugin;
}
/**
* Add user rights and the db-table
*
* @since 2.0.0
* @return void
*/
public function on_activation() {
// Check for PHP Version 5.3
if ( ! version_compare( PHP_VERSION, '5.2.4', '>=' ) ) {
deactivate_plugins( __FILE__ );
wp_die(
wp_sprintf(
'<strong>%s:</strong> ' . __( 'Sorry, This plugin requires PHP 5.2.4' ),
self :: get_plugin_data( 'Name' )
)
);
}
//add_option( self :: $option_string, array( 'php' => '1', 'hooks' => '1', 'about' => '1' ) );
$GLOBALS['wp_roles'] -> add_cap( 'administrator', '_debug_objects' );
// add table
$table = $GLOBALS['wpdb'] -> base_prefix . self::$table;
$GLOBALS['wpdb'] -> query(
"CREATE TABLE $table (
called_by varchar(96) NOT NULL,
hook_name varchar(96) NOT NULL,
hook_type varchar(15) NOT NULL,
first_call int(11) NOT NULL,
arg_count tinyint(4) NOT NULL,
file_name varchar(128) NOT NULL,
line_num smallint NOT NULL,
PRIMARY KEY (first_call,hook_name) )"
);
}
/**
* Delete user rights and the db-table
*
* @since 2.0.0
* @return void
*/
public static function on_deactivation() {
unregister_setting( self :: $option_string . '_group', self :: $option_string );
delete_option( self :: $option_string );
$GLOBALS['wp_roles'] -> remove_cap( 'administrator', '_debug_objects' );
// remove hook table
$GLOBALS['wpdb'] -> query( "DROP TABLE IF EXISTS " . self::$table );
}
/**
* Return undefined list as tree
*
* @access public
* @since 2.0.0
* @param $arr array
* @param $root_name string
* @param $unserialized_string boolean
* @return $output array
*/
public function get_as_ul_tree( $arr, $root_name = '', $unserialized_string = FALSE ) {
global $wp_object;
$wp_object = 0;
$output = '';
$wp_object ++;
if ( ! is_object($arr) && ! is_array($arr) )
return $output;
if ($root_name) {
$output .= '<ul class="root' . ($unserialized_string ? ' unserialized' : '' ) . '">' . "\n";
if ( is_object($arr) ) {
$output .= '<li class="vt-object"><span class="' . ($unserialized_string ? 'unserialized' : 'key' ) . '">' . $root_name . '</span>';
if (!$unserialized_string)
$output .= '<br />' . "\n";
$output .= '<small><em>type</em>: object ( ' . get_class($arr) . ' )</small><br/><small><em>count</em>: ' . count( get_object_vars($arr) ) . '</small><ul>';
} else {
$output .= '<li class="vt-array"><span class="' . ($unserialized_string ? 'unserialized' : 'key' ) . '">' . $root_name . '</span>';
if (!$unserialized_string)
$output .= '<br />' . "\n";
$output .= '<small><em>type</em>: array</small><br/><small><em>count</em>: ' . count($arr) . '</small><ul>';
}
}
foreach($arr as $key => $val) {
$wp_object ++;
if ( is_numeric($key) )
$key = "[". $key. "]";
$vt = gettype($val);
switch ($vt) {
case "object":
$output .= "<li class=\"vt-$vt\"><span class=\"key\">" . htmlspecialchars($key) . '</span>';
$output .= "<br/><small><em>type</em>: $vt (" . get_class($val) . ") | <em>count</em>: " . count($val) . "</small>";
if ($val) {
$output .= '<ul>';
$output .= Debug_Objects :: get_as_ul_tree($val);
$output .= '</ul>';
}
$output .= '</li>';
break;
case "array":
$output .= "<li class=\"vt-$vt\"><span class=\"key\">" . htmlspecialchars($key) . '</span>';
$output .= "<br/><small><em>type</em>: $vt | <em>count</em>: " . count($val) . '</small>';
if ($val) {
$output .= '<ul>';
$output .= Debug_Objects :: get_as_ul_tree($val);
$output .= '</ul>';
}
$output .= '</li>';
break;
case "boolean":
$output .= "<li class=\"vt-$vt\"><span class=\"key\">" . htmlspecialchars($key) . '</span>';
$output .= "<br/><small><em>type</em>: $vt</small><br/><small><em>value</em>: </small><span class=\"value\">".($val?"true":"false"). '</span></li>';
break;
case "integer":
case "double":
case "float":
$output .= "<li class=\"vt-$vt\"><span class=\"key\">" . htmlspecialchars($key) . '</span>';
$output .= "<br/><small><em>type</em>: $vt</small><br/><small><em>value</em>: </small><span class=\"value\">$val</span></li>";
break;
case "string":
$val = trim( $val );
//$val = strtolower( stripslashes( $val ) );
//$val = base64_decode($val);
$val = preg_replace( '/;n;/', ';N;', $val );
$val = str_replace( "\n", "", $val );
$val = normalize_whitespace($val);
if ( is_serialized_string( $val ) )
$obj = unserialize( $val );
else
$obj = normalize_whitespace( $val );
$is_serialized = ($obj !== false && preg_match("/^(O:|a:)/", $val));
$output .= "<li class=\"vt-$vt\"><span class=\"key\">" . htmlspecialchars($key) . '</span>';
$output .= "<br/><small><em>type</em>: $vt | <em>size</em>: ".strlen($val). " | <em>serialized</em>: ".(is_serialized($val) !== false?"true":"false"). '</small><br/>';
if ( is_serialized($val) ) {
$output .= Debug_Objects :: get_as_ul_tree($obj, "<small><em>value</em>:</small> <span class=\"value\">[unserialized]</span>", true);
}
else {
if ($val)
$output .= '<small><em>value</em>: </small><span class="value">' . htmlspecialchars($val) . '</span>';
else
$output .= '';
}
$output .= '</li>';
break;
default: //what the hell is this ?
$output .= '<li id="hook_' . $wp_object . '_' . $vt . '" class="vt-' . $vt . '"><span class="key">' . htmlspecialchars($key) . '</span>';
$output .= '<br/><small><em>type</em>: ' . $vt . '</small><br/><small><em>value</em>:</small><span class="value">' . @htmlspecialchars($val) . '</span></li>';
break;
}
}
if ( $root_name )
$output .= "\t" . '</ul>' . "\n\t" . '</li>' . "\n" . '</ul>' . "\n";
return $output;
}
} // end class
} // end if class exists