Skip to content
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/_styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,8 @@ body.admin-color-light #wp-admin-bar-query-monitor:not(.qm-all-clear):not(:hover
margin: 20px !important;
}

.qm-concerns table {
.qm-concerns table,
.qm-discovered table {
border-top: 1px solid $qm-cell-border !important;
margin-bottom: 20px !important;
}
Expand Down
3 changes: 2 additions & 1 deletion assets/query-monitor.css
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,8 @@ body.admin-color-light #wp-admin-bar-query-monitor:not(.qm-all-clear):not(:hover
font-size: 14px !important;
margin: 20px !important;
}
#query-monitor-main .qm-concerns table {
#query-monitor-main .qm-concerns table,
#query-monitor-main .qm-discovered table {
border-top: 1px solid #e0e0e0 !important;
margin-bottom: 20px !important;
}
Expand Down
52 changes: 52 additions & 0 deletions collectors/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,58 @@ public function name() {
return __( 'Hooks & Actions', 'query-monitor' );
}

public function __construct() {
parent::__construct();
add_action( 'qm/listen/start', array( $this, 'action_function_listen_start' ), 10, 1 );
add_action( 'qm/listen/stop', array( $this, 'action_function_listen_stop' ), 10, 1 );
}

public function action_function_listen_start( $label ) {
if ( ! array_key_exists( 'discovered_hooks', $this->data ) ) {
$this->data['discovered_hooks'] = array();
}

if ( array_key_exists( $label, $this->data['discovered_hooks'] ) ) {
return;
}

$this->data['discovered_hooks'][ $label ] = array();

add_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 );
}

public function action_function_listen_all( $var = null ) {
if ( in_array( current_action(), array(
'qm/listen/start',
'qm/listen/stop',
) ) ) {
return $var;
}

global $wp_actions;

end( $this->data['discovered_hooks'] );
$label = key( $this->data['discovered_hooks'] );
$last = end( $this->data['discovered_hooks'][ $label ] );

if ( current_action() === $last['hook'] ) {
$i = key( $this->data['discovered_hooks'][ $label ] );
$this->data['discovered_hooks'][ $label ][ $i ]['count']++;
} else {
$this->data['discovered_hooks'][ $label ][] = array(
'is_action' => array_key_exists( current_action(), $wp_actions ),
'hook' => current_action(),
'count' => 1,
);
}

return $var;
}

public function action_function_listen_stop( $label ) {
remove_action( 'all', array( $this, 'action_function_listen_all' ), 0, 1 );
}

public function process() {

global $wp_actions, $wp_filter;
Expand Down
75 changes: 75 additions & 0 deletions output/html/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,25 @@ class QM_Output_Html_Hooks extends QM_Output_Html {

public function __construct( QM_Collector $collector ) {
parent::__construct( $collector );
add_filter( 'qm/output/panel_menus', array( $this, 'panel_menus' ) );
add_filter( 'qm/output/menus', array( $this, 'admin_menu' ), 80 );
}

public function panel_menus( $panel_menu ) {
$data = $this->collector->get_data();

if ( empty( $data['discovered_hooks'] ) ) {
return $panel_menu;
}

$panel_menu[ 'qm-' . $this->id ]['children'][ 'qm-' . $this->id . '-discovered_hooks' ] = array(
'href' => esc_attr( '#' . $this->collector->id() . '-discovered_hooks' ),
'title' => '└ ' . __( 'Discovered Hooks', 'query-monitor' ),
);

return $panel_menu;
}

public function output() {

$data = $this->collector->get_data();
Expand Down Expand Up @@ -178,6 +194,65 @@ public static function output_hook_table( array $hooks ) {

}

protected function after_tabular_output() {
echo '</table>';
echo '</div>';

$this->output_discovered();
}

public function output_discovered() {
printf(
'<div class="qm qm-discovered" id="%1$s" role="group" aria-labelledby="%1$s" tabindex="-1">',
esc_attr( $this->current_id . '-discovered_hooks' )
);

echo '<div><table>';

printf(
'<caption><h2 id="%1$s-caption">%2$s</h2></caption>',
esc_attr( $this->current_id . '-discovered_hooks' ),
esc_html__( 'Discovered Hooks', 'query-monitor' )
);

echo '<thead>';
echo '<tr>';
echo '<th scope="col">' . esc_html__( 'Label', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Hook', 'query-monitor' ) . '</th>';
echo '<th scope="col">' . esc_html__( 'Type', 'query-monitor' ) . '</th>';
echo '</tr>';
echo '</thead>';

echo '<tbody>';

$data = $this->collector->get_data();

foreach ( $data['discovered_hooks'] as $label => $hooks ) {
foreach ( $hooks as $i => $hook ) {
echo '<tr>';

if ( 0 === $i ) {
echo '<th scope="row" rowspan="' . esc_attr( count( $hooks ) ) . '" class="qm-nowrap"><span class="qm-sticky">' . esc_html( $label ) . '</span></th>';
}

echo '<td>';
echo '<code>' . esc_html( $hook['hook'] ) . '</code>';

if ( 1 < $hook['count'] ) {
echo '<br /><span class="qm-info qm-supplemental">Fired ' . esc_html( $hook['count'] ) . ' times</span>';
}
echo '</td>';
echo '<td>' . ( $hook['is_action'] ? 'Action' : 'Filter' ) . '</td>';
echo '</tr>';
}
}

echo '</tbody>';
echo '</table></div>';

echo '</div>';
}

}

function register_qm_output_html_hooks( array $output, QM_Collectors $collectors ) {
Expand Down