-
Notifications
You must be signed in to change notification settings - Fork 1
/
otfm-wp-debug.php
349 lines (298 loc) · 9.22 KB
/
otfm-wp-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
<?php
/*
MU Plugin Name: OtFm WP-Debug
Plugin URI: https://github.com/Otshelnik-Fm/otfm-wp-debug
Description: OtFm WP-Debug is a MU-WordPress plugin for debug
Version: 1.3.0
Author: Otshelnik-Fm
Author URI: https://otshelnik-fm.ru/
License: MIT
*/
/*
╔═╗╔╦╗╔═╗╔╦╗
║ ║ ║ ╠╣ ║║║ https://otshelnik-fm.ru
╚═╝ ╩ ╚ ╩ ╩
*/
global $wpdbg_settings;
/**
* Left debug panel
* You can also open the panel via a GET-request - site.com/?wpdbg
*
* false - disabled;
* true - enabled all (in dev environment);
* 'admin' - if visible in admin;
*/
$wpdbg_settings['left_panel'] = 'admin';
/**
* vd() (like var_dump) - convenient debugging instead of print_r or var_dump
*
* @param $var mixed variable of debug.
* @param $fixed bool pass true to output in a fixed window at the top left.
* @param $excludes string|array key|keys who exclude
*
* @return string var_dump variable.
* @since 1.0.0
*/
if ( ! function_exists( 'vd' ) ) {
function vd( $var, $fixed = false, $excludes = [] ) {
if ( is_string( $fixed ) || is_array( $fixed ) ) {
$excludes = $fixed;
$fixed = false;
}
if ( ! empty( $excludes ) && ( is_array( $var ) || is_object( $var ) ) ) {
$data = [];
$excludes = is_string( $excludes ) ? explode( ' ', $excludes ) : $excludes;
foreach ( $var as $key => $subArr ) {
if ( is_array( $subArr ) ) {
foreach ( $excludes as $exclude ) {
unset( $subArr[ $exclude ] );
}
} else {
foreach ( $excludes as $exclude ) {
unset( $subArr->$exclude );
}
}
$data[ $key ] = $subArr;
}
otfm_wpdbg_print_data( $data, $fixed );
} else {
otfm_wpdbg_print_data( $var, $fixed );
}
}
}
function otfm_wpdbg_print_data( $var, $fixed ) {
$det_style = '';
if ( $fixed ) {
$det_style = 'style="position:fixed;top:48px;left:18px;z-index:5000;"';
echo '<style>.wpd_dark_mode pre{height:calc(85vh - 50px);overflow:auto;max-width:calc(100vw - 72px);}</style>';
}
echo '<style>
.wpd_dark_mode {padding:12px 6px;font-size:14px;line-height:1;background:#323641;color:#e3e8ef;}
.wpd_dark_mode pre {background:linear-gradient(to right, #171721, #163734) !important;
border:1px solid #040607;color:#e3e8ef !important;padding:12px !important;margin:12px 0 !important;font:normal normal 16px/normal monospace;}
</style>';
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo '<details open class="wpd_dark_mode" ' . $det_style . '><pre>';
if ( ! empty( $var ) ) {
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, WordPress.PHP.DevelopmentFunctions.error_log_print_r
echo htmlspecialchars( print_r( $var, true ) );
} else {
//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_var_dump
var_dump( $var );
}
echo '</pre></details>';
}
/**
* vda() (var_dump admin) - output to the screen for admin only
*
* @param $var mixed variable of debug.
* @param $fixed bool pass true to output in a fixed window at the top left.
*
* @return string var_dump variable.
* @since 1.0.0
*/
if ( ! function_exists( 'vda' ) ) {
function vda( $var, $fixed = false ) {
if ( current_user_can( 'manage_options' ) ) {
vd( $var, $fixed );
}
}
}
/**
* vdd() analog of vd, but with a die; at the end. When should I stop further work
*
* @param $var mixed variable of debug.
*
* @return string var_dump variable.
* @since 1.0.0
*/
if ( ! function_exists( 'vdd' ) ) {
/** @noinspection PhpNoReturnAttributeCanBeAddedInspection */
function vdd( $var ) {
vd( $var );
die;
}
}
/**
* vdl() (var_dump log) - we write to the server logs. When we can't display it on the screen (or this is the ajax request debug, for example).
*
* @param $var mixed variable of debug.
*
* @since 1.0.0
*/
if ( ! function_exists( 'vdl' ) ) {
function vdl( $var ) {
//phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log, WordPress.PHP.DevelopmentFunctions.error_log_print_r
error_log( print_r( $var, true ) );
}
}
/**
* vdx() (var_dump XHR) - for ajax debugging (see incoming POST data on the browser's XHR tab) Example: https://yadi.sk/i/CPGuKgwmSQTEKg
*
* @param $var mixed variable of debug.
*
* @since 1.0.0
*/
if ( ! function_exists( 'vdx' ) ) {
function vdx( $var ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
if ( is_array( $var ) ) {
$var['data_type'] = gettype( $var );
}
if ( is_object( $var ) ) {
$var->data_type = gettype( $var );
} else if ( is_string( $var ) || is_int( $var ) || is_float( $var ) || is_bool( $var ) ) {
$var .= ' | data_type: ' . gettype( $var );
} else if ( null === $var ) {
$var = 'NULL';
$var .= ' | data_type: NULL';
}
wp_send_json_error( $var );
}
}
}
/***************************************************************
*
* Left query panel
*
*/
function wpdbg_panel_activated() {
if ( isset( $_GET['wpdbg'] ) ) {
return true;
}
global $wpdbg_settings;
if ( isset( $wpdbg_settings['left_panel'] ) && false == $wpdbg_settings['left_panel'] ) {
return false;
}
if ( isset( $wpdbg_settings['left_panel'] ) && ! empty( $wpdbg_settings['left_panel'] ) ) {
if ( true === $wpdbg_settings['left_panel'] ) {
return true;
} elseif ( 'admin' === $wpdbg_settings['left_panel'] && current_user_can( 'activate_plugins' ) ) {
return true;
}
}
return false;
}
// выводим блок
add_action( 'wp_footer', 'wpdbg_panel' );
function wpdbg_panel() {
if ( ! wpdbg_panel_activated() ) {
return false;
}
if ( isset( $_GET['panel'] ) && 'off' === $_GET['panel'] ) {
return false;
}
$recourses = "\r\n" . wpdbg_meter_styles();
$recourses .= "\r\n" . wpdbg_meter_scripts();
echo $recourses . "\r\n<div id='wpdbg_box'>" . wpdbg_meter() . "</div>\r\n";
}
// метрика
function wpdbg_meter() {
$out = '<div id="wpdbg_mem" title="PHP time (sec)">' . timer_stop() . '</div>';
$out .= '<div id="wpdbg_m_old" title="Old data"></div>';
$out .= '<div id="wpdbg_db" title="SQL">' . get_num_queries() . '</div>';
$out .= '<div id="wpdbg_q_old" title="Old data"></div>';
if ( function_exists( 'memory_get_usage' ) ) {
$out .= '<div id="wpdbg_mb" title="MB">' . round( memory_get_usage() / 1024 / 1024, 3 ) . '</div>';
$out .= '<div id="wpdbg_w_old" title="Old data"></div>';
}
return $out;
}
function wpdbg_meter_styles() {
$style = '
#wpdbg_box {
animation: .5s wpdbgA forwards .5s;
background: rgba(0, 0, 0, .8);
border-left: 6px solid #70b340;
border-radius: 0 3px 3px 0;
color: #89d549;
font: 16px/1.4 arial;
left: 0;
opacity: 0;
padding: 6px 12px;
position: fixed;
bottom: 50vh;
z-index: 100;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 0 6px;
}
@keyframes wpdbgA{100%{opacity:1;}}
#wpdbg_box > :nth-child(2n) {
color: #70b340;
font-size: 13px;
margin-top: 6px;
}
@media screen and (max-width:800px) {
#wpdbg_box {
left: -130px;
transition: all .3s ease-in;
}
#wpdbg_box:hover {
left: 0;
}
#wpdbg_box::after {
content: "#";
left: 0;
position: fixed;
background: rgba(0, 0, 0, .8);
font-size: 26px;
padding: 0 9px;
margin: 12px 0 0;
bottom: calc(50vh - 36px);
}
}
';
return '<style>' . wpdbg_compress_css( $style ) . '</style>';
}
function wpdbg_meter_scripts() {
$js = '
window.addEventListener("pagehide", function(event) {
let wpdbgStore = [];
let time = document.getElementById("wpdbg_mem");
let queries = document.getElementById("wpdbg_db");
let mb = document.getElementById("wpdbg_mb");
wpdbgStore[0] = time.innerHTML;
wpdbgStore[1] = queries.innerHTML;
wpdbgStore[2] = mb.innerHTML;
wpdbgCreateCookie("wpdbgStoreMem", JSON.stringify( wpdbgStore ) )
} );
document.addEventListener("DOMContentLoaded", function(event) {
let cook = wpdbgGetCookie("wpdbgStoreMem");
if(undefined === cook){
return;
}
let data = JSON.parse(cook);
if ( ( data !== undefined ) && ( data !== null ) ) {
let time = document.getElementById("wpdbg_m_old");
let queries = document.getElementById("wpdbg_q_old");
let mb = document.getElementById("wpdbg_w_old");
time.textContent = "(" + data[0] + ")";
queries.textContent = "(" + data[1] + ")";
mb.textContent = "(" + data[2] + ")";
}
} );
function wpdbgCreateCookie(name, value ) {
let date = new Date();
date.setTime(date.getTime() + (7 * 24 * 60 * 60 * 1000));
let expires = "; expires=" + date.toGMTString();
document.cookie = name + "=" + value + expires + "; path=/";
}
function wpdbgGetCookie(name) {
let matches = document.cookie.match(new RegExp(
"(?:^|; )" + name.replace(/([\.$?*|{}\(\)\[\]\\\/\+^])/g, "\\$1") + "=([^;]*)"
));
return matches ? decodeURIComponent(matches[1]) : undefined;
}
';
return '<script>' . wpdbg_compress_js( $js ) . '</script>';
}
function wpdbg_compress_js( $js ) {
return preg_replace( '/ {2,}/', '', str_replace( [ "\r\n", "\r", "\n", "\t" ], '', $js ) );
}
function wpdbg_compress_css( $src ) {
$src_cleared = preg_replace( '/ {2,}/', '', str_replace( [ "\r\n", "\r", "\n", "\t", ' ', ' ', ' ' ], '', $src ) );
$src_non_space = str_replace( ': ', ':', $src_cleared );
return str_replace( ' {', '{', $src_non_space );
}
/************************ END ************************/