-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdebug.php
117 lines (98 loc) · 3.2 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
<?php
/**
* Provides interface for debugging variables with Query Monitor
*
* @author Mte90 <[email protected]>
* @license GPL-2.0+
* @copyright 2018
*
*/
class WPBP_Debug {
public $title;
public $output;
/**
* Check user cap and WP_DEBUG on init to see if class should continue loading
*
* @param string $title The panel title.
*/
public function __construct( $title ) {
if ( class_exists( 'QM_Collectors' ) ) {
if ( !class_exists( 'QM_Collector_WPBP_Debug' ) ) {
include 'QM_Collector_WPBP_Debug.php';
}
$this->title = $title;
$this->output = array();
QM_Collectors::add( new QM_Collector_WPBP_Debug( $this->title, $this ) );
}
/**
* Register output. The filter won't run if Query Monitor is not
* installed so we don't have to explicity check for it.
*/
add_filter( 'qm/outputter/html', array( $this, 'load' ), 101, 2 );
}
/**
* Print panel
*
* @param string $output The HTML code.
* @param object $collectors List of QM Collectors.
*
* @return array
*/
public function load (array $output, QM_Collectors $collectors ) {
if( !class_exists('QM_Collector_WPBP_Debug_Output') ) {
include 'QM_Collector_WPBP_Debug_Output.php';
}
$id = strtolower( str_replace( ' ', '-', $this->title ) );
if ( $collector = QM_Collectors::get( $id ) ) {
$output[ $id ] = new QM_Collector_WPBP_Debug_Output( $collector, $this->output, $this->title );
}
return $output;
}
/**
* Debugs a variable
* Only visible to admins if WP_DEBUG is on
* @param mixed $var The var to debug.
* @param bool $die Whether to die after outputting.
* @param string $function The function to call, usually either print_r or var_dump, but can be anything.
* @return mixed
*/
public function log( $var, $die = false, $function = 'var_dump' ) {
ob_start();
if ( is_string( $var ) ) {
echo $var . "\n";
} else {
call_user_func( $function, $var );
}
if ( $die ) {
die();
}
$this->output[] = ob_get_clean();
}
/**
* Print in Query Monitor Log panel, check https://querymonitor.com/blog/2018/07/profiling-and-logging/
* @param mixed $var The var to debug.
* @param string $type The error type based on Query Monitor methods.
* @return mixed
*/
public function qm_log( $var, $type ) {
if ( class_exists( 'QM' ) ) {
QM::$type( $var );
}
}
/**
* Timer in Query Monitor, check https://querymonitor.com/blog/2018/07/profiling-and-logging/
* @param mixed $id Timer ID.
* @param string $callback The callback to profile.
* @return mixed
*/
public function qm_timer( $id, $callback ) {
if ( class_exists( 'QM' ) ) {
// Start the timer:
do_action( 'qm/start', $id );
// Run some code
call_user_func( $callback );
// Stop the timer:
do_action( 'qm/stop', $id );
}
}
}