-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-plugin.php
More file actions
325 lines (293 loc) · 8.01 KB
/
debug-plugin.php
File metadata and controls
325 lines (293 loc) · 8.01 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
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
<?php
/*
Plugin Name: PinkCrab Debugging Plugin
Plugin URI: https://www.PinkCrab.co.uk
Description: A selection of debugging tools. Should not really be used on production sites. Contains dump(), dd(), adump() & adie() plus custom error messages over WSOD
Author: PinkCrab
Version: 1.1.0
Author URI: https://www.PinkCrab.co.uk
*/
require_once 'vendor/autoload.php';
/**
* Checks if a request is likely from Rest API.
*
* @return boolean
*/
function pinkcrab_is_rest() {
return defined( 'REST_API_VERSION' ) && strpos( $_SERVER['REQUEST_URI'], '/wp-json/' ) !== false;
}
/**
* Shows a custom error message in place of the WSOD.
*
* Will show a styled view of the error when accessed via the browser.
* Will show a simple error message when accessed via AJAX or Rest.
*
* @param string $message The error message.
* @param array $error The error array.
*/
$r = add_filter(
'wp_php_error_args',
function ( $message, $error ) {
if ( wp_doing_ajax() || pinkcrab_is_rest() ) {
include 'views/ajax-error.php';
} else {
include 'views/web-error.php';
}
},
2,
10
);
/**
* Adds an admin page to view the log under tools.
*
* @return void
*/
add_action(
'admin_menu',
function () {
add_submenu_page(
'tools.php',
'PC Debug Log',
'PC Debug Log',
'manage_options',
'pc_debug_log',
function () {
$log_file = ABSPATH . 'wp-content/pc_debug.log';
$log = explode( "\x1F", file_get_contents( $log_file ) );
require 'views/log-viewer.php';
}
);
}
);
/**
* Allows for the same level of dumping as dump()
* but using print_r
* Also shows NULL or TRUE/FALSE as strings not blank or 1.
*
* @param mixed ...$data
* @return void
*/
function adump( ...$data ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '<pre>';
}foreach ( $data as $item ) {
if ( is_null( $item ) ) {
print( 'NULL' );
} elseif ( is_bool( $item ) && $item === false ) {
print( 'FALSE' );
} elseif ( is_bool( $item ) && $item === true ) {
print( 'TRUE' );
} else {
print_r( $item );
}
}
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '</pre>';
}
}
/**
* Ajax friendly version of dd().
* Users adump to show the output and then dies.
*
* @param mixed ...$data
* @return void
*/
function adie( ...$data ) {
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '<pre>';
}
adump( $data );
if ( ! wp_doing_ajax() && ! pinkcrab_is_rest() ) {
echo '</pre>';
}
die();
}
/**
* Shows all the enqueued scripts and styles in header, if set in url.
* ?pc_show_enqueued
*/
if ( ! empty( $_GET['pc_show_enqueued'] ) ) {
add_action(
'wp_head',
function () {
// Print all loaded Scripts
global $wp_scripts;
global $wp_styles;
dump(
array(
'DEBUG' => 'Showing enqueued scripts/styles (url.com?show_enqueued=true)',
'styles' => array_map(
function ( $e ) use ( $wp_styles ) {
return $wp_styles->registered[ $e ];
},
$wp_styles->queue
),
'scripts' => array_map(
function ( $e ) use ( $wp_scripts ) {
return $wp_scripts->registered[ $e ];
},
$wp_scripts->queue
),
)
);
}
);
}
/**
* So all of defined hooks if in url.
* ?pc_show_hooks=hook,hook2
*/
if ( ! empty( $_GET['pc_show_hooks'] ) ) {
add_action(
'wp_head',
function () {
$hooks = explode( ',', $_GET['pc_show_hooks'] );
foreach ( $hooks as $hook ) {
dump(
array(
'hook' => $hook,
'callbacks' => $GLOBALS['wp_filter'][ $hook ],
)
);
}
}
);
}
/**
* Custom Logger.
*
* Saves to wp-content/pc_debug.log
*
* @param mixed $data Data to log.
* @param string $type The type of log.
*
* @return void
*/
function pclog( $data, string $type = 'log' ) {
$log_file = ABSPATH . 'wp-content/pc_debug.log';
// If the custom log file is not set, set it.
if ( ! file_exists( $log_file ) ) {
// Create the log file.
file_put_contents( $log_file, '' );
}
// Get the current log file.
$log = file_get_contents( $log_file );
$delimiter = "\x1F";
// Add the new data to the log.
$entry = sprintf(
'[%s] %s: %s' . $delimiter,
gmdate( 'Y-m-d H:i:s' ),
$type,
print_r( $data, true )
);
// Add entry to the start of the log.
file_put_contents( $log_file, $entry . $log );
}
if ( ! function_exists( 'write_log' ) ) {
/**
* Write to the PHP error log.
*
* @param mixed $log
*
* @return void
*/
function write_log( $log ) {
if ( is_array( $log ) || is_object( $log ) ) {
error_log( print_r( $log, true ) );
} else {
error_log( $log );
}
}
}
// create wp cli command to test error message.
if ( defined( 'WP_CLI' ) && WP_CLI ) {
WP_CLI::add_hook(
'before_wp_load',
/**
* The custom error handler for WP CLI.
*
* @return void
*/
function (): void {
// Handle duplicate error messages.
static $errors = array();
/**
* Gets the terminal width.
*
* @return integer
*/
function pc_get_terminal_width(): int {
$width = exec( 'tput cols' ); // Fetch terminal width
return is_numeric( $width ) ? (int) $width : 80; // Default to 80 if unavailable
}
/**
* Formats a string to be centered in the terminal.
*
* @param string $text The text to center.
*
* @return string
*/
function pc_center_text_with_equals( string $text, $border = '==' ): string {
// Fetch the terminal width dynamically
$terminal_width = exec( 'tput cols' );
if ( ! is_numeric( $terminal_width ) ) {
$terminal_width = 80; // Default to 80 if terminal width cannot be determined
}
$terminal_width = (int) $terminal_width;
// Add space for "== ==" on either side
$padding_width = 4; // '== ' and ' ==' add 4 characters
$text_width = strlen( $text );
$available_space = $terminal_width - $text_width - $padding_width;
if ( $available_space < 0 ) {
// If text is too wide, truncate it and adjust
$text = substr( $text, 0, $terminal_width - $padding_width - 3 ) . '...';
$text_width = strlen( $text );
$available_space = $terminal_width - $text_width - $padding_width;
}
// Calculate padding on both sides
$left_padding = str_repeat( ' ', floor( $available_space / 2 ) );
$right_padding = str_repeat( ' ', ceil( $available_space / 2 ) );
// Return the centered line
return "{$border}{$left_padding}{$text}{$right_padding}{$border}";
}
set_error_handler(
/**
* The custom error handler for WP CLI.
*
* @param integer $errno The error number.
* @param string $errstr The error message.
* @param string $errfile The file the error occurred in.
* @param integer $errline The line the error occurred on.
*
* @return void
*/
function ( $errno, $errstr, $errfile, $errline ) use ( &$errors ) {
// Get the error code.
// If we have no messages, show a header in pink.
if ( empty( $errors ) ) {
WP_CLI::line( str_repeat( '=', pc_get_terminal_width() ) );
WP_CLI::line( pc_center_text_with_equals( 'WP CLI ERROR' ) );
WP_CLI::line( str_repeat( '=', pc_get_terminal_width() ) );
}
$message = "Error [$errno]: $errstr in $errfile on line $errline";
// If the error has not been displayed, show it.
if ( ! in_array( $message, $errors, true ) ) {
$errors[] = $message;
// Hide the link to the WP debugging page.
$errstr = str_replace( 'Please see <a href="https://developer.wordpress.org/advanced-administration/debug/debug-wordpress/">Debugging in WordPress</a> for more information.', '', $errstr );
// Replace common HTML in error messages with terminal colors.
$replacements = array(
'<strong>' => "\033[1;37m",
'</strong>' => "\033[34m",
'<code>' => "\033[33m",
'</code>' => "\033[34m",
);
$errstr = strip_tags( str_replace( array_keys( $replacements ), array_values( $replacements ), $errstr ) );
WP_CLI::line( WP_CLI::colorize( "%WError [$errno]:%B$errstr in $errfile on line $errline%N" ) );
WP_CLI::line( str_repeat( '=', pc_get_terminal_width() ) );
}
}
);
}
);
}