Skip to content

Commit 96f1452

Browse files
committed
init implementation of johnbillion#405 (third option)
1 parent 28e5e22 commit 96f1452

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

assets/query-monitor.css

+2-1
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,8 @@ body.admin-color-light #wp-admin-bar-query-monitor:not(.qm-all-clear):not(:hover
539539
font-size: 14px !important;
540540
margin: 20px !important;
541541
}
542-
#query-monitor-main .qm-concerns table {
542+
#query-monitor-main .qm-concerns table,
543+
#query-monitor-main .qm-discovered table {
543544
border-top: 1px solid #e0e0e0 !important;
544545
margin-bottom: 20px !important;
545546
}

collectors/hooks.php

+46
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,52 @@ public function name() {
1414
return __( 'Hooks & Actions', 'query-monitor' );
1515
}
1616

17+
public function __construct() {
18+
parent::__construct();
19+
add_action( 'qm/listen/start', array( $this, 'action_function_listen_start' ), 10, 1 );
20+
add_action( 'qm/listen/stop', array( $this, 'action_function_listen_stop' ), 10, 1 );
21+
}
22+
23+
public function action_function_listen_start( $label ) {
24+
if ( !array_key_exists( 'discovered_hooks', $this->data ) )
25+
$this->data['discovered_hooks'] = array();
26+
27+
if ( array_key_exists( $label, $this->data['discovered_hooks'] ) )
28+
return;
29+
30+
$this->data['discovered_hooks'][$label] = array();
31+
32+
add_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 );
33+
}
34+
35+
public function action_function_listen_all( $var ) {
36+
if ( in_array( current_action(), array(
37+
'qm/listen/start',
38+
'qm/listen/stop',
39+
) ) )
40+
return;
41+
42+
end( $this->data['discovered_hooks'] );
43+
$label = key( $this->data['discovered_hooks'] );
44+
$last = end( $this->data['discovered_hooks'][$label] );
45+
46+
if ( current_action() === $last['action'] ) {
47+
$i = key( $this->data['discovered_hooks'][$label] );
48+
$this->data['discovered_hooks'][$label][$i]['count']++;
49+
} else {
50+
$this->data['discovered_hooks'][$label][] = array(
51+
'action' => current_action(),
52+
'count' => 1,
53+
);
54+
}
55+
56+
return $var;
57+
}
58+
59+
public function action_function_listen_stop( $label ) {
60+
remove_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 );
61+
}
62+
1763
public function process() {
1864

1965
global $wp_actions, $wp_filter;

output/html/hooks.php

+67
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@ class QM_Output_Html_Hooks extends QM_Output_Html {
1111

1212
public function __construct( QM_Collector $collector ) {
1313
parent::__construct( $collector );
14+
add_filter( 'qm/output/panel_menus', array( $this, 'panel_menus' ) );
1415
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 80 );
1516
}
1617

18+
public function panel_menus( $panel_menu ) {
19+
$data = $this->collector->get_data();
20+
21+
if ( empty( $data['discovered_hooks'] ) )
22+
return;
23+
24+
$panel_menu[ 'qm-' . $this->id ]['children'][ 'qm-' . $this->id . '-discovered_hooks' ] = array(
25+
'href' => esc_attr( '#' . $this->collector->id() . '-discovered_hooks' ),
26+
'title' => '' . __( 'Discovered Hooks', 'query-monitor' ),
27+
);
28+
29+
return $panel_menu;
30+
}
31+
1732
public function output() {
1833

1934
$data = $this->collector->get_data();
@@ -178,6 +193,58 @@ public static function output_hook_table( array $hooks ) {
178193

179194
}
180195

196+
protected function after_tabular_output() {
197+
echo '</table>';
198+
echo '</div>';
199+
200+
$this->output_discovered();
201+
}
202+
203+
function output_discovered() {
204+
printf(
205+
'<div class="qm qm-discovered" id="%1$s" role="group" aria-labelledby="%1$s" tabindex="-1">',
206+
esc_attr( $this->current_id . '-discovered_hooks' )
207+
);
208+
209+
echo '<div><table>';
210+
211+
printf(
212+
'<caption><h2 id="%1$s-caption">%2$s</h2></caption>',
213+
esc_attr( $this->current_id . '-discovered_hooks' ),
214+
esc_html__( 'Discovered Hooks', 'query-monitor' )
215+
);
216+
217+
echo '<thead>';
218+
echo '<tr>';
219+
echo '<th scope="col">' . esc_html__( 'Label', 'query-monitor' ) . '</th>';
220+
echo '<th scope="col">' . esc_html__( 'Hook', 'query-monitor' ) . '</th>';
221+
echo '<th scope="col">' . esc_html__( 'Successive Uses', 'query-monitor' ) . '</th>';
222+
echo '</tr>';
223+
echo '</thead>';
224+
225+
echo '<tbody>';
226+
227+
$data = $this->collector->get_data();
228+
229+
foreach ( $data['discovered_hooks'] as $label => $hooks ) {
230+
foreach ( $hooks as $i => $hook ) {
231+
echo '<tr>';
232+
233+
if ( 0 === $i )
234+
echo '<th scope="row" rowspan="' . esc_attr( count( $hooks ) ) . '" class="qm-nowrap"><span class="qm-sticky">' . esc_html( $label ) . '</span></th>';
235+
236+
echo '<td><code>' . esc_html( $hook['action'] ) . '</code></td>';
237+
echo '<td class="qm-num">' . esc_html( $hook['count'] ) . '</td>';
238+
echo '</tr>';
239+
}
240+
}
241+
242+
echo '</tbody>';
243+
echo '</table></div>';
244+
245+
echo '</div>';
246+
}
247+
181248
}
182249

183250
function register_qm_output_html_hooks( array $output, QM_Collectors $collectors ) {

0 commit comments

Comments
 (0)