Skip to content

Commit b4a2745

Browse files
committed
new query monitor support
1 parent bf658a9 commit b4a2745

File tree

5 files changed

+96
-66
lines changed

5 files changed

+96
-66
lines changed

QM_Collector_WPBP_Debug.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
class QM_Collector_WPBP_Debug extends QM_Collector {
4+
/**
5+
* Register with WordPress API on construct
6+
*/
7+
function __construct( $title ) {
8+
$this->title = $title;
9+
$this->id = strtolower( str_replace(' ', '_', $title ) );
10+
}
11+
12+
public function name() {
13+
return $this->title;
14+
}
15+
16+
public function process() {
17+
$this->data['log'] = 123;
18+
}
19+
20+
}

QM_Collector_WPBP_Debug_Output.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
class QM_Collector_WPBP_Debug_Output extends QM_Output_Html {
4+
public function __construct( QM_Collector $collector, $output, $title ) {
5+
parent::__construct( $collector );
6+
$this->output = $output;
7+
$this->title = $title;
8+
$this->id = strtolower( str_replace(' ', '_', $title ) );
9+
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 101 );
10+
add_filter( 'qm/output/title', array( $this, 'admin_title' ), 101 );
11+
add_filter( 'qm/output/menu_class', array( $this, 'admin_class' ) );
12+
}
13+
14+
/**
15+
* Outputs data in the footer
16+
*/
17+
public function output() {
18+
echo $this->output;
19+
}
20+
21+
/**
22+
* Adds data to top admin bar
23+
*
24+
* @param array $title
25+
*
26+
* @return array
27+
*/
28+
public function admin_title( array $title ) {
29+
$data = $this->collector->get_data();
30+
$title[] = $this->title . ' (' . count( $data['log'] ) . ')';
31+
return $title;
32+
}
33+
34+
/**
35+
* @param array $class
36+
*
37+
* @return array
38+
*/
39+
public function admin_class( array $class ) {
40+
$class[] = $this->id;
41+
return $class;
42+
}
43+
44+
public function admin_menu( array $menu ) {
45+
$data = $this->collector->get_data();
46+
$menu[] = $this->menu( array(
47+
'id' => $this->id,
48+
'href' => '#' . str_replace( '_', '-', $this->id),
49+
'title' => $this->title
50+
));
51+
return $menu;
52+
}
53+
54+
}

WPBP_Debug_Panel.php

Lines changed: 0 additions & 35 deletions
This file was deleted.

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "wpbp/debug",
3-
"description": "Add to your plugin the Debug Bar support (work also on Query Monitor)",
3+
"description": "Add your plugin to Query Monitor",
44
"license": "GPL-3.0",
55
"keywords": [
66
"wordpress",

debug.php

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
<?php
22

33
/**
4-
* Provides interface for debugging variables with Debug Bar
5-
*
6-
* @author Benjamin J. Balter <ben@balter.com> & Mte90 <mte90net@gmail.com>
4+
* Provides interface for debugging variables with Query Monitor
5+
*
6+
* @author Mte90 <mte90net@gmail.com>
77
* @license GPL-2.0+
8-
* @copyright 2014
8+
* @copyright 2018
99
*
1010
*/
1111
if ( !class_exists( 'WPBP_Debug' ) ) {
@@ -25,12 +25,24 @@ class WPBP_Debug {
2525
* Check user cap and WP_DEBUG on init to see if class should continue loading
2626
*/
2727
function __construct( $title ) {
28-
if ( !current_user_can( 'manage_options' ) || !WP_DEBUG ) {
29-
return;
28+
if( class_exists('QM_Collectors') ) {
29+
require_once( 'QM_Collector_WPBP_Debug.php' );
30+
$this->title = $title;
31+
32+
QM_Collectors::add( new QM_Collector_WPBP_Debug( $this->title ) );
3033
}
31-
$this->title = $title;
3234

33-
add_filter( 'debug_bar_panels', array( $this, 'init_panel' ) );
35+
/**
36+
* Register output. The filter won't run if Query Monitor is not
37+
* installed so we don't have to explicity check for it.
38+
*/
39+
add_filter( 'qm/outputter/html', function(array $output, QM_Collectors $collectors) {
40+
include 'QM_Collector_WPBP_Debug_Output.php';
41+
if ( $collector = QM_Collectors::get( 'wpbp' ) ) {
42+
$output['wpbp'] = new QM_Collector_WPBP_Debug_Output( $collector, $this->output, $this->title );
43+
}
44+
return $output;
45+
}, 101, 2 );
3446
}
3547

3648
/**
@@ -42,9 +54,6 @@ function __construct( $title ) {
4254
* @return mixed
4355
*/
4456
function log( $var, $die = false, $function = 'var_dump' ) {
45-
if ( !current_user_can( 'manage_options' ) || !WP_DEBUG ) {
46-
return;
47-
}
4857

4958
ob_start();
5059
if ( is_string( $var ) ) {
@@ -57,25 +66,7 @@ function log( $var, $die = false, $function = 'var_dump' ) {
5766
die();
5867
}
5968

60-
$debug = ob_get_clean();
61-
62-
$GLOBALS[ 'WPBP_Debug' ][] = $debug;
63-
64-
// Allow this to be used as a filter
65-
return $var;
66-
}
67-
68-
/**
69-
* Extend Debug_Bar_Panel
70-
* @param array $panels The default panels.
71-
* @return array passback The original panels.
72-
*/
73-
function init_panel( $panels ) {
74-
if ( !class_exists( 'WPBP_Debug_Panel' ) ) {
75-
require_once('WPBP_Debug_Panel.php');
76-
$panels[] = new WPBP_Debug_Panel( $this->title );
77-
}
78-
return $panels;
69+
$this->output = ob_get_clean();
7970
}
8071

8172
}

0 commit comments

Comments
 (0)